"Linux Gazette...making Linux just a little more fun!"


Using Sfdisk and Perl to fdisk a hard drive

By


Index:

  1. Resources
  2. Introduction to Sfdisk and the Perl Expect module
  3. How to use Sfdisk to get information about your hard drive.
  4. How to use Sfdisk to create or repartition your hard drive.
  5. How to use Expect to delete all partitions on a hard drive.
  6. How to use Expect to change the geometry of a hard drive.
  7. Comments
Future updates for this article will be located at http://www.tcu-inc.com/mark/articles/Sfdisk.html.

Resources

  1. http://www.perl.com/CPAN/authors/id/AUSCHUTZ/Expect.pm-1.07.tar.gz
  2. /usr/src/linux/Documentation/ide.txt
  3. man sfdisk
  4. man lilo
  5. man lilo.conf
  6. http://metalab.unc.edu/LDP/HOWTO/mini/LILO.html
  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
IO-Tty-0.02.tar.gz


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 "Test.data" 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,

1,136
101,254,82
201,,83,*

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 << Test.data
and then issue this command to see what you did
/sbin/sfdisk /dev/hdd 

Again, it is highly recommended you read the manpage to figure out how to format this data file. Any fields you leave blank have default values described above.


How to use Expect to delete all partitions on a hard drive.

The Perl script to delete all the partitions is not something to be fooled around with. If you test it and screw up your hard drive, it is your fault and not mine even if my script doesn't work right. It is your risk.

Basically, with the Expect module, you can automate certain takes, which can be used later for a more sophisticated program - hint, hint of what is to come.

Save the script to "Dufus_Move.pl" and issue the command

chmod 755 Dufus_Move.pl
and then to do it on your slave hard drive on your secondary controller,
./Dufus_Move.pl d

How to use Expect to change the geometry of a hard drive.

The Perl script to change the cylinders of a hard drive is not something to be fooled around with. If you test it and screw up your hard drive, it is your fault and not mine even if my script doesn't work right. It is your risk.

Anyways, that script maximizes the number of cylinders.

Save the script to "Change_Cylinders.pl" and issue the command

 chmod 755 Change_Cylinders.pl 
and then to do it on your slave hard drive on your secondary controller,
./Change_Cylinders.pl d

Comments

  1. Sfdisk is a pretty cool program, but creating a perl script as an interface for Fdisk is even cooler. Better yet, why don't they script fdisk? I wonder who "they" are.
  2. If Sfdisk could redo the geometry of a hard drive, I would have not needed anything else. Perhaps it is possible with Sfdisk, but I didn't see it.
  3. This was the first time I really used Expect for more than just simple tasks. I didn't list here everything I did.

works as a database programming assistant at The Computer Underground and as a book binder for ZING and as a geek for linux.com. Edited using Nedit and ispell.


Copyright © 1999, Mark Nielsen
Published in Issue 46 of Linux Gazette, October 1999


[ TABLE OF CONTENTS ] [ FRONT PAGE ]  Back [ Linux Gazette FAQ ]  Next