본문 바로가기
공부/Linux

Linux Lvm create, extend and reduce

by CITTA 2023. 2. 7.
728x90
반응형

Linux Lvm create, extend and reduce

lvm 볼륨을 생성하고 확장, 축소 하는 방법

  1. 신규 볼륨 생성 및 마운트 수행
  • 디스크 할당 정보 확인
[root@localhost ~]# fdisk -l |grep -i '/dev/sd'
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
[root@localhost ~]#
  • 신규 디스크 fdisk를 통하여 LVM파티션으로 변경
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
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
Building a new DOS disklabel with disk identifier 0xa85ef505.
Command (m for help): n
Partition type:
p   primary (0 primary, 0 extended, 4 free)
e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): 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 C:  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 eb  BeOS fs
e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix
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): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0xa85ef505
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   8e  Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
  • 생성된 LVM 파티션 확인
[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk label type: dos
Disk identifier: 0xa85ef505
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   8e  Linux LVM
  • 신규 LVM 파티션으로 PV 볼륨 생성
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]#
[root@localhost ~]# pvs
PV         VG     Fmt  Attr PSize   PFree
/dev/sda2  centos lvm2 a--  <19.00g      0
/dev/sdb1         lvm2 ---  <20.00g <20.00g
  • 기존 VG 확인 후 신규 VG 생성
[root@localhost ~]# vgs
VG     #PV #LV #SN Attr   VSize   VFree
centos   1   2   0 wz--n- <19.00g    0
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# vgcreate datavg /dev/sdb1
Volume group "datavg" successfully created
[root@localhost ~]#
[root@localhost ~]# vgs
VG     #PV #LV #SN Attr   VSize   VFree
centos   1   2   0 wz--n- <19.00g      0
datavg   1   0   0 wz--n- <20.00g <20.00g
  • LV 신규 생성 및 확인
[root@localhost ~]# lvcreate -n datalv -L 10GB datavg
Logical volume "datalv" created.
[root@localhost ~]# lvs
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   centos -wi-ao---- <17.00g                                                    
  swap   centos -wi-ao----   2.00g                                                    
  datalv datavg -wi-a-----  10.00g
  • LV 파일시스템 포맷 및 마운트
