Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


kvm_shrink_nested_lvm

KVM shrink nested LVM

Note! Backup your data first!

  • Remove content in VM. Free some space, reduce filesystems, logical volumes etc. For example reduce home directory.
    # log in as root via serial, or via SSH.
    
    umount /home
    fsck /dev/mapper/VolGroup-lv_home
    lvresize --resizefs -L1G /dev/mapper/VolGroup-lv_home
    mount /home
  • If you need to reduce the rootfs, boot from network, usb or cd/dvd. Then resize the filesystem. For example, edit guest to boot from systemrescuecd.
    lvresize -r -L8G /dev/VolGroup/lv_root
  • Shrink physical volume. First move LV's to beginning.
    pvdisplay -m
    pvmove -v /dev/vda2:2296-2551 /dev/vda2:0-255 --alloc anywhere
    pvmove -v /dev/vda2:2552-2799 /dev/vda2:256-503 --alloc anywhere
    pvmove -v /dev/vda2:10428-12475 /dev/vda2:504-2551 --alloc anywhere
    pvresize -v --setphysicalvolumesize 16G /dev/vda2
  • On the KVM host, reduce the LV.
    lvresize -L16G /dev/vg_kvm1/vmns2
  • On the guest force fsck check at next boot, and reboot.
    touch /forcefsck
    reboot

Boot guest from systemrescuecd iso

  • Add cdrom device.
    virsh edit vmns2
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/isos/systemresucecd-x86-2.2.0.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    </disk>
  • Boot from cdrom first.
    <os>
      <type arch='x86_64' machine='pc-0.12'>hvm</type>
      <boot dev='cdrom'/>
      <boot dev='hd'/>
    </os>
  • Boot VM and reduce rootfs. When finished, remove the cdrom device and boot device.

Auto expand root fs from initrd

  • See: https://github.com/flegmatik/linux-rootfs-resize
    cd /opt
    rpm -ivh http://ftp-stud.hs-esslingen.de/pub/epel/6/i386/epel-release-6-8.noarch.rpm
    yum install git parted cloud-utils
    git clone https://github.com/flegmatik/linux-rootfs-resize.git
    cd linux-rootfs-resize
    ./install
kvm_shrink_nested_lvm.txt · Last modified: 2021/10/09 15:14 by 127.0.0.1