首页 > 代码库 > Linux 2 unit5
Linux 2 unit5
unit5 使用逻辑卷管理灵活存储
###LVM创建###
[root@station ~]# mkdir /weixindata ##建立目录
[root@station ~]# pvcreate /dev/vdb1 ##建立pv
Physical volume /dev/vdb1 notfound
Can‘t open /dev/vdb1 exclusively. Mounted filesystem?
[root@station ~]# vgcreate vg0 /dev/vdb1 ##建立vg
Physical volume /dev/vdb1 notfound
Physical volume /dev/vdb1 notfound
Can‘t open /dev/vdb1 exclusively. Mounted filesystem?
Unable to add physical volume‘/dev/vdb1‘ to volume group ‘vg0‘.
[root@station ~]# mkfs.xfs /dev/vg0
/dev/vg0: No such file or directory
Usage: mkfs.xfs
/* blocksize */ [-blog=n|size=num]
/* metadata */ [-m crc=[0|1]
/* data subvol */ [-dagcount=n,agsize=n,file,name=xxx,size=num,
(sunit=value,swidth=value|su=num,sw=num|noalign),
sectlog=n|sectsize=num
/* force overwrite */ [-f]
/* inode size */ [-ilog=n|perblock=n|size=num,maxpct=n,attr=0|1|2,
projid32bit=0|1]
/* no discard */ [-K]
/* log subvol */ [-lagnum=n,internal,size=num,logdev=xxx,version=n
sunit=value|su=num,sectlog=n|sectsize=num,
lazy-count=0|1]
/* label */ [-L label (maximum 12characters)]
/* naming */ [-nlog=n|size=num,version=2|ci,ftype=0|1]
/* no-op info only */ [-N]
/* prototype file */ [-p fname]
/* quiet */ [-q]
/* realtime subvol */ [-r extsize=num,size=num,rtdev=xxx]
/* sectorsize */ [-slog=n|size=num]
/* version */ [-V]
devicename
is required unless -d name=xxx is given.
is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxxKiB),
xxxm (xxx MiB), xxxg (xxx GiB), xxxt(xxx TiB) or xxxp (xxx PiB).
is xxx (512 byte blocks).
[root@station ~]# mdadm -D /dev/md0 ##查看设备信息
mdadm: md device /dev/md0 does not appear to be active.
[root@station ~]# mdadm -S /dev/md0 ##停止此设备
mdadm: stopped /dev/md0
[root@station ~]# mdadm -a /dev/md0 /dev/vdb2 ##添加一个新设备
[root@station ~]# vgcreate vg0 /dev/vdb1
Volume group "vg0"successfully created
[root@station ~]# lvcreate -L 300M -n lv0 vg0 ##创建300M的lv0在vg0下
Logical volume "lv0"created
[root@station ~]# mkfs.xfs /dev/vg0/lv0 ##格式化
meta-data=http://www.mamicode.com/dev/vg0/lv0 isize=256 agcount=4,agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0,rtextents=0
[root@station ~]# mount /dev/vg0/lv0 /weixindata/ ##挂载
[root@station ~]# ll /dev/mapper/vg0-lv0
lrwxrwxrwx. 1 root root 7 Apr 22 03:52 /dev/mapper/vg0-lv0 -> ../dm-0 ##下面测试结果出现/mapper实则和/vg0相同
[root@station ~]# ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 Apr 22 03:52 /dev/vg0/lv0 -> ../dm-0
测试结果
口令:watch -n 1 ‘echo ===pvmessage==;pvs;echo===vgmessage===;vgs;echo===lvm message===;lvs;echo ===;df/weixindata‘
===pvmessage==
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 1020.00m 720.00m
VG #PV #LV #SN Attr VSize VFree
vg0 1 1 0 wz--n- 1020.00m 720.00m
LV VG Attr LSize Pool Origin Data
% Move Log Cpy%Sync Convert
lv0 vg0 -wi-ao---- 300.00m
===
Filesystem 1K-blocks Used Available U
se% Mounted on
/dev/mapper/vg0-lv0 303788 15520 288268
6% /weixindata
####设备拉伸####
[root@station ~]# lvextend -L 900M /dev/vg0/lv0 ##将原设备拉伸为900M
Extending logical volume lv0 to 900.00MiB
Logical volume lv0 successfullyresized
[root@station ~]# xfs_growfs /dev/vg0/lv0 ##文件系统的扩展
meta-data=http://www.mamicode.com/dev/mapper/vg0-lv0 isize=256 agcount=4,agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=76800, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0,rtextents=0
data blocks changed from 76800 to 230400
[root@station ~]# lvextend -L 1200M /dev/vg0/lv0 ##继续拉伸为1200
Extending logical volume lv0 to 1.17GiB
Insufficient free space: 75 extentsneeded, but only 30 available ///提醒剩余空间不足
[root@station ~]# pvcreate /dev/vdb2 ##创建另一个pv
Physical volume "/dev/vdb2"successfully created
[root@station ~]# vgextend vg0 /dev/vdb2 ##将另一个pv加入vg中
Volume group "vg0"successfully extended
[root@station ~]# lvextend -L 1200M /dev/vg0/lv0 ##继续拉伸
Extending logical volume lv0 to 1.17GiB
Logical volume lv0 successfully resized//拉伸成功
[root@station ~]# xfs_growfs /dev/vg0/lv0 ##文件系统的拉伸
meta-data=http://www.mamicode.com/dev/mapper/vg0-lv0 isize=256 agcount=12,agsize=19200 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=230400, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0,rtextents=0
data blocks changed from 230400 to 307200
###系统的缩减###
1)缩减之前所做准备
[root@station ~]# umount /weixindata/ ##卸载
[root@station ~]# mkfs.ext4 /dev/vg0/lv0 ##更改文件系统
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
76800 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=314572800
10 block groups
32768 blocks per group, 32768 fragments per group
7680 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376,294912
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@station ~]# mount /dev/vg0/lv0 /weixindata/ ##挂载
[root@station ~]# df ##查看挂载是否成功
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 104739003241020 7232880 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 472 942188 1% /dev/shm
tmpfs 942660 17080 925580 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/mapper/vg0-lv0 1176704 3600 1095280 1% /weixindata
2)开始缩减
[root@station ~]# umount /weixindata/ ##卸载
[root@station ~]# e2fsck -f /dev/vg0/lv0 ##扫描(系统自己检测文件系统多大)
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/vg0/lv0: 11/76800 files (0.0% non-contiguous), 13924/307200 blocks
[root@station ~]# resize2fs /dev/vg0/lv0 300M ##缩减文件系统为300M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 76800 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 76800 blocks long.
[root@station ~]# mount /dev/vg0/lv0 /weixindata/ ##挂载
[root@station ~]# lvreduce -L 300 /dev/vg0/lv0 ##缩减设备
WARNING: Reducing active and openlogical volume to 300.00 MiB
THIS MAY DESTROY YOUR DATA (filesystemetc.)
Do you really want to reduce lv0? [y/n]: y
Reducing logical volume lv0 to 300.00MiB
Logical volume lv0 successfullyresized
[root@station ~]# pvmove /dev/vdb1 /dev/vdb2 ##把设备1信息传给设备2
[root@station ~]# vgreduce vg0 /dev/vdb1 ##把vdb1从vg0移除
Removed "/dev/vdb1" fromvolume group "vg0"
/dev/vdb1: Moved: 1.3%
/dev/vdb1: Moved: 100.0%
[root@station ~]# pvremove /dev/vdb1 把vdb1从pv移除
Labels on physical volume"/dev/vdb1" successfully wiped
####快照####
[root@station ~]# touch /weixindata/file{1..10}
[root@station ~]# ls /weixindata/
file1 file2 file4 file6 file8 lost+found
file10 file3 file5 file7 file9
[root@station ~]# umount /weixindata/
[root@station ~]# cd /weixindata/
[root@station weixindata]# ls
[root@station weixindata]# cd
[root@station ~]# lvcreate -L 50M -nlv0backup -s /dev/vg0/lv0 ###创建快照
Rounding up size to full physicalextent 52.00 MiB
Logical volume "lv0backup"created
[root@station ~]# mount /dev/vg0/lv0backup /weixindata/
[root@station ~]# cd /weixindata/
[root@station weixindata]# ls
file1 file2 file4 file6 file8 lost+found
file10 file3 file5 file7 file9
[root@station weixindata]# rm -fr *
[root@station weixindata]# ls
[root@station weixindata]# cd
[root@station ~]# umount /weixindata/
[root@station ~]# lvremove /dev/vg0/lvobackup ##删除坏了的快照
One or more specified logical volume(s)not found.
[root@station ~]# lvcreate -L 50M -nlv0backup -s /dev/vg0/lv0 ##重新快照
Rounding up size to full physicalextent 52.00 MiB
Logical volume "lv0backup"already exists in volume group "vg0"
[root@station ~]# mount /dev/vg0/lv0backup /weixindata/
[root@station ~]# ls /weixindata/
file1 file2 file4 file6 file8 lost+found
file10 file3 file5 file7 file9
监护口令:
watch -n 1 ‘echo ===pvmessage==; pvs;echo===vgmessage===;vgs;echo===lvmmessage===;lvs;echo ===;df /weixindata‘
快照成功时监护界面:
===pvmessage==
PV VG Fmt Attr PSize PFree
/dev/vdb2 vg0 lvm2 a-- 1020.00m 668.00m
VG #PV #LV #SN Attr VSize VFree
vg0 1 2 1 wz--n- 1020.00m 668.00m
LV VG Attr LSize Pool Origin
Data% Move Log Cpy%Sync Convert
lv0 vg0 owi-a-s--- 300.00m
lv0backup vg0 swi-a-s--- 52.00m lv0
0.00
===
Filesystem 1K-blocks Used Available Use%
Mounted on
/dev/vda1 10473900 3240980 7232920 31%
/
####LVM的删除####
[root@station ~]# umount /weixindata/ ##卸载
[root@station ~]# lvremove /dev/vg0/lv0backup ##删除快照
Do you really want to remove active logical volume lv0backup? [y/n]: y
Logical volume "lv0backup"successfully removed
[root@station ~]# lvremove /dev/vg0/lv0 ##删除lv0本身
Do you really want to remove active logical volume lv0? [y/n]: y
Logical volume "lv0"successfully removed
[root@station ~]# vgremove vg0 删除vg
Volume group "vg0"successfully removed
[root@station ~]# pvremove /dev/vdb2 删除pv
Labels on physical volume"/dev/vdb2" successfully wiped
本文出自 “AELY木” 博客,转载请与作者联系!
Linux 2 unit5