• Resize LVM Root Partition Remotely Over SSH

    I recently had to resize the partition that Debian Wheezy 64bit installed. If you follow the steps explained here carefully it is possible to change the partition table even by remote SSH access.

    Resize LVM Root Partition Remotely Over SSH

    First check the current partition state :

    # lvdisplay 
      --- Logical volume ---
      LV Path                /dev/vd0/vd00
      LV Name                vd00
      VG Name                vd0
      LV UUID                1IDQIF-5m7O-0bPT-SYRa-LgEO-RcS4-837Ym5
      LV Write Access        read/write
      LV Creation host, time debian-raidtest, 2015-04-21 14:29:49 -0400
      LV Status              available
      # open                 1
      LV Size                186.26 GiB
      Current LE             47683
      Segments               4
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           254:0
       
      --- Logical volume ---
      LV Path                /dev/vd0/vd01
      LV Name                vd01
      VG Name                vd0
      LV UUID                ffXIUd-i0cw-pLpk-z3Ay-2YDt-bevw-YNbTFf
      LV Write Access        read/write
      LV Creation host, time debian-raidtest, 2015-04-21 14:30:02 -0400
      LV Status              available
      # open                 2
      LV Size                13.72 GiB
      Current LE             3513
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           254:1
    

    Delete existing partitions first
    We need to delete / deactivate swap partition. To do this, edit /etc/fstab file and delete or comment out the line configures the swap partition :

    ...
    # /dev/mapper/vd0-vd01 none            swap    sw              0       0
    ...
    

    Turn it off :

    # swapoff -a
    

    Format the swap partition as ext4 and copy over the root files:

    # mkfs.ext4 -j /dev/vd0/vd01
    # mount /dev/vd0/vd01 /mnt
    # cp -avx / /mnt/
    

    Once the files were transfered. Edit the /mnt/etc/fstab  file to reflect the new root.

    ...
    /dev/mapper/vd0-vd01 /               ext4    errors=remount-ro 0       1
    ...
    

    Modify /boot/grub/grub.cfg to add the new partition as new default boot partition:

    ...
    /dev/mapper/vd0-vd00 /               ext4    errors=remount-ro 0       1
    ### BEGIN /etc/grub.d/10_linux ###
    menuentry 'Copy of the system to boot temporary for maintenance' --class debian --class gnu-linux --class gnu --class os {
            load_video
            insmod gzio
            insmod lvm
            insmod part_msdos
            insmod part_msdos
            insmod part_msdos
            insmod part_msdos
            insmod ext2
            set root='(vd0-vd01)'
            echo    'Loading Linux 3.2.0-4-amd64 ...'
            linux   /boot/vmlinuz-3.2.0-4-amd64 root=/dev/mapper/vd0-vd01 ro  quiet
            echo    'Loading initial ramdisk ...'
            initrd  /boot/initrd.img-3.2.0-4-amd64
    }
    menuentry 'Debian GNU/Linux, with Linux 3.2.0-4-amd64' --class debian --class gnu-linux --class gnu --class os {
    ...
    

    Now we can reboot to new partition :

    # reboot
    

    After reboot we can check if we succeeded to boot from the temporary partition :

    # df -kh
    Filesystem            Size  Used Avail Use% Mounted on
    rootfs                 14G  1.1G   12G   9% /
    udev                   10M     0   10M   0% /dev
    tmpfs                 202M  216K  202M   1% /run
    /dev/mapper/vd0-vd01   14G  1.1G   12G   9% /
    tmpfs                 5.0M     0  5.0M   0% /run/lock
    tmpfs                 403M     0  403M   0% /run/shm
    

    We can now modify / resize the original LVM volume :

    # lvresize --size -100G -r /dev/vd0/vd00 
    fsck from util-linux 2.20.1
    /dev/mapper/vd0-vd00: 45395/12214272 files (0.2% non-contiguous), 1059068/48827392 blocks
    resize2fs 1.42.5 (29-Jul-2012)
    Resizing the filesystem on /dev/mapper/vd0-vd00 to 22612992 (4k) blocks.
    The filesystem on /dev/mapper/vd0-vd00 is now 22612992 blocks long.
    
      Reducing logical volume vd00 to 86.26 GiB
      Logical volume vd00 successfully resized
    

    In this example we created 100G free space by shrinking the existing volume.
    Now we can create another volume using the free space :

    # lvcreate vd0 -n vdnew --extents 100%FREE
      Logical volume "vdnew" created
    

    Check the newly created volume :

    # lvs
      LV    VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
      vd00  vd0  -wi-a---  86.26g                                           
      vd01  vd0  -wi-ao--  13.72g                                           
      vdnew vd0  -wi-a--- 100.00g 
    

    We can now go back and boot to original root partition to be able to continue with the final step.
    Edit the /boot/grub/grub.cfg on the original root and remove the tmp partition entry and reboot.

    # mount /dev/vd0/vd00 /mnt
    # vim /mnt/boot/grub/grub.cfg
    

    Delete the first menuentry that we have added earlier to make original root partition as default boot partition.
    Now we can reboot to the original but resized partition :

    # reboot
    

    If you like, you can rename the new partition and swap partition :

    # lvrename /dev/vd0/vd01 vd02 
      Renamed "vd01" to "vd02" in volume group "vd0"
    # lvrename /dev/vd0/vdnew vd01 
      Renamed "vdnew" to "vd01" in volume group "vd0"
    # lvs
      LV   VG   Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
      vd00 vd0  -wi-ao--  86.26g                                           
      vd01 vd0  -wi-a--- 100.00g                                           
      vd02 vd0  -wi-a---  13.72g                                           
    

    Last step: Activate the swap partition :

    # mkswap -f /dev/vd0/vd02 
    Setting up swapspace version 1, size = 14389244 KiB
    no label, UUID=bf2cfc0e-fd73-4ad1-9df1-26c4766034f7
    # swapon /dev/vd0/vd02
    

    Then edit /etc/fstab file to configure swap partition to be activated at boot time (using its new name)

    ...
    /dev/mapper/vd0-vd02 none            swap    sw              0       
    ...
    

    Categories: General

    Tags: , , , ,

    One thought on “Resize LVM Root Partition Remotely Over SSH

    • VPS server says:

      The issue is step 3, shrinking the root filesystem. The filesystems involved are ext4, so online resizing is supported; however, while mounted, the filesystems can only be grown. To shrink the partition requires unmounting it, which of course is not possible for the root partition in normal operation.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>