Automating hard drive partitioning. (tummy.com, ltd. Journal Entry)
tummy.com: we do linux

Friday December 05, 2008 at 11:10
Subject: Automating hard drive partitioning.
Keywords: fdisk, Technical
Posted by: Sean Reifschneider

It sure would be nice if any of the installers had a "clone partition layout from another drive" option. We often end up setting up multiple drives with similar layouts, either because of RAID or LVM, and using the partitioning GUI can be painful during the install.

We were doing this the other day on a system with 4 drives, and I used something that I discovered in some other automated partitioning work we had recently done to automate the setup. Read on for more details.

I guess I should have thought of this earlier, but I guess things like the Debian apt tools which throw away pre-typed data from stdin have conditioned me to be skeptical... After some testing, I found that it works great to use "printf" to pass commands in to "fdisk". For example:

printf 'n\np\n1\n\n+100M\nt\nfd\na\nn\np\n2\n\n\nt\n2\nfd\np\nw\n' |
fdisk /dev/sda

Of course, you have to be pretty familiar with what fdisk wants, and it can be quirky. In the above example, I have accounted for the fact that if only one partition exists, fdisk skips asking for that when setting the partition type or activating it.

The above example does the following:

  • New primary partition number 1. (n, p, 1)
  • Use the default starting position and make it 100MB in size.
  • Set the partition type to "fd", RAID auto-detect. (t, fd)
  • Set the partition as bootable. (a)
  • Create a new primary partition number 2. (n, p, 2)
  • Use the default starting and ending positions.
  • Set partition 2 to type to "fd", RAID auto-detect. (t, fd)
  • Print the partition table. (p)
  • Write and exit. (w)

Another nice thing about this is that fdisk takes 5 seconds or so to write the partition table. The other day I had 10 drives to partition in my home storage server so I did something like the above in a for loop with backgrounded commands to do all of them in 5 seconds. Saving me 45 seconds of my life. :-)

for HD in /dev/hd[a-j]; do printf $FDISKCMDS | fdisk $HD > done

Loving it.
(Post Reply)

Comment
Steve Webb
Subject: sfdisk
sfdisk can write a partition table layout to a human-readable file. You can figure out the format and write scripts to do your partitioning programatically if you'd like. I did this when I made my own distro a while back to partition based on hard drive size/type and it worked great. "man sfdisk" for more info.
Comment
taggart
Subject: d-i partitioning
Hey cool trick!

BTW are you using debian-installer preseeding? It has lots of options for partitioning.

http://d-i.alioth.debian.org/manual/en.i386/apbs04.html#preseed-partman

I'm using d-i preseeding for most of my installs now, here are my notes (although I'm not doing anything fancy with partitioning yet)

http://lackof.org/taggart/hacking/d-i_preseed/

Comment
Author: Sean Reifschneider
Subject: sfdisk and d-i preload.
As far as sfdisk goes, it is a great tool but it is not available on the CentOS install/rescue discs, which is part of why I used printf/fdisk instead. fdisk is just more universally available than sfdisk, but if sfdisk is available it's definitely worth using.

Thanks for the pointer on d-i preload, Matt. I haven't used it, but I definitely should look at it. One of my biggest annoyances about the Debian install process is just that it requires so much attention. You can't just answer some questions and then come back to a completed install, you have to keep coming back and do a little more and then wait, a little more and then wait.

I know there are some ways around that, but I'm just right at that point where I don't do enough installs that it's worth figuring it out, but I do enough installs that it's annoying. :-)

I really like that Red Hat based releases will write a kickstart file which encapsulates the install you have just done, but I've had mixed luck with modifying the kickstart file for other purposes, probably half the times it just causes the next install to break in subtle but hard to figure out ways. Though I guess I do often do a very similar install.

Thanks,
Sean

Comment
Ian! D. Allen
Subject: cloning a disk partition table
Since the partition table of a disk is kept in the first block for the types of disks I've used, I've used this shorthand to clone a partition table and make the kernel see the new table:

# dd if=/dev/sda of=/dev/sdb count=1
# fdisk /dev/sdb
Command (m for help): W
#
Comment
Leolo
Subject: sfdisk
What you want is sfdisk. Something like :

   sfdisk -d /dev/sda | sed -e 's/sda/sdb/' | sdisk /dev/sdb