Linux 分割槽擴容(根分割槽擴容,SWAP 分割槽擴容,掛載新分割槽為目錄)

sysin發表於2021-08-27

請訪問原文連結:https://sysin.org/blog/linux-partition-expansion/,檢視最新版。原創作品,轉載請保留出處。

作者:gc(at)sysin.org,主頁:www.sysin.org

目錄

    1. 根分割槽擴容
    • 1.1 標準分割槽擴容(非 LVM)
    • 1.2 LVM 分割槽擴容
    1. SWAP 分割槽擴容
    • 2.1 建立檔案作為 SWAP 分割槽
    • 2.2 標準分割槽 SWAP 擴容
    • 2.3 LVM SWAP 擴容
    1. 掛載新的磁碟到新的分割槽
    • 3.1 將磁碟掛載為新的目錄
    • 3.2 將磁碟掛載原有目錄
    1. 小結

Linux 系統在執行過程中,出現磁碟空間不足,需要擴容該如何處理?本文描述了常見的擴容場景,包括根分割槽、SWAP 分割槽以及擴容某個目錄。

1. 根分割槽擴容

1.1 標準分割槽擴容(非 LVM)

本例為 CentOS 8 虛機,兩塊磁碟,磁碟 1 容量 60G 用於根目錄(包含 /boot),磁碟 2 容量 4G 用於 SWAP。

(1)擴容前狀態如下:

[root@sysin-c8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   60G  0 disk
└─sda1   8:1    0   60G  0 part /
sdb      8:16   0    4G  0 disk
└─sdb1   8:17   0    4G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom

[root@sysin-c8 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  386M     0  386M   0% /dev
tmpfs          tmpfs     400M     0  400M   0% /dev/shm
tmpfs          tmpfs     400M   11M  389M   3% /run
tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
/dev/sda1      xfs        60G  1.8G   59G   3% /
tmpfs          tmpfs      80M     0   80M   0% /run/user/0

(2)將虛機中將磁碟 1 容量擴充套件為 100G,這個過程就不截圖了。

若支援線上新增,可通過命令重新整理磁碟狀態:partprobe /dev/sda

(3)開始擴容根目錄:

[root@sysin-c8 ~]# fdisk /dev/sda

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


Command (m for help):

(4)可以按 m 檢視一下幫助:

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help):

(5)按 p 檢視當前磁碟下的分割槽:

Command (m for help): p
Disk /dev/sda: 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: 0x7bb4c495

Device     Boot Start       End   Sectors Size Id Type
/dev/sda1  *     2048 125829119 125827072  60G 83 Linux

# 本例該磁碟僅僅一個分割槽,Boot 下面有個可啟動標記 *,/boot 沒有獨立分割槽

(6)按 d 刪除 / 分割槽:

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help):
# 本例中只有一個分割槽,所以直接刪除了,如果是有多個分割槽,會提示輸入數字選擇

(7)按 n 建立新分割槽:

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  #選擇 p primary
Partition number (1-4, default 1):  #直接回車預設 1 即 sda1
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.
Partition #1 contains a xfs signature.

Do you want to remove the signature? [Y]es/[N]o: N  #按 N 保留 xfs 簽名,移除的話分割槽的 UUID 會變更。

The signature will be removed by a write command.

Command (m for help):

(8)按 p 再次檢視狀態:

Command (m for help): p
Disk /dev/sda: 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: 0x6a72cc03

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1        2048 209715199 209713152  100G 83 Linux

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help):

(9)重要步驟:按 a 設定可引導:

本例 /boot 沒有獨立分割槽,需要需要設定 boot flag 即將分割槽設定為可引導:

/boot 獨立分割槽的不需要此步驟。

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

# 按 p 再次確認,Boot 下面有了 * 符號
Command (m for help): p
Disk /dev/sda: 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: 0x6a72cc03

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1  *     2048 209715199 209713152  100G 83 Linux

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help):

(10)按 w 儲存:

Command (m for help): w
The partition table has been altered.
Syncing disks.

(11)重要步驟:同步檔案系統中的容量。

CentOS 7 開始預設使用 xfs 檔案系統,使用 xfs_growfs 命令同步檔案系統容量。

如果是 Ext4(包括 2、3),使用 resize2fs 命令。

