LVM创建实例及相关注释

LVM创建    pv—>vg—->lv—->快照

创建前准备了四块1g硬盘分别为:sdb  sdc sdd  sde,并分别给四块盘划分了1G的空间,并指定了分区系统类型为8e(即Linux LVM

[root@localhost ~]# fdisk /dev/sdb

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-130, default 1): 

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +999M


Command (m for help): t  ##指定类型

Selected partition 1

Hex code (type L to list codes): 8e

Changed system type of partition 1 to 8e (Linux LVM)


Command (m for help): w  保存退出

The partition table has been altered!


然后依次对sdc  sdd  sde做一样的操作


[root@localhost ~]# fdisk -l|grep "^/dev/sd"

/dev/sda1   *           1          39      307200   83  Linux

/dev/sda2              39        2358    18631680   83  Linux

/dev/sda3            2358        2611     2031616   82  Linux swap / Solaris

/dev/sdb1               1         128     1028128+  8e  Linux LVM

/dev/sdc1               1         128     1028128+  8e  Linux LVM

/dev/sdd1               1         128     1028128+  8e  Linux LVM

/dev/sde1               1         128     1028128+  8e  Linux LVM

以上红色显示的是即将用于创建lvm的分区

First  创建pv

[root@localhost ~]# pvcreate /dev/sd{b,c,d,e}1     创建 PV

  Physical volume "/dev/sdb1" successfully created

  Physical volume "/dev/sdc1" successfully created

  Physical volume "/dev/sdd1" successfully created

  Physical volume "/dev/sde1" successfully created


[root@localhost ~]# pvs   查看pv

  PV         VG   Fmt  Attr PSize    PFree   

  /dev/sdb1       lvm2 a–  1004.03m 1004.03m

  /dev/sdc1       lvm2 a–  1004.03m 1004.03m

  /dev/sdd1       lvm2 a–  1004.03m 1004.03m

  /dev/sde1       lvm2 a–  1004.03m 1004.03m


[root@localhost ~]# pvdisplay  相对pvs来说显示的更详细,以下是/dev/sdb1的显示内容

  "/dev/sdb1" is a new physical volume of "1004.03 MiB"

  — NEW Physical volume —

  PV Name               /dev/sdb1

  VG Name               

  PV Size               1004.03 MiB

  Allocatable           NO

  PE Size               0   

  Total PE              0

  Free PE               0

  Allocated PE        0

  PV UUID               8etjmo-mVi0-ncst-Em9d-pMW0-z0Ol-q4fX8v


pvremove /dev/sdb1    即可删除

pvcreate  /dev/sdb1     则可再加进来


Second  创建pv后创建vg


[root@localhost ~]# vgcreate myvg /dev/sd{b,c,d}1      这里我们先加进来3块盘,另一块用于演示其他操作

  Volume group "myvg" successfully created

    如果要修改PE大小:可以使用-s参数   默认是4m  

查看

[root@localhost ~]# vgs

  VG   #PV #LV #SN Attr   VSize VFree

  myvg   3   0   0 wz–n- 2.93g 2.93g

[root@localhost ~]# vgdisplay     同样也比vgs显示的详细

  — Volume group —

  VG Name               myvg

  System ID             

  Format                lvm2

  Metadata Areas        3

  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                3

  Act PV                3

  VG Size               2.93 GiB

  PE Size               4.00 MiB

  Total PE              750

  Alloc PE / Size       0 / 0   

  Free  PE / Size       750 / 2.93 GiB

  VG UUID               hbiddu-b9z4-WTxe-CwYK-WrNu-J0Af-tgLWZR

[root@localhost ~]# vgextend myvg /dev/sde1    添加硬盘到vg  增加磁盘没什么风险

  Volume group "myvg" successfully extended

[root@localhost ~]# vgs   刚才是3个pv  现在4个且容量也增加了

  VG   #PV #LV #SN Attr   VSize VFree

  myvg   4   0   0 wz–n- 3.91g 3.91g

