ubuntu16.04 根分区设置太小,或是分区使用不合理,swap 分配过多,根目录分配太少,导致整体的服务可用性和稳定性较差,linux 2.6版本以后就可以实时扩容,下面内容是通过parted 命令进行根分区扩容(将swap 分区删除,并重新设置大小,并对根分区进行扩容操作):
查看根分区所在磁盘
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 命令分区resized
parted 的命令,默认是按照盘符的顺序进行操作,后不加参数,默认从/dev/sda 开始
步骤:
1. 先将swap 的分区删除,回收空间
2. 用resizepart 对根分区进行扩容
3. 创建新的swap分区,并设置空间大小,注意设置大小的时候要带上单位
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 # 删除已有分区回收空间
(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 #设置文件系统类型
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 #创建分区信息
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 #查看分区信息
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 更新文件系统中的磁盘大小
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 数据分区的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/