LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 01-26-2011, 05:15 PM   #1
nicklli
LQ Newbie
 
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7

Rep: Reputation: 0
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.
 
Old 01-26-2011, 05:18 PM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
What exactly is the end goal here? A script that will wipe and reformat a USB drive plugged into the computer?
 
Old 01-26-2011, 05:43 PM   #3
Person_1873
Member
 
Registered: Sep 2007
Location: Australia
Distribution: Gentoo / Debian / Rasbian / Proxmox
Posts: 519

Rep: Reputation: 44
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
 
Old 01-26-2011, 06:00 PM   #4
nicklli
LQ Newbie
 
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 01-27-2011, 02:21 AM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,383

Rep: Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762
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.
 
Old 01-27-2011, 06:49 AM   #6
nicklli
LQ Newbie
 
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 01-27-2011, 08:39 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,383

Rep: Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762Reputation: 2762
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.
 
Old 01-27-2011, 09:17 AM   #8
nicklli
LQ Newbie
 
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 01-27-2011, 02:00 PM   #9
nicklli
LQ Newbie
 
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 01-28-2011, 09:40 AM   #10
nicklli
LQ Newbie
 
Registered: Nov 2010
Distribution: Debian/Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
loop, msdos, table



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File recovery after partition table damage - XFS file system gracecourt Linux - General 4 09-28-2017 05:29 PM
Shrinking broke Partition table. EXT4 file system. sysrez Linux - General 2 08-25-2010 11:07 AM
Hard drive partition, mounting, and file system help needed please. nix-newb Linux - Hardware 4 11-02-2008 09:44 PM
help fix a partition table on a usb drive. ncsuapex Linux - Hardware 4 08-10-2007 11:29 PM
file system size larger than fysical size:superblock or partition table corrupt klizon Linux - General 0 06-18-2004 04:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration