lvm 筆記

下列為 kickstart 的範例

# System bootloader configuration
bootloader –append=” crashkernel=auto” –location=mbr –boot-drive=sda
# Partition clearing information
clearpart –all –initlabel –drives=sda

part /boot –fstype ext4 –size=1024
part pv.01 –size=1000 –grow –ondisk=sda
volgroup vg00 pv.01
logvol / –vgname=vg00 –fstype=xfs –size=40960 –name=lv_root
logvol swap –vgname=vg00 –fstype=swap –size=32768 –name=lv_swap
logvol /data –vgname=vg00 –fstype=xfs –size=10000 –name=lv_data –grow

或是要用 parted 指令也可以

# parted -s /dev/sdb mklabel gpt
# parted -s /dev/sdb mkpart primary 0 100%
# parted -s /dev/sdb set 1 lvm on
# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
# vgcreate centos /dev/sdb1
  Volume group "centos" successfully created

下面為 lvm 的基本指令

vgs 指令列出 volume 群組資訊

# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <271.25g <187.25g

用 pvs 指令列出實體 volumes 的資訊

# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <271.25g <187.25g

用 lvs 指令 列出邏輯 volumes 的資訊

# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-a----- <217.25g
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- 4.00g

要變更 volume 群組名稱用 vgrename 指令

# vgrename centos computenode1
Volume group "centos" successfully renamed to "computenode1"

要變更 lvm volume 名稱用下列指令

# lvrename computenode1 home data
Renamed "home" to "data" in volume group "computenode1"

## 用 lvs 檢查一下結果
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
data computenode1 -wi-a----- <217.25g
root computenode1 -wi-ao---- 50.00g
swap computenode1 -wi-ao---- 4.00g

改完之後記得去編輯一下 /etc/grub2.cfg 要不然無法開機

# vi /etc/grub2.cfg
linux16 /vmlinuz-3.10.0-862.el7.x86_64 root=/dev/mapper/computenode1-root ro crashkernel=auto rd.lvm.lv=computenode1/root rd.lvm.lv=computenode1/swap rhgb quiet LANG=en_US.UTF-8

還有要建立 /data 掛載目錄及編輯 /etc/fstab

#mkdir /data
# vi /etc/fstab
/dev/mapper/computenode1-root / xfs defaults 0 0
UUID=8d6dec77-95e2-4cde-8e4b-35742cd6623b /boot xfs defaults 0 0
/dev/mapper/computenode1-data /data xfs defaults 0 0
/dev/mapper/computenode1-swap swap swap defaults 0 0

調整 lvm 大小範例:想要增加 / root 空間,為了方便調整 / root volume 的大小,所以先簡單的把 data volume 清除掉。當然可以先降低 data volume 之後再來調整 / root 大小,但就覺得麻煩。所先移除 data 邏輯 volume 。

# lvremove /dev/computenode1/data
Do you really want to remove active logical volume computenode1/data? [y/n]: y
  Logical volume "data" successfully removed

再來增加 / root 空間,從 50GB 到 80GB。

# lvextend -L80G /dev/computenode1/root
# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root computenode1 -wi-ao---- 80.00g
swap computenode1 -wi-ao---- 4.00g

因為檔案系統是 xfs 所以要用 xfs_growfs 調整,如果是 ext4 or ext3 就要用 resize2fs 指令

# xfs_growfs /dev/mapper/computenode1-root
meta-data=/dev/mapper/computenode1-root isize=512 agcount=4, agsize=3276800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=13107200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6400, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 13107200 to 20971520

再把所有剩餘的空間,建立給 data volume,順便格式化。

# lvcreate -n data -l 100%FREE computenode1
Logical volume "data" created.
# mkfs.xfs /dev/mapper/computenode1-data
meta-data=/dev/mapper/computenode1-data isize=512 agcount=4, agsize=12271360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=49085440, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=23967, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

這樣就可以掛載 /data 也把 / root 從 50GB 調整為 80GB

# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/computenode1-root 80G 5.2G 75G 7% /
devtmpfs 48G 0 48G 0% /dev
tmpfs 48G 0 48G 0% /dev/shm
tmpfs 48G 9.5M 48G 1% /run
tmpfs 48G 0 48G 0% /sys/fs/cgroup
/dev/sda1 1014M 218M 797M 22% /boot
tmpfs 9.5G 0 9.5G 0% /run/user/0
/dev/mapper/computenode1-data 188G 33M 188G 1% /data

如果要把 swap 關閉並移除,可以用下列指令:

# swapoff -v /dev/mapper/computenode1-swap
swapoff /dev/mapper/computenode1-swap

# lvremove /dev/mapper/computenode1-swap
Do you really want to remove active logical volume computenode1/swap? [y/n]: y
  Logical volume "swap" successfully removed

然後關閉虛擬機器把虛擬機器的 80GB 調整為 120GB,如果現有 partition 太小需要調整 分割區大小:

# parted /dev/sda
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  xfs          boot
 2      525MB   85.9GB  85.4GB  primary               lvm

(parted) resizepart 2 113GiB
(parted) p
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 129GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  xfs          boot
 2      525MB   121GB  121GB  primary               lvm

(parted) quit
Information: You may need to update /etc/fstab.

調整完後也記得把 lvm 的 sda2 分割區調整大小

# pvresize /dev/sda2 Physical volume "/dev/sda2" changed 1 physical volume(s) resized or updated / 0 physical volume(s) not resized

然後把剛剛刪除的 swap 建立回去:

# lvm lvcreate computenode1 -n swap -L 8G
  Logical volume "swap" created.

然後格式化 swap 磁區

# mkswap /dev/computenode1/swap
Setting up swapspace version 1, size = 8388604 KiB
no label, UUID=5f64de0a-bc9e-475f-a70f-7628704697a1

在 /etc/fstab 確認是否有把 swap 描述寫入

# cat /etc/fstab | grep swap
/dev/mapper/computenode1-swap swap                    swap    defaults        0 0

然後啟用 swap

# swapon -va
swapon /dev/mapper/computenode1-swap
swapon: /dev/mapper/computenode1-swap: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/mapper/computenode1-swap: pagesize=4096, swapsize=8589934592, devsize=8589934592

然後再次調整 root partition

# lvextend -L104G /dev/computenode1/root
# lvs
  LV    VG          Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root  computenode1 -wi-ao----  104.00g
  swap  computenode1 -wi-ao----    8.00g

用 xfs_growfs 調整 root partition

# xfs_growfs /dev/mapper/computenode1-root
meta-data=/dev/mapper/computenode1 isize=256    agcount=7, agsize=3151616 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=20709376, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=6155, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 20709376 to 27262976

檢查是否調整成功

# df -h /
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/computenode1-root  104G  5.0G  100G   5% /