Linux LVM 缩减逻辑卷 增加逻辑卷

1、卷和文件系统的大小关系

缩减卷前,需要先缩减文件系统大小
因为卷的大小不能低于文件系统大小,不然文件系统的数据会被损坏(缩减的卷那部分数据会丢失)。只有卷的大小不小于文件系统大小,才能把卷分配给文件系统。
直接缩减卷会报如下错误:

root@debian:~# lvs
  LV     VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home   debian-vg -wi-ao----  72.12g                                                    
  root   debian-vg -wi-ao----  18.72g                                                    
  swap_1 debian-vg -wi-ao---- 976.00m                                                    
  tmp    debian-vg -wi-ao----   1.17g                                                    
  var    debian-vg -wi-ao----   6.55g                                                    
root@debian:~# 
root@debian:~# lvresize -L -55G /dev/debian-vg/home 
  WARNING: Reducing active and open logical volume to 17.12 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce debian-vg/home? [y/n]: y^Hn
  WARNING: Invalid input'yn'.
Do you really want to reduce debian-vg/home? [y/n]: n
  Logical volume debian-vg/home NOT reduced.
root@debian:~# lvresize -L 55G /dev/debian-vg/home 
  WARNING: Reducing active and open logical volume to 55.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce debian-vg/home? [y/n]: n
  Logical volume debian-vg/home NOT reduced.
root@debian:~# resize2fs /dev/
Display all 167 possibilities? (y or n)
root@debian:~# resize2fs /dev/debian-vg/home 17G
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/debian-vg/home is mounted on /home; on-line resizing required
resize2fs: On-line shrinking not supported

2、先缩减文件系统大小,再缩减卷大小。

本次实验操作的目标是/home 分区上的 /dev/mapper/debian–vg-home 文件系统

2.1、先卸载文件系统 /dev/mapper/debian–vg-home

root@debian:~# umount /dev/
/dev/hugepages  /dev/mqueue     /dev/pts        /dev/shm        
root@debian:~# df -lTh
Filesystem                  Type      Size  Used Avail Use% Mounted on
udev                        devtmpfs  5.9G     0  5.9G   0% /dev
tmpfs                       tmpfs     1.2G   18M  1.2G   2% /run
/dev/mapper/debian--vg-root ext4       19G  3.8G   14G  22% /
tmpfs                       tmpfs     5.9G     0  5.9G   0% /dev/shm
tmpfs                       tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs                       tmpfs     5.9G     0  5.9G   0% /sys/fs/cgroup
/dev/sda1                   ext2      470M   57M  389M  13% /boot
/dev/mapper/debian--vg-tmp  ext4      1.2G  408K  1.1G   1% /tmp
/dev/mapper/debian--vg-home ext4       71G   12M   67G   1% /home
/dev/mapper/debian--vg-var  ext4      6.4G  324M  5.7G   6% /var
tmpfs                       tmpfs     1.2G   28K  1.2G   1% /run/user/116
tmpfs                       tmpfs     1.2G     0  1.2G   0% /run/user/0
root@debian:~# umount /dev/
/dev/hugepages  /dev/mqueue     /dev/pts        /dev/shm        
root@debian:~# umount /home 
root@debian:~# df -lTh
Filesystem                  Type      Size  Used Avail Use% Mounted on
udev                        devtmpfs  5.9G     0  5.9G   0% /dev
tmpfs                       tmpfs     1.2G   18M  1.2G   2% /run
/dev/mapper/debian--vg-root ext4       19G  3.8G   14G  22% /
tmpfs                       tmpfs     5.9G     0  5.9G   0% /dev/shm
tmpfs                       tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs                       tmpfs     5.9G     0  5.9G   0% /sys/fs/cgroup
/dev/sda1                   ext2      470M   57M  389M  13% /boot
/dev/mapper/debian--vg-tmp  ext4      1.2G  408K  1.1G   1% /tmp
/dev/mapper/debian--vg-var  ext4      6.4G  324M  5.7G   6% /var
tmpfs                       tmpfs     1.2G   28K  1.2G   1% /run/user/116
tmpfs                       tmpfs     1.2G     0  1.2G   0% /run/user/0

