Partitioning with BASH Script

Hello. I've used these one-liners successfully to resize the existing FAT partition on a flash drive (to about 1GB), then add an ext2 partition to the remaining space.

parted $device --script -- resize 1 1000
mount ${device}1
parted $device --script -- mkpartfs primary ext2 1001 -- -1
mount ${device}2

But it doesn't work always. It seems to work at times only. If, later, I remove the partitions and reinstate the single FAT32 partition that uses the entire disk, I then get errors from parted when using these commands again. And I've gotten other random errors as well. I've read about the errors I received and evidently they are parted bugs, so I've lost confidence in using parted for my script.

I'm struggling a bit with figuring out the script equivalents for cfdisk, sfdisk or fdisk. Suggestions regarding how to script these specific tasks with other partition tools would be very much appreciated.

fdisk: old, reliable, more or less scriptable
cfdisk: pretty, easy to use, hell to script
sfdisk: ugly, very strict about syntax, no-nonsense interface, very scriptable

Just my �0.02

Um -- thanks, but I was hoping for some guidance on how to use those alternatives for the resizing and creation of partitions as shown with parted in my original post.

First: neither of them support resizing a partition by themselves, they only deal with the partition table. So the example above would require

  1. resizing the filesystem (if supported)
  2. recreating the (smaller) partition table entry
  3. creating the new partition
  4. creating a filesystem on the new partition

I'd suggest doing this by hand, using either fdisk or sfdisk, and writing down what you entered. You can then automate this by echo-ing it in, eg

$ echo "c
p
1
1
+1024M
t
b
a
1
w
" | fdisk /dev/sdg

would (assuming the partition table on /dev/sdg is blank) create a primary partiton, starting at cylinder 1, 1G in size, change the type of the partition to 'b' (W95 FAT32), activate the bootflag, and write the partition table to the disk.