本文承接上文继续叙述LVM的相关知识。
二)VG的创建和管理
1)VG的创建
VG的创建命令是vgcreate,其后接vg的名字跟要添加到此vg的pv。vg默认的PE为4MB,可以用-s参数指定PE大小。
[root@server ~]# vgcreate myvg /dev/sd[bc][12] #创建VG,名字为myvg Volume group "myvg" successfully created [root@server ~]# vgs VG #PV #LV #SN Attr VSize VFree myvg 4 0 0 wz--n- 80.03g 80.03g
vg创建完成后可以用vgs或者vgdisplay查看。
2)VG的移除
VG的移除命令式vgremove,
[root@server ~]# vgremove myvg Volume group "myvg" successfully removed [root@server ~]# vgs No volume groups found
3)VG的扩容
VG扩容使用vgextend,
[root@server ~]# vgextend myvg /dev/sdb3 No physical volume label read from /dev/sdb3 Physical volume "/dev/sdb3" successfully created Volume group "myvg" successfully extended
4)将PV从VG中移除
移除VG中的PV使用vgreduce,
[root@server ~]# vgreduce myvg /dev/sdb3 Removed "/dev/sdb3" from volume group "myvg"
三)LV的创建和管理
1)LV的创建
LV创建的命令式lvcreate
[root@server ~]# lvcreate -L 20G -n mylv myvg #-L指定大小,-l可以指定pe的数量,-n指定lv的名字 Logical volume "mylv" created [root@server ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mylv myvg -wi-a---- 20.00g
2)LV的查看
查看LV的命令有:lvs,lvdisplay,lvscan
[root@server ~]# lvscan ACTIVE '/dev/myvg/mylv' [20.00 GiB] inherit [root@server ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert mylv myvg -wi-a---- 20.00g [root@server ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv LV Name mylv VG Name myvg LV UUID tEAyGV-0trG-fqIb-SiVl-3KVj-9w9D-ivIXlR LV Write Access read/write LV Creation host, time server.domain2.example.com, 2014-01-12 19:32:30 -0800 LV Status available # open 0 LV Size 20.00 GiB Current LE 10240 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
3)格式化LV
格式化逻辑卷的命令跟格式化硬盘的完全一样,这里用mkfs介绍
[root@server ~]# mkfs.ext4 /dev/mapper/myvg-mylv mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes, 5242880 blocks 262144 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 160 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, 1605632, 2654208, 4096000 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 26 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
4)LV挂载
使用mount命令
[root@server ~]# mount /dev/mapper/myvg-mylv /root/mylv/ [root@server ~]# mount /dev/sda3 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/sda1 on /boot type ext4 (rw) /dev/sr0 on /mnt/media type iso9660 (ro,loop=/dev/loop0) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other) gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev) /dev/mapper/myvg-mylv on /root/mylv type ext4 (rw)#已挂载
5)增加或减小LV大小
增加使用lvextend,
[root@server ~]# lvextend -L +10G /dev/mapper/myvg-mylv Extending logical volume mylv to 30.00 GiB Logical volume mylv successfully resized [root@server ~]# resize2fs /dev/mapper/myvg-mylv #使其立即生效 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/mapper/myvg-mylv is mounted on /root/mylv; on-line resizing required old desc_blocks = 2, new_desc_blocks = 2 Performing an on-line resize of /dev/mapper/myvg-mylv to 7864320 (4k) blocks. The filesystem on /dev/mapper/myvg-mylv is now 7864320 blocks long.
减小使用lvreduce命令。缩减LV有风险,慎用。
[root@server ~]# lvreduce -L -20G /dev/mapper/myvg-mylv WARNING: Reducing active and open logical volume to 10.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y/n]: y Reducing logical volume mylv to 10.00 GiB Logical volume mylv successfully resized
6)LV删除
LV删除使用lvremove命令
[root@server ~]# umount mylv/ #删除之前先从文件系统卸载 [root@server ~]# lvremove /dev/mapper/myvg-mylv Do you really want to remove active logical volume mylv? [y/n]: y Logical volume "mylv" successfully removed