리눅스 / LVM / 만드는 방법, 수정하는 방법, 삭제하는 방법

1TB 디스크와 2TB 디스크를 추가했다고 하자. 디스크 크기에 맞게 그대로 사용할 수도 있지만, 3TB 하나인 것처럼 사용하거나 1.5TB 두 개인 것처럼 사용하고 싶을 수도 있다. 즉, 물리적인 디스크 크기와 무관하게 자유롭게 볼륨을 만들고 싶은 것이다.

이럴 때 사용하는 것이 LVM(Logical Volume Manager)이다. 여러 개의 볼륨을 하나로 묶은 후 다시 배분하여 사용한다.(이렇게 만든 볼륨을 논리적 볼륨이라고 한다.)

LVM을 이용하여 논리적 볼륨을 만들고, 수정하고, 삭제하는 방법을 알아본다.

LVM 만들기

LVM으로 볼륨을 만드는 순서는 다음과 같다.

  1. 물리적 볼륨을 만든다.
  2. 물리적 볼륨을 묶어서 볼륨 그룹을 만든다.
  3. 볼륨 그룹에 논리적 볼륨을 만든다.

간단한 예로 어떻게 구성하는지 알아보자. 목표는 100GB 디스크과 200GB 디스크로 50GB, 80GB, 170GB 볼륨을 만드는 것이다.

파티션 만들기

  • 현재 상태는 다음과 같다. 디스크 두 개를 추가해서 /dev/sdb와 /dev/sdc가 생겼고, /dev/sdb의 크기는 100GB, /dev/sdc의 크기는 200GB이다.
# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdc
  • fdisk로 파티션을 만든다. 주의할 점은 타입을 8e로 해야 한다는 것이다.
# fdisk /dev/sdb

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

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xa066b799.

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-209715199, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199):

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

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
  • 파티션을 만든 후의 상태는 다음과 같다.
# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdb1  /dev/sdc  /dev/sdc1

물리적 볼륨 만들기

  • /dev/sdb1과 /dev/sdc1로 물리적 볼륨(Physical Volume)을 만든다.
# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
root@ubuntu-2004-s-02:~# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.

볼륨 그룹 만들기

  • /dev/sdb1과 /dev/sdc1를 볼륨 그룹으로 만든다.
  • VG-01은 볼륨 그룹의 이름으로, 다른 것으로 만들어도 된다.
# vgcreate VG-01 /dev/sdb1 /dev/sdc1
  Volume group "VG-01" successfully created

논리적 볼륨 만들기

  • VG-01 볼륨 그룹에 50GB 크기의 논리적 볼륨(Logical Volume)을 만든다.
  • LG-01은 논리적 볼륨의 이름으로, 다른 것으로 만들어도 된다.
# lvcreate --size 50G --name LG-01 VG-01
  Logical volume "LG-01" created.
  • LG-02라는 이름의 80GB 크기의 논리적 볼륨을 만든다.
# lvcreate --size 80G --name LG-02 VG-01
  Logical volume "LG-02" created.
  • 나머지 공간을 다 사용하는 LG-03이라는 이름의 논리적 볼륨을 만든다.
# lvcreate --extents 100%FREE --name LG-03 VG-01
  Logical volume "LG-03" created.

포맷하고 마운트하기

  • 논리적 볼륨을 포맷한다. 아래는 LG-01을 EXT4 형식으로 포맷한 것이다.
# mkfs.ext4 /dev/VG-01/LG-01
mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 13107200 4k blocks and 3276800 inodes
Filesystem UUID: ee2a0e20-db9f-4e38-b3d1-d46f00ccc215
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
  • 적당한 이름의 폴더를 만들고 마운트한다.
# mkdir /LG-01
# mount /dev/VG-01/LG-01 /LG-01
  • 다음은 세 개의 논리적 볼륨을 포맷하고 마운트한 결과이다. 제일 아래 세 줄에 논리적 볼륨이 추가된 것을 확인할 수 있다.
# df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               1.9G     0  1.9G   0% /dev
tmpfs                              394M  1.4M  393M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  196G  8.8G  178G   5% /
tmpfs                              2.0G     0  2.0G   0% /dev/shm
tmpfs                              5.0M  4.0K  5.0M   1% /run/lock
tmpfs                              2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/loop1                          62M   62M     0 100% /snap/core20/1169
/dev/loop2                          71M   71M     0 100% /snap/lxd/21029
/dev/loop3                          68M   68M     0 100% /snap/lxd/21545
/dev/sda2                          976M  112M  798M  13% /boot
/dev/loop4                          56M   56M     0 100% /snap/core18/2128
/dev/loop5                          33M   33M     0 100% /snap/snapd/13170
/dev/loop0                          62M   62M     0 100% /snap/core20/1081
/dev/loop6                          33M   33M     0 100% /snap/snapd/12704
tmpfs                              394M   36K  394M   1% /run/user/128
tmpfs                              394M     0  394M   0% /run/user/0
/dev/mapper/VG--01-LG--01           49G   53M   47G   1% /LG-01
/dev/mapper/VG--01-LG--02           79G   57M   75G   1% /LG-02
/dev/mapper/VG--01-LG--03          167G   61M  158G   1% /LG-03

