타겟 디스크 정보

마운트 없이 정보 확인

# virt-filesystems -l -h --all -a disk.qcow2
Name                     Type       VFS     Label MBR Size Parent
/dev/sda                 device     -       -     -   500G -
/dev/sda1                partition  -       -     -   1.0M /dev/sda
/dev/sda2                partition  -       -     -   1.0G /dev/sda
/dev/sda3                partition  -       -     -   446G /dev/sda
/dev/sda3                pv         -       -     -   446G -
/dev/ubuntu-vg           vg         -       -     -   446G /dev/sda3
/dev/ubuntu-vg/ubuntu-lv lv         -       -     -   200G /dev/ubuntu-vg
/dev/sda1                filesystem unknown -     -   1.0M -
/dev/sda2                filesystem ext4    -     -   1.0G -
/dev/ubuntu-vg/ubuntu-lv filesystem ext4    -     -   200G -

NBD 연결 후 정보 확인

NBD로 VM 이미지를 장치로 등록 후, NBD 장치를 마운트 하여 정보 확인

# modprobe nbd max_part=8

# qemu-nbd --connect=/dev/nbd2 disk.qcow2

# lsblk /dev/nbd2
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
nbd2                       43:32   0   500G  0 disk
├─nbd2p1                   43:33   0     1M  0 part
├─nbd2p2                   43:34   0     1G  0 part
└─nbd2p3                   43:35   0 445.6G  0 part
  └─ubuntu--vg-ubuntu--lv 253:3    0   200G  0 lvm

# pvdisplay /dev/nbd2p3
  --- Physical volume ---
  PV Name               /dev/nbd2p3
  VG Name               ubuntu-vg
  PV Size               445.62 GiB / not usable 0

# lvdisplay ubuntu-vg
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV Size                200.00 GiB

# file -L -s  /dev/ubuntu-vg/ubuntu-lv
/dev/ubuntu-vg/ubuntu-lv: Linux rev 1.0 ext4 filesystem ...

# mount /dev/ubuntu-vg/ubuntu-lv /mnt

# df -h /mnt
Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv  196G   26G  161G  14% /mnt

# umount /mnt

NBD 해제하기

LVM을 inactive 시킨 후 NBD 장치 disconnect

# lvchange -an /dev/ubuntu-vg/ubuntu-lv

# vgchange -an ubuntu-vg
  0 logical volume(s) in volume group "ubuntu-vg" now active

# lvscan
  inactive          '/dev/ubuntu-vg/ubuntu-lv' ...

# qemu-nbd --disconnect /dev/nbd2


디스크 크기 줄이기

LVM 크기 줄이기

마운트 된 장치가 없는 것을 확인

e2fsck(파일시스템 체크) → resize2fs(파일시스템 크기감소) → lvreduce(LVM 크기 감소)

