Hard Disk Operations in Linux

Hard Disk Operations in Linux

You must perform some disk operations to install and use Linux on your system. Chapter 2 explains some of the disk operations you perform when you set up Linux. The next few sections provide some additional information about these disk operations.

When you first get a PC, the hard disk usually is set up as one huge partition, and DOS and Windows are already installed on it. (If you have bought your PC recently, it probably comes with Windows 2000 or Windows XP preinstalled.) To install Red Hat Linux, you have to start by creating at least three partitions for Linux: one for the swap space, a /boot partition for the Linux kernel files, and a /partition for the Linux file system.

If you want to retain the existing Windows operating system, you have to resize the Windows partition and create disk space for Red Hat Linux. When you install Red Hat Linux, you can use the unused disk space for all the Linux partitions.

Resizing Partitions

If you do not want to go through the trouble of reinstalling Windows, you have to alter the existing partition somehow. FIPS, which can split an existing FAT or FAT32 partition into two separate partitions, enables you to perform this task. By the way, FAT and FAT32 partitions were used in MD-DOS and Windows 95/98/Me. Windows NT, 2000, and XP also support FAT and FAT32, but they typically use the NT file system (NTFS). Chapter 2 describes how to use FIPS. You can also use one of the following utilities to repartition your hard disk:

  • PowerQuest's PartitionMagic-This commercial utility enables you to resize, split, merge, delete, undelete, and convert partitions without destroying existing data. PartitionMagic works on Windows XP and can resize partitions containing FAT, FAT32, NTFS, and Linux ext2 file systems. For more information, visit http://www.powerquest.com.

  • Acronis PartitionExpert-This commercial utility enables you to resize, copy, and move partitions without losing data. It can resize partitions containing FAT, FAT32, and NTFS, and Linux ext2 file systems, among others. For more information, visit http://www.acronis.com.

  • GNU parted-Red Hat Linux includes the GNU parted utility that can manipulate FAT, FAT32, and Linux ext2 partitions. Using parted, you can resize partitions from within Linux. For more information, visit http://www.gnu.org/software/parted/.

Partitioning Using fdisk

Partitioning the disk involves creating several smaller logical devices within a single hard disk. In Linux, you can use the partitioning program fdisk to view and alter a disk's partition table.

Even if you have already partitioned your hard disk, you can always run fdisk just to see the current partition table of a hard disk. If you have an IDE disk drive with the device name /dev/hda, for example, you can look at its partition table with fdisk as follows:

/sbin/fdisk /dev/hda
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p

Disk /dev/hda: 4320 MB, 4320862208 bytes
255 heads, 63 sectors/track, 525 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *         1        13    104391   83  Linux
/dev/hda2            14       501   3919860   83  Linux
/dev/hda3           502       525    192780   82  Linux swap

Command (m for help): q

The m command shows you a list of the single-letter commands that fdisk accepts. You can see the current partition table with a p command. This example's IDE disk has three partitions, all for Linux.

The Id field in the table of partitions the fdisk program prints (when you type p at the fdisk prompt) is a number that denotes a partition type. That partition ID is a hexadecimal number. If you want to see a list of all known partition IDs, type l (a lowercase L) at the fdisk prompt, as follows:

