红联Linux门户
Linux帮助

linux下关于逻辑卷相关配置

发布时间:2015-11-28 15:27:37来源:linux网站作者:该死的懦弱

1、创建一个物理卷

[root@docker tmp]# pvcreate /dev/sda{5..6}
Physical volume "/dev/sda5" successfully created
Physical volume "/dev/sda6" successfully created
[root@docker tmp]# pvs
PV         VG   Fmt  Attr PSize  PFree
/dev/sda5       lvm2 ---  10.00g 10.00g
/dev/sda6       lvm2 ---   2.01g  2.01g
[root@docker tmp]# pvscan
PV /dev/sda5         lvm2 [10.00 GiB]
PV /dev/sda6         lvm2 [2.01 GiB]
Total: 2 [12.01 GiB] / in use: 0 [0   ] / in no VG: 2 [12.01 GiB]


2、创建一个卷组

[root@docker tmp]# vgcreate vg0 /dev/sda{5..6}
Volume group "vg0" successfully created
[root@docker tmp]# vgs
VG   #PV #LV #SN Attr   VSize  VFree
vg0    2   0   0 wz--n- 12.00g 12.00g
[root@docker tmp]# vgscan
Reading all physical volumes.  This may take a while...
Found volume group "vg0" using metadata type lvm2


3、创建一个逻辑卷

[root@docker tmp]# lvcreate -L +1G -nlv0 /dev/vg0
Logical volume "lv0" created
[root@docker tmp]# lvs
LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
lv0  vg0  -wi-a----- 1.00g                                                   
[root@docker tmp]# lvscan
ACTIVE            '/dev/vg0/lv0' [1.00 GiB] inherit


4、格式化逻辑卷

[root@docker tmp]# mkfs.ext4 /dev/vg0/lv0
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

正在写入inode表: 完成                           
Creating journal (8192 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


5、挂载逻辑卷

[root@docker ~]# mount /dev/vg0/lv0 /luyun
[root@docker ~]# df -Th
Filesystem          Type   Size  Used Avail Use% Mounted on
/dev/sda1           ext4    48G  4.5G   42G  10% /
tmpfs               tmpfs  495M  340K  495M   1% /dev/shm
/dev/mapper/vg0-lv0 ext4   976M  1.3M  908M   1% /luyun


6、扩展逻辑卷大小

[root@docker ~]# lvextend -L +500M /dev/vg0/lv0
Size of logical volume vg0/lv0 changed from 1.00 GiB (256 extents) to 1.49 GiB (381 extents).
Logical volume lv0 successfully resized

[root@docker ~]# resize2fs /dev/vg0/lv0
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv0 is mounted on /luyun; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv0 to 390144 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 390144 blocks long.

[root@docker ~]# lvs
LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
lv0  vg0  -wi-ao---- 1.49g  


7、压缩逻辑卷为800M

[root@docker ~]# umount /luyun

[root@docker ~]# fsck /dev/vg0/lv0     #检查逻辑卷里的数据
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/vg0-lv0: clean, 11/98304 files, 14756/390144 blocks

[root@docker ~]# e2fsck -f /dev/vg0/lv0        #检查文件系统大小
e2fsck 1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第3步: 检查目录连接性
Pass 4: Checking reference counts
第5步: 检查簇概要信息
/dev/vg0/lv0: 11/98304 files (0.0% non-contiguous), 14756/390144 blocks

[root@docker ~]# resize2fs /dev/vg0/lv0 800M      #把文件系统减小为800M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/lv0 to 204800 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 204800 blocks long.

[root@docker ~]# lvresize -L 800M /dev/vg0/lv0     #把逻辑卷减小为800M
WARNING: Reducing active logical volume to 800.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from 1.49 GiB (381 extents) to 800.00 MiB (200 extents).
Logical volume lv0 successfully resized

[root@docker ~]# lvs
LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
lv0  vg0  -wi-a----- 800.00m
注意:+800M:在原有的大小上再加上800M ; 300M:不管原来有多大,最后的大小就是300M


8、当卷组空间分完后,须重新添加个物理分区,把物理分区做成物理卷加入卷组

fdisk /dev/sda   #创建一个/dev/sda7     
partx -a /dev/sda
pvcreate /dev/sda7
vgextend  vg0 /dev/sda7    这样卷组的空间就会增大


9、删除逻辑卷、卷组、物理卷

1、先手动卸载挂载点,然后把/etc/fstab中的开机自动挂载删除,再重新加载挂载表mount -a,
2、lvremove /dev/vg0/*    卸载/dev/vg0/下所有的逻辑卷
3、vgremove vg0      卸载卷组vg0    ;   
4、pvremove /dev/sdb{12..14}      卸载物理卷


10、自动挂载逻辑卷脚本

device=`fdisk -l 2> /dev/null | grep "Disk /dev/" | sort | uniq -c | sort -nr | head -1 | awk '{print $3}' | awk -F: '{print $1}'`
pvcreate $device
vgcreate vg1 $device
vgfree=`vgdisplay | grep "Free" | awk '{print $5}'`
lvcreate -l $vgfree -n lv1 vg1
mkfs.ext4 -j /dev/vg1/lv1
tune2fs -c 0 -i 0 /dev/vg1/lv1
echo "/dev/vg1/lv1  /home  ext4  defaults 0  0 " >> /etc/fstab
mount -a


linux逻辑卷划分测试:http://www.linuxdiyf.com/linux/13542.html

Linux下的逻辑卷和GRUB:http://www.linuxdiyf.com/linux/6320.html

Linux LVM逻辑卷管理技术:http://www.linuxdiyf.com/linux/5343.html

Linux新增磁盘与LVM逻辑卷操作:http://www.linuxdiyf.com/linux/2145.html

Linux逻辑卷的创建及备份:http://www.linuxdiyf.com/linux/2064.html