Ben's notes

Linux, Unix, network, radio...

User Tools

Site Tools


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

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'/>
      <address type='drive' controller='0' bus='1' unit='0'/>
  • Boot from cdrom first.
      <type arch='x86_64' machine='pc-0.12'>hvm</type>
      <boot dev='cdrom'/>
      <boot dev='hd'/>
  • Boot VM and reduce rootfs. When finished, remove the cdrom device and boot device.

Auto expand root fs from initrd

  • See:
    cd /opt
    rpm -ivh
    yum install git parted cloud-utils
    git clone
    cd linux-rootfs-resize
kvm_shrink_nested_lvm.txt · Last modified: 2021/10/09 15:14 by