一、LVM的基本使用方法
1. pv管理工具:
(1) pvs:简要pv信息显示
(2) pvdisplay:显示pv的详细信息
(3) pvcreate /dev/DEVICE: 创建pv
(4) pvmove:删除PV
2. vg管理工具:
(1) vgs:简要vg信息显示
(2) vgdisplay:显示pv的详细信息
(3)vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
(创建VG:-s表示指定PE大小,#表示数字,后面加单位)
(VolumeGroupName:VG名字 PhysicalDevicePath:路径)
(4) vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...](扩展VG)
(5) vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] (缩减VG)
(6) vgremove : 删除VG.(必须先做pvremove)
(6) vgremove : 删除VG.(必须先做pvremove)
3. lv管理工具:
- lvs: 简要lv信息显示
- lvdisplay: 显示pv的详细信息
- lvcreate -L #[mMgGtT] -n NAME VolumeGroup -l:
(创建LV,-L表示逻辑卷大小,-n表示逻辑卷名称,-l表示分配盘区数量)
4. lvremove /dev/VG_NAME/LV_NAME:删除LV
4. lvremove /dev/VG_NAME/LV_NAME:删除LV
4. 扩展逻辑卷:
# lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
(扩展逻辑卷,如原2G的LV,使用-L +3G后相当于-L 5G)
# resize2fs /dev/VG_NAME/LV_NAME
(修复逻辑卷边境,只适用于ext文件系统)
5. 缩减逻辑卷:(必须先卸载)
# umount /dev/VG_NAME/LV_NAME
# e2fsck -f /dev/VG_NAME/LV_NAME (对文件系统强制检测和修复)
# resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT] #相当于卸载后重新挂载
# lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
# mount
6. 快照:snapshot
lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name
二、确认是否安装LVM
环境:
[root@localhost ~]# cat /etc/issue
CentOS Linux release 6.0 (Final)
Kernel \r on an \m
首先确定系统中是否安装了lvm工具
[root@localhost ~]# rpm -qa | grep lvm
lvm2-libs-2.02.72-8.el6.i686
lvm2-2.02.72-8.el6.i686
上面的结果,说明系统已经安装了LVM管理工具;如果没有安装LVM管理工具,则则需要从网络下载或者从光盘装LVM rpm工具包。
三、实验
1、创建一个至少有两个PV组成的大小为20G的名为testvg的VG,要求PE大小为16MB,而后在卷组中创建大小为5G的逻辑卷testlv,挂在至/users目录
1、创建一个至少有两个PV组成的大小为20G的名为testvg的VG,要求PE大小为16MB,而后在卷组中创建大小为5G的逻辑卷testlv,挂在至/users目录
先创建2个分区大小都为10g的PV
[root@localhost ~]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n # 新建一个分区
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1 # 增加主分区sdc1,其大小为10G
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +10G
Command (m for help): n # 新建一个分区
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2 # 增加主分区sdc2,其大小为10G
First cylinder (1307-13054, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-13054, default 13054): +10G
Command (m for help): t # sdc1分区类型为8e (Linux LVM)
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): t # sdc2分区类型为8e (Linux LVM)
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): p # 查看分区
Disk /dev/sdc: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x75a96bbd
Device Boot Start End Blocks Id System
/dev/sdc1 1 1306 10490413+ 8e Linux LVM
/dev/sdc2 1307 2612 10490445 8e Linux LVM
Command (m for help): w # 保存分区
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdc # 通知内核重新读取硬盘分区表
[root@localhost ~]# partx -a /dev/sdc # 通知内核重新读取硬盘分区表
[root@localhost ~]# pvcreate /dev/sdc{1,2} # 将分区创建为物理卷
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdc2" successfully created
[root@localhost ~]# pvs # 简要查看创建的物理卷 pvdisplay 可查看详细PV信息
PV VG Fmt Attr PSize PFree
/dev/sdc1 lvm2 a- 10.00g 10.00g
/dev/sdc2 lvm2 a- 10.00g 10.00g
[root@localhost ~]# vgcreate -s 16M testvg /dev/sdc{1,2}
Volume group "testvg" successfully created
# 将物理卷创建为卷组名为testvg,PE大小为16M
[root@localhost ~]# vgdisplay # 查看卷组vg的详细信息
--- Volume group ---
VG Name testvg #卷组名字
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 20.00 GiB#卷组总大小
PE Size 16.00 MiB
Total PE 1280#以PE为单位的总大小
Alloc PE / Size 0 / 0
Free PE / Size 1280/20.00 GiB #显示VG中有多少空闲空间可用于分配给新LV或扩展现有LV
VG UUID CqSk4j-e8gz-hW61-DICB-3G3a-yrZn-N578sS
[root@localhost ~]# lvcreate -L 5G -n testlv testvg
Logical volume "testlv" created
# 在卷组中创建大小为5G的逻辑卷testlv
[root@localhost ~]# mke2fs -t ext4 /dev/testvg/testlv # 将testlv格式化
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=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
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir /users # 创建users文件夹
[root@localhost ~]# mount /dev/testvg/testlv /users
# 将/dev/testvg/testlv挂载到/users/文件夹下
[root@localhost ~]# mount # 查看是否已挂载
/dev/sda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sdb1 on /testing type ext4 (rw,acl)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
.host:/ on /mnt/hgfs type vmhgfs (rw,ttl=1)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/testvg-testlv on /users type ext4 (rw)
[root@localhost ~]# ls /users/ # 查看/users/目录下的文件
lost+found
[root@localhost ~]# df -lh # 查看硬件信息
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda1 19G 3.6G 15G 20% /
tmpfs 503M 432K 503M 1% /dev/shm
/dev/sdb1 9.9G 77M 9.3G 1% /testing
/dev/mapper/testvg-testlv 5.0G 138M 4.6G 3% /users
2、新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录
[root@localhost ~]# useradd -d /users/archlinux archlinux
# 新建用户archlinux,指定其家目录为/users/archlinux
[root@localhost ~]# echo "archlinux" | passwd --stdin archlinux
更改用户 archlinux 的密码 。
passwd: 所有的身份验证令牌已经成功更新。
[root@localhost ~]# su archlinux # 切换到用户archlinux
[archlinux@localhost root]$ cp -r /etc/pam.d /users/archlinux/
#复制/etc/pam.d目录至用户archlinux的家目录
3、扩展tetslv至7G,要求archlinux用户的文件不能丢失
[archlinux@localhost root]$ su # 切换到root用户
密码:
[root@localhost ~]# pvs # 查看pv信息,看物理卷是否能扩展
PV VG Fmt Attr PSize PFree
/dev/sdc1 testvg lvm2 a- 10.00g 5.00g
/dev/sdc2 testvg lvm2 a- 10.00g 10.00g
[root@localhost ~]# vgs # 查看vg信息,看卷组是否能扩展
VG #PV #LV #SN Attr VSize VFree
testvg 2 1 0 wz--n- 20.00g 15.00g
[root@localhost ~]# lvextend -L 7G /dev/testvg/testlv
Extending logical volume testlv to 7.00 GiB
Logical volume testlv successfully resized
# 扩展tetslv至7G
[root@localhost ~]# df –lh # 查看硬盘信息,/dev/mapper/testvg-testlv还是5G
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda1 19G 3.4G 15G 19% /
tmpfs 503M 432K 503M 1% /dev/shm
/dev/sdb1 9.9G 77M 9.3G 1% /testing
/dev/mapper/testvg-testlv 5.0G 139M 4.6G 3% /users
[root@localhost ~]# resize2fs -p /dev/testvg/testlv
# 通过resize2fs工具来修复文件系统,把新添加的文件系统更新,相当于卸载后重新挂载
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/testvg/testlv is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/testvg/testlv to 1835008 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 1835008 blocks long.
[root@localhost ~]# df –lh # 查看硬件信息
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda1 19G 3.4G 15G 19% /
tmpfs 503M 432K 503M 1% /dev/shm
/dev/sdb1 9.9G 77M 9.3G 1% /testing
/dev/mapper/testvg-testlv # 已更新,但不一定真正达,7G的10%以内是可以接受的
6.9G 140M 6.5G 3% /users
[root@localhost ~]# cd /users/archlinux/pam.d/ # 查看archlinux用户文件是否丢失
4、收缩testlv至3G,要求archlinux用户的文件不能丢失
[root@localhost ~]# umount /dev/testvg/testlv # 卸载分区
umount: /users: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@localhost ~]# e2fsck -f /dev/testvg/testlv #文件系统强制检测和修复
[root@localhost ~]# resize2fs /dev/testvg/testlv 3G
# 缩减逻辑卷大小
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 786432 blocks long.
[root@localhost pam.d]# lvreduce -L 3G /dev/testvg/testlv
# 缩减逻辑卷lv物理边界大小至3G
WARNING: Reducing active logical volume to 3.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testlv? [y/n]: y
Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
Logical volume testlv successfully resized
[root@localhost pam.d]# lvs # 查看逻辑卷
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
testlv testvg -wi-a----- 3.00g
[root@localhost pam.d]# mount /dev/testvg/testlv /users/archlinux/ # 查看逻辑卷
[root@localhost pam.d]# cd /users/archlinux/pam.d/ # 查看archlinux用户文件是否丢失
5、对testlv创建快照,并尝试给予快照备份数据,验证快照的功能
[root@localhost ~]# cp /etc/issue /users
[root@localhost ~]# lvcreate -s -L 30M -n snaplv -p r /dev/testvg/testlv
# 创建快照 –s,-L指定快照大小为30M,-n指定快照名字为snaplv,-p r只读权限
Rounding up size to full physical extent 32.00 MiB
Logical volume "snaplv" created
[root@localhost ~]# mount /dev/testvg/snaplv /mnt
# 将快照文件挂载至/mnt
mount: block device /dev/mapper/testvg-snaplv is write-protected, mounting read-only
[root@localhost ~]# cd /mnt # /查看mnt下挂载的issue
[root@localhost mnt]# ls
hgfs issue sdb sdc sdd
[root@localhost mnt]# cat issue
CentOS Linux release 6.0 (Final)
Kernel \r on an \m
[root@localhost mnt]# vim /users/issue # 编辑源卷的issue文件
[root@localhost mnt]# cat !$ # 查看修改后的issue文件
cat /users/issue
CentOS Linux release 6.0 (Final)
Kernel \r on an \m
New line. # 这一句是添加的。
[root@localhost mnt]# pwd # 再查看/mnt下快照里面的issue
/mnt
[root@localhost mnt]# cat issue # 下面并没有变化,还是原来的文件内容
CentOS Linux release 6.0 (Final)
Kernel \r on an \m
[root@localhost mnt]# cp /etc/rc.d/init.d/functions /users # 复制某文件至/users
[root@localhost mnt]# cd /users/
[root@localhost users]# ls # 发现/users源卷仍可以新增文件等操作
archlinux functions issue lost+found
6、卸载删除LVM
[root@localhost users]# umount /mnt # 卸载快照的挂载
[root@localhost users]# lvremove /dev/testvg/snaplv # 删除快照
Do you really want to remove active logical volume snaplv? [y/n]: y
Logical volume "snaplv" successfully removed
[root@localhost ~]# umount /users/archlinux/ # 卸载
[root@localhost ~]# umount /users # 发现卸载不能进行
umount: /users: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@localhost ~]# umount -f /users # 加上-f强制卸载也不行
umount2: 设备或资源忙
umount: /users: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
umount2: 设备或资源忙
[root@localhost ~]# fuser -m /users # 能识别出正在对/user访问的进程的进程号
/users: 14424c
[root@localhost ~]# pa aux | grep 14424
-bash: pa: command not found
[root@localhost ~]# ps aux | grep 14424
root 14424 0.0 0.1 6960 1728 pts/1 S+ 07:36 0:00 bash
root 18408 0.0 0.0 5984 728 pts/2 S+ 10:01 0:00 grep 14424
[root@localhost ~]# kill -9 14424 # 杀死进程
[root@localhost ~]# umount /users # 现在卸载成功了
[root@localhost ~]# lvremove /dev/testvg/testlv # 删除testlv
Do you really want to remove active logical volume testlv? [y/n]: y
Logical volume "testlv" successfully removed
[root@localhost ~]# lvs # 查看lv信息:无
No volume groups found
[root@localhost ~]# vgremove testvg # 删除testvg
Volume group "testvg" successfully removed
[root@localhost ~]# vgs # 查看vg信息:无
No volume groups found
[root@localhost ~]# pvremove /dev/sdc1 # 删除pv
Labels on physical volume "/dev/sdc1" successfully wiped
[root@localhost ~]# pvremove /dev/sdc2 # 删除pv
Labels on physical volume "/dev/sdc2" successfully wiped