LVM逻辑卷管理器

LVM逻辑卷管理器
部署逻辑卷:
功能/命令 | 物理卷管理 |卷组管理| 逻辑卷管理
扫描 | ------ -----pvscan | - vgscan | lvscan
建立 | ----------pvcreate | vgcreate | lvcreate
显示 | ---------pvdisplay | vgdisplay | lvdisplay
删除 | --------pvremove | vgremove | lvremove
扩展 | -------- ------------| vgextend | lvextend
缩小 | ---------------------| vgreduce | lvreduce
为避免实验之间互相冲突,请您自行还原虚拟机到最初始状态,并在虚拟机中添加两块新硬盘设备后开机,如图所示:
LVM逻辑卷管理器
第1步:让新添加的两块硬盘设备支持LVM逻辑卷管理器技术:
[root@linuxprobe ~]# pvcreate /dev/sdb /dev/sdc
Physical volume “/dev/sdb” successfully created
Physical volume “/dev/sdc” successfully created
第2步:把两块硬盘设备都加入到storage卷组中,然后查看下卷组的状态:
[root@linuxprobe ~]# vgcreate storage /dev/sdb /dev/sdc
Volume group “storage” successfully created
[root@linuxprobe ~]# vgdisplay
— Volume group —
VG Name storage
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
Act PV 2
VG Size 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 0 / 0 Free PE / Size 10238 / 39.99 GiB
VG UUID KUeAMF-qMLh-XjQy-ArUo-LCQI-YF0o-pScxm1
………………省略部分输出信息………………
第3步:切割出一个约为150M的逻辑卷设备:
[root@linuxprobe ~]# lvcreate -n vo -l 37 storage
Logical volume “vo” created
[root@linuxprobe ~]# lvdisplay
— Logical volume —
LV Path /dev/storage/vo
LV Name vo
VG Name storage
LV UUID D09HYI-BHBl-iXGr-X2n4-HEzo-FAQH-HRcM2I
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2017-02-01 01:22:54 -0500
LV Status available

open 0