2.2、卸载文件系统后,进行文件系统检查

root@debian:~# e2fsck --help
e2fsck: invalid option -- '-'
Usage: e2fsck [-panyrcdfktvDFV] [-b superblock] [-B blocksize]
		[-l|-L bad_blocks_file] [-C fd] [-j external_journal]
		[-E extended-options] [-z undo_file] device

Emergency help:
 -p                   Automatic repair (no questions)
 -n                   Make no changes to the filesystem
 -y                   Assume "yes" to all questions
 -c                   Check for bad blocks and add them to the badblock list
 -f                   Force checking even if filesystem is marked clean
 -v                   Be verbose
 -b superblock        Use alternative superblock
 -B blocksize         Force blocksize when looking for superblock
 -j external_journal  Set location of the external journal
 -l bad_blocks_file   Add to badblocks list
 -L bad_blocks_file   Set badblocks list
 -z undo_file         Create an undo file
root@debian:~# e2fsck -f /dev/
Display all 167 possibilities? (y or n)
root@debian:~# e2fsck /dev/
Display all 167 possibilities? (y or n)
root@debian:~# e2fsck -f /dev/m
mapper/           mem               memory_bandwidth  midi              mqueue/           
root@debian:~# e2fsck -f /dev/m
mapper/           mem               memory_bandwidth  midi              mqueue/           
root@debian:~# e2fsck -f /dev/mapper/
control            debian--vg-home    debian--vg-root    debian--vg-swap_1  debian--vg-tmp     debian--vg-var     
root@debian:~# e2fsck -f /dev/mapper/debian--vg-home 
e2fsck 1.44.5 (15-Dec-2018)
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/mapper/debian--vg-home: 237/4726784 files (0.8% non-contiguous), 444175/18906112 blocks

2.3、缩减文件系统到15G大小

root@debian:~# resize2fs /dev/mapper/debian--vg-home 15G
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/mapper/debian--vg-home to 3932160 (4k) blocks.
The filesystem on /dev/mapper/debian--vg-home is now 3932160 (4k) blocks long.

2.4、缩减卷到15G大小

root@debian:~# lvreduce -L 15G /dev/debian-vg/home 
  WARNING: Reducing active logical volume to 15.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce debian-vg/home? [y/n]: y
  Size of logical volume debian-vg/home changed from 72.12 GiB (18463 extents) to 15.00 GiB (3840 extents).
  Logical volume debian-vg/home successfully resized.

经过 缩减卷到15G大小 这个操作,空出来的卷组空闲空间为为57.12G(72.12 GiB减去15.00 GiB)

2.5、挂载/dev/debian-vg/home 到/home分区

root@debian:~# mount /dev/
Display all 167 possibilities? (y or n)
root@debian:~# mount /dev/debian-vg/home /home
root@debian:~# df -lTh
Filesystem                  Type      Size  Used Avail Use% Mounted on
udev                        devtmpfs  5.9G     0  5.9G   0% /dev
tmpfs                       tmpfs     1.2G   18M  1.2G   2% /run
/dev/mapper/debian--vg-root ext4       19G  3.8G   14G  22% /
tmpfs                       tmpfs     5.9G     0  5.9G   0% /dev/shm
tmpfs                       tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs                       tmpfs     5.9G     0  5.9G   0% /sys/fs/cgroup
/dev/sda1                   ext2      470M   57M  389M  13% /boot
/dev/mapper/debian--vg-tmp  ext4      1.2G  408K  1.1G   1% /tmp
/dev/mapper/debian--vg-var  ext4      6.4G  324M  5.7G   6% /var
tmpfs                       tmpfs     1.2G   28K  1.2G   1% /run/user/116
tmpfs                       tmpfs     1.2G     0  1.2G   0% /run/user/0
/dev/mapper/debian--vg-home ext4       15G   12M   14G   1% /home