xfs_growfs /
# 注意 xfs_growfs 使用 mountpoint
#resize2fs /dev/sda1
# resize2fs 則使用 device

(12)確認分割槽結果,可以重啟一下系統確認是否正常

[root@sysin-c8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk
└─sda1   8:1    0  100G  0 part /
sdb      8:16   0    4G  0 disk
└─sdb1   8:17   0    4G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom
[root@sysin-c8 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  386M     0  386M   0% /dev
tmpfs          tmpfs     400M     0  400M   0% /dev/shm
tmpfs          tmpfs     400M   11M  389M   3% /run
tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
/dev/sda1      xfs       100G  2.1G   98G   3% /
tmpfs          tmpfs      80M     0   80M   0% /run/user/0
[root@sysin-c8 ~]#

至此擴容成功完成。

1.2 LVM 分割槽擴容

LVM 名詞解釋:

LVM (logical volume manager) 邏輯卷管理器

其中主要分為這幾個概念:

  • 物理卷 - Physical volume 簡稱 PV

    物理卷在邏輯卷管理器中屬於最底層的,任何的邏輯卷和卷組都必需依靠物理捲來建立,物理卷可以是一個完整的硬碟,也可以是硬碟中的莫一個分割槽。

  • 卷組 - Volume group 簡稱 VG

    卷組是建立在物理卷之上,一個卷組中可以包含一個或者多個物理卷。

  • 邏輯卷 - Logical volume 簡稱 LV

    邏輯卷類似於非 LVM 系統中的硬碟分割槽,在邏輯卷之上可以建立檔案系統 (比如 /home 或者 /usr 等)。

一個建立邏輯卷的流程如下:PV -> VG -> LV,物理捲包含卷組,卷組包含邏輯卷。

本例為 CentOS 7,一塊磁碟,獨立 /boot 分割槽,兩個 LVM 分割槽,如下:

# root @ C7 in ~ [12:41:56]
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  160G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  159G  0 part
  ├─centos-root 253:0    0  155G  0 lvm  /
  └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom

# root @ C7 in ~ [12:41:56]
$ df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       155G  1.5G  154G   1% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  142M  873M  14% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0

將該虛擬磁碟擴充套件到 260G。

說明:也可以新增一塊磁碟,可以模擬新增了物理磁碟,只是下面碟符對應修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。

(1)建立分割槽

$ fdisk /dev/sda  #對原磁碟 /dev/sda 分割槽
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p  #p 檢視當前分割槽

Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364

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

Command (m for help): n  #n 新建分割槽
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p  #p 主分割槽
Partition number (3,4, default 3):  #預設按順序,直接回車
First sector (335544320-545259519, default 335544320):  #預設直接回車
Using default value 335544320
Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519):  #預設直接回車使用全部剩餘空間
Using default value 545259519
Partition 3 of type Linux and of size 100 GiB is set

Command (m for help): p  #p 再次檢視分割槽

Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   335544319   166722560   8e  Linux LVM
/dev/sda3       335544320   545259519   104857600   83  Linux

Command (m for help): w  #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.

(2)重新整理分割槽

# root @ C7 in ~ [13:31:00]
$ partprobe /dev/sda

# root @ C7 in ~ [13:31:54]
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  260G  0 disk
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0  159G  0 part
│ ├─centos-root 253:0    0  155G  0 lvm  /
│ └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
└─sda3            8:3    0  100G  0 part
sr0              11:0    1 1024M  0 rom

(3)建立 PV

$ pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

(4)檢視 VG

$ vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <159.00 GiB
  PE Size               4.00 MiB
  Total PE              40703
  Alloc PE / Size       40703 / <159.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw

VG 名稱為 centos

(5)擴充套件 VG

使用 /dev/sda3 PV 擴充套件到 centos VG 中。

$ vgextend centos /dev/sda3
  Volume group "centos" successfully extended

(6)檢視 LV

$ lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-08-22 14:12:50 +0800
  LV Status              available
  # open                 1
  LV Size                <155.00 GiB
  Current LE             39679
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-08-22 14:12:51 +0800
  LV Status              available
  # open                 2
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

需要擴充套件的根分割槽 LV 為 /dev/centos/root

(7)將 VG 中的空閒空間擴充套件到根分割槽 LV

$ lvextend -l +100%FREE /dev/centos/root
  Size of logical volume centos/root changed from <155.00 GiB (39679 extents) to 254.99 GiB (65278 extents).
  Logical volume centos/root successfully resized.

(8)重新整理根分割槽

如果是 Ext4(包括 2、3),使用 resize2fs 命令。

xfs_growfs /dev/centos/root

(9)驗證結果

# root @ C7 in ~ [13:39:53]
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  260G  0 disk
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0  159G  0 part
│ ├─centos-root 253:0    0  255G  0 lvm  /
│ └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
└─sda3            8:3    0  100G  0 part
  └─centos-root 253:0    0  255G  0 lvm  /
sr0              11:0    1 1024M  0 rom

# root @ C7 in ~ [13:39:53]
$ df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       255G  1.5G  254G   1% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  142M  873M  14% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0

可以看到根目錄容量擴充套件成功。

2. SWAP 分割槽擴容

2.1 建立檔案作為 SWAP 分割槽

這是最簡單的方式,直接在 / 目錄下建立一個檔案作為交換分割槽。

(1)建立要作為 swap 分割槽的檔案:

增加 1GB 大小的交換分割槽,則命令寫法如下,其中的 count 等於想要的塊的數量(bs*count = 檔案大小)。

mkdir /swap

dd if=/dev/zero of=/swap/swapfile bs=1M count=1024

(2)建立 SWAP 分割槽檔案系統:

mkswap /swap/swapfile #mkswap - set up a Linux swap area

(3)啟用交換分割槽檔案:

swapon /swap/swapfile #啟用 swap 檔案

此時使用 free -m 命令可以看到 Swap 的容量等於原有容量加上上述建立檔案容量之和。

(4)編輯 /etc/fstab 開機自動載入上述 swap 檔案:

增加如下一行。

/swap/swapfile swap swap defaults 0 0

(5)取消 swap 檔案

swapoff /swap/swapfile
rm -rf /swap/

然後編輯 /etc/fstab,刪除上述新增的一行即可。

2.2 標準分割槽 SWAP 擴容

本例為 CentOS 8 虛機,兩塊磁碟,磁碟 1 容量 60G 用於根目錄(包含 /boot),磁碟 2 容量 4G 用於 SWAP。

(1)擴容前狀態如下:

[root@sysin-c8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   60G  0 disk
└─sda1   8:1    0   60G  0 part /
sdb      8:16   0    4G  0 disk
└─sdb1   8:17   0    4G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom

[root@sysin-c8 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  386M     0  386M   0% /dev
tmpfs          tmpfs     400M     0  400M   0% /dev/shm
tmpfs          tmpfs     400M   11M  389M   3% /run
tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
/dev/sda1      xfs        60G  1.8G   59G   3% /
tmpfs          tmpfs      80M     0   80M   0% /run/user/0

(2)將虛機中將磁碟 2 容量擴充套件為 16G,這個過程就不截圖了。

重新整理磁碟狀態:partprobe /dev/sdb

(3)關閉 swap

swapoff /dev/sdb1

(4) 重新建立 /dev/sdb1 分割槽

[root@sysin-c8 ~]# fdisk /dev/sdb  #選擇 /dev/sdb

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


Command (m for help): p  #檢視當然分割槽
Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 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: 0x316023cd

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 8388607 8386560   4G 82 Linux swap / Solaris

Command (m for help): d  #刪除分割槽,只有一個分割槽不會提示選擇數字
Selected partition 1
Partition 1 has been deleted.

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  #主分割槽,輸入 P 或者直接回車
Partition number (1-4, default 1):   #直接回車按順序 1
First sector (2048-33554431, default 2048):   #直接回車
Last sector, +sectors or +size{K,M,G,T,P} (2048-33554431, default 33554431):   #直接回車使用全部可用空間

Created a new partition 1 of type 'Linux' and of size 16 GiB.
Partition #1 contains a swap signature.

Do you want to remove the signature? [Y]es/[N]o: N  # 選擇 N 不要移除 swap 簽名,磁碟 UUID 不變

The signature will be removed by a write command.

Command (m for help): t  #更改分割槽型別
Selected partition 1
Hex code (type L to list all codes): L  #輸入 L,檢視可用程式碼

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT
Hex code (type L to list all codes): 82  #輸入 82,更改為 SWAP
Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p  #再次檢視分割槽,已經是 swap
Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 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: 0x316023cd

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 33554431 33552384  16G 82 Linux swap / Solaris

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help): w  #儲存退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).

最後提示需要重啟或者執行 partprobe、kpartx 命令生效,這裡執行命令:partprobe /dev/sdb1

筆者在一次測試中執行 partprobe 命令報錯,可能是分割槽過程中選擇移除了 swap 簽名,分割槽的 uuid 變更,直接重啟,要多等一會兒才能進入系統,但是不影響正常執行。

(5) 建立 swap 檔案系統

mkswap /dev/sdb1
# 提示如下
Setting up swapspace version 1, size = 16 GiB (17178816512 bytes)
no label, UUID=df11dbb4-9665-4732-b865-03913713fa5e
# 注意這裡的 UUID 變更,需要修改替換 /etc/fstab

(6) 開啟 swap

swapon /dev/sdb1

(7)更改 /etc/fstab (可選)

如果在重新建立分割槽的過程中移除了 swap 簽名,則磁碟 uuid 變更,需要編輯 /etc/fstab 做相應變更:

要檢視分割槽的 uuid 使用命令 blkid 或者 lsblk -f

#UUID=c154479a-28e1-40b2-b10c-646b41693f51 swap                    swap    defaults        0 0 #原有
UUID=df11dbb4-9665-4732-b865-03913713fa5e swap                    swap    defaults        0 0

當然也可以新建額外的分割槽作為增加的 swap 分割槽,原有 swap 分割槽不變,最後編輯 /etc/fstab 自動載入即可。

2.3 LVM SWAP 擴容

本例為 CentOS 7,一塊磁碟,獨立 /boot 分割槽,兩個 LVM 分割槽,如下:

# root @ C7 in ~ [12:41:56]
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  160G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  159G  0 part
  ├─centos-root 253:0    0  155G  0 lvm  /
  └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom

# root @ C7 in ~ [12:41:56]
$ df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       155G  1.5G  154G   1% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  142M  873M  14% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0

將該虛擬磁碟容量增加 12G。

說明:也可以新增一塊磁碟,可以模擬新增了物理磁碟,只是下面碟符對應修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。

(1)關閉 swap

檢視 /etc/fstab 可以看到 swap 的 LV 名稱:

$ cat /etc/fstab | grep swap
/dev/mapper/centos-swap swap                    swap    defaults        0 0

將其關閉:

swapoff /dev/mapper/centos-swap

(2)建立分割槽

$ fdisk /dev/sda  #對原磁碟 /dev/sda 分割槽,如果是新增磁碟只是替換碟符為 /dev/sdb 流程是一樣的
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p  #p 檢視當前分割槽

Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364

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

Command (m for help): n  #n 新建分割槽
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p  #p 主分割槽
Partition number (3,4, default 3):  #預設按順序,直接回車
First sector (335544320-545259519, default 335544320):  #預設直接回車
Using default value 335544320
Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519):  #預設直接回車使用全部剩餘空間
Using default value 545259519
Partition 3 of type Linux and of size 100 GiB is set

Command (m for help): p  #p 再次檢視分割槽

Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   335544319   166722560   8e  Linux LVM
/dev/sda3       335544320   545259519   104857600   83  Linux

Command (m for help): w  #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.

$ fdisk /dev/sda  #對原磁碟 /dev/sda 分割槽
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p  #檢視當前分割槽

Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 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: 0x000af364

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

Command (m for help): n  #新建分割槽
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):  #直接回車,預設主分割槽
Using default response p
Partition number (3,4, default 3):   #直接回車預設按順序 3
First sector (335544320-360710143, default 335544320):   #直接回車
Using default value 335544320
Last sector, +sectors or +size{K,M,G} (335544320-360710143, default 360710143):   #直接回車,使用全部剩餘空間
Using default value 360710143
Partition 3 of type Linux and of size 12 GiB is set   #12G 沒錯

