LVM 설정 구성 예제


LVM 설정 구성 예제
LVM(Logical Volume Management)이란 파티션을 논리적 볼륨으로 관리하는 것입니다. LVM 설정 방법에 대해서 설명드리도록 하겠습니다.
LVM 구성 방법과 LVM 예제를 pvcreate, lvcreate, vgcreate 등의 명령어를 이용하여, 우분투에서 LVM 생성하는 방법에 대해서 알려드리겠습니다.

LVM 설정 구성 예제

LVM은 변화되는 환경에 맞춰서 볼륨 크기를 동적으로 변화할 수 있을 뿐만 아니라, 여러개의 물리 디스크를 하나의 볼륨으로 사용할 수 있는 특징이 있습니다. 우분투를 설치할 때 LVM 으로 설치할 수도 있는데, 우분투 설치 방법은 아래의 글을 참고해 주시기 바랍니다.

우분투 18.04 설치

아래의 명령어를 실행하여 LVM 패키지를 설치합니다.

$ sudo apt-get install lvm2

물리 볼륨(Physical Volume) 생성

LVM 설정 구성 예제 LVM 구성을 위해서 가장 먼저 물리 볼륨을 생성해야 하는데, 먼저 아래의 fdisk를 명령어를 이용하여 디스크를 확인 합니다.

# fdisk -l
Disk /dev/sda: 232.9 GiB, 250058268160 bytes, 488395055 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x7c052a54

Device     Boot    Start       End   Sectors   Size Id Type
/dev/sda1           2048  15624191  15622144   7.5G 82 Linux swap / Solaris
/dev/sda2  *    15624192 488394751 472770560 225.4G 83 Linux

Disk /dev/sdb: 149.1 GiB, 160041885696 bytes, 312581808 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc0cac0ca

위의 내용에서 /dev/sdb 디스크를 이용하여 LVM을 구성하도록 하겠습니다. 아래와 같이 디스크 이름을 지정하면서 fdisk를 실행합니다.

$ fdisk /dev/sdb

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

Command (m for help):

fdisk는 명령어가 한글자로 이뤄져 있습니다. 주요 명령어는 아래와 같습니다.

  • d : delete a partition
  • F : list free unpartitioned space
  • l : list known partition types
  • n : add a new partition
  • p : print the partition table
  • t : change a partition type
  • v : verify the partition table
  • i : print information about a partition

먼저 아래와 같이 파티션을 생성합니다.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-312581807, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-312581807, default 312581807): 

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

그리고 아래와 같이 파티션을 Linux LVM으로 변경합니다. 중간에 L 명어는 변경가능한 파티션 형태를 출력하기 위해서 입력한 것입니다.

Command (m for help): t 
Selected partition 1
Partition type (type L to list all types): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE 
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

위의 출력 결과에서는 Linux LVM 파티션 타입이 8e 값을 나타내기에 8e 값을 입력하였습니다. 하지만 최신 버전의 fdisk에서는 Linux LVM 값이 31을 나타낼 수 있습니다. 따라서 지원되는 파티션 유형을 확인 후 정확한 값을 입력해 주시기 바랍니다.

그리고 아래의 명령어를 실행하면 생성된 파티션 정보를 확인 할 수 있습니다.

Command (m for help): p
Disk /dev/sdb: 149.1 GiB, 160041885696 bytes, 312581808 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc0cac0ca

Device     Boot Start       End   Sectors   Size Id Type
/dev/sdb1        2048 312581807 312579760 149.1G 8e Linux LVM

마지막으로 아래와 같이 w를 입력하여 수정된 파티션 정보를 적용하고 종료 합니다.

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

Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

/dev/sdb 디스크가 사용중일 경우, 위와 같은 메세지가 나타날 수 있습니다. 이 경우 partprobe 명령어를 실행해 주시거나 시스템을 재부팅해주시기 바랍니다. 파티션이 생성되었다면, 마지막으로 pvcreate 명령어를 실행하여 물리 볼륨를 초기화 하여 LVM 생성 후 사용할 수 있도록 합니다.

# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

볼륨 그룹(Volume Group) 생성

