Lab #4: Logical Volume Management
Date: January 28, 2007
Points: 10

We're going to create logical volumes with the Linux LVM. LVM is a free logical volume manager that comes with most Linux distributions, offering capabilities such as dynamic volume creation and resizing without rebooting, volumes that span multiple disks, and snapshotting.

  1. Create an extended partition that contains the entire remainder of the disk. You need to do this since the partition table on the MBR only has space for four partitions, and you are already using three of those slots for /, /var, and swap. This extended partition will serve as the container for all future partitions. Create three new 2G partitions.
    fdisk /dev/sda
    
  2. Reboot so the OS can recognize the new partitions. This reboot is one of the disadvantages of creating disk volumes using the IDE partition table stored in the master boot record (MBR.) This is the only reboot we'll have to do in this lab, since we'll be working with logical volumes.
    reboot
    
  3. Set up two of the new partitions as LVM physical volumes (PVs). You'll have to determine the correct partition numbers M and N from fdisk.
    pvcreate /dev/sdaM
    pvcreate /dev/sdaN
    
  4. Create a new logical volume group consisting of the two PVs above. We can create one or more logical volumes using this volume group.
    vgcreate cit470 /dev/sdaM /dev/sdaN
    
  5. Create a 4GB logical volume.
    lvcreate -n my_isos --size 4G cit470
    
    That fails due to lack of disk space, despite the fact that we're using two 2GB physical volumes to build the volume group. Explain why in your lab.txt file using the information about your volume group obtained using the following command:
    vgdisplay -v cit470
    

    Now create a logical volume using all of the available space, using the lvcreate command with the -l option with the total number of physical extents (PEs) instead of using the --size option. Include this command in your lab.txt file.

  6. Check allocation (Alloc PE / Size field). Include allocation in your lab.txt file.
    vgdisplay cit470 | grep "Alloc PE"
    
  7. In the same way that Linux represents regular IDE partitions as disk devices, it also represents logical volumes as disk devices. The volume group is represented by a directory, with the logical volume devices stored within it.
    ls -l /dev/cit470
    vgdisplay -v cit470
    
  8. Create an ext3fs filesystem on the logical volume. Since ext3fs is identical to ext2fs with the exception of the journal, we create it with the mke2fs command with the -j option to create the journal.
    mke2fs -v -j /dev/cit470/my_isos
    
  9. Mount the new filesystem onto the temporary mount directory /mnt.
    mount -t ext3 /dev/cit470/my_isos /mnt
    
  10. Check that the mount succeeded by adding some files and using df.
    cp -a /boot /mnt
    df -h /mnt
    ls -l /mnt
    
  11. Unmount your volume and add it to /etc/fstab.
    umount /mnt
    mkdir /iso                          # Create new mount point
    vim /etc/fstab                      # Add entry for /dev/cit470/my_isos
    mount -a                            # Check that new fstab entry works
    df -h /iso
    
  12. Expand your new filesystem by another 2G. First, we need to create a new physical volume and add it to the volume group. You'll need to determine the physical volume number P from fdisk.
    pvcreate /dev/sdaP
    vgextend cit470 /dev/sdaP
    vgdisplay -v
    
  13. Next, we extend the size of the logical volume by the number of physical extents (4MB blocks) that exist in the new physical volume.
    umount /iso
    vgdisplay cit470 | grep Free        # Find number of new PEs
    lvextend -l+NUMBER_OF_NEW_PEs /dev/cit470/my_isos
    vgdisplay -v cit470                 # Verify logical volume is bigger
    
  14. Mount your filesystem again and notice that it's not any bigger. We extended the volume, but the filesystem remains the same size since we haven't changed it. This is to be expected, since what we did was essentially the same as instantly turning a 4GB hard disk into a 6GB hard disk without altering the disk contents or reformatting.
    mount /iso
    df -h /iso
    
  15. Now let's extend the filesystem. The first command will fail as there is not 6GB of space available in the volume group, but it will return the maximum number of blocks you can expand the filesystem to. Use that number of blocks in the second command to successfully resize the filesystem to cover the entire my_isos logical volume.
    resize2fs /dev/cit470/my_isos 6G
    resize2fs /dev/cit470/my_isos #
    
  16. Finally, verify that /iso uses all the available space on the my_isos volume.
    df -h /iso
    
 

©2008 James Walden, Ph.D.