Command (m for help): p  #再次檢視分割槽

Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 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: 0x000af364

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   335544319   166722560   8e  Linux LVM
/dev/sda3       335544320   360710143    12582912   83  Linux

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.
You have new mail.

(3)重新整理分割槽

# root @ C7 in ~ [13:31:00]
$ partprobe /dev/sda

# root @ C7 in ~ [13:31:54]
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  172G  0 disk
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0  159G  0 part
│ ├─centos-root 253:0    0  155G  0 lvm  /
│ └─centos-swap 253:1    0    4G  0 lvm
└─sda3            8:3    0   12G  0 part
sr0              11:0    1 1024M  0 rom

(4)建立 PV

$ pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

(5)檢視 VG

$ vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <159.00 GiB
  PE Size               4.00 MiB
  Total PE              40703
  Alloc PE / Size       40703 / <159.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw

VG 名稱為 centos

(6)擴充套件 VG

使用 /dev/sda3 PV 擴充套件到 centos VG 中。

$ vgextend centos /dev/sda3
  Volume group "centos" successfully extended

(7)檢視 LV

$ lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-08-22 14:12:50 +0800
  LV Status              available
  # open                 1
  LV Size                <155.00 GiB
  Current LE             39679
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi
  LV Write Access        read/write
  LV Creation host, time localhost, 2021-08-22 14:12:51 +0800
  LV Status              available
  # open                 2
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

