Linux分區(qū)擴容方式(根分區(qū)擴容,SWAP分區(qū)擴容,掛載新分區(qū)為目錄)
Linux 分區(qū)擴容(根分區(qū)擴容,SWAP 分區(qū)擴容,掛載新分區(qū)為目錄)
Linux 系統(tǒng)在運行過程中,出現(xiàn)磁盤空間不足,需要擴容該如何處理?
本文描述了常見的擴容場景,包括根分區(qū)、SWAP 分區(qū)以及擴容某個目錄。
1. 根分區(qū)擴容
1.1 標(biāo)準(zhǔn)分區(qū)擴容(本站 OVF 默認(rèn))
本例為 CentOS 8 虛機,兩塊磁盤,磁盤 1 容量 60G 用于根目錄(包含 /boot
),磁盤 2 容量 4G 用于 SWAP。
(1)擴容前狀態(tài)如下:
[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,這個過程就不截圖了。
若支持在線添加,可通過命令刷新磁盤狀態(tài):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 查看當(dāng)前磁盤下的分區(qū):
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 # 本例該磁盤僅僅一個分區(qū),Boot 下面有個可啟動標(biāo)記 *,/boot 沒有獨立分區(qū)
(6)按 d 刪除 / 分區(qū):
Command (m for help): d Selected partition 1 Partition 1 has been deleted. Command (m for help): # 本例中只有一個分區(qū),所以直接刪除了,如果是有多個分區(qū),會提示輸入數(shù)字選擇
(7)按 n 創(chuàng)建新分區(qū):
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): #直接回車默認(rèn) 1 即 sda1 First sector (2048-209715199, default 2048): #直接回車默認(rèn)值 (sysin) Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): #直接回車默認(rèn)值,使用全部剩余空間 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 簽名,移除的話分區(qū)的 UUID 會變更。 The signature will be removed by a write command. Command (m for help):
(8)按 p 再次查看狀態(tài):
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 設(shè)置可引導(dǎo):
本例 /boot
沒有獨立分區(qū),需要需要設(shè)置 boot flag 即將分區(qū)設(shè)置為可引導(dǎo):
/boot
獨立分區(qū)的不需要此步驟。
Command (m for help): a Selected partition 1 The bootable flag on partition 1 is enabled now. # 按 p 再次確認(rèn),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)重要步驟:同步文件系統(tǒng)中的容量。
CentOS 7 開始默認(rèn)使用 xfs 文件系統(tǒng),使用 xfs_growfs 命令同步文件系統(tǒng)容量。
如果是 Ext4(包括 2、3),使用 resize2fs 命令。
xfs_growfs / # 注意 xfs_growfs 使用 mountpoint #resize2fs /dev/sda1 # resize2fs 則使用 device
(12)確認(rèn)分區(qū)結(jié)果,可以重啟一下系統(tǒng)確認(rèn)是否正常
[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 分區(qū)擴容
LVM 名詞解釋:
LVM (logical volume manager) 邏輯卷管理器
其中主要分為這幾個概念:
- 物理卷 - Physical volume 簡稱 PV
- 物理卷在邏輯卷管理器中屬于最底層的,任何的邏輯卷和卷組都必需依靠物理卷來建立,物理卷可以是一個完整的硬盤,也可以是硬盤中的莫一個分區(qū)。
- 卷組 - Volume group 簡稱 VG
- 卷組是建立在物理卷之上,一個卷組中可以包含一個或者多個物理卷。
- 邏輯卷 - Logical volume 簡稱 LV
- 邏輯卷類似于非 LVM 系統(tǒng)中的硬盤分區(qū),在邏輯卷之上可以建立文件系統(tǒng) (比如
/home
或者/usr
等)。
一個建立邏輯卷的流程如下:PV -> VG -> LV,物理卷包含卷組,卷組包含邏輯卷。
本例為 CentOS 7,一塊磁盤,獨立 /boot
分區(qū),兩個 LVM 分區(qū),如下:
# root @ C7-SYSIN 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-SYSIN 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。
說明:也可以添加一塊磁盤,可以模擬新增了物理磁盤,只是下面盤符對應(yīng)修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。
(1)創(chuàng)建分區(qū)
$ fdisk /dev/sda #對原磁盤 /dev/sda 分區(qū) 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 查看當(dāng)前分區(qū) 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 新建分區(qū) Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p #p 主分區(qū) Partition number (3,4, default 3): #默認(rèn)按順序,直接回車 First sector (335544320-545259519, default 335544320): #默認(rèn)直接回車 Using default value 335544320 Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519): #默認(rèn)直接回車使用全部剩余空間 Using default value 545259519 Partition 3 of type Linux and of size 100 GiB is set Command (m for help): p #p 再次查看分區(qū) 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)刷新分區(qū)
# root @ C7-SYSIN in ~ [13:31:00] $ partprobe /dev/sda # root @ C7-SYSIN 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)創(chuàng)建 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
需要擴展的根分區(qū) LV 為 /dev/centos/root
(7)將 VG 中的空閑空間擴展到根分區(qū) 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)刷新根分區(qū)
如果是 ext4 文件系統(tǒng)(包括 2、3),使用 resize2fs 命令。
xfs_growfs /dev/centos/root
(9)驗證結(jié)果
# root @ C7-SYSIN 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-SYSIN 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 分區(qū)擴容
2.1 創(chuàng)建文件作為 SWAP 分區(qū)(本站 OVF 默認(rèn))
這是最簡單的方式,直接在 / 目錄下創(chuàng)建一個文件作為交換分區(qū),可以非常靈活完美的啟用和禁用 swap 并在線調(diào)整大小。
(1)創(chuàng)建要作為 swap 分區(qū)的文件:
增加 4GB 大小的交換分區(qū),則命令寫法如下,其中的 count 等于想要的塊的數(shù)量(bs*count = 文件大小)。
# 這里定義為 /swap.img dd if=/dev/zero of=/swap.img bs=1M count=4096 # 修改權(quán)限 chmod 600 /swap.img
(2)創(chuàng)建 SWAP 分區(qū)文件系統(tǒng):
mkswap /swap.img #mkswap - set up a Linux swap area
(3)啟用交換分區(qū)文件:
swapon /swap.img #啟用 swap 文件
此時使用 free -m
命令可以看到 Swap 的容量等于原有容量加上上述創(chuàng)建文件容量之和。
(4)編輯 /etc/fstab
開機自動加載上述 swap 文件:
增加如下一行。
/swap.img none swap defaults 0 0 # 第二個字段在 CentOS 中默認(rèn)使用 swap,兩者并沒有不一樣。 /swap.img swap swap defaults 0 0 # 關(guān)于字段的風(fēng)格,使用 tab 或者空格都可以,數(shù)量沒有要求,通常為了對齊使用多個空格
直接命令添加:
sudo sh -c "echo '/swap.img none swap defaults 0 0' >> /etc/fstab"
(5)取消 swap 文件
swapoff /swap.img rm -r /swap.img
然后編輯 /etc/fstab
,刪除上述添加的一行即可。
Ubuntu 20.04 中默認(rèn)使用 /swap.img
文件作為 SWAP 分區(qū),可以直接 swapoff 并刪除原文件,然后重新創(chuàng)建同名文件來簡單擴容。我們這里借鑒了 Ubuntu 的做法,并推薦使用該種方式。
2.2 標(biāo)準(zhǔn)分區(qū) SWAP 擴容
本例為 CentOS 8 虛機,兩塊磁盤,磁盤 1 容量 60G 用于根目錄(包含 /boot
),磁盤 2 容量 4G 用于 SWAP。
(1)擴容前狀態(tài)如下:
[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,這個過程就不截圖了。
刷新磁盤狀態(tài):partprobe /dev/sdb
(3)關(guān)閉 swap
swapoff /dev/sdb1
(4) 重新創(chuàng)建 /dev/sdb1
分區(qū)
[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 #查看當(dāng)然分區(qū) 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 #刪除分區(qū),只有一個分區(qū)不會提示選擇數(shù)字 Selected partition 1 Partition 1 has been deleted. Command (m for help): n #新建分區(qū) Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p #主分區(qū),輸入 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 #更改分區(qū)類型 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 #再次查看分區(qū),已經(jīng)是 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 命令生效,這里執(zhí)行命令:partprobe /dev/sdb1
。
筆者在一次測試中執(zhí)行 partprobe 命令報錯,可能是分區(qū)過程中選擇移除了 swap 簽名,分區(qū)的 UUID 變更,直接重啟,要多等一會兒才能進入系統(tǒng),但是不影響正常運行。
(5) 創(chuàng)建 swap 文件系統(tǒng)
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
如果在重新創(chuàng)建分區(qū)的過程中移除了 swap 簽名,則磁盤 UUID 變更,需要編輯 /etc/fstab
做相應(yīng)變更。
即使沒有移除 swap 簽名,使用 mkswap 命令后 UUID 也變更了,也需要編輯 /etc/fstab
做相應(yīng)變更。
要查看分區(qū)的 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
當(dāng)然也可以新建額外的分區(qū)作為增加的 swap 分區(qū),原有 swap 分區(qū)不變,最后編輯 /etc/fstab
自動加載即可。
(8)更改 /etc/default/grub
grub 配置文件中有個 resume 選項,指向 swap 分區(qū),如果標(biāo)準(zhǔn)分區(qū)默認(rèn)使用的是 swap 分區(qū)的 UUID。
vi /etc/default/grub # 找到如下行 GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=c154479a-28e1-40b2-b10c-646b41693f51 rhgb quiet" #原有 # 修改如下 GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=df11dbb4-9665-4732-b865-03913713fa5e rhgb quiet" # 重建配置 grub2-mkconfig -o /boot/grub2/grub.cfg # UEFI boot mode: #grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
2.3 LVM SWAP 擴容
本例為 CentOS 7,一塊磁盤,獨立 /boot
分區(qū),兩個 LVM 分區(qū),如下:
# root @ C7-SYSIN 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-SYSIN 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。
說明:也可以添加一塊磁盤,可以模擬新增了物理磁盤,只是下面盤符對應(yīng)修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。
(1)關(guān)閉 swap
查看 /etc/fstab
可以看到 swap 的 LV 名稱:
$ cat /etc/fstab | grep swap /dev/mapper/centos-swap swap swap defaults 0 0
將其關(guān)閉:
swapoff /dev/mapper/centos-swap
(2)創(chuàng)建分區(qū)
$ fdisk /dev/sda #對原磁盤 /dev/sda 分區(qū),也可以是新增的磁盤,按順序使用 /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 #查看當(dāng)前分區(qū) 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 #新建分區(qū) Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): #直接回車,默認(rèn)主分區(qū) Using default response p Partition number (3,4, default 3): #直接回車默認(rèn)按順序 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 #再次查看分區(qū) 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)刷新分區(qū)
# root @ C7-SYSIN in ~ [13:31:00] $ partprobe /dev/sda # root @ C7-SYSIN 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)創(chuàng)建 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
需要擴展的根分區(qū) LV 為 /dev/centos/swap
(8)將 VG 中的空閑空間擴展到根分區(qū) 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 分區(qū)
partprobe /dev/centos/swap
(10)驗證結(jié)果
# root @ C7-SYSIN 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-SYSIN 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)創(chuàng)建 swap 文件系統(tǒng)
$ 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 分區(qū)容量成功變?yōu)?16G,/etc/fstab
無需修改。
3. 掛載新的磁盤到新的分區(qū)
本例使用 Ubuntu 20.04,分區(qū)如下,我們新增一塊 100G 的磁盤,掛載為 /data
,同時新增一塊 100G 的磁盤,用來替換原有的 /var
目錄(假定原有磁盤分區(qū)不夠用)。當(dāng)然我們也可以直接擴容根分區(qū)參看本文上述描述。這里我們分別創(chuàng)建獨立的 /data
和 /var
分區(qū)。
創(chuàng)建獨立的 /data
和 /var
分區(qū)也可以使用 LVM 的方式,具體操作不同但是邏輯步驟是一樣的,可以參看其他文章。本文使用標(biāo)準(zhǔn)分區(qū)。
$ 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 的分區(qū)為 bios_grub,無論使用 LVM 或是標(biāo)準(zhǔn)分區(qū)都會自動創(chuàng)建。
新增硬盤后,如下,如果看不到新增磁盤需要重啟或者執(zhí)行 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)創(chuàng)建分區(qū)
# sa @ U20 in ~ [16:23:43] C:1 $ sudo fdisk /dev/sdb #將 /dev/sdb 創(chuàng)建分區(qū) [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 新建分區(qū) Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): #直接回車,主分區(qū) 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 #查看分區(qū) 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)創(chuàng)建文件系統(tǒng)(格式化分區(qū))
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)創(chuàng)建分區(qū)
# sa @ U20 in ~ [16:23:43] C:1 $ sudo fdisk /dev/sdc #將 /dev/sdc 創(chuàng)建分區(qū) [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 新建分區(qū) Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): #直接回車,主分區(qū) 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 #查看分區(qū) 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)創(chuàng)建文件系統(tǒng)(格式化分區(qū))
sudo mkfs -t xfs /dev/sdc1 # 上述使用 xfs,也可以使用 ext4
(3)掛載到 /varnew
目錄
sudo mkdir /varnew sudo mount /dev/sdc1 /varnew
(4)關(guān)鍵步驟:拷貝 /var
下的所有內(nèi)容到新的硬盤
sudo cp -a /var/** /varnew
釋義:
“-a” 選項相當(dāng)于 “-d、-p、-r” 選項,使用 “-a” 選項時,目標(biāo)文件和源文件的所有屬性都一致,包括源文件的所有者,所屬組、時間和軟鏈接性。
這里使用了兩個 “**”,確保如果 Bash 開啟了 Globstar 選項后可以復(fù)制所有文件。
(5)重命名當(dāng)前 /var
目錄(可選,也可以直接刪除)
sudo mv /var /varold
(6)重新掛載硬盤到 /var
目錄
sudo umount /dev/sdc1 sudo mkdir /var sudo mount /dev/sdc1 /var
(7)設(shè)置開機啟動自動掛載
#sudo vi /etc/fstab #在文件的最后增加一行 /dev/sdc1 /var xfs defaults 0 0
(8)刪除原有 /var
目錄
經(jīng)過確認(rèn)沒有問題后,徹底刪除原有 /var
sudo rm -rf /varold
4. 小結(jié)
使用標(biāo)準(zhǔn)分區(qū)擴容相比 LVM 似乎要簡單一些,特別是在虛機環(huán)境中,虛擬磁盤是可以直接擴展容量的,并不需要使用 LVM 的特性。
而在物理機環(huán)境中,新增物理磁盤擴容,使用 LVM 就發(fā)揮了優(yōu)勢。
所以推薦在虛機或者云環(huán)境中使用標(biāo)準(zhǔn)分區(qū)(獨立 swap 虛擬磁盤或者使用 swap 文件更便捷),物理機特別是數(shù)據(jù)量大的應(yīng)用場景,可以考慮優(yōu)先使用 LVM。
5.附加
MBR與GPT對比
類別 | 主引導(dǎo)方式 | 主分區(qū)數(shù)量 | 最大容量 | 支持多少位系統(tǒng) | 分區(qū)方法 |
---|---|---|---|---|---|
MBR | BIOS+MBR | 4 | 2T | 32和64 | fdisk |
GPT | UEFI+GPT | 128 | 18EB(1EB=1024PB=1048576TB) | 64 | parted |
Parted分區(qū)和創(chuàng)建邏輯卷LVM
當(dāng)對大于2TB硬盤進行分區(qū)時時,fdisk不再適用,需要使用parted對硬盤進行分區(qū)。
Parted分區(qū)過程:
parted -l #查看所有磁盤狀態(tài) parted /dev/vdb #通過parted工具來創(chuàng)建大于2T的分區(qū) mklabel gpt #創(chuàng)建創(chuàng)建磁盤標(biāo)簽 mkpart primary 0% 100% #創(chuàng)建整個分區(qū) q #退出 #其他命令 ------------------- (parted) mklabel #創(chuàng)建創(chuàng)建磁盤標(biāo)簽 New disk labeltype? gpt (parted) p #查看分區(qū)狀態(tài) (parted) mkpart Partition name? []? gpt2t #指定分區(qū)名稱 File system type? [ext2]ext3 #指定分區(qū)類型 Start? 1 #指定開始位置 End? 2190GB #指定結(jié)束位置 (parted) P #顯示分區(qū)信息 (parted) Q #退出
創(chuàng)建邏輯卷的過程
fdiks -l #查看分區(qū) pvcreate /dev/vdb1 #創(chuàng)建pv物理卷 vgcreate vgdata /dev/vdb1 #創(chuàng)建vg卷組 lvcreate -l +100%FREE -n lvdata vgdata #創(chuàng)建lv邏輯卷 mkfs.xfs /dev/mapper/vgdata-lvdata #格式化邏輯卷 mkdir /data #創(chuàng)建數(shù)據(jù)文件夾 mount /dev/mapper/vgdata-lvdata /data #將邏輯卷掛載到/data vim /etc/fastab #添加開機掛載 /dev/mapper/vgdata-lvdata /data xfs defaults 0 0 mount -a #檢查掛載
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Linux 6 修改ssh默認(rèn)遠(yuǎn)程端口號的操作步驟
這篇文章主要介紹了Linux 6 修改ssh默認(rèn)遠(yuǎn)程端口號的操作步驟,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09CentOS Linux系統(tǒng)搭建Android開發(fā)環(huán)境詳細(xì)介紹
這篇文章主要介紹了CentOS Linux系統(tǒng)搭建Android開發(fā)環(huán)境詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11Linux centos下設(shè)置定時備份任務(wù)的方法步驟
這篇文章主要介紹了Linux centos下設(shè)置定時備份任務(wù)的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01CentOS 7.2.1511 編譯安裝Nginx1.10.1+MySQL5.6.33+PHP5.6.26運行環(huán)境
這篇文章主要介紹了CentOS 7.2.1511 編譯安裝Nginx1.10.1+MySQL5.6.33+PHP5.6.26運行環(huán)境,需要的朋友可以參考下2016-10-10linux下apache開啟url重寫的方法(詳細(xì)說明)
Apache 2.x 中URL重寫,是通過mod_rewrite.so 來實現(xiàn)的,所以您要查看您的Apache 是否已經(jīng)被編譯進去這個模塊了,并且在Apache的配置文件httpd.conf 中已經(jīng)調(diào)用了這個模塊2012-04-04