Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents


타겟 디스크 정보

마운트 없이 정보 확인

Code Block
# modprobe nbd max_part=8


# qemu-nbd --connect=/dev/nbd2virt-filesystems -l -h --all -a disk.qcow2


#Name lsblk /dev/nbd2
NAME                   Type   MAJ:MIN RM   SIZEVFS RO TYPE MOUNTPOINT
nbd2  Label MBR Size Parent
/dev/sda                 device 43:32   0 -  500G  0 disk
├─nbd2p1  -     -   500G -
/dev/sda1        43:33   0     1Mpartition  0- part
├─nbd2p2      -     -        43:341.0M /dev/sda
/dev/sda2    0     1G  0 part
└─nbd2p3    partition  -       -     -  43:35 1.0G  0 445.6G/dev/sda
/dev/sda3    0 part
  └─ubuntu--vg-ubuntu--lv 253:3    0   200G partition 0 lvm


#- pvdisplay /dev/nbd2p3
  --- Physical volume ---
  PV Name  -   446G /dev/sda
/dev/sda3         /dev/nbd2p3
  VG Name    pv         -  ubuntu-vg
  PV Size  -     -   446G -
/dev/ubuntu-vg    445.62 GiB / not usable 0


# lvdisplay ubuntu-vg
   --- Logical volume ---
  LV Path-       -     -   446G /dev/sda3
/dev/ubuntu-vg/ubuntu-lv
 lv LV Name       -         ubuntu-lv
  VG Name  -              ubuntu200G /dev/ubuntu-vg
/dev/sda1  LV Size             filesystem unknown -  200.00 GiB


# file -L  -s  /dev/ubuntu-vg/ubuntu-lv1.0M -
/dev/ubuntu-vg/ubuntu-lv: Linux rev 1.0 ext4 filesystem data, ... (needs journal recovery) (extents) (64bit) (large files) (huge files)


# mount sda2                filesystem ext4    -     -   1.0G -
/dev/ubuntu-vg/ubuntu-lv /mnt


# df -h /mnt
Filesystem  filesystem ext4    -     -   200G -

NBD 연결 후 정보 확인

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

Code Block
# modprobe nbd max_part=8

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

# lsblk /dev/nbd2
NAME     Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv  196G   26G  161G  14% /mnt


# umount /mnt


# 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 29G
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/ubuntu-vg/ubuntu-lv to 7602176 (4k) blocks.
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 7602176 (4k) blocks long.


# lvreduce -L 29G /dev/ubuntu-vg/ubuntu-lv
  WARNING: Reducing active logical volume to 29.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 29.00 GiB (7424 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.


# 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/1900544 files (0.3% non-contiguous), 7004914/7602176 blocks


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


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


# qemu-nbd --disconnect /dev/nbd2


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


# virt-resize --resize-force /dev/sda3=30G disk.qcow2 new.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 30.0G.
The LVM PV on /dev/sda3 will be expanded using the ‘pvresize’ method.
There is a surplus of 1020.7M.  An extra partition will be created for the surplus.
**********
[   2.1] Setting up initial partition table on new.qcow2
[  12.8] Copying /dev/sda1
[  12.9] Copying /dev/sda2
[  13.8] Copying /dev/sda3
[  42.8] 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.


# qemu-nbd --connect=/dev/nbd2 new.qcow


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


# fdisk /dev/nbd2
Command (m for help): p
Disk /dev/nbd2: 32 GiB, 34359738368 bytes, 67108864 sectors
Device         Start      End  Sectors  Size Type
/dev/nbd2p1     2048     4095     2048    1M BIOS boot
/dev/nbd2p2     4096  2101247  2097152    1G Linux filesystem
/dev/nbd2p3  2101248 65015807 62914560   30G Linux filesystem
/dev/nbd2p4 65015808 67108799  2092992 1022M Linux filesystem

Command (m for help): d
Partition number (1-4, default 4): 4
Partition 4 has been deleted.

Command (m for help): w
The partition table has been altered.
Syncing disks.


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


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


# qemu-nbd --disconnect /dev/nbd2


# qemu-img create -f qcow2 new2.qcow2 100G
Formatting 'new2.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 new.qcow2 new2.qcow2
[   0.0] Examining new.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 30.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 new2.qcow2
[  12.8] Copying /dev/sda1
[  12.9] Copying /dev/sda2
[  13.9] Copying /dev/sda3
[  38.5] Expanding /dev/sda3 using the ‘pvresize’ method
[  38.5] 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 -a new2.qcow2
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

Code Block
# 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 크기 감소)

Code Block
# 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) 잡아야 함.

Code Block
# 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


디스크 크기 늘리기

Code Block
# 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             Type   partition  -   VFS   Label - Size  Parent
/dev/sda2  -   99G  /dev/sda
/dev/sda          filesystem  ext4  -   device   1.0G  -
/dev/ubuntu-vg/ubuntu-lv   filesystem  ext4  -     - 99G  100G -


# ls -alh new2new.qcow2
-rw-r--r--. 1 qemu qemu 19G 2022-05-11 Wed 18:37:23 new220G    new.qcow2


참고

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

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