1. 扩大LV的容量
扩大lv的容量不需要将文件系统卸载。
步骤如下:
A.使用fdisk设定具有8e system id的分区
B.利用pvcreate创建pv
C.利用vgextend将pv加入到vg里面扩容vg
D.利用lvresize将新加入的pv的pe加入到lv里面
E.通过resize2fs使文件系统的容量增加。
示例:
步骤1:将lv-01挂载到/backup目录下,并复制/etc/services到该文件系统中。
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.9G 7.8G 1.6G 83% / tmpfs 245M 0 245M 0% /dev/shm /dev/sda1 194M 28M 156M 16% /boot /dev/mapper/DATA_01-lv--01 155M 5.6M 142M 4% /backup [[email protected] ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert lv-01 DATA_01 -wi-ao-- 160.00m [[email protected] ~]# cp /etc/services /backup/ [[email protected] ~]# ls -l /backup/ total 638 drwx------ 2 root root 12288 Jul 17 02:10 lost+found -rw-r--r-- 1 root root 641020 Jul 17 02:11 services [[email protected] ~]#
步骤2: 查看VG的容量
[[email protected] ~]# vgdisplay --- Volume group --- VG Name DATA_01 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 17 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 2 Act PV 2 VG Size 2.00 GiB PE Size 32.00 MiB Total PE 64 Alloc PE / Size 5 / 160.00 MiB Free PE / Size 59 / 1.84 GiB VG UUID tEVjly-icsW-Jin7-1Bmn-xil1-LvpP-3PQI5C [[email protected] ~]# vgs VG #PV #LV #SN Attr VSize VFree DATA_01 2 1 0 wz--n- 2.00g 1.84g [[email protected] ~]#
说明:
当前VG中还剩余1.84g的容量,如果没有多余的空间的话,需要使用pvcreate创建PV,并使用vgextend将PV加入到VG中去。
步骤3:扩容LV,这里将其扩容到500M左右
说明:这里讲采用增加PE的数量增加LV,在我的系统中,PE的大小为32M,原来的LV的大小为160M,如果要增加到500M的话,需要增加340M,340/32=10.625,这里增加11个PE。
具体操作:
[[email protected] ~]# lvresize -l +11 /dev/DATA_01/lv-01 Extending logical volume lv-01 to 512.00 MiB Logical volume lv-01 successfully resized [[email protected] ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert lv-01 DATA_01 -wi-ao-- 512.00m [[email protected] ~]#
查看文件系统的容量:
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.9G 7.8G 1.6G 83% / tmpfs 245M 0 245M 0% /dev/shm /dev/sda1 194M 28M 156M 16% /boot /dev/mapper/DATA_01-lv--01 155M 6.2M 141M 5% /backup [[email protected] ~]#
说明:文件系统的容量依然没有增加
步骤4:增加文件系统的容量。
指令:resize2fs /dev/vgname/lvname
[[email protected] ~]# resize2fs /dev/DATA_01/lv-01 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/DATA_01/lv-01 is mounted on /backup; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/DATA_01/lv-01 to 524288 (1k) blocks. The filesystem on /dev/DATA_01/lv-01 is now 524288 blocks long. [[email protected] ~]#
查看文件系统的容量:
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.9G 7.8G 1.6G 83% / tmpfs 245M 0 245M 0% /dev/shm /dev/sda1 194M 28M 156M 16% /boot /dev/mapper/DATA_01-lv--01 496M 6.9M 464M 2% /backup [[email protected] ~]#
说明:文件系统的容量已经增加。
查看文件系统中的内容是否已经丢失:
[[email protected] ~]# ls -l /backup/ total 638 drwx------ 2 root root 12288 Jul 17 02:10 lost+found -rw-r--r-- 1 root root 641020 Jul 17 02:11 services [[email protected] ~]#
文件还在……
2. 减小LV的容量
如果要减小lv的容量做好能用图形化界面来做,如果不能使用图形化界面的话,需要严格的按照如下步骤进行,否则可能会损坏文件系统,图形化的LVM软件包为:system-config-lvm
步骤(必须严格执行):
A.对要减小容量的文件系统卸载。根文件系统是无法卸载的。
B.使用e2fsck –f检查lv的文件系统
C.使用resize2fs减小文件系统的大小。
resize2fs /dev/vg-name/lv-name <减小之后的大小>
D.减小LV的大小。lvreduce或lvresize这一步最好用PE的数量。
示例:
步骤1:先卸载文件系统
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.9G 7.8G 1.6G 83% / tmpfs 245M 0 245M 0% /dev/shm /dev/sda1 194M 28M 156M 16% /boot /dev/mapper/DATA_01-lv--01 496M 6.9M 464M 2% /backup [[email protected] ~]# umount /backup/ [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.9G 7.8G 1.6G 83% / tmpfs 245M 0 245M 0% /dev/shm /dev/sda1 194M 28M 156M 16% /boot [[email protected] ~]#
步骤2:检查文件系统
[[email protected] ~]# e2fsck -f /dev/DATA_01/lv-01 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/DATA_01/lv-01: 12/131072 files (0.0% non-contiguous), 23571/524288 blocks [[email protected] ~]#
步骤3:使用resize2fs减小文件系统的大小
说明:这里减小到50M
[[email protected] ~]# resize2fs /dev/DATA_01/lv-01 50M resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/DATA_01/lv-01 to 51200 (1k) blocks. The filesystem on /dev/DATA_01/lv-01 is now 51200 blocks long. [[email protected] ~]#
步骤4:减小LV的大小
说明:LV原先的大小是512M,现在减小到50M,减小了512-50=462M。
[[email protected] ~]# lvresize -L -462M /dev/DATA_01/lv-01 Rounding size to boundary between physical extents: 448.00 MiB WARNING: Reducing active logical volume to 64.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce lv-01? [y/n]: y Reducing logical volume lv-01 to 64.00 MiB Logical volume lv-01 successfully resized [[email protected] ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert lv-01 DATA_01 -wi-a--- 64.00m [[email protected] ~]#
步骤5:挂载测试数据是否还在
[[email protected] ~]# mount /dev/DATA_01/lv-01 /backup/ [[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.9G 7.8G 1.6G 83% / tmpfs 245M 0 245M 0% /dev/shm /dev/sda1 194M 28M 156M 16% /boot /dev/mapper/DATA_01-lv--01 49M 5.7M 41M 13% /backup [[email protected] ~]# ll /backup/ total 638 drwx------ 2 root root 12288 Jul 17 02:10 lost+found -rw-r--r-- 1 root root 641020 Jul 17 02:11 services [[email protected] ~]#
数据还在……
3. 快照卷
快照可以保留改变数据前那一刻的数据,可以作为访问原数据中的另一个通道,快照卷不应该允许用户修改,应该创建为只读(-p r)的方式,如果要创建为读写的方式,可以指定-p w选项,创建快照为-s选项。
1. 快照卷的生命周期为整个数据时长,在该数据时长内,数据增长量不能超出快照卷的大小,如果超过的话,则快照卷会自我损毁。最安全的做法是快照卷和原卷一样大。
2. 快照卷为只读的。
创建快照:
指令:lvcreate -L 50M -n lv-snapshot -s -p r /dev/vgname/lvname
示例:
[[email protected] backup]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.9G 7.8G 1.6G 83% / tmpfs 245M 0 245M 0% /dev/shm /dev/sda1 194M 28M 156M 16% /boot /dev/mapper/DATA_01-lv--01 49M 5.7M 41M 13% /backup [[email protected] backup]# lvcreate -L 50M -n lv-01-snap -p r /dev/DATA_01/lv-01 Volume group name expected (no slash) Run `lvcreate --help‘ for more information. [[email protected] backup]# lvcreate -L 50M -n lv-01-snap -s -p r /dev/DATA_01/lv-01 Rounding up size to full physical extent 64.00 MiB Logical volume "lv-01-snap" created [[email protected] backup]# lvs LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert lv-01 DATA_01 owi-aos- 64.00m lv-01-snap DATA_01 sri-a-s- 64.00m lv-01 0.02 [[email protected] backup]#
增加和减小LV的容量以及创建快照卷