Skip to main content

常用指令

Logical Volume - LV

# 顯示所有 LV 使用狀況
lvdisplay -C

# 新增 LV
lvcreate -L 10G -n LV_name VG_name

# 刪除 LV
lvremove /dev/VG_name>/LV_name

# 更名 LV
lvrename /dev/VG-name/old-LV-name /dev/VG-name/new-LV-name

# 顯示 LV 狀況
lvs
lvs -a -o name,copy_percent,devices
lvs -a -o name,copy_percent,devices <vg-name>
lvs -a --segments -o +devices

# 擴充 LV 使用空間
lvextend -L +2G /dev/vg/lv
lvextend -l +100%FREE /dev/vg_db2v9/lv_root  將剩餘空間都加上

ext2online /dev/vg/lv (RHEL v4)
resize2fs /dev/vg/lv (RHEL v5,6) 
xfs_growfs /dev/vg/lv (RHEL v7 with XFS filesystem)

# 縮小 LV 空間 (RHEL v4)
#說明:445GB -> 2GB
umount /worktmp
e2fsck -f /dev/rootVG/worktmpLV
resize2fs /dev/rootVG/worktmpLV 1843M
lvreduce -L 2GB /dev/rootVG/worktmpLV
resize2fs /dev/rootVG/worktmpLV
mount /worktmp 
#註:1843MB 大約是要 2GB 的 90%,這裡的指令也能使用單位 GB

# Shrinking LV (RHEL 5/6)
#100 GB -> 5GB
umount /opt/oracle/arclog
resize2fs /dev/VolGroup00/arclogLV 5G
lvreduce -L 5G /dev/VolGroup00/arclogLV
e2fsck -f /dev/VolGroup00/arclogLV
mount /opt/oracle/arclog

Volume Group - VG

# 顯示 VG 使用狀況
vgdisplay -C

# 啟動/關閉 VG
vgchange -a y VG_name
vgchange -a n VG_name
#註:關閉後,執行 vgdisplay 仍會顯示,但 /dev/*** 已不存在。

# 新建 VG
vgcreate VG_name /dev/pv1 /dev/pv2 

# 更名 VG
vgrename vg_esxa01db01 vg_root

# 刪除 VG
vgremove VG_name

# 增加 PV 到 VG
vgextend VG_name /dev/pv3 

# 從 VG 中移除 PV
#NOTE: PV 必須沒有任何資料(檢查指令 pvs -o+pv_used)
vgreduce VG_name /dev/mypv 

Physical Volume - PV

# 顯示 PV 使用狀況
pvdisplay -C

# 檢查所有 PV 的空間使用狀況
pvs -o+pv_used

# 新增 PV
pvcreate /dev/hdd1 

# 刪除 PV
pvremove /dev/hdd1 

# 如果整個 disk 都要作為 PV,該磁碟不能建立任何磁區,要清除舊磁區可以執行
dd if=/dev/zero of=/dev/sdd bs=512 count=1 

# 既有的 LUN Disk 增加磁碟空間後,還需要執行
pvresize /dev/sd[X]
LVM 磁區的磁碟加大

在 RHEL 8 下,移除 LVM 磁區時,可能出現提示: 

Do you want to remove the signature? [Y]es/[N]o:

注意: 這裡必須是 N,否則原有的磁區會被徹底清除。

技巧: 要確認 LVM 磁區是有完成延伸,或者不小心被移除,在 reboot 前執行

pvs

如果有顯示原有的 PV disk,表示磁區延伸成功。

Extend PV Disk: /dev/sda2 50GB to 100GB

[root@my-db2v11fp7 ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                  8:0    0  100G  0 disk
├─sda1               8:1    0    1G  0 part /boot
└─sda2               8:2    0   49G  0 part
  ├─rootvg-root    253:0    0   15G  0 lvm  /
  ├─rootvg-swap    253:1    0    4G  0 lvm  [SWAP]
  └─rootvg-worktmp 253:2    0  512M  0 lvm  /worktmp
sr0                 11:0    1 1024M  0 rom

[root@my-db2v11fp7 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  rootvg lvm2 a--  <49.00g <29.50g
  
[root@my-db2v11fp7 ~]# fdisk -ul /dev/sda
Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000def6b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM

Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000def6b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-209715199, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-209715199, default 209715199):
Using default value 209715199
Partition 2 of type Linux and of size 99 GiB is set

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000def6b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   209715199   103808000   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.


[root@my-db2v11fp7 ~]# reboot


[root@my-db2v11fp7 ~]# lsblk
NAME               MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                  8:0    0  100G  0 disk
├─sda1               8:1    0    1G  0 part /boot
└─sda2               8:2    0   99G  0 part
  ├─rootvg-root    253:0    0   15G  0 lvm  /
  ├─rootvg-swap    253:1    0    4G  0 lvm  [SWAP]
  └─rootvg-worktmp 253:2    0  512M  0 lvm  /worktmp
sr0                 11:0    1 1024M  0 rom

[root@my-db2v11fp7 ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@my-db2v11fp7 ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree
  /dev/sda2  rootvg lvm2 a--  <99.00g <79.50g