여러개의 물리 볼륨을 이용하여 논리 그룹을 구성할 수 있습니다. 하지만 위에서 생성한 물리 볼륨을 이용하여 하나의 논리그룹만 생성해 보도록 하겠습니다. 아래의 vgcreate 명령어를 이용하여 논리 그룹을 생성합니다.

# vgcreate vg /dev/sdb1
  Volume group "vg" successfully created

위의 vgcreate 명령어는 볼륨 그룹이름을 vg로 하고 /dev/sdb1의 물리 볼륨을 추가한 것입니다. 예제에서는 하나의 물리볼륨만 추가하였지만, 여러개의 물리 볼륨을 추가하여 논리 그룹을 구성할 수도 있습니다. 아래는 vgcreate 명령어 사용법 입니다.

vgcreate [VolumeGroupName] [PhysicalDevicePath] [PhysicalDevicePath...]

논리 볼륨(Logical Volume) 생성

논리 볼륨은 lvcreate 명령어를 사용하여 생성할 수 있습니다. 아래의 내용은 볼륨 vg에 lv1 이라는 이름을 갖는 10G 크기의 논리 볼륨을 생성하는 것입니다.

# lvcreate -L 10G -n lv1 vg
  Logical volume "lv1" created.

사용가능한 사이즈 모두를 할당하기 위해서는 아래의 명령어를 사용하시면 됩니다.

# lvcreate -l 100%FREE -n lv1 vg

위의 명령어로 논리 볼륨을 생성하였을 경우, /dev/vg/lv1으로 논리 볼륨에 접근하여 사용할 수 있습니다.
생성된 볼륨은 아래의 명령어로 EXT4 타입으로 포맷하실 수 있습니다.

# mkfs.ext4 /dev/vg/lv1
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 2621440 4k blocks and 655360 inodes
Filesystem UUID: d4a42516-5425-42c8-b38d-1993aefae41d
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

그리고 포맷한 볼륨을 아래의 명령어로 마운트 하여 사용하실 수 있습니다.

# mount /dev/vg/lv1 /mnt

아래와 같이 df 명령어로 확인하시면, 생성된 LVM 논리 볼륨이 성공적으로 마운트 된 것을 알 수 있습니다.

# df -h | grep "vg"
/dev/mapper/vg-lv1  9.8G   23M  9.2G   1% /mnt

LVM 확장

위에서 생성한 LVM 논리 볼륨을 확장해 보도록 하겠습니다. 우선 아래의 명령어를 이용하여 현재 마운트 된 볼륨을 해제 합니다.

# umount /dev/vg/lv1

그리고 lvextend 라는 명령어를 이용하여 논리 볼륨을 확장할 수 있습니다.
아래의 명령어는 위에서 생성한 볼륨그룹에서 10G 용량을 추가한 것입니다.

# lvextend -L +10G /dev/vg/lv1
  Size of logical volume vg/lv1 changed from 10.00 GiB (2560 extents) to 20.00 GiB (5120 extents).
  Logical volume lv1 successfully resized.

만약 남는 여유공간 모두를 확장하고자 하실 경우 아래의 명령어를 실행해 주시면 됩니다.

# lvextend -l +100%FREE /dev/vg/lv1

그리고 추가한 볼륨을 아래의 명령어로 검사한 뒤에 파일시스템 사이즈를 조절합니다.

# e2fsck -f /dev/vg/lv1
e2fsck 1.42.13 (17-May-2015)
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/vg/lv1: 12/655360 files (0.0% non-contiguous), 79664/2621440 blocks
# resize2fs /dev/vg/lv1
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/vg/lv1 to 5242880 (4k) blocks.
The filesystem on /dev/vg/lv1 is now 5242880 (4k) blocks long.

다시 볼륨을 마운트 하고 용량을 확인하면, 용량이 늘어난 것을 확인 하실 수 있으실 겁니다.

# df -h | grep "vg"
/dev/mapper/vg-lv1   20G   28M   19G   1% /mnt

위에서는 lvextend 명령어로 논리 볼륨을 확장하였습니다. 하지만 pvcreate 명령어로 물리볼륨을 생성하여 vgextend 명령어로 기존의 볼륨 그룹에서 새로운 물리 볼륩을 추가하면서 볼륨 그룹을 확장 한 다음, 다시 논리 볼륨을 확장 할 수 도 있습니다.

