LVM快速入門

LVM筆記

[toc]

環境準備

  • 準備磁盤分區:
    [root@centos6-node3 ~]# fdisk /dev/sdg
    Command (m for help): n
    Command action
    e   extended
    p   primary partition (1-4)
    p
    Partition number (1-4): 1
    First cylinder (131-261, default 131): 
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (131-261, default 261): +500M
    Command (m for help): t
    Partition number (1-4): 1
    Hex code (type L to list codes): 8e
    Changed system type of partition 3 to 8e (Linux LVM)
    Command (m for help): w
    The partition table has been altered!
    Calling ioctl() to re-read partition table.
    Syncing disks.

    按照上述的操作方法創建3個500M的分區,然後如下檢查

    [root@centos6-node3 ~]# fdisk -l
    Device Boot      Start         End      Blocks   Id  System
    /dev/sdg1               1          65      522081   8e  Linux LVM
    /dev/sdg2              66         130      522112+  8e  Linux LVM
    /dev/sdg3             131         195      522112+  8e  Linux LVM

    第一階段:準備PV(物理卷)

  • 先創建和檢查pv
    [root@centos6-node3 ~]# pvcreate /dev/sdg1
    [root@centos6-node3 ~]# pvcreate /dev/sdg2
    [root@centos6-node3 ~]# pvcreate /dev/sdg3
    [root@centos6-node3 ~]# pvdisplay /dev/sdg1    #檢查pv情況
    --- NEW Physical volume ---
    PV Name               /dev/sdg1
    VG Name               
    PV Size               509.84 MiB
    Allocatable           NO
    PE Size               0   
    Total PE              0
    Free PE               0
    Allocated PE          0
    PV UUID               tmkRdY-h9LX-V0h0-sTXI-lFZW-Cref-LcoCXE
    [root@centos6-node3 ~]# pvremove /dev/sdg3      #移除pv

