Try   HackMD

建立LVM磁區

LVM(Logic Volume Manager)邏輯磁區管理器
是Linux核心提供的功能,用於在硬碟分割區建立邏輯磁區,方便系統管理

優點

  1. File System可以跨多個磁碟,因此大小不受物理磁碟限制
  2. 可以在系統運行時,動態擴展File System大小
  3. 可以新增磁碟至LVM儲存庫

缺點
其中一個磁碟損壞,整個volume都受到影響

基本名詞

  • PP (Physical Partition)
    物理分區
    實體分區,如硬碟、硬碟分割槽或邏輯上與磁碟分割槽具有同樣功能的裝置(如:RAID分區)
  • PE (Physical Extends)
    物理擴展單元
    PV的基本單元,是LVM中可以被定址的最小儲存單元
  • PV (Physical Volume)
    物理卷
    PP的LVM抽象,一般一個PP對應一個PV,組成VG的基本單元
  • VG (Volume Group)
    卷組
    數個PV組成的儲存池
  • LV (Logic Volume)
    邏輯卷
    由VG分割出來的空間,可自由增加/減少
    檔案系統建立於LV之上

架構圖

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

建立LVM

#disk partition

$ rpm -qa lvm2                 #檢查是否有安裝LVM
$ sudo fdisk -l                #列出所有磁碟資訊
$ sudo fdisk <磁碟>           #磁碟分割
    :m                        #顯示指令選單
    :p                        #partition table
    :F                        #列出未分配空間
    :n                        #建立新磁區
    :t                        #變更磁區type(8e是LVM)
    :d                        #刪除分區
    :w                        #儲存並離開
#相較於舊的磁碟分割工具fdisk僅能處理硬碟容量上限2TB,parted更適合
 處理容量較大的硬碟
$ sudo parted <磁碟>
    :pirnt                    #顯示磁碟資訊
    :mklabel gpt              #建立磁碟分割表
    :mkpart                   #建立磁碟分割區
    :rm <分區>                #刪除partition
    :resizepart               #重新設定partition大小
    :rescue                   #修復磁碟分割區
    :set <分區> boot on        #設定x分割區為可開機

#檢視資訊

$ lsblk                       #列出系統所有可用裝置空間(包含umount)
$ df -Th                      #列出檔案系統上可用空間(不包含umount)
$ sudo blkid                  #列出硬碟UUID(編寫/etc/fstab會用到)

$ pvs                         #查看PV資訊
$ pvdisplay <PV名稱>          #查看PV詳細資訊
$ vgs                         #查看vg資訊
$ vgdisplay <VG名稱>          #查看vg詳細資訊
$ lvs                         #查看lv資訊
$ lvdisplay <LV名稱>          #查看lv詳細資訊

#PV操作指令

$ pvcreate /dev/sdx           #將partition建成PV
$ pvremove /dev/sdx           #將PV刪除

#VG操作指令

$ vgcreate <VG名稱> <PV1> <PV2>...    #將PV建立成VG
$ vgextend <VG名稱> /dev/sdx          #將VG擴展空間
$ vgreduce <VG名稱> /dev/sdx          #將VG的空間回收(以PV為單位)
$ vgremove <VG名稱>                   #將VG刪除

#LV操作指令

$ lvcreate -n <LV名稱> -L <大小> <VG名稱>      #LV建立(-L配置固定大小 -l:10%free <VG> 依照VG剩餘空間的比例配置,例:100%free 表示全用、60%vg表示使用60%空間)
$ lvextend -L +<增加大小G/M> /dev/testvg/lv   #LV增加空間
$ lvreduce -L -<減少大小G/M> /dev/testvg/lv    #LV減少空間
$ lvresize -L +-<減/加大小G/M> /dev/testvg/lv  #LV空間改變
$ lvremove /dev/testvg/lv                     #LV刪除

#掛載lv

$ mkfs.<檔案系統> /dev/testvg/lv         #LV設置檔案系統
$ mount -t <type> <device> <dir>        #type:檔案格式如:ext4、xfs
                                         device:要掛載的設備
                                         dir:掛載的路徑

修改/etc/fstab 可以讓系統在開機時自動掛載

例:/dev/mapper/testvg / xfs default 0 0 (各項參數意義)

  1. 要掛載的磁區,也可以用UUID
  2. 要掛載的位置
  3. 檔案系統格式
  4. 掛載參數
  5. dump決定是否要備份,0不用、1每日備份、2隔日
  6. fsck決定不正常開機後檢查檔案系統的順序,0不檢查、1根目錄、2其他磁區

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
ext檔案系統

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
線上增加LV空間大小

$ lvextend -L +10G /dev/testvg/lv        #先增加lv的空間 例:+10G
$ resize2fs /dev/testvg/lv               #改變檔案系統系統大小

增加空間可以線上作業,減少需要先umount


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
縮小LV空間

$ umount /dev/testvg/lv                  #先結束掛載
$ e2fsck -f /dev/testvg/lv               #磁碟檢查
$ resize2fs /dev/testvg/lv 50G           #設定檔案系統大小
$ lvresize -L 50G /dev/testvg/lv         #設定LV大小50G        

結束掛載。先設定檔案系統縮小至多少,再設定LV大小

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
這裡resize2fs跟lvresize會設一樣的大小是因為LV內僅有一個file system。若有一個LV中有多個檔案系統,則lvresize要依據檔案系統減少的大小去設定LV的大小

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
xfs檔案系統

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
線上增加LV空間大小

$ lvextend -L +10G /dev/testvg/lv        #先增加lv的空間 例:+10G
$ xfs_growfs /dev/testvg/lv              #改變檔案系統系統大小

步驟與ext檔案系統相同

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

補充:從增加磁區開始至LV擴大

  1. 改變磁區大小
$ parted
(parted) print
(parted) resizepart (磁區編號)
(輸入結束點)
  1. 將partition加入pv
$ pvresize /dev/sdx
$ pvs                #查看是否有完成變更
  1. 將LV擴大
$ lvextend -L +5GB(想要增加的大小) /dev/rhel/sdx 
$ lvs                #查看lv是否有被擴大
$ xfs_growfs /dev/testvg/lv 

參考連結

LVM基礎建立步驟(英文)
LVM基礎建立步驟(中文)
LVM基礎知識
LVM簡易操作
LVM簡易操作2
parted基礎操作
線上LV擴增/縮小
shrinking ext file system
resize2fs、xfs_growfs差別

tags RHCSA