👜
Debian11 で LVM
準備
パーティションの作成
LVMで利用するパーティションを準備します。
最初に区画を切ります。
# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1418MB 1417MB primary ext4
2 1418MB 2834MB 1417MB primary
# parted /dev/vdb mkpart primary 66% 83%
Information: You may need to update /etc/fstab.
# parted /dev/vdb mkpart primary 83% 100%
Information: You may need to update /etc/fstab.
# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1418MB 1417MB primary ext4
2 1418MB 2834MB 1417MB primary
3 2834MB 3565MB 731MB primary
4 3565MB 4295MB 730MB primary
切った区画に、LVMフラグを立てます。
# parted /dev/vdb set 3 lvm on
Information: You may need to update /etc/fstab.
# parted /dev/vdb set 4 lvm on
Information: You may need to update /etc/fstab.
# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1418MB 1417MB primary ext4
2 1418MB 2834MB 1417MB primary
3 2834MB 3565MB 731MB primary lvm
4 3565MB 4295MB 730MB primary lvm
パッケージインストール
# apt install --no-install-recommends lvm2
PV作成
# pvcreate /dev/vdb3
Physical volume "/dev/vdb3" successfully created.
# pvcreate /dev/vdb4
Physical volume "/dev/vdb4" successfully created.
# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb3 lvm2 --- 697.00m 697.00m
/dev/vdb4 lvm2 --- 696.00m 696.00m
# pvdisplay
"/dev/vdb3" is a new physical volume of "697.00 MiB"
--- NEW Physical volume ---
PV Name /dev/vdb3
VG Name
PV Size 697.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
"/dev/vdb4" is a new physical volume of "696.00 MiB"
--- NEW Physical volume ---
PV Name /dev/vdb4
VG Name
PV Size 696.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ysqn3Y-46Kk-bdOp-0Lw9-hj7O-hYVM-u12LfC
VG作成
# vgcreate testvg /dev/vdb3
Volume group "testvg" successfully created
# vgs
VG #PV #LV #SN Attr VSize VFree
testvg 1 0 0 wz--n- 696.00m 696.00m
# vgdisplay
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 696.00 MiB
PE Size 4.00 MiB
Total PE 174
Alloc PE / Size 0 / 0
Free PE / Size 174 / 696.00 MiB
VG UUID 1v3xXQ-3rHE-Gzdn-OG9S-Rkhu-dl6D-3UIp9k
LV作成
LVを作成して、ファイルシステムを作ります。
# lvcreate -L 256M -n testlv testvg
Logical volume "testlv" created.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
testlv testvg -wi-a----- 256.00m
# lvdisplay
--- Logical volume ---
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID sOuZOM-nh6J-TRDY-YEVA-bMig-FiT3-OfGrhX
LV Write Access read/write
LV Creation host, time debian11-0, 2023-08-10 18:27:15 -0400
LV Status available
# open 0
LV Size 256.00 MiB
Current LE 64
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
# mkfs -t ext4 /dev/testvg/testlv
mke2fs 1.46.2 (28-Feb-2021)
Discarding device blocks: done
Creating filesystem with 262144 1k blocks and 65536 inodes
Filesystem UUID: 164b8a99-de45-49ac-8bd6-9e592960da28
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
LV拡張
LVを拡張したら、ファイルシステムも拡張します。
# lvextend -L +256MB /dev/testvg/testlv
Size of logical volume testvg/testlv changed from 256.00 MiB (64 extents) to 512.00 MiB (128 extents).
Logical volume testvg/testlv successfully resized.
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
testlv testvg -wi-a----- 512.00m
# lvdisplay
--- Logical volume ---
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID sOuZOM-nh6J-TRDY-YEVA-bMig-FiT3-OfGrhX
LV Write Access read/write
LV Creation host, time debian11-0, 2023-08-10 18:27:15 -0400
LV Status available
# open 0
LV Size 512.00 MiB
Current LE 128
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
# fsck -f /dev/testvg/testlv
fsck from util-linux 2.36.1
e2fsck 1.46.2 (28-Feb-2021)
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/mapper/testvg-testlv: 11/65536 files (0.0% non-contiguous), 18535/262144 blocks
# resize2fs /dev/testvg/testlv
resize2fs 1.46.2 (28-Feb-2021)
Resizing the filesystem on /dev/testvg/testlv to 524288 (1k) blocks.
The filesystem on /dev/testvg/testlv is now 524288 (1k) blocks long.
LV縮小
ファイルシステムも縮小してから、LVを縮小します。
# fsck -f /dev/testvg/testlv
fsck from util-linux 2.36.1
e2fsck 1.46.2 (28-Feb-2021)
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/mapper/testvg-testlv: 11/131072 files (0.0% non-contiguous), 27050/524288 blocks
# resize2fs /dev/testvg/testlv 384M
resize2fs 1.46.2 (28-Feb-2021)
Resizing the filesystem on /dev/testvg/testlv to 393216 (1k) blocks.
The filesystem on /dev/testvg/testlv is now 393216 (1k) blocks long.
# lvchange -an /dev/testvg/testlv
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
testlv testvg -wi------- 512.00m
# lvreduce -L -128M /dev/testvg/testlv
Size of logical volume testvg/testlv changed from 512.00 MiB (128 extents) to 384.00 MiB (96 extents).
Logical volume testvg/testlv successfully resized.
# lvchange -ay /dev/testvg/testlv
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
testlv testvg -wi-a----- 384.00m
# resize2fs /dev/testvg/testlv
resize2fs 1.46.2 (28-Feb-2021)
The filesystem is already 393216 (1k) blocks long. Nothing to do!
VG拡張
VGにPVを追加して拡張します。
# vgdisplay
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 696.00 MiB
PE Size 4.00 MiB
Total PE 174
Alloc PE / Size 96 / 384.00 MiB
Free PE / Size 78 / 312.00 MiB
VG UUID 1v3xXQ-3rHE-Gzdn-OG9S-Rkhu-dl6D-3UIp9k
# vgextend testvg /dev/vdb4
Volume group "testvg" successfully extended
# vgdisplay
--- Volume group ---
VG Name testvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size <1.36 GiB
PE Size 4.00 MiB
Total PE 347
Alloc PE / Size 96 / 384.00 MiB
Free PE / Size 251 / 1004.00 MiB
VG UUID 1v3xXQ-3rHE-Gzdn-OG9S-Rkhu-dl6D-3UIp9k
# pvdisplay
--- Physical volume ---
PV Name /dev/vdb3
VG Name testvg
PV Size 697.00 MiB / not usable 0
Allocatable yes
PE Size 4.00 MiB
Total PE 174
Free PE 78
Allocated PE 96
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
--- Physical volume ---
PV Name /dev/vdb4
VG Name testvg
PV Size 696.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 173
Free PE 173
Allocated PE 0
PV UUID ysqn3Y-46Kk-bdOp-0Lw9-hj7O-hYVM-u12LfC
PEの移動
PV /dev/vdb3 の内容を /dev/vdb4 へ移動します。
# pvdisplay
--- Physical volume ---
PV Name /dev/vdb3
VG Name testvg
PV Size 697.00 MiB / not usable 0
Allocatable yes
PE Size 4.00 MiB
Total PE 174
Free PE 78
Allocated PE 96
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
--- Physical volume ---
PV Name /dev/vdb4
VG Name testvg
PV Size 696.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 173
Free PE 173
Allocated PE 0
PV UUID ysqn3Y-46Kk-bdOp-0Lw9-hj7O-hYVM-u12LfC
# pvmove /dev/vdb3
/dev/vdb3: Moved: 1.04%
/dev/vdb3: Moved: 100.00%
# pvdisplay
--- Physical volume ---
PV Name /dev/vdb3
VG Name testvg
PV Size 697.00 MiB / not usable 0
Allocatable yes
PE Size 4.00 MiB
Total PE 174
Free PE 174
Allocated PE 0
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
--- Physical volume ---
PV Name /dev/vdb4
VG Name testvg
PV Size 696.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 173
Free PE 77
Allocated PE 96
PV UUID ysqn3Y-46Kk-bdOp-0Lw9-hj7O-hYVM-u12LfC
VG縮小
VG から PVを取り除いて縮小します。
# pvdisplay
--- Physical volume ---
PV Name /dev/vdb3
VG Name testvg
PV Size 697.00 MiB / not usable 0
Allocatable yes
PE Size 4.00 MiB
Total PE 174
Free PE 174
Allocated PE 0
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
--- Physical volume ---
PV Name /dev/vdb4
VG Name testvg
PV Size 696.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 173
Free PE 77
Allocated PE 96
PV UUID ysqn3Y-46Kk-bdOp-0Lw9-hj7O-hYVM-u12LfC
# vgreduce testvg /dev/vdb3
Removed "/dev/vdb3" from volume group "testvg"
# pvdisplay
--- Physical volume ---
PV Name /dev/vdb4
VG Name testvg
PV Size 696.00 MiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 173
Free PE 77
Allocated PE 96
PV UUID ysqn3Y-46Kk-bdOp-0Lw9-hj7O-hYVM-u12LfC
"/dev/vdb3" is a new physical volume of "697.00 MiB"
--- NEW Physical volume ---
PV Name /dev/vdb3
VG Name
PV Size 697.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
LV削除
# lvchange -an /dev/testvg/testlv
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
testlv testvg -wi------- 384.00m
# lvremove /dev/testvg/testlv
Logical volume "testlv" successfully removed
VG削除
# vgs
VG #PV #LV #SN Attr VSize VFree
testvg 1 0 0 wz--n- 692.00m 692.00m
# vgremove testvg
Volume group "testvg" successfully removed
PV削除
# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb3 lvm2 --- 697.00m 697.00m
/dev/vdb4 lvm2 --- 696.00m 696.00m
# pvremove /dev/vdb4
Labels on physical volume "/dev/vdb4" successfully wiped.
PVの拡張
まずはパーティションを拡張します。
# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1418MB 1417MB primary ext4
2 1418MB 2834MB 1417MB primary
3 2834MB 3565MB 731MB primary lvm
4 3565MB 4295MB 730MB primary lvm
# parted /dev/vdb rm 4 print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1418MB 1417MB primary ext4
2 1418MB 2834MB 1417MB primary
3 2834MB 3565MB 731MB primary lvm
Information: You may need to update /etc/fstab.
# parted /dev/vdb resizepart 3 100%
Information: You may need to update /etc/fstab.
# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 4295MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 1418MB 1417MB primary ext4
2 1418MB 2834MB 1417MB primary
3 2834MB 4295MB 1461MB primary lvm
PVを拡張します。
# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb3 lvm2 --- 697.00m 697.00m
# pvdisplay
"/dev/vdb3" is a new physical volume of "697.00 MiB"
--- NEW Physical volume ---
PV Name /dev/vdb3
VG Name
PV Size 697.00 MiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
# pvresize /dev/vdb3
Physical volume "/dev/vdb3" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb3 lvm2 --- 1.36g 1.36g
# pvdisplay
"/dev/vdb3" is a new physical volume of "1.36 GiB"
--- NEW Physical volume ---
PV Name /dev/vdb3
VG Name
PV Size 1.36 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QkPFgM-XLay-1RN5-i36I-lfkE-40HT-GdJiKc
Discussion