下面是移除磁盘,这是我没要考虑是否磁盘上有数据,如果有移到其他pv,然后移除

[root@localhost ~]# pvmove /dev/sdb1     ##移走数据

  No data to move for myvg

[root@localhost ~]# vgreduce myvg /dev/sdb1   ##mypv移除/dev/sdb1

  Removed "/dev/sdb1" from volume group "myvg"

[root@localhost ~]# vgs

  VG   #PV #LV #SN Attr   VSize VFree

  myvg   3   0   0 wz–n- 2.93g 2.93g

Third   创建逻辑卷lv,及扩展大小

[root@localhost ~]# vgs

  VG   #PV #LV #SN Attr   VSize VFree

  myvg   4   0   0 wz–n- 3.91g 3.91g

我们查看一共有不到4G的空间,创建的lv大小不能超过上面vg的大小

[root@localhost ~]# lvcreate -L 2G -n mylv myvg   ##在myvg上创建一个mylv的逻辑卷大小2G 

  Logical volume "mylv" created[root@localhost ~]# lvs   #查看

  LV   VG   Attr       LSize Pool Origin Data%  Move Log Cpy%Sync Convert

  mylv myvg -wi-a—– 2.00g                                             

[root@localhost ~]# lvdisplay  查看

  — Logical volume —

  LV Path                /dev/myvg/mylv

  LV Name                mylv

  VG Name                myvg

  LV UUID                PPPW3e-xAiz-CMNM-RV2o-i8H1-yfIB-9g4KG2

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain, 2015-06-06 21:24:36 -0700

  LV Status              available

  # open                 0

  LV Size                2.00 GiB

  Current LE             512

  Segments               3

  Allocation             inherit

  Read ahead sectors     auto

  – currently set to     256

  Block device           253:0

lv的访问路径:(此两者均为符号链接,指向的文件为/dev/dm-#)

    1./dev/VG_name/LV_name

        /dev/myvg/mylv

    2./dev/mapper/VG_name-LV_name

        /dev/mapper/myvg-mylv

[root@localhost ~]# ll /dev/dm-*

brw-rw—- 1 root disk 253, 0 Jun  6 21:24 /dev/dm-0

[root@localhost ~]# ll /dev/myvg/

lrwxrwxrwx 1 root root 7 Jun  6 21:24 mylv -> ../dm-0

[root@localhost ~]# ll /dev/mapper

lrwxrwxrwx 1 root root      7 Jun  6 21:24 myvg-mylv -> ../dm-0


lv创建好就是一个文件系统,我们就可以使用它了

[root@localhost ~]# mke2fs -t ext4 /dev/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

131072 inodes, 524288 blocks

26214 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=536870912

16 block groups

32768 blocks per group, 32768 fragments per group

8192 inodes per group

Superblock backups stored on blocks: 

32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            

Creating journal (16384 blocks): done

Writing superblocks and filesystem accounting information: done

格式化后创建目录挂载吧:

[root@localhost ~]# mkdir /mnt/mylv

[root@localhost ~]# mount /dev/myvg/mylv /mnt/mylv

[root@localhost ~]# mount

/dev/sda2 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)

/dev/sda1 on /boot type ext4 (rw)

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)

/dev/mapper/myvg-mylv on /mnt/mylv type ext4 (rw)

如果想要长期使用,命令挂载只是暂时的,还要添加到/etc/fstab文件中最后一行增加:

/dev/myvg/mylv         /mnt/mylv                ext4    defaults        0 0

pv总空间为3.91G,现在只划给mylv  2G空间,如果不够用可以扩展mylv

 1.  lvextend  扩展物理边界  

  [root@localhost ~]# lvextend -L 3G /dev/myvg/mylv   ###这里表示扩展到3G

  Extending logical volume mylv to 3.00 GiB

  Logical volume mylv successfully resized

[root@localhost ~]# df -h

