Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

[Ubuntu]在VM中擴展Ubuntu的磁碟空間(LVM)

內容目錄

在VM中擴展Ubuntu的硬碟空間(LVM)

手上管理的專案中有幾台服務是建置在VMware上,最近處理一些事情發現空間不夠用了,於是參考了這篇進行擴展磁碟空間。
Ubuntu版本為16.04.5

什麼是LVM

LVM (Logical Volume Manager),可以翻譯成邏輯捲軸管理員吧。LVM的機制目的為『可以彈性調整檔案空間的容量』達到幾個特性:容易利用、管理映碟。
當空間不足時,則需要使用vg和lv來調整空間,調整時不會影響到原先存放的資料<---重要。
老實說我也是這次要擴展才去看了一下LVM的機制說明XD

作法

所以預期我需要做幾件事:

  1. 先在VM中配置一組新硬碟
  2. 在Ubuntu中設定LVM擴展空間

VM中配置一組新硬碟

在VM介面中新增一顆硬碟,設定為100G,原本只有40G

Ubuntu中設定LVM擴展空間

這邊設定就較為繁瑣,當上一個動作做完後,開啟Ubuntu後應該能看到有顆新硬碟/dev/sdb,驗證為100G

root@ubuntu:/# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda5  /dev/sdb

root@ubuntu:/# sudo fdisk -l | grep "Disk /dev"
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 sectors
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk /dev/mapper/ubuntu--vg-root: 38.3 GiB, 41150316544 bytes, 80371712 sectors
Disk /dev/mapper/ubuntu--vg-swap_1: 980 MiB, 1027604480 bytes, 2007040 sectors

使用fdisk /dev/sdb 作partition,注意Command下的指令

root@ubuntu:/# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x96b1b396.

Command (m for help): p
Disk /dev/sdb: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0x96b1b396

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199):

Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

將這個partition指定為Linux LVM

root@ubuntu:/# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

成功的話會看到新增一個/dev/sdb1

root@ubuntu:/# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda5  /dev/sdb  /dev/sdb1

在剛剛切割好的磁碟/dev/sdb1建立physical volume,並且擴展(extend)到原本的volume group到這個volume。
volume group的名稱,可通過vgdisplay的命令查到。

root@ubuntu:/# sudo pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
root@ubuntu:/# sudo vgextend  ubuntu-vg /dev/sdb1
  Volume group "ubuntu-vg" successfully extended

使用vgdisplay指令可以看volume group資訊,也確認一下剛剛的操作有沒有成功

  Volume group "ubuntu-vg" successfully extended
root@ubuntu:/# sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               139.28 GiB
  PE Size               4.00 MiB
  Total PE              35655
  Alloc PE / Size       10056 / 39.28 GiB
  Free  PE / Size       25599 / 100.00 GiB
  VG UUID               Lz4ixT-KeGT-kcDb-lf9N-Tykq-LChm-SGwOlG

發現原本40G空間變成了139G!

接著確認目前的磁區與檔案系統,要將新擴展的空間掛載到根目錄

root@ubuntu:/# df -m
Filesystem                  1M-blocks  Used Available Use% Mounted on
udev                              980     0       980   0% /dev
tmpfs                             201     6       195   3% /run
/dev/mapper/ubuntu--vg-root     38500 36046       476  99% /
tmpfs                            1001     0      1001   0% /dev/shm
tmpfs                               5     0         5   0% /run/lock
tmpfs                            1001     0      1001   0% /sys/fs/cgroup
/dev/sda1                         720    58       626   9% /boot
tmpfs                             201     0       201   0% /run/user/112
tmpfs                             201     0       201   0% /run/user/0
root@ubuntu:/# df
Filesystem                  1K-blocks     Used Available Use% Mounted on
udev                          1003008        0   1003008   0% /dev
tmpfs                          204816     6016    198800   3% /run
/dev/mapper/ubuntu--vg-root  39423400 36910900    486824  99% /
tmpfs                         1024068        0   1024068   0% /dev/shm
tmpfs                            5120        0      5120   0% /run/lock
tmpfs                         1024068        0   1024068   0% /sys/fs/cgroup
/dev/sda1                      736752    59236    640092   9% /boot
tmpfs                          204816        0    204816   0% /run/user/112
tmpfs                          204816        0    204816   0% /run/user/0
root@ubuntu:/# sudo lvextend /dev/mapper/ubuntu--vg-root dev/sdb1
  Size of logical volume ubuntu-vg/root changed from 38.32 GiB (9811 extents) to 138.32 GiB (35410 extents).
  Logical volume root successfully resized.

最後resize 這個空間為ext4,成功的話就能看到根目錄增加了100G

root@ubuntu:/# sudo resize2fs /dev/
Display all 199 possibilities? (y or n)
root@ubuntu:/# sudo resize2fs /dev/ubuntu-vg/
root    swap_1
root@ubuntu:/# sudo resize2fs /dev/ubuntu-vg/root
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/ubuntu-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 9
The filesystem on /dev/ubuntu-vg/root is now 36259840 (4k) blocks long.

root@ubuntu:/# df -m
Filesystem                  1M-blocks  Used Available Use% Mounted on
udev                              980     0       980   0% /dev
tmpfs                             201     6       195   3% /run
/dev/mapper/ubuntu--vg-root    139289 36058     97160  28% /
tmpfs                            1001     0      1001   0% /dev/shm
tmpfs                               5     0         5   0% /run/lock
tmpfs                            1001     0      1001   0% /sys/fs/cgroup
/dev/sda1                         720    58       626   9% /boot
tmpfs                             201     0       201   0% /run/user/112
tmpfs                             201     0       201   0% /run/user/0