LVM 수정하기

볼륨 추가

볼륨 그룹에 볼륨을 추가하여 전체 공간을 확장할 수 있다. 위 예제에 이어 200GB 하드 디스크를 추가로 장착했다고 하면...

  • 디스크를 추가하면 아래와 같은 상태가 된다. /dev/sdd가 추가한 디스크이다.
# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdb1  /dev/sdc  /dev/sdc1  /dev/sdd
  • Linux LVM 파티션을 만든다.
# fdisk /dev/sdd

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

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x8aa237e3.

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):
First sector (2048-419430399, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-419430399, default 419430399):

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

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
  • 물리적 볼륨을 만든다.
# pvcreate /dev/sdd1
  Physical volume "/dev/sdd1" successfully created.
  • VG-01 볼륨 그룹에 추가한다.
# vgextend VG-01 /dev/sdd1
  Volume group "VG-01" successfully extended
  • 200GB의 여유 공간이 생긴다.
# vgdisplay VG-01
  --- Volume group ---
  VG Name               VG-01
  System ID
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  9
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <499.99 GiB
  PE Size               4.00 MiB
  Total PE              127997
  Alloc PE / Size       76798 / 299.99 GiB
  Free  PE / Size       51199 / <200.00 GiB
  VG UUID               YLV2F1-x00c-4CZt-LsCh-3q9O-iE3L-Ynx2vG

확장된 공간에 새로운 논리적 볼륨을 만들거나, 기존 논리적 볼륨의 크기를 늘릴 수 있다.

논리적 볼륨 확장하기

  • lvextend 명령어로 논리적 볼륨의 크기를 확장한다. 아래는 /dev/VG-01/LG-01의 크기를 1GB 늘리는 것.
# lvextend -L+1G /dev/VG-01/LG-01
  Size of logical volume VG-01/LG-01 changed from 50.00 GiB (12800 extents) to 51.00 GiB (13056 extents).
  Logical volume VG-01/LG-01 successfully resized.
  • resize2fs 명령어로 파일 시스템의 크기를 확장해야 반영된다.
# resize2fs /dev/VG-01/LG-01
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/VG-01/LG-01 is mounted on /LG-01; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 7
The filesystem on /dev/VG-01/LG-01 is now 13369344 (4k) blocks long.
  • 아래는 /dev/VG-01/LG-01의 크기를 60GB로 늘리는 것. 마찬가지로 resize2fs 명령으로 파일 시스템 크기를 확장한다.
# lvextend -L60G /dev/VG-01/LG-01
  Size of logical volume VG-01/LG-01 changed from 51.00 GiB (13056 extents) to 60.00 GiB (15360 extents).
  Logical volume VG-01/LG-01 successfully resized.
# resize2fs /dev/VG-01/LG-01
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/VG-01/LG-01 is mounted on /LG-01; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 8
The filesystem on /dev/VG-01/LG-01 is now 15728640 (4k) blocks long.

LVM 삭제하기

LVM 삭제는 LVM 생성의 역순으로 진행한다.

논리적 볼륨 삭제하기

lvremove 명령어로 논리적 볼륨을 삭제한다. 아래는 /dev/VG-01/LG-03를 삭제하는 것.

# lvremove /dev/VG-01/LG-03
Do you really want to remove and DISCARD active logical volume VG-01/LG-03? [y/n]: y
  Logical volume "LG-03" successfully removed

볼륨 그룹 삭제하기

vgremove 명령어로 볼륨 그룹을 삭제한다. 아래는 VG-01을 삭제하는 것으로, 볼륨 그룹 안에 논리적 볼륨이 남아 있으면 삭제할 것인지 묻는다.

# vgremove VG-01
Do you really want to remove volume group "VG-01" containing 2 logical volumes? [y/n]: y
Do you really want to remove and DISCARD active logical volume VG-01/LG-01? [y/n]: y
  Logical volume "LG-01" successfully removed
Do you really want to remove and DISCARD active logical volume VG-01/LG-02? [y/n]: y
  Logical volume "LG-02" successfully removed
  Volume group "VG-01" successfully removed

물리적 볼륨 삭제하기

pvremove 명령어로 물리적 볼륨을 삭제한다. 아래는 /dev/sdd1을 삭제하는 것.

# pvremove /dev/sdd1
  Labels on physical volume "/dev/sdd1" successfully wiped.
같은 카테고리의 다른 글

리눅스 / 명령어 / history

리눅스를 쓰다 보면 자주 입력한 명령어를 다시 보고 싶거나, 실수로 종료한 터미널에서 이전 작업 기록이 필요할 때가 있죠. 이럴 때 유용하게 사용할 수 있는 명령어가 바로 history입니다.

Ubuntu 16.04 / phpMyAdmin / 특정 IP에서만 접속 가능하게 만드는 방법