Command (m for help): l
 0  Empty           1c  Hidden Win95 FA 70  DiskSecure Mult bb  Boot Wizard hid
 1  FAT12           1e  Hidden Win95 FA 75  PC/IX           be  Solaris boot
 2  XENIX root      24  NEC DOS         80  Old Minix       c1  DRDOS/sec (FAT-
 3  XENIX usr       39  Plan 9          81  Minix / old Lin c4  DRDOS/sec (FAT-
 4  FAT16 <32M      3c  PartitionMagic  82  Linux swap      c6  DRDOS/sec (FAT-
 5  Extended        40  Venix 80286     83  Linux           c7  Syrinx
 6  FAT16           41  PPC PReP Boot   84  OS/2 hidden C:  da  Non-FS data
 7  HPFS/NTFS       42  SFS             85  Linux extended  db  CP/M / CTOS / .
 8  AIX             4d  QNX4.x          86  NTFS volume set de  Dell Utility
 9  AIX bootable    4e  QNX4.x 2nd part 87  NTFS volume set df  BootIt
 a  OS/2 Boot Manag 4f  QNX4.x 3rd part 8e  Linux LVM       e1  DOS access
 b  Win95 FAT32     50  OnTrack DM      93  Amoeba          e3  DOS R/O
 c  Win95 FAT32 (LB 51  OnTrack DM6 Aux 94  Amoeba BBT      e4  SpeedStor
 e  Win95 FAT16 (LB 52  CP/M            9f  BSD/OS          eb  BeOS fs
 f  Win95 Ext'd (LB 53  OnTrack DM6 Aux a0  IBM Thinkpad hi ee  EFI GPT
10  OPUS            54  OnTrackDM6      a5  FreeBSD         ef  EFI (FAT-12/16/
11  Hidden FAT12    55  EZ-Drive        a6  OpenBSD         f0  Linux/PA-RISC b
12  Compaq diagnost 56  Golden Bow      a7  NeXTSTEP        f1  SpeedStor
14  Hidden FAT16 <3 5c  Priam Edisk     a8  Darwin UFS      f4  SpeedStor
16  Hidden FAT16    61  SpeedStor       a9  NetBSD          f2  DOS secondary
17  Hidden HPFS/NTF 63  GNU HURD or Sys ab  Darwin boot     fd  Linux raid auto
18  AST SmartSleep  64  Novell Netware  b7  BSDI fs         fe  LANstep
1b  Hidden Win95 FA 65  Novell Netware  b8  BSDI swap       ff  BBT

Booting from the Hard Disk

To automatically boot Linux from a hard disk, you need a boot loader. Red Hat Linux comes with two boot loaders-the Linux Loader (LILO) and the GNU Grand Unified Bootloader (GRUB).

The boot loader programs usually reside in the master boot record of a disk and are the first to be loaded. The boot loader program, in turn, prompts you for the operating system to start (which typically means a disk partition from which to boot). Starting an operating system basically involves loading that operating system's main program into memory and running it. For Linux, this step involves loading the Linux kernel into memory and giving control to the kernel.


GRUB is the default boot loader for Red Hat Linux, but you can opt to install LILO instead of GRUB. You get the option to select the boot loader as you install Red Hat Linux following the steps outlined in Chapter 2.

If you decide to install LILO, you'll find the LILO documentation in the /usr/share/doc directory on your system. Type cd /usr/share/doc/lilo* to change to the LILO documentation directory (the exact name depends on the current version of LILO). You should consult the README file in that directory for the latest word on installing and configuring LILO.

To configure LILO to boot an operating system, you have to perform two steps:

  1. Prepare the LILO configuration file, which contains information that is necessary for installing LILO. The default LILO configuration file is /etc/lilo.conf.

  2. Run the /sbin/lilo program (referred to as the map installer in LILO's README file) to update the boot sector and create the /boot/map file, which contains information that LILO uses during the boot process.


    Chapter 21 describes how to configure GRUB to install a new kernel. You can consult that chapter to learn more about GRUB.

If, during installation, you forget to assign a label to the Windows XP partition in the Boot Loader Installation screen, GRUB will not show the Windows XP partition as an option when it starts. That leaves with you with the problem that you cannot boot Windows XP anymore, even though the XP partition is intact. To recover from this problem, you have to edit the GRUB configuration file, /etc/grub.conf.

Insider Insight 

Assuming that the XP partition is /dev/hda2, here's what you add to the end of the /etc/grub.conf: to enable GRUB to boot the Windows XP partition:

title Windows XP
        root (hd0,1)
        chainloader +1

For most PCs that come with Windows XP preinstalled, the second disk partition (/dev/hda2) is the one with the Windows XP file system; the first disk partition is a hidden one used to store files needed to install Windows XP. You do not have to restart GRUB or do anything special for these changes to take effect. Simply save the /etc/grub.conf file and reboot. This time GRUB should display a menu that includes Windows XP as an option. To boot Windows XP, all you have to do is select that option.

Creating Swap Space

Swap space is a disk partition Linux uses as an extension of its memory. When some memory-resident data is not needed immediately, Linux stores that data in the swap space. To create the swap space, you have to create a disk partition using fdisk. Make sure that you set the type of that disk partition to Linux swap (partition ID 82 in hexadecimal). Typically, you set up the swap partition and turn on swapping as you install Red Hat Linux from this book's CD-ROMs.

If you have to set up the swap space outside the installation program, you have to use the mkswap command to initialize the swap partition. You need the size of the swap partition (in number of blocks) before you use the mkswap command. Use the Linux fdisk program, and type the p command at the fdisk prompt to find the size of the swap partition.

For example, to initialize /dev/hda4 as a swap partition, type the following command:

/sbin/mkswap -c /dev/hda4 192772

In this case, 192772 is the block size of the swap partition. You can skip the size parameter because the mkswap command can determine the size from the partition table.

The size of the swap partition depends on the amount of virtual memory you need. To Linux, the total amount of memory in your PC is the combined total of the swap partition's size and the amount of physical RAM. Although there is no formula to tell you how much virtual memory you need, the conventional wisdom is that you should have at least as much swap space as physical memory.

You can have a swap partition as large as 2GB, and you can have up to eight swap partitions. All you have to do is run the mkswap command for each swap partition you create.

After mkswap finishes, use the /sbin/swapon command to turn on swapping. Linux then begins to use the swap space.

The /proc/swaps file contains information about the swap spaces your Linux system is using. For example, here's what I get when I check /proc/swaps on my system:

cat /proc/swaps
Filename                        Type            Size    Used    Priority
/dev/hda3                       partition       192772  59528   -1

The output shows the current swap spaces. Although it is customary to use a disk partition as the swap space, you can also use a file as swap space. The first column in the list of swap spaces is labeled Filename because the swap space can be a file. If you want to use a file as swap space, create the file with a command, such as the following:

dd if=/dev/zero of=swapfile bs=1024 count=131072

This creates a file named swapfile of size 131,072. Type /sbin/mkswap -c swapfile to initialize that file, then type /sbin/swapon swapfile to use the file for swapping.

Insider Insight 

To ensure that Linux uses the swap space every time it boots, you need a line in the /etc/fstab file that indicates the swap partition's name. If the swap partition is /dev/hda3, for example, add the following line to /etc/fstab:

/dev/hda3       swap       swap    defaults     0 0

If you create a swap space when you install Linux, the installation program adds the appropriate line in the /etc/fstab file. You may need to add such a line if you create additional swap spaces later.

If you put a partition's name in /etc/fstab but forget to run mkswap on that partition, Linux displays the following error message:

Unable to find swap-space signature

The fix is to run mkswap to initialize the swap partition.

Creating File Systems

To use a disk partition in Linux, you have to create a file system on that partition. You can think of this procedure as formatting the partition for Linux. When you install Linux by following the steps described in Chapter 2, one of the steps creates the file system; the setup program asks you whether or not you want to format a partition. For a Linux partition, the setup program uses the mke2fs command to create a Linux file system.

Linux supports several types of file systems, including the following:

  • MS-DOS file system-This DOS file system is based on the file allocation table (FAT). This file system type is designated by the keyword msdos.

  • Microsoft Windows file system with support for long filenames-This is a DOS-compatible FAT file system with support for long filenames. This file type is identified by the keyword vfat. You should use this file system to access all DOS/Windows disks from Linux.

  • Minix file system-Minix is the original UNIX clone that inspired the creation of Linux. Linux started by using the Minix file system, which limits filenames to 14 or 30 characters. The minix keyword identifies this file-system type.

  • Extended file system-This old Linux file system goes by the keyword ext. You should not use this file system anymore.

  • Second extended file system-This is the latest and greatest Linux file system. The keyword ext2 refers to this file-system type. You can use longer filenames in this file system.

  • Third extended file system-This is an enhancement of the ext2 file system with journaling to enable rapid recovery in case of any errors. The keyword ext3 refers to this file-system type.

The Red Hat Linux installation program automatically creates an ext3 file system on the Linux partition. To create an ext3 file system manually, you have to use the mke2fs command as follows:

/sbin/mke2fs -c -j /dev/hda3

This command creates an ext3 file system on the /dev/hda3 partition. The mke2fs command automatically figures out the size of the partition and uses it to size the file system. The -c option forces mke2fs to check for bad blocks (using a fast read-only test) before creating the file system. The -j option causes mke2fs to create the file system with an ext3 journal.

Insider Insight 

When you install Linux on a hard disk that uses standard IDE, MFM, or RLL controllers, always install the ext3 file system and use the bad-block-checking options when you create the file system.