CentOS6 lvm配置

1、添加一塊20G的新硬盤並重啓Linux,硬盤被識別爲sdb。


2、在sdb上創建兩個新分區,大小爲10G和5G。


    [root@centos6 ~]# 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-10443, default 1): 

    Using default value 1

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


    Command (m for help): n

    Command action

       e   extended

       p   primary partition (1-4)

    p

    Partition number (1-4): 2

    First cylinder (1307-10443, default 1307): 

    Using default value 1307

    Last cylinder, +cylinders or +size{K,M,G} (1307-10443, default 10443): +5G


3、將分區系統ID設置爲LVM類型,並保存。


    Command (m for help): t

    Partition number (1-4): 1

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

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


    Command (m for help): t

    Partition number (1-4): 2

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

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


    Command (m for help): w

    The partition table has been altered!


4、讓Linux內核重讀分區表。(由於此硬盤爲新硬盤,之前並沒有分區,所於此步可以忽略。對於之前有分區的硬盤,添加了分區之後需要這一步操作。)


    [root@centos6 ~]# partx -a /dev/sdb

    BLKPG: Device or resource busy

    error adding partition 1

    BLKPG: Device or resource busy

    error adding partition 2

    [root@centos6 ~]# partx -a /dev/sdb

    BLKPG: Device or resource busy

    error adding partition 1

    BLKPG: Device or resource busy

    error adding partition 2


5、創建物理卷PV。


    [root@centos6 ~]# pvcreate /dev/sdb1

      Physical volume "/dev/sdb1" successfully created


6、查看PV信息。


    [root@centos6 ~]# pvs

      PV         VG   Fmt  Attr PSize  PFree 

      /dev/sdb1       lvm2 ---- 10.00g 10.00g


    [root@centos6 ~]# pvdisplay

      --- Physical volume ---

      PV Name               /dev/sdb1

      VG Name               vg0

      PV Size               10.00 GiB / not usable 4.54 MiB

      Allocatable           yes 

      PE Size               4.00 MiB

      Total PE              2560

      Free PE               2560

      Allocated PE          0

      PV UUID               8oFFsN-mdFh-kgkQ-eSmt-IBhS-HJWP-xnnUzJ


7、創建卷組VG。


    [root@centos6 ~]# vgcreate vg0 /dev/sdb1

      Volume group "vg0" successfully created


8、查看VG信息。


    [root@centos6 ~]# vgs

      VG   #PV #LV #SN Attr   VSize  VFree 

      vg0    1   0   0 wz--n- 10.00g 10.00g


    [root@centos6 ~]# vgdisplay

      --- Volume group ---

      VG Name               vg0

      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.00 GiB

      PE Size               4.00 MiB

      Total PE              2560

      Alloc PE / Size       0 / 0   

      Free  PE / Size       2560 / 10.00 GiB

      VG UUID               E1lTkT-ttoh-K7rG-edfj-GTNJ-3zhB-5dJSci


9、擴展VG大小。


    [root@centos6 ~]# pvcreate /dev/sdb2

      Physical volume "/dev/sdb2" successfully created

    [root@centos6 ~]# vgextend vg0 /dev/sdb2

      Volume group "vg0" successfully extended

    [root@centos6 ~]# vgs

      VG   #PV #LV #SN Attr   VSize  VFree 

      vg0    2   0   0 wz--n- 15.01g 15.01g


10、縮減VG大小。


    [root@centos6 ~]# pvmove /dev/sdb2

      No data to move for vg0

    [root@centos6 ~]# vgreduce vg0 /dev/sdb2

      Removed "/dev/sdb2" from volume group "vg0"

    [root@centos6 ~]# vgs

      VG   #PV #LV #SN Attr   VSize  VFree 

      vg0    1   0   0 wz--n- 10.00g 10.00g


11、創建邏輯卷LV。


    [root@centos6 ~]# lvcreate -L 2G -n lv0 vg0

      Logical volume "lv0" created.


12、查看LV。


    [root@centos6 ~]# lvs

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

      lv0  vg0  -wi-a----- 2.00g                                                    


    [root@centos6 ~]# lvdisplay

      --- Logical volume ---

      LV Path                /dev/vg0/lv0

      LV Name                lv0

      VG Name                vg0

      LV UUID                wqjJ5n-uXjY-0uj2-dwSE-wA4T-LfuL-3xlFTS

      LV Write Access        read/write

      LV Creation host, time centos6.localdomain, 2019-02-26 23:54:43 +0800

      LV Status              available

      # open                 0

      LV Size                2.00 GiB

      Current LE             512

      Segments               1

      Allocation             inherit

      Read ahead sectors     auto

      - currently set to     256

      Block device           253:0


