Certification Objective 4.01-Partitioning Hard Disks


It's best to create partitions using Disk Druid during the installation process. This can save you grief as an administrator, and especially during the Red Hat exams. However, mistakes are made. You might forget to create a critical partition during the Installation and Configuration part of the exam, for example.

As suggested by the Red Hat Exam Prep guide, you may need to do more with partitions on an installed system. In the real world, you might need to create a larger /home directory partition for your users. For this purpose, the standard is still the fdisk utility, which is described shortly along with the emerging alternative, parted.

Before you use fdisk to create or revise partitions, you should check your free space and the partitions that are currently mounted. You can do this with the df and mount commands. The following example illustrates how the df command displays the total, used, and available free space on all currently mounted filesystems.

Note the numbers under the 1k-blocks column. In this case (except for the mounted DVD), they add up to about 35GB of allocated space. If your hard drive is larger, you may have unallocated space that you can use for another partition. Just remember to leave room for expansion in appropriate directories, such as /home, /tmp, and /var.

 [root@Enterprise root]# df Filesystem 1k-blocks      Used Available Use% Mounted on /dev/mapper/VolGroup00-LogVol00              9903432   2333948   7058304  25% / /dev/hda3     101105     19821     76063  21% /boot tmpfs         451200         0   451200    0% /dev/shm /dev/mapper/VolGroup00-LogVol02              4951688    149164   4546936   4% /home /dev/md0      302684     10326    276730   4% /tmp /dev/mapper/VolGroup00-LogVol03              4951688    194308   4501792   5% /var /dev/hdc        7384      7384         0 100%                        /media/Red Hat Enterprise Linux Server /dev/hda2   11715984   9509432   2206552  82% /DosD [root@Enterprise root]# 

The second command, mount, includes the filesystem type. In this case, examine the partition represented by device /dev/hda2 mounted with the VFAT file type on the /DosD directory. It provides direct access to the D: drive of the Windows operating system. For the following example, I've set up the data shown from the mount command in columns for clarity; what you actually see from the RHEL command line is less organized.

 [root@Enterprise root]# mount /dev/mapper/VolGroup00-LogVol00 on / type ext3         (rw) proc      on /proc type proc                           (rw) sysfs     on /sys type sysfs                           (rw) devpts    on /dev/pts type devpts                      (rw,gid=5,mode=620) /dev/hda3 on /boot type ext3                           (rw) tmpfs     on /dev/shm type tmpfs                       (rw) /dev/mapper/VolGroup00-LogVol02 on /home type ext3     (rw) /dev/md0  on /tmp type ext3                            (rw) /dev/mapper/VolGroup00-LogVol01 on /var type ext3      (rw) none      on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc    on /var/lib/nfs/rpc_pipefs type rpc_pipefs   (rw) /dev/hdc  on /media/RHEL-5 i386 Disc 1type iso9660     (ro,noexec,nosuid,nodev,uid=500) /dev/hda2 on DosD type vfat                            (rw) [root@Enterprise root]# 