# e2fsck -f /dev/ubuntu-vg/ubuntu-lv
e2fsck 1.45.6 (20-Mar-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/ubuntu-vg/ubuntu-lv: 479453/13107200 files (0.1% non-contiguous), 7712317/52428800 blocks

# resize2fs /dev/ubuntu-vg/ubuntu-lv 28G
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/ubuntu-vg/ubuntu-lv to 7340032 (4k) blocks.
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 7340032 (4k) blocks long.

# lvreduce -L 28G /dev/ubuntu-vg/ubuntu-lv
  WARNING: Reducing active logical volume to 28.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce ubuntu-vg/ubuntu-lv? [y/n]: y
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 200.00 GiB (51200 extents) to 28.00 GiB (7168 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

VM 디스크 축소

qemu-img create (새로운 VM 디스크를 마련) → (virt-resize --resize-force /dev/sda3=29G) 파티션 축소

축소할 파티션 용량(29GB)은 위에서 줄인 LVM 파티션 용량(28GB)보다 크게 잡아야 함.

VM 디스크 용량(32GB)은 파티션 용량의 합보다 크게 (1MB/sda1+1GB/sda2+29GB/sda3) 잡아야 함.

# qemu-img create -f qcow2 tmp.qcow2 32G
Formatting 'tmp.qcow2', fmt=qcow2 size=34359738368 cluster_size=65536 lazy_refcounts=off refcount_bits=16

# virt-resize --no-extra-partition --resize-force /dev/sda3=29G disk.qcow2 tmp.qcow2
[   0.0] Examining disk.qcow2
**********
Summary of changes:
/dev/sda1: This partition will be left alone.
/dev/sda2: This partition will be left alone.
/dev/sda3: This partition will be resized from 445.6G to 29.0G.
The LVM PV on /dev/sda3 will be expanded using the ‘pvresize’ method.
There is a surplus of 2.0G.  The surplus space will be ignored.  
Run a partitioning program in the guest to partition this extra space if you want.
**********
[   2.1] Setting up initial partition table on tmp.qcow2
[  12.8] Copying /dev/sda1
[  12.8] Copying /dev/sda2
[  13.8] Copying /dev/sda3
[  41.5] Expanding /dev/sda3 using the ‘pvresize’ method
Resize operation completed with no errors.  Before deleting the old disk,
carefully check that the resized disk boots and works correctly.

# virt-filesystems -l -h --all -a tmp.qcow2
Name                     Type       VFS     Label MBR Size Parent
/dev/sda                 device     -       -     -   32G  -
/dev/sda1                partition  -       -     -   1.0M /dev/sda
/dev/sda2                partition  -       -     -   1.0G /dev/sda
/dev/sda3                partition  -       -     -   29G  /dev/sda
/dev/sda3                pv         -       -     -   29G  -
/dev/ubuntu-vg           vg         -       -     -   29G  /dev/sda3
/dev/ubuntu-vg/ubuntu-lv lv         -       -     -   28G  /dev/ubuntu-vg
/dev/sda1                filesystem unknown -     -   1.0M -
/dev/sda2                filesystem ext4    -     -   1.0G -
/dev/ubuntu-vg/ubuntu-lv filesystem ext4    -     -   28G  -

# ls -alh tmp.qcow2
20G    tmp.qcow2


디스크 크기 늘리기

# qemu-img create -f qcow2 new.qcow2 100G
Formatting 'new.qcow2', fmt=qcow2 size=107374182400 cluster_size=65536 lazy_refcounts=off refcount_bits=16

# virt-resize --expand /dev/sda3 --LV-expand /dev/ubuntu-vg/ubuntu-lv tmp.qcow2 new.qcow2
[   0.0] Examining tmp.qcow2
**********
Summary of changes:
/dev/sda1: This partition will be left alone.
/dev/sda2: This partition will be left alone.
/dev/sda3: This partition will be resized from 29.0G to 99.0G.
The LVM PV on /dev/sda3 will be expanded using the ‘pvresize’ method.
/dev/ubuntu-vg/ubuntu-lv: This logical volume will be expanded to maximum size.
The filesystem ext4 on /dev/ubuntu-vg/ubuntu-lv will be expanded using the ‘resize2fs’ method.
**********
[   2.2] Setting up initial partition table on new.qcow2
[  12.9] Copying /dev/sda1
[  12.9] Copying /dev/sda2
[  13.9] Copying /dev/sda3
[  41.2] Expanding /dev/sda3 using the ‘pvresize’ method
[  41.2] Expanding /dev/ubuntu-vg/ubuntu-lv using the ‘resize2fs’ method
Resize operation completed with no errors.  Before deleting the old disk,
carefully check that the resized disk boots and works correctly.

# virt-filesystems -l -h --all -a new.qcow2
Name                     Type       VFS     Label MBR Size Parent
/dev/sda1                filesystem unknown -     -   1.0M -
/dev/sda2                filesystem ext4    -     -   1.0G -
/dev/ubuntu-vg/ubuntu-lv filesystem ext4    -     -   99G  -
/dev/ubuntu-vg/ubuntu-lv lv         -       -     -   99G  /dev/ubuntu-vg
/dev/ubuntu-vg           vg         -       -     -   99G  /dev/sda3
/dev/sda3                pv         -       -     -   99G  -
/dev/sda1                partition  -       -     -   1.0M /dev/sda
/dev/sda2                partition  -       -     -   1.0G /dev/sda
/dev/sda3                partition  -       -     -   99G  /dev/sda
/dev/sda                 device     -       -     -   100G -

# ls -alh new.qcow2
20G    new.qcow2


참고

https://blog.naver.com/dlatjgns/220031793234

https://libguestfs.org/virt-resize.1.html