Your Linux Data Center Experts

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.

comments powered by Disqus

Join our other satisfied clients. Contact us today.