LVM

LVM = Logical Volume Manager

  • 하나 혹은 여러 디스크를 논리적으로 볼륨을 할당하여 사용할 수 있다.
  • 파티션을 사용하는 것과 비교했을 때 장점
    • 볼륨이 PE(Physical Extent)단위로 저장되어 디스크상에서 이어져있을 필요가 없다.
    • 여러 디스크에 걸쳐 볼륨을 생성할 수 있다. (stripe)
    • 속도가 빠른 ssd를 활용하여 캐시 영역을 생성할 수 있다.

PV = Physical Volume

  • 물리 디스크

VG = Volume Group

  • 1개 이상의 PV로 만든다.

LV = Logical Volume

  • VG 위에 논리 볼륨을 만든다.
  • VG 안에 속한 특정 PV에 할당하여 생성할 수 있다.
  • VG 안에 속한 다른 PV로 이동할 수 있다.
  • 여러 PV에 걸쳐 생성할 수 있다.

우분투 20.04 부터는 기본으로 LVM을 볼륨을 나누어져 설치된다.

초기화 및 볼륨 생성하기

root@pve:~# fdisk /dev/sdc

Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): g
Created a new GPT disklabel (GUID: 82445B73-BCCC-CD44-BA26-69587AB66B90).

Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-125045390, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-125045390, default 125045390):

Created a new partition 1 of type 'Linux filesystem' and of size 59.6 GiB.

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

PV, VG, LV를 각각 생성한다. VG의 이름은 vg, LV의 이름은 lv01 로 생성한다.

root@pve:~# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
root@pve:~# vgcreate vg01 /dev/sdc1
  Volume group "vg01" successfully created
root@pve:~# vgs -a
  VG   #PV #LV #SN Attr   VSize    VFree
  vg01   1   0   0 wz--n-   59.62g  59.62g
root@pve:~# lvcreate -L 59G -n lv01 vg01
  Logical volume "lv01" created.

파티션 생성 작업 없이 디스크에 바로 pv를 생성할 수도 있다.

root@pve:~# pvcreate /dev/sdc
root@pve:~# vgcreate vg01 /dev/sdc
root@pve:~# lvcreate -L 59G -n lv01 vg01

볼륨이 생성됐다. ext4 파티션으로 포맷한 후 mount한다.

root@pve:~# mkfs.ext4 /dev/vg01/lv01
mke2fs 1.44.5 (15-Dec-2018)
Discarding device blocks: done
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: 13254511-64e7-4da1-b9a9-9c309daba9c5
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done

root@pve:~# mkdir lv01
root@pve:~# mount /dev/vg01/lv01 lv01
root@pve:~# mount
/dev/mapper/vg01-lv01 on /root/lv01 type ext4 (rw,relatime)

raid 볼륨 생성하기

3개 디스크를 갖는 vg를 만든다.

vgcreate vg01 /dev/sdb1 /dev/sdc1 /dev/sdd1

raid0 볼륨을 만든다. -i3는 –stripes 3과 같으며 3개 디스크에 나누어 저장된다.

root@pve:~# lvcreate --type raid0 -L 10G -i3 -n lv01 vg01
  Using default stripesize 64.00 KiB.
  Rounding size 10.00 GiB (2560 extents) up to stripe boundary size <10.01 GiB(2562 extents).
  Logical volume "lv01" created.

root@pve:~# lvs -ao +devices vg01 | grep lv01
  lv01            vg01 rwi-a-r---  <10.01g lv01_rimage_0(0),lv01_rimage_1(0),lv01_rimage_2(0)
  [lv01_rimage_0] vg01 iwi-aor---   <3.34g /dev/sdd1(484755)
  [lv01_rimage_1] vg01 iwi-aor---   <3.34g /dev/sdb1(42667)
  [lv01_rimage_2] vg01 iwi-aor---   <3.34g /dev/sdc1(17249)