One of the benefits is that you can move and copy files between the Linux and the DOS partitions using standard Linux commands. You cannot, however, run any Windows applications within Linux unless you run a DOS or Windows emulation package such as Wine (www.winehq.org) or CrossOver Office (www.codeweavers.com). Options such as VMware and Xen fall into a different category and are covered in "Studying with a Virtual Machine" in the Online Learning Center (http://highered.mhhe.com/sites/0072264543).

The fdisk Utility

The fdisk utility is a universally available tool that you should know well. There are many commands within fdisk, more in expert mode, but you need to know only the few discussed here.

Though you can modify the physical disk partition layout using many programs, this section explores the Linux implementation of fdisk. FDISK.EXE from DOS has the same name and is also used for creating partitions, but it doesn't incorporate any Linux-compatible features. It also uses a different interface.

Using fdisk: Starting, Getting Help, and Quitting

The following screen output lists commands that show how to start the fdisk program, how to get help, and how to quit the program. The /dev/hda drive is associated with the first PATA/IDE drive on a regular PC. Your computer may have a different hard drive; you can check the output from the df and mount commands for clues.

As you can see, once you start fdisk, it opens its own command line prompt:

 # 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): q 

There are a wide variety of commands associated with fdisk-and more when you run the x command to access fdisk's extra functionality.

Using fdisk: In a Nutshell

At the fdisk command line prompt, start with the print command (p) to print the partition table. This allows you to review the current entries in the partition table. Assuming you have free space, you then create a new (n) partition, either primary (p) or logical (l). If it doesn't already exist, you can also create an extended partition (e) to contain your logical partitions. Remember that you can have up to four primary partitions, which would correspond to numbers 1 through 4. One of the primary partitions can be redesignated as an extended partition. The remaining partitions are logical partitions, numbered 5 and above. The Linux fdisk utility won't allow you to create more than 16 partitions on the drive.

When you assign space to a partition, you're assigning a block of cylinders on that hard disk. If you have free space, the fdisk default starts the new partition at the first available cylinder. The actual size of the partition depends on disk geometry; do not worry about exact size here.

Using fdisk: Deleting Partitions

The following example removes the only configured partition. The sample output screen first starts fdisk. Then you print (p) the current partition table, delete (d) the partition by number (1 in this case), write (w) the changes to the disk, and quit (q) from the program. Needless to say, do not perform this action on any partition where you need the data.

 # fdisk /dev/hdb Command (m for help): p Disk /dev/hdb: 255 heads, 63 sectors, 525 cylinders Units = cylinders of 16065 * 512 bytes Device    Boot    Start       End    Blocks   Id  System /dev/hdb1   *         1       525   4217031    6  FAT16 Command (m for help): d Partition number (1-1): 1 

This is the last chance to change your mind before deleting the current partition. If you want to change your mind, exit from fdisk with the q command. If you're pleased with the changes that you've made and want to make them permanent, proceed with the w command:

 Command (m for help): w 

You did it! Now you have an empty hard disk or hard disk area where you can create the partitions you need.

You no longer have to reboot to get Linux to read the new partition table. Now, the partprobe command rereads the partition table without a reboot.

Using fdisk: Creating Partitions

The following screen output sample shows the steps used to create (n) the first (/boot) partition, make it bootable (a), and then finally write (w) the partition information to the disk. (Note that although you may ask for a 100MB partition, the geometry of the disk may not allow that precise size, as shown in the example.)

 # fdisk /dev/hdb Command (m for help): n Command action    e   extended    p   primary partition (1-4) p Partition number (1-4): First cylinder (1-256, default 1): 1 Last cylinder or +size or +sizeM or +sizeK (2-256,def 256): +100M Command (m for help): a Partition number (1-4): 1 Command (m for help): p Disk /dev/hdb: 255 heads, 63 sectors, 256 cylinders Units = cylinders of 16065 * 512 bytes    Device Boot Start         End     Blocks   Id  System /dev/hdb1   *      1          12      98163   83  Linux Command (m for help): 

Repeat the commands to create any other partitions that you might need. One possible group of partitions is illustrated here:

 Command (m for help): p Disk /dev/hdb: 255 heads, 63 sectors, 256 cylinders Units = cylinders of 16065 * 512 bytes    Device Boot Start   End   Blocks  Id  System /dev/hdb1   *      1     2    16044  83  Linux /dev/hdb2          3    18    64176  82  Linux swap / Solaris /dev/hdb3         19   169 1203300   83  Linux /dev/hdb4        170   250  649782    5  Extended /dev/hdb5        170   201  248682   83  Linux /dev/hdb6        202   257  449232   83  Linux Command (m for help): w 

On the Job 

The number of blocks that you see may vary slightly depending on the size of your hard disk; the number of heads, sectors, and cylinders on that disk; as well as the version of fdisk that you're using.

Using fdisk: A New PC with No Partitions

After installing Linux on a new PC, you'll want to use fdisk to configure additional physical disks attached to the system. For example, if the additional disk is the first disk attached to the secondary IDE controller, run the fdisk /dev/hdc command. Remember the limitations on partitions. If you need more than four partitions on the new physical disk, configure type Primary for the first three partitions, and then Extended for the rest of the disk as partition 4. You can then creating logical partitions 5–16 within the extended partition.

Using fdisk: Creating a Swap Partition

You need to create a partition before you can reassign it as a swap partition. At the fdisk prompt, run the l command. You'll see a large number of file types, listed as hex codes. When you create a partition, fdisk creates a Linux Native type partition by default. As you can see from the output of the l command, the associated hex code is (83).

It's easy to reassign a partition as a swap partition. Run the p command. Remember the number of the partition you want to change. Make sure that partition doesn't contain data that you want to save.

Now run the t command. Type in the number associated with the partition that you want to change. Type in the hex code for the type you want-in this case, 82 for a Linux swap partition. For example, I could run the following sequence of commands to set up a new swap partition on the second IDE hard drive. The commands that I type are in boldface. The details of what you see depend on the partitions that you may have created. It'll be a 1GB swap space on the first primary partition (/dev/hdb1).

 # fdisk /dev/hdb Command (m for help): n Command action      e  extended      p  primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-10402, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-10402, default 10402): +1000M Command (m for help): p Disk /dev/hdb: 5368 MB, 5368709120 bytes 16 heads, 63 sectors/track, 10402 cylinders Units = cylinders of 1008 * 512 = 516096 bytes    Device Boot    Start       End    Blocks    Id  System /dev/hdb1             1      1939    977224+   83  Linux Command (m for help): t Selected partition 1 Hex code (type L to list codes): 82 Changed system type of partition 1 to 82 (Linux swap / Solaris) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. # 

The fdisk utility doesn't actually write the changes to your hard disk until you run the write (w) command. You have a chance to cancel your changes with the quit (q) command. To make sure Linux rereads the partition table after fdisk writes it, run the partprobe command.

The parted Utility

The parted utility is becoming increasingly popular. It's an excellent tool developed by the GNU foundation. As with fdisk, you can use it to create, check, and destroy partitions, but it can do more. You can also use it to resize and copy partitions, as well as the filesystems contained therein. For the latest information, see www.gnu.org/software/parted.

On the Job 

It's much easier to make a mistake with parted . For example, I accidentally ran the mklabel command from the (parted) prompt on an existing RHEL system. It deleted all existing partitions. Fortunately, I had a snapshot of this system on a VMware server and was able to recover quickly with little trouble.

During our discussion of parted, we'll proceed from section to section assuming that parted is still open with the following prompt:

 (parted) 

If you use parted and then check your partitions with fdisk, you might get errors such as:

 Partition 1 does not end on cylinder boundary. 

Don't worry about it. While fdisk partitions are associated with hard drive cylinders, parted is not so limited.

Using parted: Starting, Getting Help, and Quitting

The next screen output lists commands that show how to start the parted utility, how to get help, and how to quit the program. In this case, the /dev/sdb drive is associated with the second SATA drive on a regular PC. Your computer may have a different hard drive; you can check the output from the df and mount commands for clues.

As you can see in Figure 4-1, once you start parted, it opens its own command line prompt. The explanations are line-wrapped due to limitations in formatting in this book.

image from book
Figure 4-1: parted Command Options

As you can see, a wide variety of commands are available within the parted interface. If you're familiar with fdisk, you can see that parted can do more: you can even format and resize partitions from parted. Unfortunately, the format functionality is limited and does not allow you to create or resize ext3 partitions, at least as of this writing. The current Exam Prep guide lists only resizing logical volumes as an RHCE requirement.

Using parted: In a Nutshell

At the parted command line prompt, start with the print the partition table command. This allows you to review the current entries in the partition table, assuming one exists. Assuming you have free space, you then make a new (mkpart) partition or even make and format the filesystem (mkpartfs). If you need more information about command options, use the help command with it; here's an example:

 (parted) help mkpart mkpart PART-TYPE [FS-TYPE] START END     make a partition       PART-TYPE is one of: primary, logical, extended       FS-TYPE is one of: ext3, ext2, fat32, fat16, hfsx,       hfs+,  hfs, jfs, linux-swap,ntfs, reiserfs, hp-ufs,       sun-ufs, xfs, apfs2, apfs1, asfs, amufs5, amufs4,       amufs3, amufs2, amufs1, amufs0, amufs, affs7, affs6,       affs5, affs4, affs3, affs2, affs1, affs0       START and END are disk locations, such as 4GB or 10%.       Negative values count from the end of the disk.       For example, -1s specifies exactly the last sector.       mkpart makes a partition without creating a new       file system on the partition.       FS-TYPE may be specified to set an appropriate       partition ID. 

If that's too much for you, just run the command. You'll be prompted for the necessary information. Remember that you can have up to four primary partitions, corresponding to numbers 1 through 4. One of the primary partitions can be redesignated as an extended partition. The remaining partitions are logical partitions, numbered 5 and above. While the Linux parted utility allows you to create more than 15 partitions, in this case, anything beyond /dev/sdb15 is not recognized by Linux.

Using parted: Deleting Partitions

Deleting partitions is easy. All you need to do from the (parted) prompt is use the rm command to delete the partition that you no longer need.

Of course, before deleting any partition, you should:

  • Save any data you need from that partition.

  • Unmount the partition.

  • Make sure it isn't configured in /etc/fstab, so Linux doesn't try to mount it the next time you boot.

  • After starting parted, run the print command to identify the partition you want to delete, as well as its ID number.

For example, if you want to delete partition /dev/sdb10 from the (parted) prompt, run the following command:

 (parted) rm 10 

Using parted: A New PC (or Hard Drive) with No Partitions

Whenever you install a new hard drive, you may need to create a new partition table. For example, after I add a new hard drive to my virtual RHEL system, just about any command I run in parted leads to the following message:

 Error: Unable to open /dev/sdb - unrecognised disk label. 

Before I can do anything else with this drive, I need to create a label. As shown from the list of available commands, I can do so with the mklabel command. As strange as it sounds, the default label to be used for Linux is msdos; here are the commands I run:

 (parted) mklabel New disk label type? msdos 

On the Job 

Be careful! Never run mklabel from the (parted) prompt on a hard drive that stores data that you need.

Now you can create a new partition. Let me show you how mkpart works on the new hard drive. Naturally, if an extended partition already exists, you'll be able to create a logical partition.

 (parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? ext3 Start? 0 End? 100MB 

Now you can review the results:

 (parted) print Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number  Start   End     Size   Type     File system  Flags  1      0.51kB  100MB   100MB  primary  ext2 

If this is the first partition you've created, the filesystem type is empty; otherwise it's ext2, even if you've specified another format such as ext3. Unfortunately, parted does not work perfectly, and it does not always create ext3 filesystems from the command line interface. (It works if an ext3 filesystem is already on the hard drive.)

If you now exit from parted, you can reboot or run the partprobe command to get Linux to read the new partition table. For the purpose of this chapter, don't exit from parted just yet.

On the Job 

The GUI parted tools (GParted, QTParted) do support formatting to a wider variety of filesystem formats, even though they're just "front ends" to parted. They may be available from third-party repositories such as those described in Chapter 5.

Using parted: Creating a Swap Partition

Now let's repeat the process to create a swap partition. Make the start of the new partition 1MB after the end of the previous partition. You can still use the same commands, just substitute the linux-swap file system type as appropriate:

 (parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? linux-swap Start? 101MB End? 1100MB 

Now you can review the result:

 (parted) print Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number  Start   End    Size   Type     File system  Flags  1      0.51kB  100MB  100MB  primary  ext2  2      101MB   1100MB 1000MB primary 

Let's repeat the process, creating a regular partition after the swap partition:

 (parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? ext2 Start? 1101MB End? 2100MB 

If you exit from parted, you can reboot, or run the partprobe command to make sure Linux reads the new partition table. Now go ahead and exit from parted. After exiting, you can implement your changes; format and then activate the swap partition on /dev/sdb2 using the following commands:

 (parted) quit # partprobe # mkswap /dev/sdb2 # swapon /dev/sdb2 

On the Job 

Sometimes you'll see errors when you run the partprobe command, even on a correctly configured system. For example, if you haven't put a disk in a floppy drive, you'll see errors related to the associated device (usually fd0). If the disk in your CD/DVD drive is read-only (as are most CD/DVD disks), you'll see an error message about being unable to open read-write.

Now the new regular Linux partition is formatted to ext2. You can change it to ext3 with the following command:

 # tune2fs -j /dev/sdb2 



RHCE Red Hat Certified Engineer Linux Study Guide (Exam RH302)
Linux Patch Management: Keeping Linux Systems Up To Date
ISBN: 0132366754
EAN: 2147483647
Year: 2004
Pages: 227
Authors: Michael Jang

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net