扩展CentOS硬盘大小

通常,Azure Centos系统盘为30G,可能有时候为了省钱会扩展到64G,即停止VM实例后在磁盘->配置中调整磁盘大小。那么,多出来的这34G,我们如何扩展到系统盘中利用起来,而不浪费呢。这里我们需要将未分配的34G分配到系统盘中。步骤如下:

(请使用管理员权限执行)

如何扩展分区?

执行:

lsblk
得到如下分区表结构

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sdb      8:16   0    4G  0 disk 
└─sdb1   8:17   0    4G  0 part /mnt/resource
fd0      2:0    1    4K  0 disk 
sda      8:0    0   64G  0 disk 
├─sda2   8:2    0 29.5G  0 part /
└─sda1   8:1    0  500M  0 part /boot

可以看到有34G空间未分配

下面开始扩展sda2分区

有三个步骤:>更改分区表,以便sda2在磁盘末尾结束
>重读分区表(需要重启)
>使用pvresize调整LVM光盘的大小

第1步 – 分区表

执行:

fdisk /dev/sda
得到:

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
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: 68.7 GB, 68719476736 bytes, 134217728 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
Disk label type: dos
Disk identifier: 0x000a8b4c
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 62914559 30944256 83 Linux
 

输入:

d

得到:

Partition number (1,2, default 2):

然后输入:

2

以此删除第二个分区.得到:

Partition 2 is deleted
Command (m for help):
 

然后输入:

n

以创建新的第二个分区.确保start等于先前打印的分区表的开头。确保结尾位于磁盘的末尾(通常是默认值)。得到:

Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):

然后输入:

t

得到:

Select (default p): t
Invalid partition type `t'
Command (m for help):

然后输入:

t

得到:

Selected partition 1
Hex code (type L to list all codes):

然后输入:

8e

得到:

Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help):

来将新的第二个分区的分区类型切换为8e(Linux LVM).

然后输入:

p

得到:

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 8e Linux LVM
Command (m for help):

以检查新的分区布局,并确保新的第二个分区的开始位于旧的第二个分区的确切位置.

如果一切正常。输入:

w

得到

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

此步骤很必要的,因为重新读取分区表。

最后一步,重新加载分区

执行:(注意,需要管理员权限)

resize2fs xfs_growfs /dev/sda2

然后就可以发现系统分区已经成功扩展!

小小人儿的技术站版权所有,转载请注明来源:https://xinb.cc/everything/azure%e6%89%a9%e5%b1%95centos7-8%e7%b3%bb%e7%bb%9f%e7%9b%98%e5%a4%a7%e5%b0%8f/
THE END
分享
二维码
< <上一篇
下一篇>>