root@pve:~# lvremove vg01/lv01
Do you really want to remove and DISCARD active logical volume vg01/lv01? [y/n]: y
  Logical volume "lv01" successfully removed

raid5 볼륨을 만든다. stripes 개수는 사용할 디스크 개수-1로 입력해야 한다. (-i2 => 디스크 3개 사용)

root@pve:~# lvcreate --type raid5 -L 10G -i2 -n lv01 vg01
  Using default stripesize 64.00 KiB.
  Logical volume "lv01" created.

root@pve:~# lvs -ao +devices vg01 | grep lv01
  lv01            vg01 rwi-a-r---   10.00g  lv01_rimage_0(0),lv01_rimage_1(0),lv01_rimage_2(0)
  [lv01_rimage_0] vg01 Iwi-aor---    5.00g  /dev/sdd1(484756)
  [lv01_rimage_1] vg01 Iwi-aor---    5.00g  /dev/sdb1(42668)
  [lv01_rimage_2] vg01 Iwi-aor---    5.00g  /dev/sdc1(17250)
  [lv01_rmeta_0]  vg01 ewi-aor---    4.00m  /dev/sdd1(484755)
  [lv01_rmeta_1]  vg01 ewi-aor---    4.00m  /dev/sdb1(42667)
  [lv01_rmeta_2]  vg01 ewi-aor---    4.00m  /dev/sdc1(17249)

root@pve:~# lvremove vg01/lv01
Do you really want to remove and DISCARD active logical volume vg01/lv01? [y/n]: y
  Logical volume "lv01" successfully removed

stripe 볼륨을 만든다. raid0과 비슷하지만 이후 용량을 추가할 때 디스크 개수를 다르게 지정할 수 있다.

root@pve:~# lvcreate -L 10G -i3 -n lv01 vg01
  Using default stripesize 64.00 KiB.
  Rounding size 10.00 GiB (2560 extents) up to stripe boundary size <10.01 GiB(2562 extents).
  Logical volume "lv01" created.

root@pve:~# lvs -ao +devices vg01/lv01
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert Devices
  lv01 vg01 -wi-a----- <10.01g                                                     /dev/sdd1(484755),/dev/sdb1(42667),/dev/sdc1(17249)

root@pve:~# lvextend -L +10G -i2 vg01/lv01
  Using stripesize of last segment 64.00 KiB
  Size of logical volume vg01/lv01 changed from <10.01 GiB (2562 extents) to <20.01 GiB (5122 extents).
  Logical volume vg01/lv01 successfully resized.

root@pve:~# lvs -ao +devices vg01/lv01
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert Devices
  lv01 vg01 -wi-a----- <20.01g                                                     /dev/sdd1(484755),/dev/sdb1(42667),/dev/sdc1(17249)
  lv01 vg01 -wi-a----- <20.01g                                                     /dev/sdd1(485609),/dev/sdb1(43521)

root@pve:~# lvremove vg01/lv01
Do you really want to remove and DISCARD active logical volume vg01/lv01? [y/n]: y
  Logical volume "lv01" successfully removed

lv 삭제하기

삭제하려는 볼륨은 mount되어있지 않아야 한다.

root@pve:~# umount lv01

root@pve:~# lvremove vg01/lv01
Do you really want to remove and DISCARD active logical volume vg01/lv01? [y/n]: y
  Logical volume "lv01" successfully removed

cache 디스크 설정하기

root@pve:~# vgs -a
  VG   #PV #LV #SN Attr   VSize    VFree
  pve    1  23   0 wz--n- <465.26g <16.00g
  wd04   1   3   0 wz--n-   <3.64t  <2.35t

WD04 vg에 ssd를 추가하여 캐시로 지정할 것이다.

root@pve:~# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
root@pve:~# vgextend wd04 /dev/sdc1
  Volume group "wd04" successfully extended

캐시용 볼륨을 생성한다. 데이터용, 메타용 2개를 생성한다.

