Page 1 of 1

scripted partitioning

Posted: 17 May 2009, 11:00
by Tuipveus
I have wondered if there are any easy way to script partitioning. Currently I have made dump of partitiontable which contains partitions sda1, sda2 and sda3.

I managed like to restore sda1 and sda2 from dump, but I would like to make a script making sda3 to use all available space on device.

cfdisk and fdisk are not scriptable, so my choices are either sfdisk or parted.

With sfdisk I am not able to use all available space automatically and calculating correct amount of cylinders or sectors seems to be impossible. Parted seems to be easier, but parted always leaves about 8 megabyte gap between sda2 and sda3.

Linux has usually been very scriptable, but with partitions that doesn't seem to be true. I think only exact solution would be to make script with "expect" and use cfdisk, which is about the only program which can build correct partition table without hassle.

I found following links usefull, but still not giving solutions:
Does this have possible rounding-problems? At least it has some other issues (MB/GB). ... 00071.html

Question: How to make new partition to sda -device, to use all available space left in there?

Posted: 17 May 2009, 12:55
by Tuipveus
After googling couple of days, I asked from my friend and he got the solution with singel googling. Doh. :oops:

All I have to do is write key-presses for fdisk -command to text-file.
commands.txt wrote:n


and then pass them to fdisk:

fdisk /dev/sda <commands.txt

Posted: 18 May 2009, 06:46
by admin
I think you can also pass commands to parted, which is quite powerful. You can use something like "mkpart primary ext2 0 50%" if you have various disks of different sizes and you want to create a partition that use 50% of the disk.

Posted: 20 May 2009, 20:32
by Tuipveus
But parted doesn't make partitions exactly same like I ask. It leaves gap between partitions even if I specify everything right.

Posted: 28 May 2009, 19:49
by sciurus
I've found parted to be the easiest way to do this. For imaging I've used it like this


parted -s $DEVICE mklabel msdos
parted -s $DEVICE mkpart primary linux-swap 0GB 0.5GB
parted -s $DEVICE mkpart primary ext2 0.5GB 100%
parted -s $DEVICE set $LINUXPARTNUM boot on