[SOLVED] Automating USB drive configuration of partion table, partition and file system
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Automating USB drive configuration of partion table, partition and file system
Hi
I have tried to automate the configuration of a usb drive with not much success.
The problem that I have is that I have a large amount of usb drives that have a partition table of type "loop" and I need to change them to "msdos". The size of the drives vary and I need to use FAT32 or FAT16 file system.
I've tried various partitioning commands and gui applications but cant find one that I can give a one line command to to set the partition table, maximum partition size and file system.
I've 1000 usb drives that have a loop partition table and needs changing to msdos. A quick fix today to make sure they could be changed was to use gparted to rename the partition table and set the partition size and then to put the drive into an XP machine to format it to FAT32.
As I say this was a quick fix to make sure they were changeable, which thankfully they are.
what I would like to do next is be able to put upto 7 sticks int a USB hub and run a script that would locate each drive and change the partition table followed by a repartition and formatting.
I've already got a ruby script that will rotate through devices but its the actual one line command to configure the USB drive that I am stuck on.
reformat() {
# Commands to re-create a functional USB stick with VFAT partition:
# two parameters:
# (1) the name of the USB device to be formatted:
# (2) FAT label to use when formatting the USB device:
local TOWIPE="$1"
local THELABEL="$2"
# Sanity checks:
if [ ! -b $TOWIPE ]; then
echo "*** Not a block device: '$TOWIPE' !"
exit 1
fi
# Wipe the MBR:
dd if=/dev/zero of=$TOWIPE bs=512 count=1
# create a FAT32 partition (type 'b')
/sbin/fdisk $TOWIPE <<EOF
n
p
1
t
b
w
EOF
# Some desktop environments auto-mount the partition on sight...:
if mount | grep -q ${TOWIPE}1 ; then
echo "--- Un-mounting ${TOWIPE}1 because your desktop auto-mounted it..."
umount -f ${TOWIPE}1
fi
# We set the fat label to '$THELABEL' when formatting.
# It will enable the installer to mount the fat partition automatically
# and pre-fill the correct pathname for the "SOURCE" dialog.
# Format with a vfat filesystem:
/sbin/mkdosfs -F32 -n ${THELABEL} ${TOWIPE}1
}
makebootable() {
# Only parameter: the name of the USB device to be set bootable:
USBDRV="$1"
# Sanity checks:
if [ ! -b $USBDRV ]; then
echo "Not a block device: '$USBDRV' !"
exit 1
fi
# Set the bootable flag for the first partition:
/sbin/sfdisk -A $USBDRV 1
}
Last edited by allend; 01-27-2011 at 03:24 AM.
Reason: Added the full function, rather than a portion.
Fabulous script but I've not been able to get it to work for me, but I think I have worked out what is happening with it, however don't know how to fix it.
On this bit:
/sbin/fdisk $TOWIPE <<EOF
n
p
1
t
b
w
EOF
the terminal outputs the GNU Fdisk version, copyright and warranty information and stops at "Using /dev/sda"
I've tried a simplified script having tested it manually in fdisk:
/sbin/fdisk /dev/sda <<EOF
o
w
EOF
which also stops at "Using /dev/sda"
Everything else in the script I understand and have tried different settings between the <<EOF and EOF but to no avail.
This little script certainly works in my Slackware
Code:
#!/bin/sh
/sbin/fdisk /dev/sdc <<EOF
l
p
q
EOF
And I have successfully used the full usbimg2disk.sh to create a bootable USB installer.
I do not why it is not working for you.
I assume you are doing this with root privileges.
I'm on debian lenny as root and I've just tried your last script and still no joy. I'm now on plan B which is to install Slackware on a different machine and see if I can make it work on that.
Never used Slackware before so it could get eductional!
Jut want to say thank you for your help. My final solution was:
Code:
#!/bin/bash
#used to set partition table to msdos and file system to fat16
#set function
reformat(){
#Make partition table
parted -s $1 mklabel "msdos"
#create partition and format it
parted -s $1 mkpartfs primary fat16 0 995
#output the partition information for verification
parted -s $1 print
}
#start of action
#create drive variable
vsd="/dev/sda"
#check drive exists
if [ -e $vsd ]
then
#run function
reformat $vsd
echo "done" $vsd
echo ""
else
#if drive doesn't exist
echo "nope $vsd doesnt exist"
fi
#end of action
I then copied and pasted the action section 6 more times and change the variable for each drive (sdb, sdc etc) as I new I only had 7 drives.
Before and after executing the above I did:
Code:
ls -l /dev/sd*
in a second terminal to check that the drive had registered and then that the partition had been created. Probably wasn't completely necessary but it gave me a double check.
It could probably have been a bit neater, however time wasn't on my side. But saying that, it solved my problem.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.