리눅스 기초 다지기 - 20. LVM

LVM(Logical Volume Management)

: 논리 볼륨 관리자, 여러 개의 물리적인 하드디스크 파티션을 Volume Group 으로 묶은 다음, 논리적인 디스크로 할당하여 유연하게 관리 할 수 있게 해 주는 기술

 

 

구성요소

  • PV(Physical Volume)
  • PE(Physical Extent)
  • VG(Volume Group)
  • LV(Logical Volume)
  • LE(Logical Extent)

 

PV (Physical Volume)

 

PV는 실제 물리 디스크를 LVM 용도로 초기화된 물리 디스크 또는 파티션이다.

PV는 일정한 크기의 PE(Physical Extent)들로 구성이 된다.

 

PV 확인

  • pvs
  • pvscan
  • pvdisplay

PV 생성

  • pvcreate

PV 삭제

  • pvremove

 

PE(Physical Extent)

 

PV를 구성하는 일정한 크기의 블록으로 기본크기는 4MB이다.

 

VG(Volume Group)

 

PV들을 하나의 그룹으로 묶은 공간이다.

 

 

LV(Logical Volume)

 

논리 볼륨으로 실제 사용자가 다루게 될 스토리지이다.

 

 

LE(Logical Extent)

 

LV를 구성하는 일정한 크기의 블록으로 기본크기는 4MB이다.

 

LVM 구성

 

PV로 만들 파티션 생성