LVM 정보 확인

LVM 에 대한 물리볼륨, 볼륨 그룹, 논리 볼륨을 확인 하기 위해서 각각 pvdisplay, vgdisplay, lvdisplay 명령어가 제공됩니다.
아래의 내용은 각각의 명령어를 실행한 결과입니다.

# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg
  PV Size               149.05 GiB / not usable 2.84 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              38156
  Free PE               33036
  Allocated PE          5120
  PV UUID               EpiTAx-ooae-Vw70-dG9m-aJt0-X4Nm-Aeuevo
# vgdisplay 
  --- Volume group ---
  VG Name               vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  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               149.05 GiB
  PE Size               4.00 MiB
  Total PE              38156
  Alloc PE / Size       5120 / 20.00 GiB
  Free  PE / Size       33036 / 129.05 GiB
  VG UUID               y42lgU-enXo-inOd-OfS5-aiXv-aKx6-ncD1MP
# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg/lv1
  LV Name                lv1
  VG Name                vg
  LV UUID                EupoOK-AXvP-Mk8R-rFGs-BJsV-jBK7-zV2WeV
  LV Write Access        read/write
  LV Creation host, time ubuntu, 2018-04-08 23:07:32 +0900
  LV Status              available
  # open                 1
  LV Size                20.00 GiB
  Current LE             5120
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           252:0

LVM fstab 설정

생성된 LVM의 논리 볼륨을 부팅시에 자동으로 마운트 될 수 있도록 fstab 설정 하도록 하겠습니다.
먼저 아래의 lvs 명령어를 실행하여 현재의 논리 볼륨과 볼륨 그룹을 확인 합니다.

# lvs
  LV    VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data  vg   -wi-ao---- 50.00g                                                    
  extra vg   -wi-ao---- 50.00g

그리고 아래의 명령어를 실행하여 mapper로 지정된 장치명을 확인합니다.

# ls /dev/mapper/
control  vg-data  vg-extra

위에서 확인한 내용 처럼 /dev/mapper/vgname-lvname 으로 장치명이 지정된 것을 알 수 있습니다.
위의 논리 볼륨을 마운트할 디렉토리를 아래와 같이 생성합니다

# mkdir /data
# mkdir /extra

위의 /dev/mapper/vg-data 와 /dev/mapper/vg-extra를 부팅시 자동으로 마운트 되게 하기 위해서는 /etc/fstab 파일에 아래와 같은 내용을 추가합니다.

/dev/mapper/vg-data /data ext4 defaults 0 0
/dev/mapper/vg-extra /extra ext4 defaults 0 0

그리고 아래의 명령어를 실행 한 다음, 시스템을 재부팅하면 부팅시 자동으로 논리 볼륨이 마운트 된 것을 확인 하실 수 있으실 겁니다.

# update-initramfs -u

LVM 에러 해결

lvdisplay 라는 명령어를 실행 시, 아래와 같은 메세지가 나타나는 경우입니다.

Device for PV [PV UUID] not found or rejected by a filter

# lvdisplay 
  WARNING: Device for PV qnsgxh-ZVT8-qjVi-ASZZ-MQ8S-2gDF-Xj7YII not found or rejected by a filter.

다음과 같은 명령어를 실행하면, 문제가 해결됩니다.

# pvscan --cache
# pvscan

그리고 pvdisplay 라는 명령어를 실행했을 경우, 다음과 같이 에러가 발생하는 경우입니다.

read failed after 0 of 4096 at 0: Input/output error

# pvdisplay 
  /dev/vg-name/lv-name: read failed after 0 of 4096 at 0: Input/output error
  /dev/vg-name/lv-name: read failed after 0 of 4096 at 114366021632: Input/output error
  /dev/vg-name/lv-name: read failed after 0 of 4096 at 114366078976: Input/output error
  /dev/vg-name/lv-name: read failed after 0 of 4096 at 4096: Input/output error

이 경우 아래와 같은 명령어를 실행하면, 문제가 해결됩니다.

# dmsetup remove /dev/vg-name/lv-name

( 본문 인용시 출처를 밝혀 주시면 감사하겠습니다.)