3、扩大root卷(使用缩减下来的home卷的空间)

扩展root前root大小查看

root@debian:~# df -lTh
Filesystem                  Type      Size  Used Avail Use% Mounted on
udev                        devtmpfs  5.9G     0  5.9G   0% /dev
tmpfs                       tmpfs     1.2G   18M  1.2G   2% /run
/dev/mapper/debian--vg-root ext4       19G  3.8G   14G  22% /
tmpfs                       tmpfs     5.9G     0  5.9G   0% /dev/shm
tmpfs                       tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs                       tmpfs     5.9G     0  5.9G   0% /sys/fs/cgroup
/dev/sda1                   ext2      470M   57M  389M  13% /boot
/dev/mapper/debian--vg-tmp  ext4      1.2G  408K  1.1G   1% /tmp
/dev/mapper/debian--vg-var  ext4      6.4G  324M  5.7G   6% /var
tmpfs                       tmpfs     1.2G   28K  1.2G   1% /run/user/116
tmpfs                       tmpfs     1.2G     0  1.2G   0% /run/user/0
/dev/mapper/debian--vg-home ext4       15G   12M   14G   1% /home

扩展root 增加的空间为57.12G

  --- Volume group ---
  VG Name               debian-vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                5
  Open LV               5
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <99.52 GiB
  PE Size               4.00 MiB
  Total PE              25477
  Alloc PE / Size       10854 / <42.40 GiB
  Free  PE / Size       14623 / 57.12 GiB
  VG UUID               4sHIMB-ia0f-0F05-n8sT-tTp8-mVsf-mMWm2F
   
root@debian:~# vgs
  VG        #PV #LV #SN Attr   VSize   VFree 
  debian-vg   1   5   0 wz--n- <99.52g 57.12g
root@debian:~# lvextend -L +50.12G /dev/debian-vg/root 
  Rounding size to boundary between physical extents: 50.12 GiB.
  Size of logical volume debian-vg/root changed from 18.72 GiB (4793 extents) to 68.84 GiB (17624 extents).
  Logical volume debian-vg/root successfully resized.
root@debian:~# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  debian-vg   1   5   0 wz--n- <99.52g 7.00g
root@debian:~# lvextend -L +7G /dev/debian-vg/root 
  Size of logical volume debian-vg/root changed from 68.84 GiB (17624 extents) to 75.84 GiB (19416 extents).
  Logical volume debian-vg/root successfully resized.
root@debian:~# vgs
  VG        #PV #LV #SN Attr   VSize   VFree
  debian-vg   1   5   0 wz--n- <99.52g    0 
root@debian:~# resize2fs /dev/mapper/debian--vg-root 
resize2fs 1.44.5 (15-Dec-2018)
Filesystem at /dev/mapper/debian--vg-root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 10
The filesystem on /dev/mapper/debian--vg-root is now 19881984 (4k) blocks long.

root@debian:~# df -lTh
Filesystem                  Type      Size  Used Avail Use% Mounted on
udev                        devtmpfs  5.9G     0  5.9G   0% /dev
tmpfs                       tmpfs     1.2G   18M  1.2G   2% /run
/dev/mapper/debian--vg-root ext4       75G  3.8G   68G   6% /
tmpfs                       tmpfs     5.9G     0  5.9G   0% /dev/shm
tmpfs                       tmpfs     5.0M  4.0K  5.0M   1% /run/lock
tmpfs                       tmpfs     5.9G     0  5.9G   0% /sys/fs/cgroup
/dev/sda1                   ext2      470M   57M  389M  13% /boot
/dev/mapper/debian--vg-tmp  ext4      1.2G  408K  1.1G   1% /tmp
/dev/mapper/debian--vg-var  ext4      6.4G  324M  5.7G   6% /var
tmpfs                       tmpfs     1.2G   28K  1.2G   1% /run/user/116
tmpfs                       tmpfs     1.2G     0  1.2G   0% /run/user/0
/dev/mapper/debian--vg-home ext4       15G   12M   14G   1% /home
root@debian:~# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值