gdisk /dev/sdb
Command (? for help): n
Partition number (1-128, default 1):
First sector (34-20971486, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8e00
Changed type of partition to 'Linux LVM'

Command (? for help): p
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Model: VBOX HARDDISK
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5C2FA19E-F590-4C19-923A-DA612C69AF97
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        20971486   10.0 GiB    8E00  Linux LVM

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.

 

 

파티션 재인식

partprobe /dev/sdb

 

 

생성한 파티션 확인

[root@control ~]# gdisk -l /dev/sdb
GPT fdisk (gdisk) version 1.0.7

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.
Disk /dev/sdb: 20971520 sectors, 10.0 GiB
Model: VBOX HARDDISK
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5C2FA19E-F590-4C19-923A-DA612C69AF97
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        20971486   10.0 GiB    8E00  Linux LVM

 

pv 생성

[root@control ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.

 

pv 확인 (간단하게)

[root@control ~]# pvs
PV         VG Fmt  Attr PSize  PFree
/dev/sda2  rl lvm2 a--  48.13g    0

 

pv 확인(세부적으로)

[root@control ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               rl
  PV Size               <48.14 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12322
  Free PE               0
  Allocated PE          12322
  PV UUID               asiLW2-5HM0-gDr2-VSc3-Xc00-rnLY-BciB82

  "/dev/sdb1" is a new physical volume of "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               <10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               X8lJ1d-DS0W-2dBY-rdfD-Z1rx-5fTy-kplyMi

 

vg 생성

# PE 크기 기본 4mb
[root@control ~]# vgcreate hk /dev/sdb1
Volume group "hk" successfully created

# PE 크기를 2mb로 지정
[root@control ~]# vgcreate -s 2mb hk /dev/sdb1

 

vg 확인 (간단하게)

[root@control ~]# vgs
 VG #PV #LV #SN Attr   VSize   VFree
 hk   1   0   0 wz--n- <10.00g <10.00g
 rl   1   1   0 wz--n-  48.13g      0

 

vg 확인 (세부적으로)

[root@control ~]# vgdisplay
  --- Volume group ---
  VG Name               hk
  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               <10.00 GiB
  PE Size               4.00 MiB
  Total PE              2559
  Alloc PE / Size       0 / 0
  Free  PE / Size       2559 / <10.00 GiB
  VG UUID               D8StCK-9bTL-lnUJ-BuXl-Em0n-IpQn-H42Sew

  --- Volume group ---
  VG Name               rl
  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               48.13 GiB
  PE Size               4.00 MiB
  Total PE              12322
  Alloc PE / Size       12322 / 48.13 GiB
  Free  PE / Size       0 / 0
  VG UUID               Dcl90g-cCXL-HKAc-fnZS-Gzkr-Wf2R-cpq0EN

 

 

lv 생성 [-L 사이즈(용량)]

# -n lv 이름 지정
# -L 사이즈 지정
# hk 볼륨 그룹 선택
lvcreate -n openstack -L 2GB hk
Logical volume "openstack" created.

 

 

lv 생성 [-l 사이즈(pe)]

# -r --resizefs
[root@control ~]# lvextend -r -l +100 /dev/hk/openstack
  Size of logical volume hk/openstack changed from 4.00 GiB (1024 extents) to 4.39 GiB (1124 extents).
  File system xfs found on hk/openstack.
  File system mount is needed for extend.
Continue with xfs file system extend steps: mount, xfs_growfs? [y/n]:y
  Extending file system xfs to 4.39 GiB (4714397696 bytes) on hk/openstack...
mount /dev/hk/openstack /tmp/tmp.ixNsjTFZo8_lvresize_5034
mount done
xfs_growfs /dev/hk/openstack
meta-data=/dev/mapper/hk-openstack   isize=512    agcount=8, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1
data     =                       bsize=4096   blocks=1048576, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1048576 to 1150976
xfs_growfs done
cleanup unmount /tmp/tmp.ixNsjTFZo8_lvresize_5034
cleanup unmount done
  Extended file system xfs on hk/openstack.
  Logical volume hk/openstack successfully resized.

 

 

lv 용량 추가 [-l 남은 공간 모두(100%FREE)]

[root@control ~]# lvextend -r -l +100%FREE /dev/hk/openstack
  Size of logical volume hk/openstack changed from 4.39 GiB (1124 extents) to <12.00 GiB (3071 extents).
  File system xfs found on hk/openstack.
  File system mount is needed for extend.
Continue with xfs file system extend steps: mount, xfs_growfs? [y/n]:y
  Extending file system xfs to <12.00 GiB (12880707584 bytes) on hk/openstack...
mount /dev/hk/openstack /tmp/tmp.0FcYgf5S3E_lvresize_5067
mount done
xfs_growfs /dev/hk/openstack
meta-data=/dev/mapper/hk-openstack   isize=512    agcount=9, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1    bigtime=1 inobtcount=1
data     =                       bsize=4096   blocks=1150976, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1150976 to 3144704
xfs_growfs done
cleanup unmount /tmp/tmp.0FcYgf5S3E_lvresize_5067
cleanup unmount done
  Extended file system xfs on hk/openstack.
  Logical volume hk/openstack successfully resized.

 

 

xfs 파일시스템 검사 (umount 해야함)

# -n read-only 검사만 하고 복구는 하지 않는다.
[root@control ~]# xfs_repair -n /dev/hk/openstack
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
        - scan filesystem freespace and inode maps...
        - found root inode chunk
Phase 3 - for each AG...
        - scan (but don't clear) agi unlinked lists...
        - process known inodes and perform inode discovery...
        - agno = 0
        - agno = 1
        - agno = 2
        - agno = 3
        - agno = 4
        - agno = 5
        - agno = 6
        - agno = 7
        - agno = 8
        - agno = 9
        - agno = 10
        - agno = 11
        - agno = 12
        - agno = 13
        - agno = 14
        - agno = 15
        - agno = 16
        - agno = 17
        - agno = 18
        - agno = 19
        - agno = 20
        - agno = 21
        - agno = 22
        - agno = 23
        - process newly discovered inodes...
Phase 4 - check for duplicate blocks...
        - setting up duplicate extent list...
        - check for inodes claiming duplicate blocks...
        - agno = 0
        - agno = 2
        - agno = 3
        - agno = 5
        - agno = 6
        - agno = 7
        - agno = 8
        - agno = 9
        - agno = 10
        - agno = 11
        - agno = 1
        - agno = 13
        - agno = 14
        - agno = 15
        - agno = 16
        - agno = 17
        - agno = 19
        - agno = 20
        - agno = 21
        - agno = 22
        - agno = 4
        - agno = 23
        - agno = 18
        - agno = 12
No modify flag set, skipping phase 5
Phase 6 - check inode connectivity...
        - traversing filesystem ...
        - traversal finished ...
        - moving disconnected inodes to lost+found ...
Phase 7 - verify link counts...
No modify flag set, skipping filesystem flush and exiting.

# 문제가 있을 시 복구 명령어
xfs_repair /dev/hk/openstack

 

ext4 파일시스템 검사 및 복구 (umount 해야함)

# 파일시스템 ext4 일 때
e2fsck /dev/hk/openstack2
e2fsck 1.46.5 (30-Dec-2021)
/dev/hk/openstack2: clean, 11/65536 files, 12955/262144 blocks

 

확인

[root@control ~]# lvs
  LV     VG Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  openstack  hk -wi-a----- <12.00g
  openstack2 hk -wi-ao----   1.00g
  openstack3 hk -wi-a-----  <7.00g
  root   rl -wi-ao----  48.13g

[root@control ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/hk/openstack
  LV Name                openstack
  VG Name                hk
  LV UUID                nTMBq5-0ZNJ-d86y-f6Im-YjfY-cD2L-6C9T69
  LV Write Access        read/write
  LV Creation host, time control, 2023-08-08 03:05:46 +0900
  LV Status              available
  # open                 0
  LV Size                <12.00 GiB
  Current LE             3071
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/hk/openstack2
  LV Name                openstack2
  VG Name                hk
  LV UUID                xJgKGg-CUI8-gIPl-Z73b-NIpQ-BnrY-CyvtDy
  LV Write Access        read/write
  LV Creation host, time control, 2023-08-08 03:08:55 +0900
  LV Status              available
  # open                 1
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/hk/openstack3
  LV Name                openstack3
  VG Name                hk
  LV UUID                iWlsK7-8y9j-LcxW-O9bB-jInu-77TX-q4qLtz
  LV Write Access        read/write
  LV Creation host, time control, 2023-08-08 03:11:10 +0900
  LV Status              available
  # open                 0
  LV Size                <7.00 GiB
  Current LE             1791
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3

  --- Logical volume ---
  LV Path                /dev/rl/root
  LV Name                root
  VG Name                rl
  LV UUID                P0o4g3-Y3Dd-FXWn-Ryqz-OODl-XgAQ-drUjRH
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2023-08-07 02:23:41 +0900
  LV Status              available
  # open                 1
  LV Size                48.13 GiB
  Current LE             12322
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

 

마운트 및 확인

mount /test/openstack

[root@control ~]# df -hT
Filesystem            Type      Size  Used Avail Use% Mounted on
devtmpfs              devtmpfs  4.0M     0  4.0M   0% /dev
tmpfs                 tmpfs     3.8G     0  3.8G   0% /dev/shm
tmpfs                 tmpfs     1.5G  8.7M  1.5G   1% /run
/dev/mapper/rl-root   ext4       48G  1.1G   44G   3% /
/dev/sda1             ext4      920M  173M  684M  21% /boot
/dev/mapper/hk-openstack2 ext4      974M   24K  907M   1% /test/openstack2
/dev/sda3             vfat      952M  7.0M  945M   1% /boot/efi
tmpfs                 tmpfs     768M     0  768M   0% /run/user/0
/dev/mapper/hk-openstack  xfs        12G  120M   12G   1% /test/openstack

 

LVM 축소

 

LVM을 축소할 때 파일시스템이 있을 경우, 먼저 파일시스템을 줄여야 한다.

또한 축소 전, 백업은 필수이다.

 

 

파일시스템 축소 명령어

ext4 → resize2fs

xfs → 축소 기능을 지원하지 않음

 

축소 대상 lvm 정보 확인

# 축소 대상 lv 정보 확인 -> 용량은 300MB(272MB)이며 파일시스템은 ext4이다.
[root@control ~]# df -hT
Filesystem           Type      Size  Used Avail Use% Mounted on
/dev/mapper/hk-openstack ext4      272M   14K  253M   1% /test/openstack

# lvs
[root@control ~]# lvs
  LV    VG Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  openstack hk -wi-ao---- 300.00m
  root  rl -wi-ao----  16.41g
  swap  rl -wi-ao----   2.00g

 

 

용량 축소

# 마운트 해제
umount /test/openstack

# 축소 전 파일시스템 검사
# xfs -> xfs_repair, ext4 -> e2fsck
[root@control ~]# e2fsck /dev/hk/openstack
e2fsck 1.46.5 (30-Dec-2021)
/dev/hk/openstack: clean, 11/76912 files, 29591/307200 blocks

# 파일시스템 축소
[root@control ~]# resize2fs -f /dev/hk/openstack 200M
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/hk/openstack to 204800 (1k) blocks.
The filesystem on /dev/hk/openstack is now 204800 (1k) blocks long.

# LVM 파티션 용량 200MB 축소
[root@control ~]# lvreduce --resizefs -L -200M /dev/hk/openstack
  File system ext4 found on hk/openstack.
  File system size (200.00 MiB) is equal to the requested size (200.00 MiB).
  File system reduce is not needed, skipping.
  Size of logical volume hk/openstack changed from 300.00 MiB (75 extents) to 200.00 MiB (50 extents).
  Logical volume hk/openstack successfully resized.

 

—resizefs

: 이 옵션을 사용하면 lvreduce 명령에서 논리 볼륨을 축소하기 전에 파일 시스템을 줄입니다. 파일 시스템이 가득 차거나 파일 시스템이 축소를 지원하지 않는 경우 파일 시스템을 축소하는 데 실패하는 경우 lvreduce 명령이 실패하고 논리 볼륨을 축소하려고 시도하지 않습니다.

 

마운트 및 확인

# 마운트
[root@control ~]# mount -t ext4 /dev/hk/openstack /test/openstack

# 마운트 확인
[root@control ~]# df -hT
/dev/mapper/hk-openstack ext4      179M   14K  165M   1% /test/openstack

[root@control ~]# lvs
  LV    VG Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  openstack hk -wi-ao---- 200.00m
  root  rl -wi-ao----  16.41g
  swap  rl -wi-ao----   2.00g