一. 何为LVM?
Logical Volume Manager的缩写,它可以把多个分区、硬盘甚至RAID组合成一个存储设备来使用,并可以扩展或缩减空间。LVM有三层组成组成:底层的PV,中间的VG,上层的LV,如图所示
PV: physical volume,指的是底层的物理卷:硬盘,分区或者RAID;
VG:volume group,指的是PV的组合;
PE:physical extend,在VG层上定义的物理块区;
LV: logical volume,逻辑上划分出来的分区,如果VG留有剩余空间,LV甚至可以扩展,若LV有剩余空间,可以对它进行缩减操作(有风险);
其实做出LV还是不能投入使用,LV只相当于一般的分区,若想使用它,还得对他进行格式化和挂载操作。
二. 有哪些常用命令?
前提准备
在开始我们的实验之前,我们得准备好两个分区,注意分区类型一定得是8e(linux LVM)类型的,如下是我准备好的分区:(注意分区类型,我已经设置为8e;关于如何分区请使用fdisk命令。)
/dev/sda7 77611008 98582527 10485760 8e Linux LVM /dev/sda8 98584576 119556095 10485760 8e Linux LVM
系统上的PV的信息显示:
pvs:显示所有pv的简要信息;
pvdisplay:显示所有pv的详细信息;
PV的创建命令:
pvcreate:
[root@localhost ~]# pvcreate /dev/sda7 /dev/sda8 Physical volume "/dev/sda7" successfully created Physical volume "/dev/sda8" successfully created
注意:两个分区直接用空格隔开,当然你也可以使用命令行展开功能:/dev/sda{7,8}
创建VG:
[root@localhost ~]# vgcreate -s 16M TEST_VG /dev/sda{7,8} Volume group "TEST_VG" successfully created
创建LV:
[root@localhost ~]# lvcreate -L 5G -n TEST_LV TEST_VG Logical volume "TEST_LV" created.
创建文件系统ext4:
参数是LV的全路径,如果不知道,可以通过 fdisk -l | grep "LV_NAME" 命令查看;
在此LV_NAME指的是TEST_LV
[root@localhost ~]# mke2fs -t ext4 /dev/mapper/TEST_VG-TEST_LV 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 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 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 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
挂载至某个目录:
[root@localhost ~]# mount /dev/mapper/TEST_VG-TEST_LV /users/
为了对LVM的扩展、缩减功能及快照功能进行测试,我在挂载目录/users目录下创建了archlinux目录,并把/etc/pam.d/目录复制至archlinux中,便于以后进行验证:
[root@localhost ~]# cp -r /etc/pam.d/ /users/archlinux/ [root@localhost ~]# ls -al /users/archlinux/ total 24 drwx------. 3 archlinux archlinux 4096 Dec 5 15:09 . drwxr-xr-x. 4 root root 4096 Dec 5 15:08 .. -rw-r--r--. 1 archlinux archlinux 18 Mar 5 2015 .bash_logout -rw-r--r--. 1 archlinux archlinux 193 Mar 5 2015 .bash_profile -rw-r--r--. 1 archlinux archlinux 231 Mar 5 2015 .bashrc drwxr-xr-x. 2 root root 4096 Dec 5 15:09 pam.d
LVM的扩展:
[root@localhost ~]# lvextend -L 7G /dev/mapper/TEST_VG-TEST_LV Size of logical volume TEST_VG/TEST_LV changed from 5.00 GiB (320 extents) to 7.00 GiB (448 exte nts). Logical volume TEST_LV successfully resized
因为在LV之上还有一层文件系统,所以扩展LV之后,还应扩展文件系统,扩展文件系统要用到resize2fs命令:
[root@localhost ~]# resize2fs /dev/mapper/TEST_VG-TEST_LV <--resize2fs命令 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/mapper/TEST_VG-TEST_LV is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 1 The filesystem on /dev/mapper/TEST_VG-TEST_LV is now 1835008 blocks long. [root@localhost ~]# df -lh <--检查是否扩展成功 Filesystem Size Used Avail Use% Mounted on /dev/sda3 4.8G 43M 4.5G 1% / devtmpfs 485M 0 485M 0% /dev tmpfs 494M 0 494M 0% /dev/shm tmpfs 494M 6.8M 487M 2% /run tmpfs 494M 0 494M 0% /sys/fs/cgroup /dev/mapper/centos-usr 9.8G 921M 8.4G 10% /usr /dev/sda5 4.8G 20M 4.6G 1% /home /dev/sda1 4.8G 116M 4.5G 3% /boot /dev/mapper/centos-var 9.8G 139M 9.1G 2% /var /dev/mapper/TEST_VG-TEST_LV 6.8G 23M 6.4G 1% /users <--6.8G,在误差允许范围内
缩减LV:
1. 应先umount挂载的LV装置;
2. 使用e2fsck对LV进行校验;
3. 使用resize2fs缩减文件系统;
4. 使用lvreduce缩减LV;
[root@localhost ~]# umount /dev/mapper/TEST_VG-TEST_LV <--umount [root@localhost ~]# ls -al /users/archlinux/ <--检查,存在于TEST_LV的archlinux已不存在 ls: cannot access /users/archlinux/: No such file or directory [root@localhost ~]# e2fsck /dev/mapper/TEST_VG-TEST_LV <--校验文件系统 e2fsck 1.42.9 (28-Dec-2013) /dev/mapper/TEST_VG-TEST_LV: clean, 49/458752 files, 67359/1835008 blocks [root@localhost ~]# e2fsck -f /dev/mapper/TEST_VG-TEST_LV <--应使用-f选项校验文件系统 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 /dev/mapper/TEST_VG-TEST_LV: 49/458752 files (0.0% non-contiguous), 67359/1835008 blocks [root@localhost ~]# resize2fs /dev/mapper/TEST_VG-TEST_LV 3G <--resize2fs,调文件系统小至3G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/mapper/TEST_VG-TEST_LV to 786432 (4k) blocks. The filesystem on /dev/mapper/TEST_VG-TEST_LV is now 786432 blocks long. [root@localhost ~]# lvreduce -L 3G /dev/mapper/TEST_VG-TEST_LV <--减小LV至3G WARNING: Reducing active logical volume to 3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce TEST_LV? [y/n]: y Size of logical volume TEST_VG/TEST_LV changed from 7.00 GiB (448 extents) to 3.00 GiB (192 exte nts). Logical volume TEST_LV successfully resized [root@localhost ~]# mount /dev/mapper/TEST_VG-TEST_LV /users/ <--重新挂载 [root@localhost ~]# ls -al /users/archlinux/ <--查看其中文件 total 24 drwx------. 3 archlinux archlinux 4096 Dec 5 15:09 . drwxr-xr-x. 4 root root 4096 Dec 5 15:08 .. -rw-r--r--. 1 archlinux archlinux 18 Mar 5 2015 .bash_logout -rw-r--r--. 1 archlinux archlinux 193 Mar 5 2015 .bash_profile -rw-r--r--. 1 archlinux archlinux 231 Mar 5 2015 .bashrc drwxr-xr-x. 2 root root 4096 Dec 5 15:09 pam.d [root@localhost ~]# df -lh <--查看空间是否正确 Filesystem Size Used Avail Use% Mounted on /dev/sda3 4.8G 43M 4.5G 1% / devtmpfs 485M 0 485M 0% /dev tmpfs 494M 0 494M 0% /dev/shm tmpfs 494M 6.8M 487M 2% /run tmpfs 494M 0 494M 0% /sys/fs/cgroup /dev/mapper/centos-usr 9.8G 921M 8.4G 10% /usr /dev/sda5 4.8G 20M 4.6G 1% /home /dev/sda1 4.8G 116M 4.5G 3% /boot /dev/mapper/centos-var 9.8G 139M 9.1G 2% /var /dev/mapper/TEST_VG-TEST_LV 2.9G 16M 2.7G 1% /users
使用快照功能:
创建LV的快照同样须使用lvcreate命令,只是需要加上-s选项指定要创建快照(snapshot),并使用-L选项指定快照的大小,使用-n指定快照的名字;最好使用-p选项将其设置为只读;
如下:
[root@localhost ~]# lvcreate -L 3G -n TEST_SLV -s /dev/mapper/TEST_VG-TEST_LV <--创建快照 Logical volume "TEST_SLV" created. [root@localhost ~]# lvchange -p r /dev/mapper/TEST_VG-TEST_SLV <--忘了设置只读属性,使用lvchange修改 Logical volume "TEST_SLV" changed. [root@localhost ~]# mount /dev/mapper/TEST_VG-TEST_SLV /users/backup/ <--挂载验证,提示只读 mount: /dev/mapper/TEST_VG-TEST_SLV is write-protected, mounting read-only [root@localhost ~]# ls -al /users/backup/archlinux/ <--与TEST_LV内容相同 total 24 drwx------. 3 archlinux archlinux 4096 Dec 5 15:09 . drwxr-xr-x. 4 root root 4096 Dec 5 15:08 .. -rw-r--r--. 1 archlinux archlinux 18 Mar 5 2015 .bash_logout -rw-r--r--. 1 archlinux archlinux 193 Mar 5 2015 .bash_profile -rw-r--r--. 1 archlinux archlinux 231 Mar 5 2015 .bashrc drwxr-xr-x. 2 root root 4096 Dec 5 15:09 pam.d [root@localhost ~]# cp /etc/fstab /users/archlinux/fstab.bak <--复制fstab至TEST_LV,为fstab.bak [root@localhost ~]# ls -al /users/backup/archlinux/ <--快照TEST_SLV没有fstab.bak total 24 drwx------. 3 archlinux archlinux 4096 Dec 5 15:09 . drwxr-xr-x. 4 root root 4096 Dec 5 15:08 .. -rw-r--r--. 1 archlinux archlinux 18 Mar 5 2015 .bash_logout -rw-r--r--. 1 archlinux archlinux 193 Mar 5 2015 .bash_profile -rw-r--r--. 1 archlinux archlinux 231 Mar 5 2015 .bashrc drwxr-xr-x. 2 root root 4096 Dec 5 15:09 pam.d [root@localhost ~]# ls -al /users/archlinux/ <--TEST_LV挂载的archlinux中已存在fatab.bak total 28 drwx------. 3 archlinux archlinux 4096 Dec 5 15:52 . drwxr-xr-x. 5 root root 4096 Dec 5 15:49 .. -rw-r--r--. 1 archlinux archlinux 18 Mar 5 2015 .bash_logout -rw-r--r--. 1 archlinux archlinux 193 Mar 5 2015 .bash_profile -rw-r--r--. 1 archlinux archlinux 231 Mar 5 2015 .bashrc -rw-r--r--. 1 root root 841 Dec 5 15:52 fstab.bak drwxr-xr-x. 2 root root 4096 Dec 5 15:09 pam.d
三. 接下来我们看看如何删除快照,LV,VG,PV;
卸载快照挂载的分区:
[root@localhost ~]# umount /dev/mapper/TEST_VG-TEST_SLV
移除LV的快照:
[root@localhost ~]# lvremove /dev/mapper/TEST_VG-TEST_SLV Do you really want to remove active logical volume TEST_SLV? [y/n]: Y Logical volume "TEST_SLV" successfully removed
移除LV(在移除之前先执行卸载umount):
[root@localhost ~]# umount /dev/mapper/TEST_VG-TEST_LV [root@localhost ~]# lvremove /dev/mapper/TEST_VG-TEST_LV Do you really want to remove active logical volume TEST_LV? [y/n]: y Logical volume "TEST_LV" successfully removed
移除VG:
[root@localhost ~]# vgremove TEST_VG Volume group "TEST_VG" successfully removed
注意:直接输入VG的名称即可,如果想不起来可以使用vgs查看。
删除PV:
[root@localhost ~]# pvremove /dev/sda7 /dev/sda8 Labels on physical volume "/dev/sda7" successfully wiped Labels on physical volume "/dev/sda8" successfully wiped
注意:移除PV的参数是底层PV的名称,如果想不起来请使用pvs查看。
接下来就是删除分区了,这个比较简单请自行查阅相关资料吧。
本文中有很多命令使用了OPTION,没有细说,有不明白的就请自行man吧^-^。
原创文章,作者:Jacen,如若转载,请注明出处:http://www.178linux.com/9691