How can I convert the root filesystem to LVM 1?

The following information has been provided by Red Hat, but is outside the scope of our posted Service Level Agreements ( https://www.redhat.com/support/service/sla/ ) and support procedures. The information is provided as-is and any configuration settings or installed applications made from the information in this article could make your Operating System unsupported by Red Hat Support Services. The intent of this article is to provide you with information to accomplish your system needs. Use the information in this article at your own risk.

Release found:
Red Hat Enterprise Linux 3

Why is it required?
At times, a user wants to upgrade the root partition (/) to LVM due to the root partition (/) getting filled up. This article shows the steps involved in the upgrade. However, the contents herein contain a lot of RISK and as previously stated, unsupported by Red Hat Technical Support.

How to upgrade:
Backup the System. It is strongly recommended that the whole system is backed up before attempting to convert to root filesystem on LVM 1.

Example: In this example, the whole system was installed in a single root partition with the exception of /boot partition. The system had a 2 GB disk partitioned as:



/dev/hda1  /boot
/dev/hda2  swap
/dev/hda3  /


The root partition (/) partition covered all of the disk not used by /boot and swap. An important prerequisite of this procedure is that the root partition should have the usage of less than half the capacity making it easy to create its copy in a logical volume. If this is not the case then a second disk drive should be used. The procedure in that case is similar but there is no need to shrink the existing root partition and /dev/hda4 should be replaced with /dev/hdb1 in the examples.

To do this it is easiest to use GNU parted. This software allows the user to grow and shrink partitions that contain filesystems. It is possible to use resize2fs and fdisk to do this but GNU parted makes it much less prone to error. The application parted can be installed using up2date command.

Once the software Parted is installed on the system and the Backup is done, follow the steps below:

  1. Boot in single user mode from GRUB. Booting single-user ensures that the root filesystem is mounted read-only and no programs are accessing the disk.
  2. Run Parted to shrink the root partition, do this so there is room on the disk for a complete copy of it in a logical volume. In this example a 1.8 GB partition is shrunk to 1 GB. The command below displays the sizes and names of the partitions on the disk. 
    
     # parted /dev/hda
     (parted) p
     
    Note: (parted) is the prompt that will appear after the parted command.
  3. Resize the partition: 
    
     (parted) resize 3 145 999
     
    The first number (3) is the partition number (hda3), the second number (145) is the same starting position that hda3 currently has. This number should be the same as shown by the p command in the previous step. The last number (999) should make the partition around half the size it currently is.
  4. Create a new partition. 
    
     (parted) mkpart primary ext2 1000 1999
     
    This makes a new partition to hold the initial LVM 1 data. It should start just beyond the newly shrunk hda3 and finish at the end of the disk.
  5. Quit parted. 
    
     (parted) q
     

  6. Reboot the system so that the new partition table gets updated.
  7. Change the partition type on the newly created partition from Linux to LVM (8e). The application Parted does not understand LVM 1 partitions so this has to be done using fdisk command. 
    
     # fdisk /dev/hda
     Command (m for help): t
     Partition number (1-4): 4
     Hex code (type L to list codes): 8e
     Changed system type of partition 4 to 8e (Unknown)
     Command (m for help): w
     

  8. Inform the Operating System for the partition table changes. 
    
     #partprobe
     

  9. Initialize LVM 1
    
     # vgscan
     

  10. Make the new partition into a Physical Volume. 
    
     # pvcreate /dev/hda4
     

  11. Create a new volume group. 
    
     # vgcreate vg /dev/hda4
     

  12. Create a logical volume to hold the new root (/). 
    
     # lvcreate -L250M -n root vg
     

  13. Make a filesystem in the logical volume and copy the root files onto it. 
    
     # mkfs.ext3 /dev/vg/root
     # mount /dev/vg/root /mnt/
     # find / -xdev | cpio -pvmd /mnt
     

  14. Update the /etc/fstab file. Edit /mnt/etc/fstab on the new root so that / is mounted on /dev/vg/root. For example: 
    
     /dev/hda3       /    ext3       defaults 1 1
     
    becomes: 
    
     /dev/vg/root    /    ext3       defaults 1 1
     

  15. Add the rest of the disk into LVM 1. When satisfied, add the old root partition to LVM 1 and spread it over the disk.
    • First set the partition type to 8e(LVM) 
      
        # fdisk /dev/hda
        Command (m for help): t
        Partition number (1-4): 3
        Hex code (type L to list codes): 8e
        Changed system type of partition 3 to 8e (Unknown)
        Command (m for help): w
        

    • Convert it into a Physical Volume and add it to the volume group: 
      
        # pvcreate /dev/hda3
        # vgextend vg /dev/hda3
        


Now the disk is ready and the system can be used as normal.

Comments

Popular Posts