需要擴充套件的根分割槽 LV 為 /dev/centos/swap

(8)將 VG 中的空閒空間擴充套件到根分割槽 LV

$ lvextend -l +100%FREE /dev/centos/swap
  Size of logical volume centos/swap changed from 4.00 GiB (1024 extents) to <16.00 GiB (4095 extents).
  Logical volume centos/swap successfully resized.

(9)重新整理 swap 分割槽

partprobe /dev/centos/swap

(10)驗證結果

# root @ C7 in ~ [15:54:13]
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  172G  0 disk
├─sda1            8:1    0    1G  0 part /boot
├─sda2            8:2    0  159G  0 part
│ ├─centos-root 253:0    0  155G  0 lvm  /
│ └─centos-swap 253:1    0   16G  0 lvm
└─sda3            8:3    0   12G  0 part
  └─centos-swap 253:1    0   16G  0 lvm
sr0              11:0    1 1024M  0 rom

# root @ C7 in ~ [15:54:13]
$ df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs       155G  1.5G  154G   1% /
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  142M  873M  14% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0

可以看到根目錄容量擴充套件成功。

(11)建立 swap 檔案系統

$ mkswap /dev/mapper/centos-swap
mkswap: /dev/mapper/centos-swap: warning: wiping old swap signature.
Setting up swapspace version 1, size = 16773116 KiB
no label, UUID=fb85ef4d-59aa-4554-a3bd-b4cc37746c51