LV Size 148.00 MiB
Current LE 37
Segments 1
Allocation inherit
Read ahead sectors auto

  • currently set to 8192
    Block device 253:2
    ………………省略部分输出信息………………
    第4步:把生成好的逻辑卷格式化后挂载使用:
    [root@linuxprobe ~]# mkfs.ext4 /dev/storage/vo
    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
    38000 inodes, 151552 blocks
    7577 blocks (5.00%) reserved for the super user
    First data block=1
    Maximum filesystem blocks=33816576
    19 block groups
    8192 blocks per group, 8192 fragments per group
    2000 inodes per group
    Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done
    [root@linuxprobe ~]# mkdir /linuxprobe
    [root@linuxprobe ~]# mount /dev/storage/vo /linuxprobe
    第5步:查看挂载状态,并写入到配置文件永久生效:
    [root@linuxprobe ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/rhel-root 18G 3.0G 15G 17% /
    devtmpfs 905M 0 905M 0% /dev
    tmpfs 914M 140K 914M 1% /dev/shm
    tmpfs 914M 8.8M 905M 1% /run
    tmpfs 914M 0 914M 0% /sys/fs/cgroup
    /dev/sr0 3.5G 3.5G 0 100% /media/cdrom
    /dev/sda1 497M 119M 379M 24% /boot
    /dev/mapper/storage-vo 145M 7.6M 138M 6% /linuxprobe
    [root@linuxprobe ~]# echo “/dev/storage/vo /linuxprobe ext4 defaults 0 0” >> /etc/fstab
    扩容逻辑卷;
    [root@linuxprobe ~]# umount /linuxprobe
    第1步:把上个实验中的逻辑卷vo扩展至290M:
    [root@linuxprobe ~]# lvextend -L 290M /dev/storage/vo
    Rounding size to boundary between physical extents: 292.00 MiB
    Extending logical volume vo to 292.00 MiB
    Logical volume vo successfully resized
    第2步:检查磁盘完整性,重置硬盘容量:
    [root@linuxprobe ~]# e2fsck -f /dev/storage/vo
    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/storage/vo: 11/38000 files (0.0% non-contiguous), 10453/151552 blocks
    [root@linuxprobe ~]# resize2fs /dev/storage/vo
    resize2fs 1.42.9 (28-Dec-2013)
    Resizing the filesystem on /dev/storage/vo to 299008 (1k) blocks.
    The filesystem on /dev/storage/vo is now 299008 blocks long.
    第3步:重新挂载硬盘设备并查看挂载状态:
    [root@linuxprobe ~]# mount -a
    [root@linuxprobe ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/rhel-root 18G 3.0G 15G 17% /
    devtmpfs 985M 0 985M 0% /dev
    tmpfs 994M 80K 994M 1% /dev/shm
    tmpfs 994M 8.8M 986M 1% /run
    tmpfs 994M 0 994M 0% /sys/fs/cgroup
    /dev/sr0 3.5G 3.5G 0 100% /media/cdrom
    /dev/sda1 497M 119M 379M 24% /boot
    /dev/mapper/storage-vo 279M 2.1M 259M 1% /linuxprobe
    缩小逻辑卷:
    [root@linuxprobe ~]# umount /linuxprobe
    第1步:检查文件系统的完整性:
    [root@linuxprobe ~]# e2fsck -f /dev/storage/vo
    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/storage/vo: 11/74000 files (0.0% non-contiguous), 15507/299008 blocks
    第2步:把LV逻辑卷的容量减小到120M:
    [root@linuxprobe ~]# resize2fs /dev/storage/vo 120M
    resize2fs 1.42.9 (28-Dec-2013)
    Resizing the filesystem on /dev/storage/vo to 122880 (1k) blocks.
    The filesystem on /dev/storage/vo is now 122880 blocks long.
    [root@linuxprobe ~]# lvreduce -L 120M /dev/storage/vo
    WARNING: Reducing active logical volume to 120.00 MiB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce vo? [y/n]: y
    Reducing logical volume vo to 120.00 MiB
    Logical volume vo successfully resized
    第3步:把文件系统重新挂载并查看系统状态:
    [root@linuxprobe ~]# mount -a
    [root@linuxprobe ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/rhel-root 18G 3.0G 15G 17% /
    devtmpfs 985M 0 985M 0% /dev
    tmpfs 994M 80K 994M 1% /dev/shm
    tmpfs 994M 8.8M 986M 1% /run
    tmpfs 994M 0 994M 0% /sys/fs/cgroup
    /dev/sr0 3.5G 3.5G 0 100% /media/cdrom
    /dev/sda1 497M 119M 379M 24% /boot
    /dev/mapper/storage-vo 113M 1.6M 103M 2% /linuxprobe
    逻辑卷快照:
    [root@linuxprobe ~]# vgdisplay
    — Volume group —
    VG Name storage
    System ID
    Format lvm2
    Metadata Areas 2
    Metadata Sequence No 4
    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 39.99 GiB
    PE Size 4.00 MiB
    Total PE 10238
    Alloc PE / Size 30 / 120.00 MiB Free PE / Size 10208 / 39.88 GiB
    VG UUID CTaHAK-0TQv-Abdb-R83O-RU6V-YYkx-8o2R0e
    ………………省略部分输出信息………………
    [root@linuxprobe ~]# echo “Welcome to Linuxprobe.com” > /linuxprobe/readme.txt
    [root@linuxprobe ~]# ls -l /linuxprobe
    total 14
    drwx------. 2 root root 12288 Feb 1 07:18 lost+found
    -rw-r–r--. 1 root root 26 Feb 1 07:38 readme.txt
    第1步:使用-s参数来生成一个快照卷,使用-L参数来指定切割的大小,另外要记得在后面写上这个快照是针对那个LV逻辑卷设备做的。
    [root@linuxprobe ~]# lvcreate -L 120M -s -n SNAP /dev/storage/vo
    Logical volume “SNAP” created
    [root@linuxprobe ~]# lvdisplay
    — Logical volume —
    LV Path /dev/storage/SNAP
    LV Name SNAP
    VG Name storage
    LV UUID BC7WKg-fHoK-Pc7J-yhSd-vD7d-lUnl-TihKlt
    LV Write Access read/write
    LV Creation host, time localhost.localdomain, 2017-02-01 07:42:31 -0500
    LV snapshot status active destination for vo
    LV Status available

open 0

LV Size 120.00 MiB
Current LE 30
COW-table size 120.00 MiB
COW-table LE 30
Allocated to snapshot 0.01% #快照使用的百分比
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto

  • currently set to 8192
    Block device 253:3
    ………………省略部分输出信息………………
    第2步:在LV设备卷所挂载的目录中创建一个100M的垃圾文件,这样再来看快照卷的状态就会发现使用率上升了:
    [root@linuxprobe ~]# dd if=/dev/zero of=/linuxprobe/files count=1 bs=100M
    1+0 records in
    1+0 records out
    104857600 bytes (105 MB) copied, 3.35432 s, 31.3 MB/s
    [root@linuxprobe ~]# lvdisplay
    — Logical volume —
    LV Path /dev/storage/SNAP
    LV Name SNAP
    VG Name storage
    LV UUID BC7WKg-fHoK-Pc7J-yhSd-vD7d-lUnl-TihKlt
    LV Write Access read/write
    LV Creation host, time localhost.localdomain, 2017-02-01 07:42:31 -0500
    LV snapshot status active destination for vo
    LV Status available

open 0

LV Size 120.00 MiB
Current LE 30
COW-table size 120.00 MiB
COW-table LE 30
Allocated to snapshot 83.71%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto

  • currently set to 8192
    Block device 253:3
    第3步:为了校验SNAP快照卷的效果,需要对逻辑卷进行快照合并还原操作,在这之前记得先卸载掉逻辑卷设备与目录的挂载~
    [root@linuxprobe ~]# umount /linuxprobe
    [root@linuxprobe ~]# lvconvert --merge /dev/storage/SNAP
    Merging of volume SNAP started.
    vo: Merged: 21.4%
    vo: Merged: 100.0%
    Merge of snapshot into logical volume vo has finished.
    Logical volume “SNAP” successfully removed
    第4步:快照卷会被自动删除掉,并且刚刚在逻辑卷设备被快照后再创建出来的100M垃圾文件也被清除了:
    [root@linuxprobe ~]# mount -a
    [root@linuxprobe ~]# ls /linuxprobe/
    lost+found readme.txt
    删除逻辑卷:
    [root@linuxprobe ~]# umount /linuxprobe
    [root@linuxprobe ~]# vim /etc/fstab

#/etc/fstab
#Created by anaconda on Fri Feb 19 22:08:59 2017

#Accessible filesystems, by reference, are maintained under ‘/dev/disk’
#See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

/dev/mapper/rhel-root / xfs defaults 1 1
UUID=50591e35-d47a-4aeb-a0ca-1b4e8336d9b1 /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0
/dev/storage/vo /linuxprobe ext4 defaults 0 0
第2步:把LV逻辑卷设备删除,需要手工输入y来确认操作:
[root@linuxprobe ~]# lvremove /dev/storage/vo
Do you really want to remove active logical volume vo? [y/n]: y
Logical volume “vo” successfully removed
第3步:把VG卷组删除,此处只需写卷组名称即可,而无需设备完整绝对路径:
[root@linuxprobe ~]# vgremove storage
Volume group “storage” successfully removed
第4步:把PV物理卷设备移除:
[root@linuxprobe ~]# pvremove /dev/sdb /dev/sdc
Labels on physical volume “/dev/sdb” successfully wiped
Labels on physical volume “/dev/sdc” successfully wiped

上一篇:【1064 30 完全二叉搜索树】 Complete Binary Search Tree


下一篇:网页防封链接制作的原理有哪些?