root@pve:~# lvcreate -L 45G -n CacheDataLV wd04 /dev/sdc1
  Logical volume "CacheDataLV" created.
root@pve:~# lvcreate -L  1G -n CacheMetaLV wd04 /dev/sdc1
  Logical volume "CacheMetaLV" created.
root@pve:~# lvextend -l +100%FREE wd04/CacheDataLV
  Size of logical volume wd04/CacheDataLV changed from 45.00 GiB (11520 extents) to <2.40 TiB (628544 extents).
  Logical volume wd04/CacheDataLV successfully resized.
root@pve:~# pvs -a
  PV                    VG   Fmt  Attr PSize    PFree
  /dev/sdc1             wd04 lvm2 a--    59.62g      0
root@pve:~# lvconvert --type cache-pool --poolmetadata wd04/CacheMetaLV wd04/CacheDataLV
  WARNING: Converting wd04/CacheDataLV and wd04/CacheMetaLV to cache pool's data and metadata volumes with metadata wiping.
  THIS WILL DESTROY CONTENT OF LOGICAL VOLUME (filesystem etc.)
Do you really want to convert wd04/CacheDataLV and wd04/CacheMetaLV? [y/n]: y
  Converted wd04/CacheDataLV and wd04/CacheMetaLV to cache pool.

root@pve:~# lvs -a
  LV                  VG   Attr       LSize    Pool     Origin Data%  Meta%  Move Log Cpy%Sync Convert
  CacheDataLV         wd04 Cwi---C---   45.00g
  [CacheDataLV_cdata] wd04 Cwi-------   45.00g
  [CacheDataLV_cmeta] wd04 ewi-------    5.00g
  [lvol0_pmspare]     wd04 ewi-------    5.00g
  thinpool            wd04 twi-aotz--  300.00g                 43.96  19.96
  [thinpool_tdata]    wd04 Twi-ao----  300.00g
  [thinpool_tmeta]    wd04 ewi-ao----  400.00m

root@pve:~# pvs -a
  PV                VG   Fmt  Attr PSize    PFree
  /dev/sdc1         wd04 lvm2 a--    59.62g   9.62g

root@pve:~# lvconvert --type cache --cachepool wd04/CacheDataLV --cachemode writeback wd04/thinpool
Do you want wipe existing metadata of cache pool wd04/CacheDataLV? [y/n]: y
  WARNING: Cached thin pool's data cannot be currently resized and require manual uncache before resize!
  Logical volume wd04/thinpool_tdata is now cached.

root@pve:~# lvs -a
  LV                     VG   Attr       LSize    Pool          Origin                 Data%  Meta%  Move Log Cpy%Sync Convert
  [CacheDataLV]          wd04 Cwi---C---   45.00g                                      0.01   0.12            0.00
  [CacheDataLV_cdata]    wd04 Cwi-ao----   45.00g
  [CacheDataLV_cmeta]    wd04 ewi-ao----    5.00g
  [lvol0_pmspare]        wd04 ewi-------    5.00g
  thinpool               wd04 twi-aotz--  300.00g                                      43.96  19.96
  [thinpool_tdata]       wd04 Cwi-aoC---  300.00g [CacheDataLV] [thinpool_tdata_corig] 0.01   0.12            0.00
  [thinpool_tdata_corig] wd04 owi-aoC---  300.00g
  [thinpool_tmeta]       wd04 ewi-ao----  400.00m

cache 해제 하기

root@pve:~# lvconvert --uncache wd04/thinpool
Do you really want to remove and DISCARD logical volume wd04/CacheDataLV? [y/n]: y
  Logical volume "CacheDataLV" successfully removed
  Logical volume wd04/thinpool_tdata is not cached.

disk 제거하기

# sdc1 의 데이터를 vg 내의 다른 디스크로 옮긴다.
pvmove /dev/sdc1

# vg에서 /dev/sdc1를 제거한다.
vgreduce wd04 /dev/sdc1