linux 根分区扩容

场景: 按照标准分区创建系统 设置 /boot /swap / 3个目录 /dev/sda3 挂载在/ 目录上 ,由于磁盘空间不中 对原磁盘扩容 (虚拟磁盘)

# umount /dev/sda3
umount: /: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
# 根分区卸载不掉那就不卸载了

# fdisk /dev/sda
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

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000b70b3

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 8e Linux LVM
/dev/sda2 616448 4810751 2097152 82 Linux swap / Solaris
/dev/sda3 4810752 61433855 28311552 83 Linux

Command (m for help): d
Partition number (1-3, default 3): 3
Partition 3 is deleted

Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (4810752-62914559, default 4810752):
Using default value 4810752
Last sector, +sectors or +size{K,M,G} (4810752-62914559, default 62914559):
Using default value 62914559
Partition 3 of type Linux and of size 27.7 GiB is set

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000b70b3

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 616447 307200 8e Linux LVM
/dev/sda2 616448 4810751 2097152 82 Linux swap / Solaris
/dev/sda3 4810752 62914559 29051904 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.

# partprobe #不管用,必须reboot重启
Error: Partition(s) 3 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.7G 0 part /
sr0 11:0 1 4.5G 0 rom

重启之后

# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 27.7G 0 part / #磁盘分区空间变为了27.7,增加了10G
sr0 11:0 1 4.5G 0 rom

# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 18G 3.6G 15G 21% /
devtmpfs devtmpfs 1.7G 0 1.7G 0% /dev
tmpfs tmpfs 1.8G 84K 1.8G 1% /dev/shm
tmpfs tmpfs 1.8G 9.0M 1.8G 1% /run
tmpfs tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 152M 146M 51% /boot
tmpfs tmpfs 351M 16K 351M 1% /run/user/42
tmpfs tmpfs 351M 0 351M 0% /run/user/0

# xfs_growfs / #扩展XFS文件系统!!!

meta-data=/dev/sda3 isize=512 agcount=4, agsize=1160384 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4641536, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
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 4641536 to 7262976

!!!要是提示只读

# mount -o remount rw /     #改为读写

然后在 # xfs_growfs /
!!!

# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda3 xfs 28G 3.6G 25G 13% / #文件系统变为了28G
devtmpfs devtmpfs 1.7G 0 1.7G 0% /dev
tmpfs tmpfs 1.8G 84K 1.8G 1% /dev/shm
tmpfs tmpfs 1.8G 9.0M 1.8G 1% /run
tmpfs tmpfs 1.8G 0 1.8G 0% /sys/fs/cgroup
/dev/sda1 xfs 297M 152M 146M 51% /boot
tmpfs tmpfs 351M 16K 351M 1% /run/user/42
tmpfs tmpfs 351M 0 351M 0% /run/user/0