우분투 16.04에서 phpMyAdmin을 패키지로 설치했을 때, 기본 설정은 모든 IP에서 접속 가능입니다. 만약 특정 IP에서만 접속 가능하게 하려면 /etc/apache2/conf-available/phpmyadmin.conf에 다음 코드를 추가합니다. <Directory /usr/share/phpmyadmin> Order deny,allow Deny from all Allow from xxx.xxx.xxx.xxx </Directory> xxx.xxx.xxx.xxx는 접속하려는 IP로 바꿉니다. 여러 IP를 설정하고 싶을 때는 띄어쓰기로 구분합니다. <Directory /usr/share/phpmyadmin> Order deny,allow Deny from all ...

리눅스 / RAID / Linear RAID, RAID0, RAID1, RAID5, RAID6

RAID 뜻과 종류 RAID Redundant Array of Inexpensive Disk or Redundant Array of Independent Disk 여러 개의 하드디스크를 하나처럼 사용하게 하는 기술. 예를 들어 1TB 하드디스크 두 개를 묶어서 2TB 하드디스크처럼 사용할 수 있다. 크게 하드웨어 RAID와 소프트웨어 RAID로 구분할 수 있다. 하드웨어 RAID는 성능이 좋으나 비싸다. 소프트웨어 RAID는 운영체제에서 지원하는 것으로 OS RAID라고도 한다. Linear RAID 두 ...

리눅스 / 버전 확인하는 방법

리눅스에서 현재 사용 중인 배포판의 버전을 확인하는 방법은 여러 가지가 있습니다. 그 중 두 가지를 소개합니다. hostnamectl 명령어로 확인 hostnamectl 명령어로 리눅스 버전을 확인할 수 있습니다. 아래는 Rocky Linux에서 실행한 결과입니다. # hostnamectl Static hostname: rocky-9-01 Icon name: computer-vm Chassis: ...

Ubuntu Server / 메모

MariaDB / 포트 변경 MariaDB의 기본 접속 포트는 3306입니다. 이를 다른 포트로 변경하고 싶다면 /etc/mysql/mariadb.conf.d/50-server.cnf를 수정합니다. port = 3306 을 찾아서 원하는 포트로 변경한 후 저장합니다. 그리고 MariaDB를 재시작합니다. service mysql restart MariaDB / Can't connect to MySQL server on ... (10061) 다음과 같은 에러를 만났다면... Can't connect to MySQL server on ... (10061) /etc/mysql/mariadb.conf.d/50-server.cnf에 있는 bind-address = 127.0.0.1 를 ...

리눅스 / 명령어 / du / 디렉토리와 파일 용량 확인하는 명령어

du는 disk usage의 약자로 디렉토리(폴더)와 파일의 용량을 출력해주는 명령어입니다.

Ubuntu 22.04 Server / SSH / 포트 변경하는 방법

보안 상의 이유로 SSH 포트는 변경하는 것이 좋다. Ubuntu 22.04 Server에서 SSH 포트 변경하는 방법은 다음과 같다. 1980으로 변경한다고 가정한다. /etc/ssh/sshd_config에서 다음의 코드를... #Port 22 다음으로 변경한다. Port 1980 방화벽이 활성화되어 있다면 1980 포트를 열어준다. ufw allow 1980/tcp SSH 서비스를 다시 시작한다. systemctl restart ssh SSH 서비스를 다시 시작해도 적용되지 않으면, 컴퓨터를 다시 시작한다.

Ubuntu 18.04 / 메모

SSH 설정 설정 파일 /etc/ssh/sshd_config 포트 변경하기 다음 코드를 #Port 22 다음처럼 바꾼다. Port 1234 포트 번호는 자신이 원하는 것으로... root 계정 로그인 가능하게 만들기 다음 코드를 #PermitRootLogin prohibit-password 다음처럼 만든다. PermitRootLogin yes 설정 변경 적용하기 service ssh restart

Ubuntu 14.04 / LESS 설치하는 방법

우분투 14.04에 LESS를 설치하는 방법을 메모합니다. Node.js 설치 apt-get install nodejs NPM 설치 apt-get install npm LESS 설치 npm install -g less less-plugin-clean-css /usr/local/bin/lessc 파일 첫 줄에 다음과 같은 코드가 있습니다. #!/usr/bin/env node 다음처럼 바꿉니다. #!/usr/bin/env nodejs

Rocky Linux 9 / Apache / Virtulahost 만드는 방법

Rocky Linux 9 / Apache / Virtulahost 만드는 방법

하나의 서버에서 여러 사이트를 운영할 수 있다. Rocky Linux 9에서 Apache 웹서버로 구현하는 방법을 정리한다. Selinux가 활성화되어 있으면 작동하지 않고, 별도의 설정이 필요하다. 여기서는 Selinux를 끄고 진행한다. 목표 하나의 서버에서 두 개의 사이트를 운영한다. http://aaa.manualfactory.net로 접속하면 Welcome to AAA!, http://bbb.manualfactory.net으로 접속하면 Welcome to BBB!를 출력한다. DNS 설정 aaa.manualfactory.net. bbb.manualfactory.net 모두 같은 IP로 가도록 설정한다. 폴더와 파일 생성 /home/aaa ...