首頁 > 軟體

Linux使用lvresize擴充套件或縮減LV大小

2020-06-16 17:39:08

環境:CentOS 6.7

一、新建並掛載目錄 /u01 和 /data

1.建立目錄掛節點/u01,/data

mkdir -p /{u01,data}

2.建立lv,名稱是lv_u01,大小10g,從vg_linuxidc中劃分空間,這裡的vg_linuxidc是vg的名稱。

lvcreate -L 10g -n lv_u01 vg_linuxidc

3.格式化為ext4檔案系統

mkfs.ext4 /dev/mapper/vg_linuxidc-lv_u01

4.同樣的,建立lv_data

lvcreate -L 10g -n lv_data vg_linuxidc
mkfs.ext4 /dev/mapper/vg_linuxidc-lv_data

5.在/etc/fstab中新增掛載資訊,實現開機自動掛載

vi /etc/fstab
/dev/mapper/vg_linuxidc-lv_u01      /u01                    ext4    defaults        1 1
/dev/mapper/vg_linuxidc-lv_data      /data                    ext4    defaults        1 1

二、lv擴容:

第一種方法:直接增大5G,寫“+5G”
1.lv擴容5G

lvresize -L +5G /dev/mapper/vg_linuxidc-lv_root

2.檔案系統大小變更

resize2fs /dev/mapper/vg_linuxidc-lv_root

第二種方法:原來是10G,增大5G就是“15G”
1.lv擴容到15G

lvresize -L 15G /dev/mapper/vg_linuxidc-lv_u01

2.檔案系統大小變更

resize2fs  /dev/mapper/vg_linuxidc-lv_u01

註:以上的lvresize命令操作也可以通過lvextend命令實現。

三、lv縮減:

1.需要先解除安裝目錄

umount /u01

2.這一步是否有根據執行下一步的執行返回決定,看下面的例子

e2fsck -f /dev/mapper/vg_linuxidc-lv_u01

3.檔案系統大小變更

resize2fs  /dev/mapper/vg_linuxidc-lv_u01 10G

4.lv縮減

lvresize -L 10G /dev/mapper/vg_linuxidc-lv_u01

5.掛載所有/etc/fstab中記錄的目錄

mount -a

附:lv縮減的一個實際操作過程:

[root@LINUXIDC-DB ~]# umount /u01
[root@LINUXIDC-DB ~]# resize2fs  /dev/mapper/vg_linuxidc-lv_u01 10G
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/mapper/vg_linuxidc-lv_u01' first.

[root@LINUXIDC-DB ~]# e2fsck -f /dev/mapper/vg_linuxidc-lv_u01
e2fsck 1.41.12 (17-May-2010)
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/vg_linuxidc-lv_u01: 12/983040 files (0.0% non-contiguous), 100865/3932160 blocks
[root@LINUXIDC-DB ~]# resize2fs  /dev/mapper/vg_linuxidc-lv_u01 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg_linuxidc-lv_u01 to 2621440 (4k) blocks.
The filesystem on /dev/mapper/vg_linuxidc-lv_u01 is now 2621440 blocks long.

[root@LINUXIDC-DB ~]# lvresize -L 10G /dev/mapper/vg_linuxidc-lv_u01
  WARNING: Reducing active logical volume to 10.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_u01? [y/n]: y
  Size of logical volume vg_linuxidc/lv_u01 changed from 15.00 GiB (3840 extents) to 10.00 GiB (2560 extents).
  Logical volume lv_u01 successfully resized
[root@LINUXIDC-DB ~]# mount -a
[root@LINUXIDC-DB ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_linuxidc-lv_root
                       15G  1.8G   13G  13% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/sda1             190M   36M  145M  20% /boot
/dev/mapper/vg_linuxidc-lv_data
                      9.8G   23M  9.2G   1% /data
/dev/mapper/vg_linuxidc-lv_u01
                      9.8G   23M  9.2G   1% /u01
[root@LINUXIDC-DB ~]#

同樣lvreduce命令也可以實現lv的縮減。
lvresize命令實際上是包含了lvextend和lvreduce的功能。

--LVM管理的swap擴充套件,比如增大1024M:

[root@linuxidc-DB ~]# swapoff /dev/mapper/vg_linuxidc-lv_swap

[root@linuxidc-DB ~]# lvextend -L +1024m /dev/mapper/vg_linuxidc-lv_swap

[root@linuxidc-DB ~]# mkswap /dev/mapper/vg_linuxidc-lv_swap

[root@linuxidc-DB ~]# swapon /dev/mapper/vg_linuxidc-lv_swap

本文永久更新連結地址http://www.linuxidc.com/Linux/2016-06/132475.htm


IT145.com E-mail:sddin#qq.com