CentOS7下根分区扩容

为更好熟悉CentOS7,日前在PC上安装了VirtualBox软件来跑CentOS7练练手。虚机CentOS7暂采用1Core CPU,2G Mem,10G Disk配置,最小化安装并升级下系统后快照备份存档以备后用,后期可根据需要随时调整配置。
今天准备整个PostgreSQL玩玩的,首先需要对10G的磁盘进行扩展。安装系统时选择的是默认分区方案,root分区是用lvm管理的,分区格式是xfs。如下所示:

1
2
3
4
5
6
7
8
9
10
[root@myprecious ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos_myprecious-root xfs 8.5G 855M 7.7G 10% /
devtmpfs devtmpfs 487M 0 487M 0% /dev
tmpfs tmpfs 497M 0 497M 0% /dev/shm
tmpfs tmpfs 497M 6.5M 491M 2% /run
tmpfs tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 xfs 497M 124M 374M 25% /boot
tmpfs tmpfs 100M 0 100M 0% /run/user/0
[root@myprecious ~]#

所以扩展root分区就很容易了。首先再添加一块虚拟磁盘到虚机,重启后fdisk -l查看虚拟磁盘是否被识别。如下,可以看到新增磁盘被标记为/dev/sdb,有53.7G的空间。

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
[root@myprecious ~]# fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes, 20971520 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: 0x0008ee5f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20971519 9972736 8e Linux LVM
Disk /dev/sdb: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos_myprecious-root: 8.3 GB, 53724839936 bytes, 104931328 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 /dev/mapper/centos_myprecious-swap: 1073 MB, 1073741824 bytes, 2097152 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
[root@myprecious ~]#

创建磁盘分区

惯例,为/dev/sdb磁盘创建分区,如下:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
[root@myprecious ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4cb24dcf.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
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
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)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-624152, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-624152, default 624152):
Using default value 624152
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 53.7 GB, 53687091200 bytes
16 heads, 63 sectors/track, 624152 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4cb24dcf
Device Boot Start End Blocks Id System
/dev/sdb1 1 624152 314572576+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@myprecious ~]#

整块磁盘/dev/sdb被分区为/dev/sdb1,将该分区格式化为xfs

1
[root@myprecious ~]# mkfs.xfs -f /dev/sdb1

创建物理卷PV

查看并创建物理卷PV:

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
32
33
34
35
36
37
38
39
[root@myprecious ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos_myprecious
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2434
Free PE 0
Allocated PE 2434
PV UUID 0bE8O8-syyl-TSyE-p9Bv-sZEJ-3GkE-vNC4XB
[root@myprecious ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@myprecious ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos_myprecious
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2434
Free PE 0
Allocated PE 2434
PV UUID 0bE8O8-syyl-TSyE-p9Bv-sZEJ-3GkE-vNC4XB
"/dev/sdb1" is a new physical volume of "53.7 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 53.7 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 8Z7hoj-Ub4d-xLr3-z7ti-Z5zM-oVTv-TuLtv7
[root@myprecious ~]#

创建卷组VG

以上,PV /dev/sdb1已创建完成,但未被分配。将PV /dev/sdb1分配到系统的卷组VG上,这里为centos_myprecious,如下:

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
[root@myprecious ~]# vgdisplay
--- Volume group ---
VG Name centos_myprecious
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 59.50 GiB
PE Size 4.00 MiB
Total PE 15233
Alloc PE / Size 13065 / 51.04 GiB
Free PE / Size 2168 / 8.47 GiB
VG UUID jvcc7L-quO1-MQOz-KDX3-v4QN-ES9G-q4h7H7
[root@myprecious ~]# vgextend centos_myprecious /dev/sdb1
Physical volume "/dev/sdb1" successfully created
Volume group "centos_myprecious" successfully extended
[root@myprecious ~]#

查看逻辑卷LV

在分配完卷组后,就可以见卷组中未分配的空间分配给root分区了,这里为/dev/sdb1分区。

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
32
33
34
[root@myprecious ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos_myprecious/root
LV Name root
VG Name centos_myprecious
LV UUID KqVmPb-MNpe-VPmH-ato8-aeVU-JbOz-HNgG0M
LV Write Access read/write
LV Creation host, time myprecious, 2017-02-20 14:35:34 +0800
LV Status available
# open 1
LV Size 8.47 GiB
Current LE 2168
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
--- Logical volume ---
LV Path /dev/centos_myprecious/swap
LV Name swap
VG Name centos_myprecious
LV UUID 4HGA1K-1bYu-Vvmx-j70O-l0U7-NwvG-etzOF6
LV Write Access read/write
LV Creation host, time myprecious, 2017-02-20 14:35:33 +0800
LV Status available
# open 2
LV Size 1.00 GiB
Current LE 256
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

扩展逻辑卷LV时的小插曲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@myprecious ~]# lvextend -l 100%FREE /dev/centos_myprecious/root
Size of logical volume centos_myprecious/root changed from 8.47 GiB (2168 extents) to 50.04 GiB (12809 extents).
Logical volume root successfully resized.
[root@myprecious ~]# e2fsck -f /dev/centos_myprecious/root
e2fsck 1.42.9 (28-Dec-2013)
/dev/centos_myprecious/root is mounted.
e2fsck: Cannot continue, aborting.
[root@myprecious ~]# resize2fs /dev/centos_myprecious/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos_myprecious/root
Couldn't find valid filesystem superblock.
[root@myprecious ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_myprecious-root 8.5G 854M 7.7G 10% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.5M 491M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 497M 124M 374M 25% /boot
tmpfs 100M 0 100M 0% /run/user/0
[root@myprecious ~]#

在使用resize2fs操作后报错,查看分区信息发现根分区并没有变多,甚至重启后执行resize2fs操作依旧报错。之前的centos6扩展ext2/ext3/ext4明明都是好好的,问题出在哪儿呢?查找资料,发现是resize2fs不能扩展xfs分区所致。扩展xfs分区需要使用xfs_growfs。如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@myprecious ~]# xfs_growfs /dev/centos_myprecious/root
meta-data=/dev/mapper/centos_myprecious-root isize=256 agcount=4, agsize=555008 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=2220032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2220032 to 13116416
[root@myprecious ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos_myprecious-root 51G 904M 50G 2% /
devtmpfs 487M 0 487M 0% /dev
tmpfs 497M 0 497M 0% /dev/shm
tmpfs 497M 6.5M 491M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 497M 124M 374M 25% /boot
tmpfs 100M 0 100M 0% /run/user/0
[root@myprecious ~]#

使用xfs_growfs后,根分区被正常扩展。没注意到centos7中根分区已经有centos6中的ext4变更为xfs,相应的扩展分区的命令变更为xfs_growfs
有时候真的是不能想当然,一个小细节的差异也会导致达不到预期的效果。

文章目录
  1. 1. 创建磁盘分区
  2. 2. 创建物理卷PV
  3. 3. 创建卷组VG
  4. 4. 查看逻辑卷LV
  5. 5. 扩展逻辑卷LV时的小插曲