(12)開啟 swap

swapon /dev/mapper/centos-swap

使用 free -m 可以檢視 swap 分割槽容量成功變為 16G,/etc/fstab 無需修改。

3. 掛載新的磁碟到新的分割槽

本例使用 Ubuntu 20.04,分割槽如下,我們新增一塊 100G 的磁碟,掛載為 /data ,同時新增一塊 100G 的磁碟,用來替換原有的 /var 目錄(假定原有磁碟分割槽不夠用)。當然我們也可以直接擴容根分割槽參看本文上述描述。這裡我們分別建立獨立的 /data/var 分割槽。

建立獨立的 /data/var 分割槽也可以使用 LVM 的方式,具體操作不同但是邏輯步驟是一樣的,可以參看其他文章。本文使用標準分割槽。

$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                         8:0    0  160G  0 disk
├─sda1                      8:1    0    1M  0 part
├─sda2                      8:2    0    1G  0 part /boot
└─sda3                      8:3    0  159G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0  159G  0 lvm  /
sr0                        11:0    1 1024M  0 rom

# sa @ U20 in ~ [16:14:05]
$ df -Th
Filesystem                        Type      Size  Used Avail Use% Mounted on
udev                              devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                             tmpfs     391M  1.1M  390M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4      156G  6.0G  142G   5% /
tmpfs                             tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda2                         ext4      976M  103M  806M  12% /boot
tmpfs                             tmpfs     391M     0  391M   0% /run/user/1000

Ubuntu 比較意思,這裡有個 1M 的分割槽,可能跟 這裡的討論 相關。

新增硬碟後,如下,如果看不到新增磁碟需要重啟或者執行 partprobe 命令。

