磁盤管邏輯卷(LVM)

磁盤管理邏輯卷(LVM)

邏輯卷(Logical Volume)是由邏輯磁盤形成的虛擬盤,也可稱爲磁盤分區。

邏輯卷(lvm)允許對捲進行方便操作的抽象層,包括重新設定文件系統的大小 ,允許在多個物理設備間重新組織文件系統 ,將一個或多個底層設備組成一個邏輯設備的模塊 。物理卷創建邏輯卷是由物理區域(PE)組成。通關過交換PE來進行資料的轉換,將原來LV內的PE轉移到其他的 設備中以降低LV的容量,或將其他設備中的PE加到LV中以加大容量,彈性的更改文件了系統容量。

磁盤管邏輯卷(LVM)

設備名:/dev/dm-#
軟鏈接:
/dev/mapper/VGname/LVname
/dev/mapper/vol0-root
/dev/VGname/LVname
/dev/vol0/root
查詢設備名或鏈接命令

ll /dev/dm*   #查看lv2801邏輯卷名字

        brw-rw----. 1 root disk 253, 0 Feb 24 09:35 /dev/dm-0
ll /dev/mapper/vgmagedu-lv2801 #查看lv2801邏輯卷名字

        lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/mapper/vgmagedu-lv2801 -> ../dm-0
