Linux - General This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-26-2011, 05:15 PM
|
#1
|
|
LQ Newbie
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7
Rep:
|
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.
Any help much appreciated.
|
|
|
|
01-26-2011, 05:18 PM
|
#2
|
|
Guru
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,817
|
What exactly is the end goal here? A script that will wipe and reformat a USB drive plugged into the computer?
|
|
|
|
01-26-2011, 05:43 PM
|
#3
|
|
Member
Registered: Sep 2007
Location: Australia
Distribution: Arch Linux
Posts: 489
Rep:
|
it'd be simple just to do it manually, it only takes two commands
Code:
fdisk /dev/sdX
o
n
p
1
Enter
Enter
w
mkfs.vfat -F32 /dev/sdX1
|
|
|
|
01-26-2011, 06:00 PM
|
#4
|
|
LQ Newbie
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7
Original Poster
Rep:
|
Thanks for the quick response.
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.
|
|
|
|
01-27-2011, 02:21 AM
|
#5
|
|
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 2,743
|
Pinched from Alien_Bob's usbimg2disk.sh
Code:
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.
|
|
|
|
01-27-2011, 06:49 AM
|
#6
|
|
LQ Newbie
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7
Original Poster
Rep:
|
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.
|
|
|
|
01-27-2011, 08:39 AM
|
#7
|
|
Senior Member
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 2,743
|
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.
|
|
|
|
01-27-2011, 09:17 AM
|
#8
|
|
LQ Newbie
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7
Original Poster
Rep:
|
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!
I'll let you know how I get on.
|
|
|
|
01-27-2011, 02:00 PM
|
#9
|
|
LQ Newbie
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7
Original Poster
Rep:
|
quick update: I got Slackware installed and had chance for a brief test and was able to make
Code:
#!/bin/sh
/sbin/fdisk /dev/sdc <<EOF
l
p
q
EOF
work.
Job for this evening is to workout how to make it work on debian. Although if I am unsuccessful I'll be using Slackware in the morning.
|
|
|
|
01-28-2011, 09:40 AM
|
#10
|
|
LQ Newbie
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7
Original Poster
Rep:
|
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:
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:22 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|