标签:lvm 磁盘
30.lvm.md
lvm :在操作系统之上创建灵活的动态分区方案
屏蔽了底层磁盘布局,便于动态调整磁盘容量
注意:/boot 分区用于存放引导文件,不能应用lvm机制。
LVM机制的基本概念
pv vg lv
基本用法
pv vg lv ++ scan create display remove+、 extend-、reduce
LVM: Logical Volume Manager, Version: 2
dm: device mapper,将一个或多个底层块设备组织成一个逻辑设备的模块;
/dev/dm#
/dev/mapper/VG_NAME-LV_NAME
/dev/mapper/vol0-root
/dev/VG_NAME/LV_NAME
/dev/vol0/root
pv管理工具:
pvs:简要pv信息显示
# pvs PV VG Fmt Attr PSize PFree /dev/sdd1 lvm2 --- 100.00m 100.00m /dev/sdd2 lvm2 --- 100.00m 100.00m /dev/sdd3 lvm2 --- 100.00m 100.00m /dev/sdd4 lvm2 --- 100.00m 100.00m
pvdisplay:显示pv的详细信息
# pvs PV VG Fmt Attr PSize PFree /dev/sdd1 lvm2 --- 100.00m 100.00m /dev/sdd2 lvm2 --- 100.00m 100.00m /dev/sdd3 lvm2 --- 100.00m 100.00m /dev/sdd4 lvm2 --- 100.00m 100.00m
pvcreate /dev/DEVICE: 创建pv
# pvcreate /dev/sdd{1,2,3,4} Physical volume "/dev/sdd1" successfully created Physical volume "/dev/sdd2" successfully created Physical volume "/dev/sdd3" successfully created Physical volume "/dev/sdd4" successfully created
pvremove DEVICE
# pvremove /dev/sdd{1,2,3,4} Labels on physical volume "/dev/sdd1" successfully wiped Labels on physical volume "/dev/sdd2" successfully wiped Labels on physical volume "/dev/sdd3" successfully wiped Labels on physical volume "/dev/sdd4" successfully wiped
vg管理工具:
vgs
# vgs VG #PV #LV #SN Attr VSize VFree vg_magedu 4 0 0 wz--n- 396.00m 396.00m
vgdisplay
# vgdisplay --- Volume group --- VG Name vg_magedu System ID Format lvm2 Metadata Areas 4 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 4 Act PV 4 VG Size 396.00 MiB PE Size 1.00 MiB Total PE 396 Alloc PE / Size 0 / 0 Free PE / Size 396 / 396.00 MiB VG UUID TFOU38-hneL-lnmW-YhDm-q45f-tqvW- YzZYNQ
vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath..]
# vgcreate -s 1M vg_magedu /dev/sdd{1,2,3,4} Volume group "vg_magedu" successfully created
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
# vgextend /dev/vg_magedu /dev/sdd4 Volume group "vg_magedu" successfully extended
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
# vgreduce /dev/vg_magedu /dev/sdd4 Removed "/dev/sdd4" from volume group "vg_magedu" #最后一个只能删除vg. # vgreduce /dev/magedu /dev/sdd4 Can‘t remove final physical volume "/dev/sdd4" from volume group "magedu" # vgremove /dev/magedu
说明:由于可能lv已经占用了该要减少的pv,要做下面的流程 :
vg缩减:检查lv容量,如果量够不够一个pv,不能缩减;量够的如果pv在vg前面可能被用到,被用到就pvmove到空闲pv中,在执行vgreduce:
#缩减vg,先查看pv是否再用,lv用到多少,在检测pv是否再用,如在用pvmove到空闲的pv在执行 #vgreduce pvmove /dev/sdd1 /dev/sdd3 # pvmove SRC_PV DET_PV vgreduce /dev/magedu /dev/sdd1 #vgreduce PATH_TO_VG PATH_TO_LV
vg扩展:找到可扩展进来的pv,vgexteng VG_NAME PV_NAME
# vgextend /dev/magedu /dev/sdd4 Volume group "magedu" successfully extended # pvs PV VG Fmt Attr PSize PFree /dev/sdd1 lvm2 --- 100.00m 100.00m /dev/sdd2 magedu lvm2 a-- 96.00m 36.00m /dev/sdd3 magedu lvm2 a-- 96.00m 0 /dev/sdd4 magedu lvm2 a-- 96.00m 96.00m
vgremove
lv管理工具:
lvs
# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lvm1 vg_magedu -wi-a----- 100.00m
lvdisplay
# lvdisplay --- Logical volume --- LV Path /dev/vg_magedu/lvm1 LV Name lvm1 VG Name vg_magedu LV UUID CY6dcf-fh1z-XJym-NCzX-FPmw-naDw-1EWbmm LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-03-16 06:01:55 -0700 LV Status available # open 0 LV Size 100.00 MiB Current LE 100 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
lvcreate -L #[mMgGtT] -n NAME VolumeGroup
# lvcreate -L 100M -n lvm1 /dev/vg_magedu WARNING: ext4 signature detected on /dev/vg_magedu/lvm1 at offset 1080. Wipe it? [y/n]: y Wiping ext4 signature on /dev/vg_magedu/lvm1. Logical volume "lvm1" created.
lvremove /dev/VG_NAME/LV_NAME
# umount /mail # lvremove /dev/magedu/magedu Do you really want to remove active logical volume magedu? [y/n]: y Logical volume "magedu" successfully removed
扩展逻辑卷:
# lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
# lvextend -L +75M /dev/vg_magedu/lvm1 Size of logical volume vg_magedu/lvm1 changed from 25.00 MiB (25 extents) to 100.00 MiB (100 extents). Logical volume lvm1 successfully resized.
# resize2fs /dev/VG_NAME/LV_NAME
缩减逻辑卷:
# 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
# resize2fs /dev/magedu/magedu 100M resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/magedu/magedu is mounted on /mail;on-line resizing required resize2fs: On-line shrinking not supported # umount /mail # resize2fs /dev/magedu/magedu 100M resize2fs 1.42.9 (28-Dec-2013) Please run ‘e2fsck -f /dev/magedu/magedu‘ first. # resize2fs /dev/magedu/magedu 100M^C # e2fsck -f /dev/magedu/magedu 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/magedu/magedu: 12/37544 files (8.3% non-contiguous),10393/153600 blocks # resize2fs /dev/magedu/magedu 100M resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/magedu/magedu to 102400 (1k)blocks. The filesystem on /dev/magedu/magedu is now 102400 blocks long. # lvreduce -L 50M /dev/magedu/magedu Rounding size to boundary between physical extents: 52.00 MiB WARNING: Reducing active logical volume to 52.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce magedu? [y/n]: y Size of logical volume magedu/magedu changed from 152.00 MiB (38 extents) to 52.00 MiB (13 extents). Logical volume magedu successfully resized.
快照:snapshot
快照:快照其实也是个逻辑卷lv,快照只能对逻辑卷lvm进行备份,并且只能对同一个卷组的逻辑卷进行备份;
备份,不用在需要格式化快照卷
冷备份:卸载文件系统,不能读,不能写;
温备份:不卸载文件系统,能读取文件系统内容但是不能写;
热备份:不卸载文件系统,不能影响正常读写;
快照机制
生成:拷贝元数据到快照卷,并建立对应chunk位图,置位位图为空
拷贝过程中,检测chunk位图,检测对应chunk位,未改变则将原始卷对应数据拷贝到快照卷对应位置,
置位chunk位图对应chunk位,对原始卷的进行写操作,否则不做任何操作;
恢复时:按照chunk恢复,量也很小。查找chunk位图,有标记则复制快照卷对应数据到原始卷做还原,否则不变。
鸟哥的恢复:卸载镜像卷,将镜像卷压缩打包到文件另一个目录,删除源卷,删除镜像卷,建立新的原卷,将打包文件解压到新的原卷 。
lvcreate -L
#[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name
# ls /mail -lhi total 29K 12 -rw-r--r--. 1 root root 14K Mar 16 06:11 functions 11 drwx------. 2 root root 12K Mar 16 06:10 lost+found 13 -rw-r--r--. 1 root root 512 Mar 16 06:11 sda_mbr.bak # df -h /mail Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_magedu-lvm1 93M 1.6M 85M 2% /mail # lvcreate -s -p r -L 75M -n lvm1s /dev/vg_magedu/lvm1 #-s指定快照卷,后面指定同组的lv,这样就不用指定vg了。 Logical volume "lvm1s" created. # df -lh /mails Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_magedu-lvm1s 93M 1.6M 85M 2% /mails #镜像卷和原卷大小一致。 # dd if=/dev/sdb of=/mail/sdb_mbr.bak bs=512 count=1 1+0 records in 1+0 records out 512 bytes (512 B) copied, 0.000481676 s, 1.1 MB/s # ls /mail -al total 36 drwxr-xr-x. 3 root root 1024 Mar 16 06:16 . dr-xr-xr-x. 22 root root 4096 Mar 16 06:12 .. -rw-r--r--. 1 root root 13948 Mar 16 06:11 functions drwx------. 2 root root 12288 Mar 16 06:10 lost+found -rw-r--r--. 1 root root 512 Mar 16 06:11 sda_mbr.bak -rw-r--r--. 1 root root 512 Mar 16 06:16 sdb_mbr.bak # ls /mails functions lost+found sda_mbr.bak
说明:LVM容的是数据实现上的错误,不能解决底层硬件故障;
RAID容的硬件错误
LVM和RAID的相比是错误
在不同磁盘建立镜像卷提高效率是不行的,不如RAID1呢
应用:数据库实时备份
数据追溯的
lvm 恢复
# 卸载lvm源卷,格式化为ext3文件系统,源文件系统为ext4; # mount /dev/vg_magedu/lvm1 /mail # ls /mail functions lost+found mails sda_mbr.bak sda_mbr.bak.bz2 sda_mbr.bak.gzip sdb_mbr.bak test.data var # 建立镜像卷,-l 60 建立60个PE的卷,PE大小为1M,那么卷大小为60M。 # lvcreate -l 60 -n lvm1s -s -p r /dev/vg_magedu/lvm1 # lvs # mount /dev/vg_magedu/lvm1s /mails # ls /mails functions lost+found mails sda_mbr.bak sda_mbr.bak.bz2 sda_mbr.bak.gzip sdb_mbr.bak test.data var # 镜像卷文件已经拷贝过来了,查看能看得到。建立新20M的文件。 # dd if=/dev/zero of=/mail/test2.data bs=20M count=1 # 将镜像卷内容打包到其他目录。 # tar -zcvf /tmp/lvmsback.tar.gz /mails # 取消原卷挂载,并格式化,再挂载 # umount /mail # blkid /dev/vg_magedu/lvm1 # /dev/vg_magedu/lvm1: UUID="26a7f721-d66e-4ea1-a95a-a1589bff51ab" TYPE="ext4" # mkfs.ext3 /dev/vg_magedu/lvm1 # mount /dev/vg_magedu/lvm1 /mail # 将打包文件释放到新的文件系统中,文件恢复完成。 lvm1s镜像卷创建时只读,可写了。 # tar -zxvf /tmp/lvmsback.tar.gz -C /mail # mv /mail/mails/* /mail # ls /mail functions lost+found mails sda_mbr.bak sda_mbr.bak.bz2 sda_mbr.bak.gzip sdb_mbr.bak test.data var
dd命令
用法:拷贝数据,镜像磁盘,备份mbr,开拓swap文件
dd if=数据来源 of=目标 bs=# blocksize count=# 传递次数
dd if=/dev/sda of=/dev/sdb
dd if=/dev/sda of=/sda.mbr bs=512 count=1
常用用法:
建立200M的文件形式的swap文件
dd if=/dev/zero of=/swap-test bs=100M count=2 mkswap /swap-test swapon /swap-test swapon # swapon NAME TYPE SIZE USED PRIO /dev/sda2 partition 1.8G 0B -1 /swap-test file 200M 0B -1
本文出自 “君临城” 博客,请务必保留此出处http://kingslanding.blog.51cto.com/9130940/1754083
标签:lvm 磁盘
原文地址:http://kingslanding.blog.51cto.com/9130940/1754083