ll /dev/testvg/*  #查看lv2801邏輯卷名字

        lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/vgmagedu/lv2801 -> ../dm-0

pv管理工具

顯示物理卷信息

pvscan  簡單查看創建pvcreate的信息
pvs   簡單查看創建pvcreate的信息
pvdisplay   詳細查看創建pvcreate的信息

[root@centos6 ~]#pvs  -------> 簡單查看創建pvcreate的信息
    PV         VG     Fmt  Attr PSize  PFree
    /dev/sdp   testvg lvm2 a--u  9.98g 9.98g
    /dev/sdq   testvg lvm2 a--u 10.98g 5.98g
    [root@centos6 ~]#pvscan   ------->簡單查看創建pvcreate的信息 
    PV /dev/sdq   VG testvg          lvm2 [10.98 GiB / 5.98 GiB free]
    PV /dev/sdp   VG testvg          lvm2 [9.98 GiB / 9.98 GiB free]
    Total: 2 [20.97 GiB] / in use: 2 [20.97 GiB] / in no VG: 0 [0   ]
    [root@centos6 ~]#pvdisplay   ------>詳細查看創建pvcreate的信息
--- Physical volume ---
PV Name               /dev/sdq
VG Name               testvg
PV Size               11.00 GiB / not usable 16.00 MiB
Allocatable           yes
PE Size               16.00 MiB
Total PE              703
Free PE               383
Allocated PE          320
PV UUID               ZtmVQG-U43k-nEg8-ndK3-AOAO-3Uti-AC3HXO

--- Physical volume ---
    PV Name               /dev/sdp
VG Name               testvg
PV Size               10.00 GiB / not usable 16.00 MiB
Allocatable           yes
PE Size               16.00 MiB
Total PE              639
Free PE               639
Allocated PE          0
PV UUID               2F7Jxh-4acs-2HX6-jGrh-S7f2-lnKo-Yfnc49

創建物理卷

pvcreate /dev/sda9   // 在 硬盤頭部信息中添加成LVM格式

[root@centos6 ~]#pvcreate /dev/sdr   ----->創建物理卷(pv)
        Physical volume "/dev/sdr" successfully created
    [root@centos6 ~]#pvs   -------> 簡單查看創建pvcreate的信息
        PV         VG     Fmt  Attr PSize   PFree
        /dev/sdb          lvm2 ----   2.00g   2.00g
        /dev/sdp   testvg lvm2 a--u   9.98g   9.98g
        /dev/sdq   testvg lvm2 a--u  10.98g   5.98g
/dev/sdr          lvm2 ---- 512.00m 512.00m*

刪除物理卷

        pvremove /dev/sda9 /dev/md1   //刪除頭部的LVM格式信息即刪除物理卷pv

    [root@centos6 ~]#pvremove /dev/sdr    ---->刪除物理卷
        Labels on physical volume "/dev/sdr" successfully wiped
    [root@centos6 ~]#pvs    -------> 簡單查看創建pvcreate的信息
        PV         VG     Fmt  Attr PSize  PFree
        /dev/sdb          lvm2 ----  2.00g 2.00g
        /dev/sdp   testvg lvm2 a--u  9.98g 9.98g
        /dev/sdq   testvg lvm2 a--u 10.98g 5.98g

以二進制方式查看

hexdump -C -n 10240 /dev/sdq    //以二進制方式查看硬盤前1024字節信息

        [root@centos6 ~]#hexdump -C -n 1024 /dev/sdb
    00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000200  4c 41 42 45 4c 4f 4e 45  01 00 00 00 00 00 00 00  |LABELONE........|
    00000210  fe f1 4c 80 20 00 00 00  4c 56 4d 32 20 30 30 31  |..L. ...LVM2 001|
    00000220  33 36 46 38 75 50 39 50  41 74 58 6c 67 6a 4d 65  |36F8uP9PAtXlgjMe|
    00000230  74 52 44 71 33 35 32 43  6f 78 78 79 75 53 6a 43  |tRDq352CoxxyuSjC|
    00000240  00 00 00 80 00 00 00 00  00 00 10 00 00 00 00 00  |................|
    00000250  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000260  00 00 00 00 00 00 00 00  00 10 00 00 00 00 00 00  |................|
    00000270  00 f0 0f 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    00000280  00 00 00 00 00 00 00 00  02 00 00 00 00 00 00 00  |................|
    00000290  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00000400

vg管理工具

顯示卷組

    vgs    -------> 簡單查看邏輯卷組(vg)的信息
    vgscan   -------> 簡單查看邏輯卷組(vg)的信息
    vgdisplay   -------> 詳細查看邏輯卷組(vg)的信息

[root@centos6 ~]#vgs  -------> 簡單查看邏輯卷組(vg)的信息
    VG     #PV #LV #SN Attr   VSize  VFree
    testvg   2   3   2 wz--n- 20.97g 15.97g
[root@centos6 ~]#vgscan   -------> 簡單查看邏輯卷組(vg)的信息
    Reading all physical volumes.  This may take a while...
    Found volume group "testvg" using metadata type lvm2
[root@centos6 ~]#vgdisplay   -------> 詳細查看邏輯卷組(vg)的信息
    --- Volume group ---
    VG Name               testvg
    System ID
    Format                lvm2
    Metadata Areas        2
    Metadata Sequence No  11
    VG Access             read/write
    VG Status             resizable
    MAX LV                0
    Cur LV                3
    Open LV               0
    Max PV                0
    Cur PV                2
    Act PV                2
    VG Size               20.97 GiB
    PE Size               16.00 MiB
    Total PE              1342
    Alloc PE / Size       320 / 5.00 GiB
    Free  PE / Size       1022 / 15.97 GiB
    VG UUID               ZTCj4I-vEja-SKe2-3EcL-Prme-iqLe-Cmix8v

創建卷組

    vgcreate vgmagedu /dev/sda9
    vgcreate -s 16M  卷組名(vg)   磁盤   #指定PE大小爲16MB

        [root@centos6 ~]#vgcreate vg1 /dev/sdr
    Volume group "vg1" successfully created
[root@centos6 ~]#vgs
    VG     #PV #LV #SN Attr   VSize   VFree
    testvg   2   3   2 wz--n-  20.97g  15.97g
    vg1      1   0   0 wz--n- 508.00m 508.00m

管理卷組()

**在vg裏添加額外的pv

        vgextend testvg /dev/sdq   //增加捲組大小

[root@centos6 ~]#vgextend vg1  /dev/sdq
    Volume group "vg1" successfully extended
[root@centos6 ~]#vgs
    VG   #PV #LV #SN Attr   VSize  VFree
    vg1    2   0   0 wz--n- 11.49g 11.49g

**在vg內移除pv

        語法:
        vgreduce  vg名  pv名 

            [root@centos6 ~]#vgreduce vg1 /dev/sdq
            Removed "/dev/sdq" from volume group "vg1"
            [root@centos6 ~]#vgs
            VG   #PV #LV #SN Attr   VSize   VFree
            vg1    1   0   0 wz--n- 508.00m 508.00m

刪除一個vg!卷組

        語法:
        vgremove      刪除卷組
        先做pvmove,再做vgremove

[root@centos6 ~]#vgremove vg1
    Volume group "vg1" successfully removed
[root@centos6 ~]#vgs
[root@centos6 ~]#

修改卷組名

語法:
vgrename 源vg名     新vg名

[root@centos6 ~]#vgrename vg1 vg2    
    Volume group "vg1" successfully renamed to "vg2"
[root@centos6 ~]#vgs
    VG   #PV #LV #SN Attr   VSize   VFree
    vg2    1   0   0 wz--n- 508.00m 508.00m

vgreduce 卷組 分區 ---刪除卷組中的邏輯捲來縮小卷組大小,不可以刪除全部

將某個pv從vg中刪除,但該pv包含數據

        pvmove   搬移PV中的資料(只限於同一VG中)
        語法:
            pvmove  源pv    [目標pv]

    1.pvmove /dev/sda9 [/dev/md1]    //目標pv可以不添加
        [root@centos7 123]# pvs     //搬移數居前pv大小
    PV         VG  Fmt  Attr PSize    PFree
    /dev/sda6  vg5 lvm2 a--  1020.00m 1020.00m
    /dev/sdb   vg5 lvm2 a--    <2.00g   <1.51g
[root@centos7 3333]# pvmove /dev/sdb /dev/sda6         //搬移數據/dev/sdb 到/dev/sda6
    /dev/sdb: Moved: 2.40%
    /dev/sdb: Moved: 100.00%
[root@centos7 3333]# pvs     //搬移數居後pv大小
    PV         VG  Fmt  Attr PSize    PFree
    /dev/sda6  vg5 lvm2 a--  1020.00m 520.00m
    /dev/sdb   vg5 lvm2 a--    <2.00g  <2.00g

        2.vgreduce vgmagedu /dev/sda9     //刪除vg中的pv
        3.pvremove /dev/sda9      //刪除pv

跨主機遷移卷組

    vgchange -an vgtest # -an 關掉不在激活
    vgexport vgtest # 設置爲被導出狀態
    vgimport vgtest   #設置爲導入狀態
    vgchange -ay  #-ay 設置爲激活狀態

lv管理工具

顯示邏輯卷

    lvs    -------> 簡單查看邏輯卷(lv)的信息
    lvscan   -------> 簡單查看邏輯卷(lv)的信息
    Lvdisplay   -------> 詳細查看邏輯卷(lv)的信息

[root@centos6 ~]#lvs     -------> 簡單查看邏輯卷(lv)的信息
    LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv1  vg2  -wi-a----- 52.00m                                                   
[root@centos6 ~]#lvscan     -------> 簡單查看邏輯卷(lv)的信息
    ACTIVE            '/dev/vg2/lv1' [52.00 MiB] inherit
[root@centos6 ~]#lvdisplay     -------> 詳細查看邏輯卷(lv)的信息
    --- Logical volume ---
    LV Path                /dev/vg2/lv1
    LV Name                lv1
    VG Name                vg2
    LV UUID                sKWovu-PhYU-2QeD-RZz5-UI5B-XsJY-iVI0cd
    LV Write Access        read/write
    LV Creation host, time centos6.magedu.com, 2018-02-28 18:05:59 -0500
    LV Status              available
    \# open                 0
    LV Size                52.00 MiB
    Current LE             13
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     256
    Block device           253:0

創建邏輯卷

    lvcreate -L 55M -n lv2801 vgmagedu  #-L 指定大小
    lvcreate -l 10 -n lv2802 vgmagedu #指定PE 個數
    lvcreate -l +100%FREE -n lv2803 vgmagedu #剩餘全部分配

[root@centos6 ~]#lvcreate -L 50M -n lv1 vg2
    Rounding up size to full physical extent 52.00 MiB
    Logical volume "lv1" created.
[root@centos6 ~]#lvs
    LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv1  vg2  -wi-a----- 52.00m     

增加邏輯卷lv容量

    lvextend -L 200M /dev/vgmagedu/lv2801 -r

減小邏輯卷lv容量

    lvreduce -L 100M /dev/vgmage01/lv2801

刪除一個邏輯卷lv

    lvremove: lvremove /dev/VG_NAME/LV_NAME

對邏輯卷lv進行容量大小的調整(增加|減小)

    lvresize -L [-|+]100M /dev/vgmage01/lv2801

查詢指定邏輯卷名字

ll /dev/dm*   #查看lv2801邏輯卷名字
        brw-rw----. 1 root disk 253, 0 Feb 24 09:35 /dev/dm-0
ll /dev/mapper/vgmagedu-lv2801 #查看lv2801邏輯卷名字
        lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/mapper/vgmagedu-lv2801 -> ../dm-0
ll /dev/testvg/*  #查看lv2801邏輯卷名字
        lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/vgmagedu/lv2801 -> ../dm-0
格式化邏輯卷,創建文件系統
    mkfs.ext4 /dev/vgmagedu/lv2801 
掛載邏輯卷
    mount /dev/vgmagedu/lv2801 /users/ 

擴展和縮減邏輯卷

擴展邏輯卷

ext4文件系統

1,如果卷組空間不夠擴展需求,應先擴展卷組

    vgextend vgmagedu /dev/md1 (如沒有可用pv,應該先創建pv)
    lvextend /dev/vgmagedu/lv2801 -L 100M #擴展邏輯卷-L 100M 指定邏輯捐卷最終大小
    lvextend /dev/vgmagedu/lv2801 -L +100M ##擴展邏輯卷-L +100M 指定邏輯捐卷大小再加100M
    resize2fs /dev/vgmagedu/lv2801 #重製文件系統大小(可以不指定大小默認全部分配)

[root@centos6 ~]#vgs   -->查看空間夠lv擴展
    VG   #PV #LV #SN Attr   VSize  VFree
    vg2    2   1   0 wz--n- 11.49g 10.42g
        [root@centos6 ~]#lvs   -->擴展前
    LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv1  vg2  -wi-ao---- 900.00m                                                  
[root@centos6 ~]#lvextend /dev/vg2/lv1 -L +200M    --->擴展200M
    Size of logical volume vg2/lv1 changed from 900.00 MiB (225 extents) to 1.07 GiB (275 extents).
    Logical volume lv1 successfully resized.
[root@centos6 ~]#lvs     -->擴展後
    LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv1  vg2  -wi-ao---- 1.07g
[root@centos6 ~]#resize2fs /dev/vg2    --->重製文件系統大小(可以不指定大小默認全部分配)
resize2fs 1.41.12 (17-May-2010)
open: Is a directory while opening /dev/vg2
[root@centos6 ~]#resize2fs /dev/vg2/lv1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg2/lv1 is mounted on /media; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 5
Performing an on-line resize of /dev/vg2/lv1 to 1126400 (1k) blocks.
The filesystem on /dev/vg2/lv1 is now 1126400 blocks long.
[root@centos6 ~]#df -h   -->查看文件系統,擴展出來了
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             20G  4.6G   15G  25% /
tmpfs                491M   72K  491M   1% /dev/shm
/dev/sda3             20G  196M   19G   2% /app
/dev/sda1            190M   35M  146M  20% /boot
/dev/md127           991M  1.3M  939M   1% /backup
/dev/md5             2.0G  3.0M  1.9G   1% /mydata
/dev/md3             5.8G   12M  5.5G   1% /magedata
/dev/mapper/vg2-lv1  1.1G  2.2M 1007M   1% /media

2,如果卷組空間足夠,直接擴展

        lvextend -L 200M /dev/vgmagedu/lv2801 -r 
        (加上-r一步到位,即擴展邏輯卷,同時擴展文件系統大小)

        [root@centos6 ~]#df  ----->擴展前
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/sda2            20511356 4745176  14717604  25% /
tmpfs                  502056      72    501984   1% /dev/shm
/dev/sda3            20511356  200504  19262276   2% /app
/dev/sda1              194241   35157    148844  20% /boot
/dev/md127            1014680    1284    961020   1% /backup
/dev/md5              2029392    3072   1921568   1% /mydata
/dev/md3              6049536   12280   5723300   1% /magedata
/dev/mapper/vg2-lv1     47463     842     43959   2% /media

[root@centos6 ~]#lvextend /dev/vg2/lv1 -L 800M -r  --->擴展 直接同步到文件系統中
    Size of logical volume vg2/lv1 changed from 52.00 MiB (13 extents) to 800.00 MiB (200 extents).
    Logical volume lv1 successfully resized.
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg2-lv1 is mounted on /media; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 4
Performing an on-line resize of /dev/mapper/vg2-lv1 to 819200 (1k) blocks.
The filesystem on /dev/mapper/vg2-lv1 is now 819200 blocks long.

[root@centos6 ~]#df   --->擴展後
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/sda2            20511356 4745184  14717596  25% /
tmpfs                  502056      72    501984   1% /dev/shm
/dev/sda3            20511356  200504  19262276   2% /app
/dev/sda1              194241   35157    148844  20% /boot
/dev/md127            1014680    1284    961020   1% /backup
/dev/md5              2029392    3072   1921568   1% /mydata
/dev/md3              6049536   12280   5723300   1% /magedata
/dev/mapper/vg2-lv1    791053    2054    748300   1% /media

xfs文件系統

    對於xfs文件系統,也可直接在lvextend中直接用-r,一步到位,如果要分開操作,先擴展邏輯卷,再拉伸文件系統,則應該使用xfs_growfs如下
    xfs_growfs /dev/vgmagedu/lv2801  //(加入後動態加入要需要使用xfs_growfs動態收縮磁盤)
    原因是:
    lv使用的系統是xfs,resize2fs  是針對 ext2/ext3/ext4 file system resizer,命令用在lvm中動態收縮磁盤。解決方法: 使用xfs_growfs 這個命令代替。

減小邏輯卷

1.umount  卸載掛載
2.e2fsck -f /dev/vgmage01/lv2801  進行掃描
3.resize2fs /dev/vgmage01/lv2801 100M  先減文件系統
4.lvreduce -L 100M /dev/vgmage01/lv2801 再減邏輯卷
5.mount  最後掛載

[root@centos6 ~]#df -h   -->縮小前查看文件系統
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             20G  4.6G   15G  25% /
tmpfs                491M   72K  491M   1% /dev/shm
/dev/sda3             20G  196M   19G   2% /app
/dev/sda1            190M   35M  146M  20% /boot
/dev/md127           991M  1.3M  939M   1% /backup
/dev/md5             2.0G  3.0M  1.9G   1% /mydata
/dev/md3             5.8G   12M  5.5G   1% /magedata
/dev/mapper/vg2-lv1  1.1G  2.2M 1007M   1% /media
[root@centos6 ~]#lvs  --->縮小前超看lv
    LV   VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv1  vg2  -wi-ao---- 1.07g
[root@centos6 ~]#umount /media   ----->>卸載掛載
[root@centos6 ~]#e2fsck  -f /dev/vg2/lv1   ---->進行掃描
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/vg2/lv1: 11/262752 files (9.1% non-contiguous), 39530/1126400 blocks

[root@centos6 ~]#resize2fs /dev/vg2/lv1 500M   ---->先減文件系統500M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg2/lv1 to 512000 (1k) blocks.
The filesystem on /dev/vg2/lv1 is now 512000 blocks long.

[root@centos6 ~]#lvreduce /dev/vg2/lv1 -L 500M   -----> 再減邏輯卷500M
    WARNING: Reducing active logical volume to 500.00 MiB. 
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg2/lv1? [y/n]: y
    Size of logical volume vg2/lv1 changed from 1.07 GiB (275 extents) to 500.00 MiB (125 extents).
    Logical volume lv1 successfully resized.
[root@centos6 ~]#lvs    ------>查看邏輯卷lv
    LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv1  vg2  -wi-a----- 500.00m                                                  
[root@centos6 ~]#mount /dev/vg2/lv1 /media/     --->掛載邏輯卷
[root@centos6 ~]#df -h     ------>查看文件系統
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             20G  4.6G   15G  25% /
tmpfs                491M   72K  491M   1% /dev/shm
/dev/sda3             20G  196M   19G   2% /app
/dev/sda1            190M   35M  146M  20% /boot
/dev/md127           991M  1.3M  939M   1% /backup
/dev/md5             2.0G  3.0M  1.9G   1% /mydata
/dev/md3             5.8G   12M  5.5G   1% /magedata
/dev/mapper/vg2-lv1  482M  1.9M  455M   1% /media

減小邏輯卷(錯誤示例CentOS5)

centos6和7中拒絕此錯誤操作
centos5 拓展分區爲用戶家目錄掛載目錄
    格式化完成後進入/etc/fstab文件中編輯一條dev/vgmage01/lvhome的開機檢測自動掛載信息
    lvreduce -L 100M /dev/vgmage01/lvhome #直接減小邏輯卷
    resize2fs /dev/vgmage01/lvhome 100M #報錯
    e2fsck -f /dev/vgmage01/lvhome #報錯
    reboot
-----修復過程
    登錄用戶名密碼
    當前啓動掛載磁盤以只讀方式掛載,需要掛載磁盤變爲可讀寫方式
    mount -o remount.rw /
    在編輯/etc/fstab文件
    vim /etc/fstab
    將故障硬盤對用最後的 1 2 改成 1 0 
    reboot
    系統可以正常啓動了 
 -----用正常順序減小邏輯卷
    lvs查看邏輯卷大小
    lvextend -L 200M -r /dev/vgmage01/lvhome #恢復減少邏輯卷大小
    1.umount  卸載掛載
    2.e2fsck -f /dev/vgmage01/lvhome  進行掃描
    3.resize2fs /dev/vgmage01/lvhome 100M  先減文件系統
    4.lvreduce -L 100M /dev/vgmage01/lvhome 再減邏輯卷
    5.mount  最後掛載

遷移卷組到新系統試驗

將包含lvm的磁盤遷移到其他主機
要先添加一個硬盤後創建爲邏輯卷,

mount /dev/vgtest/lvtest/ /mnt/datatest/

1.umount /mnt/datatest/ #卸載
2.vgchange -an vgtest # -an 關掉不在激活
3.vgexport vgtest # 設置爲被導出狀態
4.遷移硬盤
5.在新的主機 pvscan 或 vgs
6.vgimport vgtest #設置爲導入狀態
7.vgchange -ay #-ay 設置爲激活狀態

創建遷移前狀態
[root@centos6 ~]#mount /dev/vg2/lv1  /mnt/test/   ----->掛載邏輯卷
[root@centos6 ~]#cp /etc/passwd /mnt/test/   --->複製文件到掛載點
[root@centos6 ~]#ll /mnt/test    --->查看掛載點文件
total 16
drwx------. 2 root root 12288 Feb 28 18:10 lost+found
-rw-r--r--. 1 root root  2195 Feb 28 19:21 passwd

[root@centos6 ~]#lvdisplay /dev/vg2/lv1     ------>查看邏輯捲來自那個邏輯卷組
    --- Logical volume ---
    LV Path                /dev/vg2/lv1
    LV Name                lv1
    VG Name                vg2
    LV UUID                sKWovu-PhYU-2QeD-RZz5-UI5B-XsJY-iVI0cd
    LV Write Access        read/write
    LV Creation host, time centos6.magedu.com, 2018-02-28 18:05:59 -0500
    LV Status              available
    \# open                 0
    LV Size                500.00 MiB
    Current LE             125
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     256
    Block device           253:1

[root@centos6 ~]#pvdisplay         ----->查看pv被那個vg使用
    --- Physical volume ---
    PV Name               /dev/sdb
    VG Name               vg5
    PV Size               2.00 GiB / not usable 4.00 MiB
    Allocatable           yes
    PE Size               4.00 MiB
    Total PE              511
    Free PE               386
    Allocated PE          125
    PV UUID               KQeIVz-Hp1X-e3jK-ivO1-VpTZ-vM86-K2an9a

    --- Physical volume ---
    PV Name               /dev/sdr
    VG Name               vg2
    PV Size               512.00 MiB / not usable 4.00 MiB
    Allocatable           yes
    PE Size               4.00 MiB
    Total PE              127
    Free PE               2
    Allocated PE          125
    PV UUID               Bcm38a-IvoA-RsIy-Kfhs-1Gqn-sRVZ-PPS8nk

    --- Physical volume ---
    PV Name               /dev/sdq
    VG Name               vg2
    PV Size               11.00 GiB / not usable 4.00 MiB
    Allocatable           yes
    PE Size               4.00 MiB
    Total PE              2815
    Free PE               2815
    Allocated PE          0
    PV UUID               ZtmVQG-U43k-nEg8-ndK3-AOAO-3Uti-AC3HXO

    "/dev/sdp" is a new physical volume of "10.00 GiB"
    --- NEW Physical volume ---
    PV Name               /dev/sdp
    VG Name
    PV Size               10.00 GiB
    Allocatable           NO
    PE Size               0
    Total PE              0
    Free PE               0
    Allocated PE          0
    PV UUID               2F7Jxh-4acs-2HX6-jGrh-S7f2-lnKo-Yfnc49

把/dev/sdq上的數據遷移至/dev/sdb上

[root@centos6 ~]#pvmove  /dev/sdq /dev/sdb
    Physical Volume "/dev/sdb" not found in Volume Group "vg2".
[root@centos6 ~]#vgreduce  vg2 /dev/sdq
    Removed "/dev/sdq" from volume group "vg2"
[root@centos6 ~]#pvremove /dev/sdq   ---->   刪除pv /dev/sdq
    Labels on physical volume "/dev/sdq" successfully wiped
[root@centos6 ~]#pvdisplay     --->查看pv被那個vg使用
    --- Physical volume ---
    PV Name               /dev/sdr
    VG Name               vg2
    PV Size               512.00 MiB / not usable 4.00 MiB
    Allocatable           yes
    PE Size               4.00 MiB
    Total PE              127
    Free PE               2
    Allocated PE          125
    PV UUID               Bcm38a-IvoA-RsIy-Kfhs-1Gqn-sRVZ-PPS8nk

    --- Physical volume ---
    PV Name               /dev/sdb
    VG Name               vg5
    PV Size               2.00 GiB / not usable 4.00 MiB
    Allocatable           yes
    PE Size               4.00 MiB
    Total PE              511
    Free PE               386
    Allocated PE          125
    PV UUID               KQeIVz-Hp1X-e3jK-ivO1-VpTZ-vM86-K2an9a

    "/dev/sdp" is a new physical volume of "10.00 GiB"
    --- NEW Physical volume ---
    PV Name               /dev/sdp
    VG Name
    PV Size               10.00 GiB
    Allocatable           NO
    PE Size               0
    Total PE              0
    Free PE               0
    Allocated PE          0
    PV UUID               2F7Jxh-4acs-2HX6-jGrh-S7f2-lnKo-Yfnc49

卸載

[root@centos6 ~]#umount /media/
[root@centos6 ~]#df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  4.6G   15G  25% /
tmpfs           491M   72K  491M   1% /dev/shm
/dev/sda3        20G  196M   19G   2% /app
/dev/sda1       190M   35M  146M  20% /boot
/dev/md127      991M  1.3M  939M   1% /backup
/dev/md5        2.0G  3.0M  1.9G   1% /mydata
/dev/md3        5.8G   12M  5.5G   1% /magedata
2.vgchange -an vgtest   # -an 關掉不在激活
3.vgexport vgtest   # 設置爲被導出狀態

[root@centos6 ~]#vgchange -an vg2       ------>  -an 關掉不在激活
    0 logical volume(s) in volume group "vg2" now active
    [root@centos6 ~]#vgex            ----># 設置爲被導出狀態
vgexport  vgextend  
[root@centos6 ~]#vgexport vg2
    Volume group "vg2" successfully exported

4.遷移硬盤
5.虛擬機可以通過這個命令系統從新讀取硬盤

     echo '- - -' > /sys/class/scsi_host/host0/scan
     echo '- - -' > /sys/class/scsi_host/host1/scan
     echo '- - -' > /sys/class/scsi_host/host2/scan

5.在新的主機 pvscan 或 vgs 
6.vgimport vgtest   #設置爲導入狀態
7.vgchange -ay  #-ay 設置爲激活狀態

[root@centos7 ~]# vgs
    VG  #PV #LV #SN Attr   VSize  VFree
    vg5   1   1   0 wz--n- <2.00g <1.51g
[root@centos7 ~]# pvscan
    PV /dev/sdb   VG vg5             lvm2 [<2.00 GiB / <1.51 GiB free]
    Total: 1 [<2.00 GiB] / in use: 1 [<2.00 GiB] / in no VG: 0 [0   ]

[root@centos7 ~]# vgimport vg5
    Volume group "vg5" is not exported

[root@centos7 ~]# vgchange -ay
    1 logical volume(s) in volume group "vg5" now active
[root@centos7 ~]# mount /dev/vg5/lv5 /media/

創建邏輯卷示例

[root@centos6 ~]#pvcreate /dev/sdb    ----->創建pv
    Physical volume "/dev/sdb" successfully created
[root@centos6 ~]#pvs    ---->查看pv
    PV         VG   Fmt  Attr PSize   PFree
    /dev/sdb        lvm2 ----   2.00g  2.00g

[root@centos6 ~]#vgcreate vg5 /dev/sdb   --->創建vg
    Volume group "vg5" successfully created   --->查看vg
[root@centos6 ~]#vgs
    VG   #PV #LV #SN Attr   VSize  VFree
    vg5    1   0   0 wz--n-  2.00g  2.00g

[root@centos6 ~]#lvcreate -L 500M -n lv5 vg5   --->創建lv
    Logical volume "lv5" created.
[root@centos6 ~]#lvs       --->查看lv
    Volume group vg2 is exported
    LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv5  vg5  -wi-a----- 500.00m     
查詢指定邏輯卷名字
    ll /dev/dm*   #查看lv2801邏輯卷名字
            brw-rw----. 1 root disk 253, 0 Feb 24 09:35 /dev/dm-0
    ll /dev/mapper/vgmagedu-lv2801 #查看lv2801邏輯卷名字
            lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/mapper/vgmagedu-lv2801 -> ../dm-0
    ll /dev/testvg/*  #查看lv2801邏輯卷名字
            lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/vgmagedu/lv2801 -> ../dm-0
[root@centos6 ~]#mkfs.ext4 /dev/vg5/lv5   -->格式化  創建文件系統
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks:
                8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@centos6 ~]#mount /dev/vg5/lv5 /media/    --->掛載
[root@centos6 ~]#df -h     ----->查看文件xit
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             20G  4.6G   15G  25% /
tmpfs                491M   72K  491M   1% /dev/shm
/dev/sda3             20G  196M   19G   2% /app
/dev/sda1            190M   35M  146M  20% /boot
/dev/md127           991M  1.3M  939M   1% /backup
/dev/md5             2.0G  3.0M  1.9G   1% /mydata
/dev/md3             5.8G   12M  5.5G   1% /magedata
/dev/mapper/vg5-lv5  477M  2.3M  449M   1% /media

實驗兩個硬盤做lvm跨主機遷移

創建pv兩個
[root@centos7 media]# pvcreate /dev/sdc
    Physical volume "/dev/sdc" successfully created.
[root@centos7 media]# pvcreate /dev/sdd
    Physical volume "/dev/sdd" successfully created.
[root@centos7 media]# pvs
    PV         VG  Fmt  Attr PSize  PFree
    /dev/sdb   vg5 lvm2 a--  <2.00g <1.51g
    /dev/sdc       lvm2 ---   3.00g  3.00g
    /dev/sdd       lvm2 ---   3.00g  3.00g

創建vg
[root@centos7 media]# vgcreate  vg3 /dev/sdc
    Volume group "vg3" successfully created
[root@centos7 media]# vgs
    VG  #PV #LV #SN Attr   VSize  VFree
    vg3   1   0   0 wz--n- <3.00g <3.00g
    vg5   1   1   0 wz--n- <2.00g <1.51g

加一個pv擴大vg
[root@centos7 media]# vgextend vg3 /dev/sdd
    Volume group "vg3" successfully extended
[root@centos7 media]# vgs
    VG  #PV #LV #SN Attr   VSize  VFree
    vg3   2   0   0 wz--n-  5.99g  5.99g
    vg5   1   1   0 wz--n- <2.00g <1.51g

創建lv

[root@centos7 media]# lvcreate -L 4G -n lv3 vg3
    Logical volume "lv3" created.
[root@centos7 media]# lvs
    LV   VG  Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    lv3  vg3 -wi-a-----   4.00g                                                   
    lv5  vg5 -wi-ao---- 500.00m    
格式化
[root@centos7 media]# mkfs.ext4 /dev/vg3/lv3
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
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 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

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
掛載

[root@centos7 media]# mkdir /media/3333
[root@centos7 media]# mount /dev/vg3/lv3 /media/3333/
[root@centos7 media]#

[root@centos7 media]# df -h

Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             20G  3.5G   17G  18% /
devtmpfs             978M     0  978M   0% /dev
tmpfs                993M     0  993M   0% /dev/shm
tmpfs                993M  9.0M  984M   1% /run
tmpfs                993M     0  993M   0% /sys/fs/cgroup
/dev/sda5             20G   12G  8.9G  56% /app
/dev/sda1            197M  136M   61M  70% /boot
tmpfs                199M   12K  199M   1% /run/user/42
/dev/mapper/vg3-lv3  3.9G   16M  3.6G   1% /media/3333

掛載點文件
[root@centos7 3333333]# ll -h
total 3.8G
drwx------. 2 root root  16K Mar  3 21:23 lost+found
-rw-r--r--. 1 root root 3.8G Mar  3 21:40 passwd
-rw-r--r--. 1 root root  35K Mar  3 21:36 passwd1

[root@centos7 ~]# umount /media/3333333/    ------>卸載
[root@centos7 ~]# vgchange -an vg3     ----------->-an 關掉設備不在激活
    0 logical volume(s) in volume group "vg3" now active
[root@centos7 ~]# vgexport vg3   ----------->設置爲導出狀態
    Volume group "vg3" successfully exported
[root@centos7 ~]# pvs     ----------->查看pv狀態
    PV         VG  Fmt  Attr PSize  PFree
    /dev/sdc   vg3 lvm2 ax-  <3.00g      0                ---> 導出狀態
    /dev/sdd   vg3 lvm2 ax-  <3.00g 504.00m    --->導出狀態

遷移硬盤

[root@centos6 ~]#pvscan       ------->從新讀取
    PV /dev/sds    is in exported VG vg3 [3.00 GiB / 0    free]
    PV /dev/sdr    is in exported VG vg3 [3.00 GiB / 504.00 MiB free]
    PV /dev/sdq    is in exported VG vg2 [508.00 MiB / 8.00 MiB free]
    PV /dev/sdo                      lvm2 [10.00 GiB]
    Total: 4 [16.49 GiB] / in use: 3 [6.49 GiB] / in no VG: 1 [10.00 GiB]
[root@centos6 ~]#vgs       ----------->從新讀取
    VG   #PV #LV #SN Attr   VSize   VFree
    vg2    1   1   0 wzx-n- 508.00m   8.00m
    vg3    2   1   0 wzx-n-   5.99g 504.00m
[root@centos6 ~]#vgimport vg3 vg    -----------> 設置爲導入狀態
    Volume group "vg3" successfully imported
    Volume group "vg" not found
    Cannot process volume group vg
[root@centos6 ~]#pvs    ----------->查看pv
    PV         VG   Fmt  Attr PSize   PFree
    /dev/sdo        lvm2 ----  10.00g  10.00g
    /dev/sdq   vg2  lvm2 ax-u 508.00m   8.00m
    /dev/sdr   vg3  lvm2 a--u   3.00g 504.00m
    /dev/sds   vg3  lvm2 a--u   3.00g      0
[root@centos6 ~]#vgs    ----------->查看vg
    VG   #PV #LV #SN Attr   VSize   VFree
    vg2    1   1   0 wzx-n- 508.00m   8.00m
    vg3    2   1   0 wz--n-   5.99g 504.00m
[root@centos6 ~]#vgchange -ay    ----------->設置爲激活狀態
    1 logical volume(s) in volume group "vg3" now active
    Volume group "vg2" is exported
[root@centos6 ~]#mount /dev/vg3/lv3 /mnt/    ----------->掛載
[root@centos6 ~]#df    ----------->檢查掛載
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/sda2            20511356 4745636  14717144  25% /
tmpfs                  502056      72    501984   1% /dev/shm
/dev/sda3            20511356  200504  19262276   2% /app
/dev/sda1              194241   35157    148844  20% /boot
/dev/md127            1014680    1284    961020   1% /backup
/dev/md5              2029392    3072   1921568   1% /mydata
/dev/md3              6049536   12280   5723300   1% /magedata
/dev/mapper/vg3-lv3   5545568   16412   5256536   1% /mnt
[root@centos6 ~]#cd /mnt   
[root@centos6 mnt]#ls   
lost+found  passwd1
[root@centos6 mnt]#cat passwd1    ----------->查看源文件
pps***oot:x:0:0:root:/root:/bin/bash
rd:x:997:996:User for colord:/var/lib/colord:/sbin/nologin
saslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
chrony:x:995:993::/var/lib/chrony:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
--------省略部分-------

邏輯卷管理器快照

邏輯卷管理器快照
快照就是將當時的系統信息記錄下來,就好像照相一般, 若將來有任何數據改動了,則原始數據會被移動到快照區 ,沒有改動的區域則由快照區和文件系統共享

磁盤管邏輯卷(LVM)

由於快照區與原本的LV共用很多PE的區塊,因此快照去與被 快照的LV必須在同一個VG.系統恢復的時候的文件數量不能高 於快照區的實際容量

創建可讀寫快照

    lvcreate -L 1G -s -n kuaizhaoname /dev/vgname/lvname 
    \# -L 1G //指定大小 -s //快照選項 -n name //快照名字 /dev/..//快照源文件2
    lvdisply  #查看lv
    mkdir /mnt/snapname  #創建快照目錄
    掛載快照:mount /dev/vgname/kuaizhaoname /mnt/snapname

快照創建成功,當對原文將修改數據空間超過快照空間達到100%,快照會被撐死,快照已不能使用了.
(爲快照永遠不死,快照與源文件空間相同,一般大小爲原文件20%-30%)
快照可以用於冷備份,創建一個只讀快照給定一個空間證備份期間原文件不被更改,備份快照就可以了
虛擬機快照:先安裝完成一個系統,創建n+1個快照,對源系統進行關機處理以保證源文件不被修改,使用快照系統,如果快照系統出問題,直接刪除,從新在做一個快照使用.

爲現有邏輯卷創建只讀快照

    #lvcreate -l  64  -s  -n snap-data -p r /dev/vg0/data 

掛載快照

#mkdir -p  /mnt/snap 
#mount -o ro /dev/vg0/snap-data  /mnt/snap 

恢復快照
#umount /dev/vg0/snap-data
#umount /dev/vg0/data
#lvconvert --merge /dev/vg0/snap-data
刪除快照
#umount /mnt/databackup
#lvremove /dev/vg0/databackup

示例

    1.創建一個至少有兩個PV組成的大小爲20G的名爲testvg的VG;
    2.要求PE大小爲16MB, 而後在卷組中創建大小爲5G的邏輯卷testlv;掛載至/users目錄
    \# 先 lsblk 或 blkid 查看硬盤
    [root@centos6 ~]#mkfs.ext4 /dev/sdr  #格式化,創建文件系統 /dev/sdq
    mke2fs 1.41.12 (17-May-2010)
    /dev/sdr is entire device, not just one partition!
    Proceed anyway? (y,n) y
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    720896 inodes, 2883584 blocks
    144179 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2952790016
    88 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

    Writing inode tables: done                            
    Creating journal (32768 blocks): 
    done
    Writing superblocks and filesystem accounting information: done

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

    [root@centos6 ~]#mkfs.ext4 /dev/sdq  #格式化,創建文件系統 /dev/sdq
    mke2fs 1.41.12 (17-May-2010)
    /dev/sdq is entire device, not just one partition!
    Proceed anyway? (y,n) y
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    655360 inodes, 2621440 blocks
    131072 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=2684354560
    80 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

    Writing inode tables: done                            
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 37 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    [root@centos6 ~]#blkid
    1/dev/sdr: UUID="7b516c4d-832d-4ad8-8282-4688513fee68" TYPE="ext4" 
    /dev/sdq: UUID="285addd4-7dee-4fad-8c47-2fa9f4067ab4" TYPE="ext4" 

    [root@centos6 ~]#vgcreate -s 16M testvg /dev/sdr  #創建卷組 testvg 並設置PE大小爲16MB
    Volume group "testvg" successfully created
    [root@centos6 ~]#vgdisplay  #查看卷組 testvg詳細信息
    --- Volume group ---
    VG Name              testvg
    System ID            
    Format                lvm2
    Metadata Areas        1
    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                1
    Act PV                1
    VG Size              10.98 GiB
    PE Size              16.00 MiB
    Total PE              703
    Alloc PE / Size      0 / 0  
    Free  PE / Size      703 / 10.98 GiB
    VG UUID              ZTCj4I-vEja-SKe2-3EcL-Prme-iqLe-Cmix8v

    [root@centos6 ~]#vgextend testvg /dev/sdq  #管理卷組 增加捲組 testvg的大小
    Volume group "testvg" successfully extended
    [root@centos6 ~]#vgdisplay  #查看testvg卷組詳細信息
    --- Volume group ---
    VG Name              testvg
    System ID            
    Format                lvm2
    Metadata Areas        2
    Metadata Sequence No  2
    VG Access            read/write
    VG Status            resizable
    MAX LV                0
    Cur LV                0
    Open LV              0
    Max PV                0
    Cur PV                2
    Act PV                2
    VG Size              20.97 GiB
    PE Size              16.00 MiB
    Total PE              1342
    Alloc PE / Size      0 / 0  
    Free  PE / Size      1342 / 20.97 GiB
    VG UUID              ZTCj4I-vEja-SKe2-3EcL-Prme-iqLe-Cmix8v

    [root@centos6 ~]#lvcreate -L 5G -n testlv testvg  #創建邏輯卷 testlv
        Logical volume "testlv" created.
    [root@centos6 ~]#lvs  #查看邏輯卷 testlv 
    LV    VG    Attr      LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
        testlv testvg -wi-a----- 5.00g                                                    
    -----三種查看邏輯卷testlv磁盤目錄
    [root@centos6 ~]#ll /dev/dm*  #查看testlv邏輯卷名字
    brw-rw----. 1 root disk 253, 0 Feb 24 09:35 /dev/dm-0
    [root@centos6 ~]#ll /dev/mapper/testvg-testlv #查看testlv邏輯卷名字
    lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/mapper/testvg-testlv -> ../dm-0
    [root@centos6 ~]#ll /dev/testvg/*  #查看testlv邏輯卷名字
    lrwxrwxrwx. 1 root root 7 Feb 24 09:31 /dev/testvg/testlv -> ../dm-0

    [root@centos6 ~]#mkfs.ext4 /dev/testvg/testlv #格式化,創建文件系統
    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
    327680 inodes, 1310720 blocks
    65536 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=1342177280
    40 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

    Writing inode tables: done                            
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    This filesystem will be automatically checked every 38 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    [root@centos6 ~]#mkdir /mnt/qwe #創建掛載目錄
    [root@centos6 ~]#mv /qwe /users
    [root@centos6 ~]#mount /dev/testvg/testlv /users/  #掛載
    [root@centos6 ~]#df -l  #查看掛載
    Filesystem          1K-blocks    Used Available Use% Mounted on
    /dev/sda2            20511356 4712940  14749840  25% /
    tmpfs                  502056      72    501984  1% /dev/shm
    /dev/sda3            20511356  200464  19262316  2% /app
    /dev/sda1              194241  35157    148844  20% /boot
    /dev/md127            1014680    1284    961020  1% /backup
    /dev/md5              2029392    3072  1921568  1% /mydata
    /dev/md3              6049536  12280  5723300  1% /magedata
    /dev/mapper/testvg-testlv
                                                5029504  10232  4757128  1% /users

    2、 新建用戶archlinux,要求其家目錄爲/users/archlinux,而後su切換至archlinux用戶,複製/etc/pam.d目錄至自己的家目錄
    [root@centos6 /]#useradd -d /users/archlinux archlinux  #/users/archlinux爲未創建目錄

    3、擴展testlv至7G,要求archlinux用戶的文件不能丟失

    [root@centos6 /]#lvextend -L 7G /dev/testvg/testlv -r
        Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
        Logical volume testlv successfully resized.
    resize2fs 1.41.12 (17-May-2010)
    Filesystem at /dev/mapper/testvg-testlv is mounted on /users; on-line resizing required
    old desc_blocks = 1, new_desc_blocks = 1
    Performing an on-line resize of /dev/mapper/testvg-testlv to 1835008 (4k) blocks.
    The filesystem on /dev/mapper/testvg-testlv is now 1835008 blocks long.

    [root@centos6 /]#lvs
    LV    VG    Attr      LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    testlv testvg -wi-ao---- 7.00g   

    4、收縮testlv至3G,要求archlinux用戶的文件不能丟失

    [root@centos6 /]#umount /dev/testvg/testlv 
        umount: /dev/testvg/testlv: not mounted
    [root@centos6 /]#e2fsck -f /dev/testvg/testlv 
    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/testvg/testlv: 78/458752 files (0.0% non-contiguous), 64509/1835008 blocks
    [root@centos6 /]#e2fsck -f /dev/testvg/testlv 
    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/testvg/testlv: 78/458752 files (0.0% non-contiguous), 64509/1835008 blocks
    [root@centos6 /]#resize2
    -bash: resize2: command not found
    [root@centos6 /]#resize2fs /dev/testvg/testlv 3G
    resize2fs 1.41.12 (17-May-2010)
    Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
    The filesystem on /dev/testvg/testlv is now 786432 blocks long.
    [root@centos6 /]#lvreduce -L 3G /dev/testvg/testlv 
        WARNING: Reducing active logical volume to 3.00 GiB.
        THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce testvg/testlv? [y/n]: y
        Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
        Logical volume testlv successfully resized.
    [root@centos6 /]#df
    Filesystem    1K-blocks    Used Available Use% Mounted on
    /dev/sda2      20511356 4713184  14749596  25% /
    tmpfs            502056      72    501984  1% /dev/shm
    /dev/sda3      20511356  200464  19262316  2% /app
    /dev/sda1        194241  35157    148844  20% /boot
    /dev/md127      1014680    1284    961020  1% /backup
    /dev/md5        2029392    3072  1921568  1% /mydata
    /dev/md3        6049536  12280  5723300  1% /magedata
    [root@centos6 /]#mount /dev/testvg/testlv /users/
    [root@centos6 /]#df -h
    Filesystem            Size  Used Avail Use% Mounted on
    /dev/sda2              20G  4.5G  15G  25% /
    tmpfs                491M  72K  491M  1% /dev/shm
    /dev/sda3              20G  196M  19G  2% /app
    /dev/sda1            190M  35M  146M  20% /boot
    /dev/md127            991M  1.3M  939M  1% /backup
    /dev/md5              2.0G  3.0M  1.9G  1% /mydata
    /dev/md3              5.8G  12M  5.5G  1% /magedata
    /dev/mapper/testvg-testlv
                                                2.9G  7.8M  2.7G  1% /users

    5、對testlv創建快照,並嘗試基於快照備份數據,驗正快照 的功能
    [root@centos6 diska]#lvcreate -L 1G -s -n testlvsnap  /dev/testvg/testlv 
    Logical volume "testlvsnap1" created.
    [root@centos6 diska]#lvs
    LV          VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
    testlv      testvg owi-a-s--- 3.00g                                                    
    testlvsnap  testvg swi-aos--- 1.00g      testlv 0.00   
    [root@centos6 diska]#mount /dev/testvg/testlvsnap /mnt/diska/
    [root@centos6 diska]#df
    Filesystem           1K-blocks    Used Available Use% Mounted on
    /dev/sda2             20511356 4713600  14749180  25% /
    tmpfs                   502056      72    501984   1% /dev/shm
    /dev/sda3             20511356  200464  19262316   2% /app
    /dev/sda1               194241   35157    148844  20% /boot
    /dev/md127             1014680    1284    961020   1% /backup
    /dev/md5               2029392    3072   1921568   1% /mydata
    /dev/md3               6049536   12280   5723300   1% /magedata
    /dev/mapper/testvg-testlvsnap
                                 2965264    7928   2800064   1% /mnt/qwe/qwe
    /dev/mapper/testvg-testlvsnap
                                 2965264    7928   2800064   1% /mnt/diska
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章