Using Sfdisk and Perl to fdisk a hard drive LG #46


Future updates for this article will be located at


  2. /usr/src/linux/Documentation/ide.txt
  3. man sfdisk
  4. man lilo
  5. man lilo.conf
  7. man hdparm # side issue — you might find this useful for other things.

Introduction to Sfdisk the Perl Expect module

I am really getting mad at myself. I only tested this with RedHat 6.0 (again). Debian would be a cool alternative.

The purpose of the article is to explain how to automate the fdisking of a hard drive by primarily using Sfdisk and the Perl Expect module. Why would you want to do this? Well, it can be one way of many ways to solve the problem where you need to have your bootable files for Linux before the 1024th cylinder. There are other ways to solve the boot problem, but we will stick to the cylinder method for this article. It also can be used to automatically partition new hard drives.

Sfdisk is a tool that allows you to change the partitioning of your hard disks through scripts. It also lets you get information about your hard drives. Although it is a pretty cool program, it does have limitations. It works and it has most of the necessary powers of fdisk, but not all of it.

Disk Druid is a program used by RedHat to initialize hard drives (change their geometry) before you install Linux to the hard drive.

Perl is a very very cool programming language. The « Expect » perl module is a module in perl which adds a relatively user-friendly way of making a script which automates commands. In other words, when you execute an Expect script, it types commands to the computer as though you were typing them — like a macro but more advanced and usable for any console based program. Perl is just so cool to use with everything.

There are two other modules you have to install with the Expect perl module. IO-Stty-.02.tar.gz

READ  Backing Up Your Data LG #32


How to use Sfdisk to get information about your hard drive.

With RedHat 6.0, they included sfdisk by default. I guess the BSD games had to go to save space. Anyways, here are some simple commands to get information about the master hard drive on your primary controller to an PC compatible computer.

To get the geometry of your hard drive,

/sbin/sfdisk -g /dev/hdd

Here is how to get the size (in bytes) of the total space of your hard drive,

/sbin/sfdisk -s /dev/hdd

Here is how to change the id of a partition 5 on your first hard drive to the Linux partition,

sfdisk --change-id /dev/hdd 5 83

How to use Sfdisk to create or repartition your hard drive.

Well, one powerful feature of sfdisk is to repartition your hard drive or create new partitions. Create a file called « » which has entries in the following format, one per line, and the fields are comma delimited:

Start, Size, ID, Bootable

Start = cylinder to start at (first available cylinder if left blank), Size = number of cylinders (all if left blank), ID = type of partition (Linux, Swap, MSDOS, or other), and Bootable = if this partition is bootable. There are other options, but we won’t get into them in this article.

To make it so you have 1 partition of 136 cylinders, a second partition with 254 cylinders of the SWAP ID (82), and a third Linux partition bootable that grabs the rest of the space (ID = 83), make a file list this,


and then issue this command to take this configuration and execute it on your slave hard drive on your secondary controller

/sbin/sfdisk /dev/hdd