Scripting Partition Creation In Linux Using Fdisk
This post details a way to script partition creation under Linux using fdisk. I was originally trying to use sfdisk to perform this task as it is designed to be scriptable, reading lines of the form
where each line fills one partition descripton. The problem for me was that sfdisk only appears to accept partition sizes defined in cylinders; for partitioning similar sized disks with different geometries this is inconvenient.
Even though it is less elegant, there’s no reason one can’t script partition creation with fdisk. The way I did it was to partition manually first, noting down the input, and then to copy the input to a file to be passed as input to fdisk. e.g.
fdisk /dev/hda < fdisk.input
where fdisk.input might look something like e.g.
n p 1 +15000M n p 2 +2000M t 2 82 w
This creates 2 primary partitions, approximately 15GB and 2GB, and changes the type of partition 2 to swap. The partition table is then written out to disk.
If you run fdisk and get lost, typing h within fdisk brings up the command actions:
Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)
See the -uM switch for sfdisk.
echo “,64,83,*” >> parttable
echo “,512,82,-” >> parttable
echo “,,8e,-” >> parttable
cat parttable | sfdisk /dev/hda -uM
Does the trick.
Comment by blank3 — August 18, 2006 @ 4:43 am
Thanks for the tips dude!
Comment by Kristian Hermansen — September 26, 2006 @ 10:16 am
Thank you! I’ve been trying to figure this out for months. Of course, now I feel like an idiot for not thinking of this sooner, but at least it’s solved.
Comment by Brian — September 11, 2007 @ 2:39 pm
Thanks a lot it helped me too….
Comment by vijay — October 16, 2007 @ 11:01 pm