一、创建LVM逻辑卷
事先添加了三块虚拟物理磁盘/dev/sdb 1G, /dev/sdc 2G, /dev/sdd 3G
使用fdisk –l命令查看:
[root@localhost ~]# fdisk -l Disk /dev/sda: 8589 MB, 8589934592 bytes, 16777216 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: 0x0000ebc2 Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 16777215 、7875584 8e Linux LVM Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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 /dev/sdc: 2147 MB, 2147483648 bytes, 4194304 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 /dev/sdd: 3221 MB, 3221225472 bytes, 6291456 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 /dev/mapper/rhel-root: 7159 MB, 7159676928 bytes, 13983744 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 /dev/mapper/rhel-swap: 859 MB, 859832320 bytes, 1679360 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
[root@localhost ~]# |
①将物理硬盘格式化成PV(物理卷) 使用的是 pvcreate 命令
先格式化物理磁盘/dev/sdb和/dev/sdc
[root@localhost ~]# pvcreate /dev/sdb /dev/sdc Physical volume "/dev/sdb" successfully created Physical volume "/dev/sdc" successfully created |
创建完PV以后,我们可以使用pvdisplay(显示详细信息)、pvs命令来查看当前pv的信息
[root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 rhel lvm2 a-- 7.51g 40.00m --原来就有一个pv /dev/sdb lvm2 --- 1.00g 1.00g /dev/sdc lvm2 --- 2.00g 2.00g [root@localhost ~]# vgs --原来就有一个名为rhel的vg VG #PV #LV #SN Attr VSize VFree rhel 1 2 0 wz--n- 7.51g 40.00m [root@localhost ~]# lvs ---原来有两个lv(root和swap) LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root rhel -wi-ao---- 6.67g swap rhel -wi-ao---- 820.00m [root@localhost ~]# |
pvdisplay
[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name rhel PV Size 7.51 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 1922 Free PE 10 Allocated PE 1912 PV UUID qMyN56-3szx-N7EG-s5Zh-YsrE-eScv-fdEHHZ "/dev/sdc" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID A1eJ9z-GBd1-HdB4-m9Mh-IVSk-7CTY-TCKFUC "/dev/sdb" is a new physical volume of "1.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 1.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID zYkj1L-uojt-XGyM-craq-t8nQ-b6tl-Z84bfq |
②创建卷组(VG),并将PV加入到卷组中 通过 vgcreate 命令
在创建完PV以后,这时候我们需要创建一个VG,然后将我们的PV都加入到这个卷组当中,在创建卷组时要给该卷组起一个名字
[root@localhost ~]# vgcreate rusky /dev/sdb /dev/sdc Volume group "rusky" successfully created |
在创建好VG以后,我们也可以使用 vgdisplay 或者 vgs 命来来查看VG的信息
[root@localhost ~]# vgs VG #PV #LV #SN Attr VSize VFree rhel 1 2 0 wz--n- 7.51g 40.00m rusky 2 0 0 wz--n- 2.99g 2.99g [root@localhost ~]# vgdisplay --- Volume group --- VG Name rusky System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 --当前这里有两个PV,分别是我们的 /dev/sdb 和 /dev/sdc Act PV 2 VG Size 2.99 GiB --当前VG的大小 PE Size 4.00 MiB --LVM默认的PE大小就是4M Total PE 766 --因为VG里面存放的就是各个PV中的PE,所以PE的数量就是VG大小除以默认PE的大小 Alloc PE / Size 0 / 0 Free PE / Size 766 / 2.99 GiB VG UUID Giffac-ePqA-taJU-9wCc-v910-GQbc-047yvE --- Volume group --- VG Name rhel System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 7.51 GiB PE Size 4.00 MiB Total PE 1922 Alloc PE / Size 1912 / 7.47 GiB Free PE / Size 10 / 40.00 MiB VG UUID e3iOPE-zKM4-yxRI-9IfH-rjeY-hXAT-5DPGcE |
③基于卷组(VG)创建逻辑卷(LV) 通过 lvcreate 命令
因为创建好的PV、VG都是底层的东西,我们上层使用的是逻辑卷,所以我们要基于VG创建我们的逻辑卷才行
[root@localhost ~]# lvcreate -n mylv -L 500MB rusky Logical volume "mylv" created. |
通过lvcreate命令基于VG创建逻辑卷,名字为mylv,大小为500MB。使用lvdisplay或lvs查看创建好的逻辑卷信息
[root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root rhel -wi-ao---- 6.67g swap rhel -wi-ao---- 820.00m mylv rusky -wi-a----- 500.00m |
[root@localhost ~]# lvdisplay rusky --lvdisplay后面加VG Name,查看指定VG的信息 --- Logical volume --- LV Path /dev/rusky/mylv --逻辑卷的路径 LV Name mylv --逻辑卷的名字 VG Name rusky --逻辑卷所属卷组的名字 LV UUID LsGSlF-IZ0U-x7X2-Lqts-GqNb-DGm9-oVAHvE LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-10 15:48:53 +0800 LV Status available # open 0 LV Size 500.00 MiB --逻辑卷的大小 Current LE 125 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 |
这样,我们的逻辑卷就创建好了。
当我们每创建完一个LV时,VG与PV的信息都是时时在变化的,并且我们创建LV的大小是根据当前VG的大小来决定的,不能超过当前VG的剩余大小!
每创建好一个逻辑卷,都会在 /dev 目录下出现一个以该卷组命名的文件夹,基于该卷组创建的所有的逻辑卷都是存放在这个文件夹下面:
[root@localhost ~]# ls /dev/rusky/mylv /dev/rusky/mylv |
我们每创建一个新的逻辑卷,该VG目录下都会多出这么一个设备。
二、格式化并使用逻辑卷
我们已经创建好了我们的PV、VG以及LV,这时候我们如果要使用逻辑卷,就必须将其格式化成我们需要用的文件系统,并将其挂载起来,然后就可以像使用分区一样去使用逻辑卷了
[root@localhost ~]# mkfs.ext4 /dev/rusky/mylv mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 128016 inodes, 512000 blocks 25600 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=34078720 63 block groups 8192 blocks per group, 8192 fragments per group 2032 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done |
格式化我们的逻辑卷以后,就可以使用 mount 命令将其进行挂载,我们将其挂载到 /mnt/myLvm 目录下
[root@localhost ~]# mkdir -p /mnt/myLvm [root@localhost ~]# mount /dev/rusky/mylv /mnt/myLvm/ [root@localhost ~]# mount sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=926752k,nr_inodes=231688,mode=755) …… /dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota) tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=188420k,mode=700) /dev/mapper/rusky-mylv on /mnt/myLvm type ext4 (rw,relatime,seclabel,data=http://www.mamicode.com/ordered) [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 6.7G 2.9G 3.8G 44% / devtmpfs 906M 0 906M 0% /dev tmpfs 921M 0 921M 0% /dev/shm tmpfs 921M 8.7M 912M 1% /run tmpfs 921M 0 921M 0% /sys/fs/cgroup /dev/sda1 497M 157M 341M 32% /boot tmpfs 185M 0 185M 0% /run/user/0 /dev/mapper/rusky-mylv 477M 2.3M 445M 1% /mnt/myLvm [root@localhost ~]# |
挂载完成后,我们就可以像正常的文件系统一样来使用了。
三、删除逻辑卷
我们在创建好逻辑卷后可以通过创建文件系统,挂载逻辑卷来使用它,如果说我们不想用了也可以将其删除掉。
【注意:】对于创建物理卷、创建卷组以及创建逻辑卷我们是有严格顺序的,同样,对于删除逻辑卷、删除卷组以及删除物理卷也是有严格顺序要求的
①首先将正在使用的逻辑卷卸载掉 通过 umount 命令
②将逻辑卷先删除 通过 lvremove 命令
③删除卷组 通过 vgremove 命令
④最后再来删除我们的物理卷 通过 pvremove 命令
[root@localhost mnt]# umount /dev/rusky/mylv [root@localhost mnt]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 6.7G 2.9G 3.8G 44% / devtmpfs 906M 0 906M 0% /dev tmpfs 921M 0 921M 0% /dev/shm tmpfs 921M 8.7M 912M 1% /run tmpfs 921M 0 921M 0% /sys/fs/cgroup /dev/sda1 497M 157M 341M 32% /boot tmpfs 185M 0 185M 0% /run/user/0 [root@localhost mnt]# lvremove /dev/rusky/mylv Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed [root@localhost mnt]# vgremove rusky Volume group "rusky" successfully removed [root@localhost mnt]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 rhel lvm2 a-- 7.51g 40.00m /dev/sdb lvm2 --- 1.00g 1.00g /dev/sdc lvm2 --- 2.00g 2.00g [root@localhost mnt]# pvremove /dev/sdb /dev/sdc Labels on physical volume "/dev/sdb" successfully wiped Labels on physical volume "/dev/sdc" successfully wiped [root@localhost mnt]# |
此时我们的刚创建的逻辑卷 mylv,卷组rusky以及物理卷 /dev/sdb和/dev/sdc 已经从我们当前操作系统上删除掉了,通过 lvs、vgs、pvs命令可以查看一下
[root@localhost mnt]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root rhel -wi-ao---- 6.67g swap rhel -wi-ao---- 820.00m [root@localhost mnt]# vgs VG #PV #LV #SN Attr VSize VFree rhel 1 2 0 wz--n- 7.51g 40.00m [root@localhost mnt]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 rhel lvm2 a-- 7.51g 40.00m [root@localhost mnt]# |
附录:开机自动挂载逻辑卷的方式
添加到/etc/fstab,加下如一行,使其开机自动挂载:
/dev/rusky/mylv /mnt/myLvm/ ext4 defaults 1 2 |
原文出处:http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/24/3096087.html
Linux磁盘管理:LVM逻辑卷的创建及使用