13、格式化LV爲ext4文件系統並掛載。


    [root@centos6 ~]# mke2fs -t ext4 /dev/vg0/lv0

    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


    This filesystem will be automatically checked every 28 mounts or

    180 days, whichever comes first.  Use tune2fs -c or -i to override.


    [root@centos6 ~]# mkdir /backup

    [root@centos6 ~]# mount /dev/vg0/lv0 /backup

    [root@centos6 ~]# df -h

    Filesystem           Size  Used Avail Use% Mounted on

    /dev/sda3             17G  3.4G   13G  22% /

    tmpfs                490M     0  490M   0% /dev/shm

    /dev/sda1            976M   34M  892M   4% /boot

    /dev/mapper/vg0-lv0  2.0G  3.0M  1.9G   1% /backup


14、擴展LV大小。


    [root@centos6 ~]# lvextend -L 5G /dev/vg0/lv0

      Size of logical volume vg0/lv0 changed from 2.00 GiB (512 extents) to 5.00 GiB (1280 extents).

      Logical volume lv0 successfully resized.


15、擴展LV中的ext4文件系統大小。


    [root@centos6 ~]# resize2fs /dev/vg0/lv0

    resize2fs 1.41.12 (17-May-2010)

    Filesystem at /dev/vg0/lv0 is mounted on /backup; on-line resizing required

    old desc_blocks = 1, new_desc_blocks = 1

    Performing an on-line resize of /dev/vg0/lv0 to 1310720 (4k) blocks.

    The filesystem on /dev/vg0/lv0 is now 1310720 blocks long.


    [root@centos6 ~]# df -h

    Filesystem           Size  Used Avail Use% Mounted on

    /dev/sda3             17G  3.4G   13G  22% /

    tmpfs                490M     0  490M   0% /dev/shm

    /dev/sda1            976M   34M  892M   4% /boot

    /dev/mapper/vg0-lv0  4.9G  4.0M  4.7G   1% /backup


16、縮減LV中的ext4文件系統大小。


    [root@centos6 ~]# umount /dev/vg0/lv0

    [root@centos6 ~]# e2fsck -f /dev/vg0/lv0

    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/vg0/lv0: 11/327680 files (0.0% non-contiguous), 37982/1310720 blocks

    [root@centos6 ~]# resize2fs /dev/vg0/lv0 3G

    resize2fs 1.41.12 (17-May-2010)

    Resizing the filesystem on /dev/vg0/lv0 to 786432 (4k) blocks.

    The filesystem on /dev/vg0/lv0 is now 786432 blocks long.


17、縮減LV大小。


    [root@centos6 ~]# lvreduce -L 3G /dev/vg0/lv0

      WARNING: Reducing active logical volume to 3.00 GiB.

      THIS MAY DESTROY YOUR DATA (filesystem etc.)

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

      Size of logical volume vg0/lv0 changed from 5.00 GiB (1280 extents) to 3.00 GiB (768 extents).

      Logical volume lv0 successfully resized.

    [root@centos6 ~]# mount /dev/vg0/lv0 /backup

    [root@centos6 ~]# df -h

    Filesystem           Size  Used Avail Use% Mounted on

    /dev/sda3             17G  3.4G   13G  22% /

    tmpfs                490M     0  490M   0% /dev/shm

    /dev/sda1            976M   34M  892M   4% /boot

    /dev/mapper/vg0-lv0  2.9G  3.0M  2.8G   1% /backup


18、創建快照卷。


    [root@centos6 ~]# cp /etc/issue /backup

    [root@centos6 ~]# cat /backup/issue

    CentOS release 6.10 (Final)

    Kernel \r on an \m


    [root@centos6 ~]# lvcreate -L 3G -p r -s /dev/vg0/lv0 -n lv0-snap

      Logical volume "lv0-snap" created.

    [root@centos6 ~]# lvs

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

      lv0      vg0  owi-aos--- 3.00g                                                    

      lv0-snap vg0  sri-a-s--- 3.00g      lv0    0.00


19、掛載快照卷,修改源卷文件後查看快照卷文件是否變化。


    [root@centos6 ~]# mount /dev/vg0/lv0-snap /mnt

    mount: block device /dev/mapper/vg0-lv0--snap is write-protected, mounting read-only

    [root@centos6 ~]# echo "New line." >> /backup/issue

    [root@centos6 ~]# cat /backup/issue

    CentOS release 6.10 (Final)

    Kernel \r on an \m


    New line.

    [root@centos6 ~]# cat /mnt/issue

    CentOS release 6.10 (Final)

    Kernel \r on an \m


20、卸載及刪除快照卷和源LV。


    [root@centos6 ~]# umount /mnt

    [root@centos6 ~]# lvremove /dev/vg0/lv0-snap

    Do you really want to remove active logical volume lv0-snap? [y/n]: y

      Logical volume "lv0-snap" successfully removed

    [root@centos6 ~]# umount /backup

    [root@centos6 ~]# lvremove /dev/vg0/lv0

    Do you really want to remove active logical volume lv0? [y/n]: y

      Logical volume "lv0" successfully removed


21、刪除VG。


    [root@centos6 ~]# vgremove vg0

      Volume group "vg0" successfully removed


22、刪除PV。


    [root@centos6 ~]# pvremove /dev/sdb{1,2}

      Labels on physical volume "/dev/sdb1" successfully wiped

      Labels on physical volume "/dev/sdb2" successfully wiped


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章