[root@localhost ~]# mkfs.ext4 /dev/
Display all 167 possibilities? (y or n)
[root@localhost ~]# mkfs.ext4 /dev/datavg/datalv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
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
[root@localhost ~]#
[root@localhost ~]# mkdir /DATA
[root@localhost ~]# mount /dev/datavg/datalv /DATA
[root@localhost ~]#
[root@localhost ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   13M  1.9G   1% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        17G  3.9G   14G  23% /
/dev/sda1                 xfs      1014M  185M  830M  19% /boot
tmpfs                     tmpfs     378M   12K  378M   1% /run/user/42
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/datavg-datalv ext4      9.8G   37M  9.2G   1% /DATA
  1. LVM 확장
  • 확장할 대상 확인 및 VG 여유량 확인
[root@localhost ~]# vgs
VG     #PV #LV #SN Attr   VSize   VFree
centos   1   2   0 wz--n- <19.00g      0
datavg   1   1   0 wz--n- <20.00g <10.00g
[root@localhost ~]#
[root@localhost ~]# lvs
LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
root   centos -wi-ao---- <17.00g
swap   centos -wi-ao----   2.00g
datalv datavg -wi-ao----  10.00g
 - 여유량 대비 필요한 용량 확인하여 증설 후 resize를 통해 lvm 변경용량 확인
[root@localhost ~]# lvextend -L +5G /dev/datavg/datalv
Size of logical volume datavg/datalv changed from 10.00 GiB (2560 extents) to 15.00 GiB (3840 extents).
Logical volume datavg/datalv successfully resized.
[root@localhost ~]#
[root@localhost ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   13M  1.9G   1% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        17G  3.9G   14G  23% /
/dev/sda1                 xfs      1014M  185M  830M  19% /boot
tmpfs                     tmpfs     378M   12K  378M   1% /run/user/42
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/datavg-datalv ext4      9.8G   37M  9.2G   1% /DATA
[root@localhost ~]#
[root@localhost ~]# resize2fs /dev/datavg/datalv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/datavg/datalv is mounted on /DATA; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/datavg/datalv is now 3932160 blocks long.
[root@localhost ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   13M  1.9G   1% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        17G  3.9G   14G  23% /
/dev/sda1                 xfs      1014M  185M  830M  19% /boot
tmpfs                     tmpfs     378M   12K  378M   1% /run/user/42
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/datavg-datalv ext4       15G   41M   14G   1% /DATA
  1. LVM 축소
  • LV 확인 및 umount 후 파일시스템 체크
[root@localhost /]# umount /DATA
[root@localhost /]#
[root@localhost /]# df -T
Filesystem              Type     1K-blocks    Used Available Use% Mounted on
devtmpfs                devtmpfs   1913612       0   1913612   0% /dev
tmpfs                   tmpfs      1930644       0   1930644   0% /dev/shm
tmpfs                   tmpfs      1930644   12540   1918104   1% /run
tmpfs                   tmpfs      1930644       0   1930644   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs       17811456 4036772  13774684  23% /
/dev/sda1               xfs        1038336  189040    849296  19% /boot
tmpfs                   tmpfs       386132      12    386120   1% /run/user/42
tmpfs                   tmpfs       386132       0    386132   0% /run/user/0
[root@localhost /]#
[root@localhost /]# e2fsck -f -v /dev/datavg/datalv
e2fsck 1.42.9 (28-Dec-2013)
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
      20 inodes used (0.00%, out of 655360)
       0 non-contiguous files (0.0%)
       0 non-contiguous directories (0.0%)
         # of inodes with ind/dind/tind blocks: 0/0/0
         Extent depth histogram: 12
   98014 blocks used (3.74%, out of 2621440)
       0 bad blocks
       1 large file

       9 regular files
       2 directories
       0 character device files
       0 block device files
       0 fifos
       0 links
       0 symbolic links (0 fast symbolic links)
       0 sockets
      11 files

  • 파일시스템 사이즈를 축소 (10G → 9G)
[root@localhost /]# resize2fs -f /dev/datavg/datalv 9G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/datavg/datalv to 2359296 (4k) blocks.
The filesystem on /dev/datavg/datalv is now 2359296 blocks long.
  • LV 사이즈를 기존 10G에서 1G를 축소
[root@localhost /]# lvreduce -L -1G /dev/datavg/datalv
WARNING: Reducing active logical volume to 9.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce datavg/datalv? [y/n]: y
Size of logical volume datavg/datalv changed from 10.00 GiB (2560 extents) to 9.00 GiB (2304 extents).
Logical volume datavg/datalv successfully resized.
  • lvdisplay로 확인
[root@localhost /]# lvdisplay /dev/datavg/datalv
--- Logical volume ---
LV Path                /dev/datavg/datalv
LV Name                datalv
VG Name                datavg
LV UUID                uKjfCb-x6JB-ToVj-9oFi-3Xkn-22Dg-NCuVap
LV Write Access        read/write
LV Creation host, time localhost.localdomain, 2022-01-06 23:56:59 +0900
LV Status              available
open 0
LV Size                9.00 GiB
Current LE             2304
Segments               1
Allocation             inherit
Read ahead sectors     auto
currently set to 8192
Block device 253:2
  • 마운트 후 확인
[root@localhost /]# mount /dev/datavg/datalv /DATA
[root@localhost /]#
[root@localhost /]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   1.9G     0  1.9G   0% /dev
tmpfs                      1.9G     0  1.9G   0% /dev/shm
tmpfs                      1.9G   13M  1.9G   1% /run
tmpfs                      1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root     17G  3.9G   14G  23% /
/dev/sda1                 1014M  185M  830M  19% /boot
tmpfs                      378M   12K  378M   1% /run/user/42
tmpfs                      378M     0  378M   0% /run/user/0
/dev/mapper/datavg-datalv  8.8G   95M  8.2G   2% /DATA
728x90
반응형

댓글