scripted partitioning

Topics about disk partitioning (fdisk, parted, gparted, partimage), Volumes Management (lvm, evms, dmraid), Storage, file systems, ...
Post Reply
Tuipveus
Posts: 105
Joined: 18 Nov 2006, 13:09
Contact:

scripted partitioning

Post by Tuipveus » 17 May 2009, 11:00

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:

http://www.oesf.org/forum/index.php?showtopic=24973
Does this have possible rounding-problems? At least it has some other issues (MB/GB).

http://osdir.com/ml/suse.autoinstall/20 ... 00071.html
https://www.ctm.ulpgc.es/~eduardo/grub/grub.html

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

Tuipveus
Posts: 105
Joined: 18 Nov 2006, 13:09
Contact:

Post by Tuipveus » 17 May 2009, 12:55

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
p
3


p
w

and then pass them to fdisk:

fdisk /dev/sda <commands.txt

admin
Site Admin
Posts: 2715
Joined: 17 Jul 2003, 09:44

Post by admin » 18 May 2009, 06:46

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.

Tuipveus
Posts: 105
Joined: 18 Nov 2006, 13:09
Contact:

Post by Tuipveus » 20 May 2009, 20:32

But parted doesn't make partitions exactly same like I ask. It leaves gap between partitions even if I specify everything right.

sciurus
Posts: 15
Joined: 26 May 2008, 22:56

Post by sciurus » 28 May 2009, 19:49

I've found parted to be the easiest way to do this. For imaging I've used it like this

DEVICE="/dev/sda"
LINUXPARTNUM="2"

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

Post Reply