Logical Volume Manager (LVM):論理ボリュームマネージャ

UbuntuによるサーバシステムのボリュームをLVMにより管理。

LVM : ArchWiki

https://wiki.archlinux.org/title/LVM

Physical volume (PV)
Unix block device node, usable for storage by LVM. Examples: a hard disk, an MBR or GPT partition, a loopback file, a device mapper device (e.g. dm-crypt). It hosts an LVM header.

Volume group (VG)
Group of PVs that serves as a container for LVs. PEs are allocated from a VG for a LV.

Logical volume (LV)
“Virtual/logical partition” that resides in a VG and is composed of PEs. LVs are Unix block devices analogous to physical partitions, e.g. they can be directly formatted with a file system.

Physical extent (PE)
The smallest contiguous extent (default 4 MiB) in the PV that can be assigned to a LV. Think of PEs as parts of PVs that can be allocated to any LV.

LVMによる論理ボリュームの変更(拡張)

Note: lvresize(8) provides more or less the same options as the specialized lvextend(8) and lvreduce(8) commands, while allowing to do both types of operation. Notwithstanding this, all those utilities offer a -r /--resizefs option which allows to resize the file system together with the LV using fsadm(8) (ext2 , ext3, ext4, ReiserFS and XFS supported). Therefore it may be easier to simply use lvresize for both operations and use --resizefs to simplify things a bit, except if you have specific needs or want full control over the process.

lvresizeはlvextendとlvreduceの機能を含んでいるため、特に理由がなければlvresizeを使用。

Resizing the logical volume and file system in one go

Note: Only ext2 , ext3, ext4, ReiserFS and XFS file systems are supported. For a different type of file system see #Resizing the logical volume and file system separately.

ext2 , ext3, ext4, ReiserFS , XFS ファイルシステムの場合のみ --resizefs オプションを付与してファイルシステムのリサイズも可能。

ボリュームグループ/論理ボリュームを指定して lvresize コマンドを実行

Extend the logical volume mediavol in MyVolGroup by 10 GiB and resize its file system all at once:

# lvresize -L +10G --resizefs MyVolGroup/mediavol

Set the size of logical volume mediavol in MyVolGroup to 15 GiB and resize its file system all at once:

# lvresize -L 15G --resizefs MyVolGroup/mediavol

If you want to fill all the free space on a volume group, use the following command:

# lvresize -l +100%FREE --resizefs MyVolGroup/mediavol

Resizing the logical volume and file system separately

For file systems not supported by fsadm(8) will need to use the appropriate utility to resize the file system before shrinking the logical volume or after expanding it.

To extend logical volume mediavol within volume group MyVolGroup by 2 GiB without touching its file system:

$ sudo lvresize -L +2G MyVolGroup/mediavol

Now expand the file system (ext4 in this example) to the maximum size of the underlying logical volume:

$ sudo resize2fs /dev/MyVolGroup/mediavol

To reduce the size of logical volume mediavol in MyVolGroup by 500 MiB, first calculate the resulting file system size and shrink the file system (ext4 in this example) to the new size:

$ sudo resize2fs /dev/MyVolGroup/mediavol *NewSize*

When the file system is shrunk, reduce the size of logical volume:

$ sudo lvresize -L -500M MyVolGroup/mediavol

ボリュームグループの確認(PE : Physical Extendsフリー容量の確認)

$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <928.46 GiB
  PE Size               4.00 MiB
  Total PE              237685
  Alloc PE / Size       25600 / 100.00 GiB
  Free  PE / Size       212085 / <828.46 GiB
  VG UUID              xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

論理ボリュームの確認

$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/ubuntu-vg/ubuntu-lv
  LV Name                ubuntu-lv
  VG Name                ubuntu-vg
  LV UUID                xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  LV Write Access        read/write
  LV Creation host, time ubuntu-server, 2022-07-18 02:29:35 +0000
  LV Status              available
  # open                 1
  LV Size                100.00 GiB
  Current LE             25600
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

論理ボリュームのマウントポイントの確認

$ sudo lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0                       7:0    0  79.9M  1 loop /snap/lxd/22923
loop1                       7:1    0  44.7M  1 loop /snap/snapd/15534
loop2                       7:2    0  61.9M  1 loop /snap/core20/1405
loop3                       7:3    0  61.9M  1 loop /snap/core20/1518
loop4                       7:4    0    47M  1 loop /snap/snapd/16292
sda                         8:0    0 931.5G  0 disk 
├─sda1                      8:1    0     1G  0 part /boot/efi
├─sda2                      8:2    0     2G  0 part /boot
└─sda3                      8:3    0 928.5G  0 part 
  └─ubuntu--vg-ubuntu--lv 253:0    0   100G  0 lvm  /
sr0                        11:0    1  1024M  0 rom 

ファイルシステムの確認

$ df -Th
Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  783M  1.8M  781M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    98G   33G   61G  35% /
tmpfs                             tmpfs  3.9G     0  3.9G   0% /dev/shm
tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4   2.0G  127M  1.7G   7% /boot
/dev/sda1                         vfat   1.1G  5.3M  1.1G   1% /boot/efi
tmpfs                             tmpfs  783M  4.0K  783M   1% /run/user/1000

フリースペースを100%使用する場合

$ sudo lvresize -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
$ sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

ファイルシステムが ext4 である場合は、ボリュームグループ内の論理ボリュームを指定して

$ sudo lvresize -l +100%FREE --resizefs ubuntu-vg/ubuntu-lv

LVMパーティションのマウント

LVMのHDDをUSB-SATA変換ケーブル等で外付けHDDとして使用する際、lvm2によるマウント作業が必要になります。

lvm2のインストールからLVMでパーティショニングされた領域をマウントするまでの流れ

$ sudo vgchange -ayボリュームグループ:VGをアクティブ にしないとマウントできません。

$ sudo apt-get install lvm2   #This step may or may not be required.
$ sudo pvscan                 #Use this to verify your LVM partition(s) is/are detected.
$ sudo vgscan --mknodes       #Scans for LVM Volume Group(s)
$ sudo vgchange -ay           #Activates LVM Volume Group(s) 
$ sudo lvscan                 #Scans for available Logical Volumes
$ sudo mount /dev/YourVolGroup00/YourLogVol00 /YourMountPoint

アンマウント

$ sudo umount /dev/YourVolGroup00/YourLogVol00

VGの非アクティブ化

$ sudo vgchange -an

LVM2マニュアル

“can’t read superblock” スーパーブロックを読み込むことができません

$ sudo mount /dev/vgubuntu/root /mnt
mount: /mnt: /dev/mapper/vgubuntu-root のスーパーブロックを読み込むことができません.

ボリュームグループを一度 非アクティブ ⇒ 再度アクティブに設定 ⇒ マウント

$ sudo vgchange -an vgubuntu
  0 logical volume(s) in volume group "vgubuntu" now active
$ sudo vgchange -ay vgubuntu
  2 logical volume(s) in volume group "vgubuntu" now active
$ sudo mount /dev/vgubuntu/root /mnt