ubuntu16.04 根分區(qū)擴容

ubuntu16.04 根分區(qū)設(shè)置太小,或是分區(qū)使用不合理,swap 分配過多,根目錄分配太少,導(dǎo)致整體的服務(wù)可用性和穩(wěn)定性較差,linux 2.6版本以后就可以實時擴容,下面內(nèi)容是通過parted 命令進行根分區(qū)擴容(將swap 分區(qū)刪除,并重新設(shè)置大小,并對根分區(qū)進行擴容操作):

查看根分區(qū)所在磁盤

root # fdisk -l
Disk /dev/sda: 109.2 TiB, 120015311339520 bytes, 234404904960 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes


Disk /dev/sdb: 447.1 GiB, 480103981056 bytes, 937703088 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x5ee33106

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sdb1            2048     63487     61440   30M 83 Linux
/dev/sdb2  *        63488 195375103 195311616 93.1G 83 Linux
/dev/sdb3       195377150 937701375 742324226  354G  5 Extended
/dev/sdb5       195377152 937701375 742324224  354G 82 Linux swap / Solaris

parted 命令分區(qū)resized

parted 的命令,默認是按照盤符的順序進行操作,后不加參數(shù),默認從/dev/sda 開始
步驟:

1. 先將swap 的分區(qū)刪除,回收空間
2. 用resizepart 對根分區(qū)進行擴容
3. 創(chuàng)建新的swap分區(qū),并設(shè)置空間大小,注意設(shè)置大小的時候要帶上單位
root # parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print all
Model: ATA Micron_5200_MTFD (scsi)
Disk /dev/sdb: 480GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system     Flags
 1      1049kB  32.5MB  31.5MB  primary   xfs
 2      32.5MB  100GB   100GB   primary   ext4            boot
 3      100GB   480GB   380GB   extended
 5      100GB   480GB   380GB   logical   linux-swap(v1)


Model: AVAGO MR9361-8i (scsi)
Disk /dev/sda: 120TB
Sector size (logical/physical): 512B/4096B
Partition Table: loop
Disk Flags:

Number  Start  End    Size   File system  Flags
 1      0.00B  120TB  120TB  ext4


(parted) rm 5 # 刪除已有分區(qū)回收空間
(parted) rm 3
(parted) print
Model: ATA Micron_5200_MTFD (scsi)
Disk /dev/sdb: 480GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  32.5MB  31.5MB  primary  xfs
 2      32.5MB  100GB   100GB   primary  ext4         boot

(parted) resizepart 2
Warning: Partition /dev/sdb2 is being used. Are you sure you want to continue?
Yes/No? yes
End?  [100GB]? 380G
(parted) print
Model: ATA Micron_5200_MTFD (scsi)
Disk /dev/sdb: 480GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  32.5MB  31.5MB  primary  xfs
 2      32.5MB  380GB   380GB   primary  ext4         boot

(parted) mkpart
Partition type?  primary/extended? primary
File system type?  [ext2]? linux-swap #設(shè)置文件系統(tǒng)類型
Start? 380 #需要增加單位此處為G
End? 480
Warning: You requested a partition from 380MB to 480MB (sectors 742187..937500).
The closest location we can manage is 380GB to 380GB (sectors 742187501..742187501).
Is this still acceptable to you?
Yes/No? No
(parted) mkpart #創(chuàng)建分區(qū)信息
Partition type?  primary/extended? primary
File system type?  [ext2]? linux-swap
Start? 380G
End? 480G
(parted)
(parted) P
Model: ATA Micron_5200_MTFD (scsi)
Disk /dev/sdb: 480GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  32.5MB  31.5MB  primary  xfs
 2      32.5MB  380GB   380GB   primary  ext4            boot
 3      380GB   480GB   100GB   primary  linux-swap(v1)  lba

(parted) p #查看分區(qū)信息
Model: ATA Micron_5200_MTFD (scsi)
Disk /dev/sdb: 480GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  32.5MB  31.5MB  primary  xfs
 2      32.5MB  380GB   380GB   primary  ext4            boot
 3      380GB   480GB   100GB   primary  linux-swap(v1)  lba

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

resize2fs 更新文件系統(tǒng)中的磁盤大小

root#  resize2fs /dev/sdb2
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/sdb2 is mounted on /; on-line resizing required
old_desc_blocks = 12, new_desc_blocks = 45
The filesystem on /dev/sdb2 is now 92765501 (4k) blocks long.

修改/etc/fstab 更新新的swap 的uuid

// 生成swap 數(shù)據(jù)分區(qū)的UUID
root #  mkswap /dev/sdb3  
root # lsblk -f
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda    ext4         cdc2e9d4-8c36-480d-b4eb-bb8a3d436da9 /mnt/data
sdb
├─sdb1 xfs          6fa0130f-c478-41d1-a4da-1aeb8b6c0a60
├─sdb2 ext4         93958216-43dc-47b7-889c-03abbe9e3050 /
└─sdb3 swap         391c489b-47b5-4054-914e-58691a4ec16e
root # vim /etc/fstab 
// 修改后保存即可

參考資料:

https://dade2.net/kb/how-to-resize-a-root-partition-in-ubuntu/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容