準備卷組(VG)

  • 創建VG
    [root@centos6-node3 ~]# vgcreate volume-group1 /dev/sdg1 /dev/sdg2
    [root@centos6-node3 ~]# vgdisplay      #查看VG信息
    --- Volume group ---
    VG Name               volume-group1
    System ID             
    Format                lvm2
    Metadata Areas        2
    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                2
    Act PV                2
    VG Size               1016.00 MiB
    PE Size               4.00 MiB
    Total PE              254
    Alloc PE / Size       0 / 0   
    Free  PE / Size       254 / 1016.00 MiB
    VG UUID               NZfaUa-lchX-qQLk-WdtO-clN8-msE2-b3q6YP
  • 刪除VG
    [root@centos6-node3 ~]# vgremove volume-group1

    準備邏輯卷(LV)

  • 創建LV
    root@centos6-node3 ~]# lvcreate -L 100M -n lv1 volume-group1    #從VG畫出100M的空間作爲lv1
    [root@centos6-node3 ~]# lvdisplay    #查看lv
    /dev/md126: read failed after 0 of 4096 at 0: Input/output error
    --- Logical volume ---
    LV Path                /dev/volume-group1/lv1
    LV Name                lv1
    VG Name                volume-group1
    LV UUID                Aq4zSR-Rc3A-l4lv-7aoS-qADe-4Q1I-vhGUe1
    LV Write Access        read/write
    LV Creation host, time centos6-node3, 2017-12-12 23:08:44 +0800
    LV Status              available
    # open                 0
    LV Size                100.00 MiB
    Current LE             25
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     256
    Block device           253:2

    最後一步,將LV格式化並掛載

  • 格式化&初始化:
    [root@centos6-node3 ~]# mkfs.ext4 /dev/volume-group1/lv1  #格式化
    [root@centos6-node3 ~]# mkdir /mnt/lvm-mount
    [root@centos6-node3 ~]# mount /dev/volume-group1/lv1 /mnt/lvm-mount   #掛載
    [root@centos6-node3 ~]# dd if=/dev/zero of=/mnt/lvm-mount/test1 bs=1M count=89    #數據寫入
  • 刪除LV
    [root@centos6-node3 ~]# umount /mnt/lvm-mount/    #要先卸載
    [root@centos6-node3 lvm-mount]# lvremove /dev/volume-group1/lv1      #刪除LV(高危操作)
  • 拓展LVM卷
    [root@centos6-node3 lvm-mount]# umount /mnt/lvm-mount/   #擴容之前一定要卸載
    [root@centos6-node3 lvm-mount]# lvresize -L 200M /dev/volume-group1/lv1     #擴容200M
    [root@centos6-node3 lvm-mount]# e2fsck -f /dev/volume-group1/lv1      #檢查磁盤錯誤
    [root@centos6-node3 ~]# resize2fs /dev/volume-group1/lv1    #更新lv
    [root@centos6-node3 ~]# lvdisplay     #檢查lv1
    /dev/md126: read failed after 0 of 4096 at 0: Input/output error
    --- Logical volume ---
    LV Path                /dev/volume-group1/lv1
    LV Name                lv1
    VG Name                volume-group1
    LV UUID                emCezv-2nBN-0vgu-y54a-R93P-KCBG-SSF7ZI
    LV Write Access        read/write
    LV Creation host, time centos6-node3, 2017-12-12 23:21:15 +0800
    LV Status              available
    # open                 0
    LV Size                200.00 MiB
    Current LE             50
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     256
    Block device           253:2
    [root@centos6-node3 ~]# mount /dev/volume-group1/lv1 /mnt/lvm-mount/   #掛盤即可
  • LVM縮容
  • 卸載卷
    [root@centos6-node3 ~]# df    #回家目錄卸載,不然會報錯
    Filesystem           1K-blocks    Used Available Use% Mounted on
    /dev/mapper/vg_centos6node3-lv_root
                      38645208 1022088  35653356   3% /
    tmpfs                   502056       0    502056   0% /dev/shm
    /dev/sda1               487652   52916    409136  12% /boot
    /dev/mapper/volume--group1-lv1
                        194466   92687     91539  51% /mnt/lvm-mount
    [root@centos6-node3 ~]# umount /dev/mapper/volume--group1-lv1
    [root@centos6-node3 ~]# e2fsck -f /dev/volume-group1/lv1    #檢查磁盤錯誤
  • 縮容

    [root@centos6-node3 ~]# resize2fs /dev/volume-group1/lv1 100M  #縮小文件系統
    [root@centos6-node3 ~]# lvresize -L 100M /dev/volume-group1/lv1   #減小邏輯卷的大小
    [root@centos6-node3 ~]# lvdisplay     #檢查是否縮小了100M
    /dev/md126: read failed after 0 of 4096 at 0: Input/output error
    --- Logical volume ---
    LV Path                /dev/volume-group1/lv1
    LV Name                lv1
    VG Name                volume-group1
    LV UUID                emCezv-2nBN-0vgu-y54a-R93P-KCBG-SSF7ZI
    LV Write Access        read/write
    LV Creation host, time centos6-node3, 2017-12-12 23:21:15 +0800
    LV Status              available
    # open                 0
    LV Size                100.00 MiB
    Current LE             25
    Segments               1
    Allocation             inherit
    Read ahead sectors     auto
    - currently set to     256
    Block device           253:2

    其他問題

    拓展卷組

    讓我們假設我們的卷組'volume-group1'已經滿了,需要擴大。手上的硬盤(sdg)已經沒有其他空閒分區,我們添加了另外一個硬盤(sdc)。我們將看到如何把sdc的分區添加到卷組以擴展。

  • 檢查卷組狀態:
    [root@centos6-node3 ~]# vgdisplay volume-group1 
    /dev/md126: read failed after 0 of 4096 at 0: Input/output error
    --- Volume group ---
    VG Name               volume-group1
    System ID             
    Format                lvm2
    Metadata Areas        2
    Metadata Sequence No  6
    VG Access             read/write
    VG Status             resizable
    MAX LV                0
    Cur LV                1
    Open LV               0
    Max PV                0
    Cur PV                2
    Act PV                2
    VG Size               1016.00 MiB
    PE Size               4.00 MiB
    Total PE              254
    Alloc PE / Size       25 / 100.00 MiB
    Free  PE / Size       229 / 916.00 MiB
    VG UUID               QgBiHO-391u-jrra-QfiG-BKhF-GCBR-EuhEY0
  • 初始化一個分區
    [root@centos6-node3 ~]# fdisk /dev/sdf
    Command (m for help): n
    Command action
    e   extended
    p   primary partition (1-4)
    p
    Partition number (1-4): 1
    First cylinder (1-261, default 1): 
    Using default value 1
    Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): 
    Using default value 261
    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): p
    Disk /dev/sdf: 2147 MB, 2147483648 bytes
    255 heads, 63 sectors/track, 261 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x27eee55a
    Device Boot      Start         End      Blocks   Id  System
    /dev/sdf1               1         261     2096451   8e  Linux LVM
    Command (m for help): w
    The partition table has been altered!
    Calling ioctl() to re-read partition table.
    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.
  • 創建pv
    [root@centos6-node3 ~]# pvcreate /dev/sdf1
  • 拓展vg
    [root@centos6-node3 ~]# vgextend volume-group1 /dev/sdg3
    [root@centos6-node3 ~]# vgdisplay volume-group1   #檢查是否擴容
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章