$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                         8:0    0  160G  0 disk
├─sda1                      8:1    0    1M  0 part
├─sda2                      8:2    0    1G  0 part /boot
└─sda3                      8:3    0  159G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0  159G  0 lvm  /
sdb                         8:16   0  100G  0 disk
sdc                         8:32   0  100G  0 disk
sr0                        11:0    1 1024M  0 rom

3.1 將磁碟掛載為新的目錄

(1)建立分割槽

# sa @ U20 in ~ [16:23:43] C:1
$ sudo fdisk /dev/sdb  #將 /dev/sdb 建立分割槽
[sudo] password for sa:

Welcome to fdisk (util-linux 2.34).
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 0x9a7ae73a.

Command (m for help): n  #n 新建分割槽
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):   #直接回車,主分割槽

Using default response 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): p  #檢視分割槽
Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: VMware Virtual S
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: 0x9a7ae73a

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 209715199 209713152  100G 83 Linux

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

(2)建立檔案系統(格式化分割槽)

sudo mkfs -t ext4 /dev/sdb1
# 上述使用 ext4,也可以使用 xfs

(3)掛載到 /data 目錄

sudo mkdir /data
sudo mount /dev/sdb1  /data

(4)開機自動載入

編輯 /etc/fstab ,新增如下一行:

/dev/sdb1 /data ext4 defaults 0 0

(5)驗證

重啟後檢視是否正確載入了:

$ df -Th
Filesystem                        Type      Size  Used Avail Use% Mounted on
udev                              devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                             tmpfs     391M  1.2M  390M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4      156G  6.0G  142G   5% /
tmpfs                             tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda2                         ext4      976M  103M  806M  12% /boot
tmpfs                             tmpfs     391M     0  391M   0% /run/user/1000
/dev/sdb1                         ext4       98G   61M   93G   1% /data

3.2 將磁碟掛載原有目錄

(1)建立分割槽

# sa @ U20 in ~ [16:23:43] C:1
$ sudo fdisk /dev/sdc  #將 /dev/sdc 建立分割槽
[sudo] password for sa:

Welcome to fdisk (util-linux 2.34).
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 0x9a7ae73a.

Command (m for help): n  #n 新建分割槽
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):   #直接回車,主分割槽

Using default response 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): p  #檢視分割槽
Disk /dev/sdc: 100 GiB, 107374182400 bytes, 209715200 sectors
Disk model: VMware Virtual S
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: 0x9a7ae73a

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdc1        2048 209715199 209713152  100G 83 Linux

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

(2)建立檔案系統(格式化分割槽)

sudo mkfs -t xfs /dev/sdc1
# 上述使用 xfs,也可以使用 ext4

(3)掛載到 /varnew 目錄

sudo mkdir /varnew
sudo mount /dev/sdc1  /varnew

(4)關鍵步驟:拷貝 /var 下的所有內容到新的硬碟

sudo cp -a /var/** /varnew

釋義:

"-a" 選項相當於 "-d、-p、-r" 選項,使用 "-a" 選項時,目標檔案和原始檔的所有屬性都一致,包括原始檔的所有者,所屬組、時間和軟連結性。

這裡使用了兩個 “**”,確保如果 Bash 開啟了 Globstar 選項後可以複製所有檔案。

(5)重新命名當前 /var 目錄(可選,也可以直接刪除)

sudo mv /var /varold

(6)重新掛載硬碟到 /var 目錄

sudo umount /dev/sdc1
sudo mkdir /var
sudo mount /dev/sdc1 /var

(7)設定開機啟動自動掛載

#sudo vi /etc/fstab
#在檔案的最後增加一行
/dev/sdc1 /var xfs defaults 0 0

(8)刪除原有 /var 目錄

經過確認沒有問題後,徹底刪除原有 /var

sudo rm -rf /varold

4. 小結

使用標準分割槽擴容相比 LVM 似乎要簡單一些,特別是在虛機環境中,虛擬磁碟是可以直接擴充套件容量的,並不需要使用 LVM 的特性。

而在物理機環境中,新增物理磁碟擴容,使用 LVM 就發揮了優勢。

所以推薦在虛機或者雲環境中使用標準分割槽(獨立 swap 虛擬磁碟更便捷),物理機特別是資料量大的應用場景,可以考慮優先使用 LVM。

相關文章