Filesystem             Size  Used Avail Use% Mounted on

/dev/sda2               18G  2.9G   14G  18% /

tmpfs                  495M     0  495M   0% /dev/shm

/dev/sda1              291M   34M  242M  13% /boot

/dev/mapper/myvg-mylv  2.0G   67M  1.9G   4% /mnt/mylv

 2.resizefs   扩展逻辑边界

                    [root@localhost ~]# resize2fs /dev/myvg/mylv     ###这里表示扩展至物理边界大小

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/myvg/mylv is mounted on /mnt/mylv; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 1

Performing an on-line resize of /dev/myvg/mylv to 786432 (4k) blocks.

The filesystem on /dev/myvg/mylv is now 786432 blocks long.

[root@localhost ~]# df -h

Filesystem             Size  Used Avail Use% Mounted on

/dev/sda2               18G  2.9G   14G  18% /

tmpfs                  495M     0  495M   0% /dev/shm

/dev/sda1              291M   34M  242M  13% /boot

/dev/mapper/myvg-mylv  3.0G   67M  2.8G   3% /mnt/mylv

        缩减很危险!!!!!

        缩减要离线,首先卸载 umount /mnt/mylv

                1.先确定缩减的目录数据有多少,并确定对应的目标逻辑卷大小中有足够的空间可容纳原来的所有数据

                        [root@localhost ~]# du -sh /mnt/mylv

                        20K /mnt/mylv

                2.先减少文件系统,并要执行强制检测

                        e2fsck -f

[root@localhost ~]# e2fsck -f /dev/myvg/mylv

e2fsck 1.41.12 (17-May-2010)

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/myvg/mylv: 11/196608 files (0.0% non-contiguous), 29500/786432 blocks

                3.缩减边界

                        resize2fs device

[root@localhost ~]# resize2fs /dev/myvg/mylv 1G

resize2fs 1.41.12 (17-May-2010)

Resizing the filesystem on /dev/myvg/mylv to 262144 (4k) blocks.

The filesystem on /dev/myvg/mylv is now 262144 blocks long.

                4.缩减物理边界

                    lvreduce -L 1G /dev/myvg/mylv

[root@localhost ~]# lvreduce -L 1G /dev/myvg/mylv

  WARNING: Reducing active logical volume to 1.00 GiB

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce mylv? [y/n]: y

  Reducing logical volume mylv to 1.00 GiB

  Logical volume mylv successfully resized

[root@localhost ~]# mount /dev/myvg/mylv /mnt/mylv

[root@localhost ~]# df -h

Filesystem             Size  Used Avail Use% Mounted on

/dev/sda2               18G  2.9G   14G  18% /

tmpfs                  495M     0  495M   0% /dev/shm

/dev/sda1              291M   34M  242M  13% /boot

/dev/mapper/myvg-mylv 1008M   67M  891M   7% /mnt/mylv

  快照卷:只能读,不能写

   

注意:快照卷是对某逻辑卷

[root@localhost ~]# mount -o remount,ro /dev/myvg/mylv /mnt/mylv  ##先改成只读

[root@localhost ~]# lvcreate -L 1G -n mylv-snap -p r -s /dev/myvg/mylv    ##对mylv做快照为mylv-snap 大小为1G

[root@localhost ~]# mount -o remount,rw /dev/myvg/mylv /mnt/mylv   #快照完,立刻改为读写或者一个命令行直接执行者三个命令,或者写脚本

  Logical volume "mylv-snap" created


[root@localhost ~]# mount /dev/myvg/mylv-snap /mnt/snap/   挂载快照

mount: block device /dev/mapper/myvg-mylv–snap is write-protected, mounting read-only


如果不想用了,要删除怎么办??

     umount /mnt/snap

     lvremove /dev/myvg/mylv-snap

     

     另外还有:pvremove     vgremove等删除命令,可以用man 查看

原创文章,作者:comk,如若转载,请注明出处:http://www.178linux.com/5104