Linux分区扩容笔记

因为需要搭建Kubernetes集群,我向老师申请了几个服务器以供使用。在拿到服务器后,发现硬盘实际容量有200G,但是在登录系统后仅有30G。在使用fdisk -l命令后输出如下:

1
2
3
4
5
6
7
8
9
10
Disk /dev/vda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c411d

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 62916607 31457280 83 Linux
/dev/vda2 62916608 67108863 2096128 82 Linux swap / Solaris

第一行中提到,硬盘/dev/vda中有200G容量。但是在下面的分区表中发现,该硬盘的两个分区分别是30G和2G(其中2G用于swap,即内存不够用时使用),也就是说,有170G左右的容量并没有被使用。

同时,我使用df -hT命令查看一下文件系统的使用情况,结果如下:

1
2
3
4
5
6
7
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1 ext4 30G 1.5G 27G 6% /
devtmpfs devtmpfs 16G 0 16G 0% /dev
tmpfs tmpfs 16G 0 16G 0% /dev/shm
tmpfs tmpfs 16G 17M 16G 1% /run
tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup
tmpfs tmpfs 3.2G 0 3.2G 0% /run/user/0

于是我决定对硬盘进行重新分区,将所有空间利用起来。

利用fdisk对硬盘重新分区

fdisk是操作硬盘分区表的工具。输入fdisk /dev/vda以对该硬盘进行分区操作.输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@virt2 ~]# fdisk /dev/vda
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): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

在输入m后得到了操作表。首先我们输入p以查看当前的分区表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Command (m for help): p

Disk /dev/vda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c411d

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 62916607 31457280 83 Linux
/dev/vda2 62916608 67108863 2096128 82 Linux swap / Solaris

Command (m for help):

然后使用d命令删除当前的两个分区表

1
2
3
4
5
6
7
8
9
Command (m for help): d
Partition number (1,2, default 2):
Partition 2 is deleted

Command (m for help): d
Selected partition 1
Partition 1 is deleted

Command (m for help):

然后使用n命令新建两个分区表。与之前一样,一个用来当启动分区,一个用于交换。由于一共有200G空间,我决定给启动分区184G的空间,交换分区16G的空间。关于交换分区的大小,可以参考此文档.

分区1创建过程如下:

1
2
3
4
5
6
7
8
9
10
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-419430399, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +184G
Partition 1 of type Linux and of size 184 GiB is set

其中的技巧是使用+184G来指定分区大小,从而让fdisk为我们计算结束扇区。

分区二创建过程如下:

1
2
3
4
5
6
7
8
9
10
11
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (385878016-419430399, default 385878016):
Using default value 385878016
Last sector, +sectors or +size{K,M,G} (385878016-419430399, default 419430399):
Using default value 419430399
Partition 2 of type Linux and of size 16 GiB is set

其中无需更改扇区结束处,因为fdisk默认会使用所有未分配的容量。

同时,因为此分区用于交换,我们需要修改改分区的system ID。

输入t后,指定分区2,然后可以输入L来显示所有的ID以供参考。我们可以看到82代表了Linux Swap。所以我们将其修改为82. 过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 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 C: 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 eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

到这里分区的删除、重新创建就已经结束了。最后一定要注意使用w命令将其写入硬盘,否则所有的修改都是无效的。

1
2
3
4
5
6
7
8
9
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.

最后sudo reboot重启机器,以应用新的分区表。

重启后使用fdisk -l命令发现,新的分区表已经生成成功。

1
2
3
4
5
6
7
8
9
10
11
12
[root@virt2 ~]# fdisk -l

Disk /dev/vda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c411d

Device Boot Start End Blocks Id System
/dev/vda1 2048 385878015 192937984 83 Linux
/dev/vda2 385878016 419430399 16776192 82 Linux swap / Solaris

但是使用df -hT 时,发现事情没有这么简单…

1
2
3
4
5
6
7
8
[root@virt2 ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 30G 1.5G 27G 6% /
devtmpfs devtmpfs 16G 0 16G 0% /dev
tmpfs tmpfs 16G 0 16G 0% /dev/shm
tmpfs tmpfs 16G 8.6M 16G 1% /run
tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup
tmpfs tmpfs 3.2G 0 3.2G 0% /run/user/0

在这里,/dev/vda1分区的容量还是只有30G…

然后我在man df里看到了一些相关解释:

1
2
3
4
If  an  argument is the absolute file name of a disk device node containing a mounted file system, 
df shows the space available on that file system rather than on the file system containing the device node.
This version of df cannot show the space available on unmounted file systems,
because on most kinds of systems doing so requires very nonportable intimate knowledge of file system structures.

该段大意为:当参数为/dev/vda1这种代表硬盘分区的绝对文件名,并且该分区已经挂载文件系统时,df显示的是这个文件系统的容量信息,而不是硬盘分区的容量信息。

也就是说,虽然现在我们的硬盘分区大小已经设置成功,但是文件系统并没有做出相应的改变。

在网上搜集相关资料后,发现一个相关命令resize2fs. 通过man resize2fs命令发现了一段关键信息:

1
2
3
4
5
6
If you wish to enlarge a filesystem, you must make sure you can expand the  size of  the  underlying partition first. 
This can be done using fdisk(8) by deleting the partition and recreating it with a larger size or using lvex-tend(8),
if you're using the logical volume manager lvm(8).
When recreating the partition, make sure you create it with the same starting disk cylinder as before!
Otherwise, the resize operation will certainly not work, and you may lose your entire filesystem.
After running fdisk(8),run resize2fs to resize the ext2 filesystem to use all of the space in the newly enlarged partition.

所以我输入命令 resize2fs /dev/vda1 成功得扩大了文件系统容量。

1
2
3
4
5
[root@virt2 ~]# resize2fs /dev/vda1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 23
The filesystem on /dev/vda1 is now 48234496 blocks long.

然后用 df -hT来查看文件系统

1
2
3
4
5
6
7
8
[root@virt2 ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 181G 1.5G 172G 1% /
devtmpfs devtmpfs 16G 0 16G 0% /dev
tmpfs tmpfs 16G 0 16G 0% /dev/shm
tmpfs tmpfs 16G 8.6M 16G 1% /run
tmpfs tmpfs 16G 0 16G 0% /sys/fs/cgroup
tmpfs tmpfs 3.2G 0 3.2G 0% /run/user/0

发现已经成功扩容。

最后使用mkswap /dev/vda2 && swapon /dev/vda2 使用swap交换。