CentOS虚拟机系统盘扩容修复

举报
Ragnar 发表于 2020/07/16 15:39:03 2020/07/16
【摘要】 CentOS虚拟机系统盘扩容修复CentOS7镜像规格支持系统盘为20G,当选择系统盘100G时,虚拟机GPT table end位置还是20G的。所以在挂载前要先修复分区,系统才能识别相对扩容的磁盘空间。# parted /dev/vdaGNU Parted 3.1Using /dev/vdaWelcome to GNU Parted! Type 'help' to view a list...

CentOS虚拟机系统盘扩容修复

CentOS7镜像规格支持系统盘为20G,当选择系统盘100G时,虚拟机GPT table end位置还是20G的。所以在挂载前要先修复分区,系统才能识别相对扩容的磁盘空间。

# parted /dev/vda
GNU Parted 3.1
Using /dev/vda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p  
Error: The backup GPT table is not at the end of the disk, as it should be.
This might mean that another operating system believes the disk is smaller.
Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel?  f  # 选择fix进行修复
Warning: Not all of the space available to /dev/vda appears to be used, you can
fix the GPT to use all of the space (an extra 167772160 blocks) or continue
with the current setting?
Fix/Ignore? f  # 选择fix进行修复
q # 退出

修复后可以用gdisk进行分区

# gdisk /dev/vda
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
 MBR: protective
 BSD: not present
 APM: not present
 GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/vda: 209715200 sectors, 100.0 GiB # 这里显示磁盘容量100G
Logical sector size: 512 bytes
Disk identifier (GUID): B6FFDF47-0FC5-4B8E-8FCA-7E74BD7D2DDA
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166  # 最后的sector
Partitions will be aligned on 2048-sector boundaries
Total free space is 167776189 sectors (80.0 GiB)  # 空闲的扇区容量

Number  Start (sector)    End (sector)  Size       Code  Name
  1            2048          411647   200.0 MiB   EF00  EFI System Partition
  2          411648         2508799   1024.0 MiB  0700  
  3         2508800        41940991   18.8 GiB    8E00  
Command (? for help): n # 增加新的分区
Partition number (4-128, default 4):  # 分区编号,输入<enter>为default
First sector (34-209715166, default = 41940992) or {+-}size{KMGTP}:  
Last sector (41940992-209715166, default = 209715166) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'  # 创建一个80G的分区
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): w  # 将分区更新写入

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y  # 确认
OK; writing new GUID partition table (GPT) to /dev/vda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.

添加新的分区后更新核心分区表,可以看到vda4已经更新了

# partprobe -s
/dev/vda: gpt partitions 1 2 3 4
# lsblk /dev/vda
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda             252:0    0  100G  0 disk
├─vda4          252:4    0   80G  0 part
├─vda2          252:2    0    1G  0 part /boot
├─vda3          252:3    0 18.8G  0 part
│ ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
│ └─centos-root 253:0    0 16.8G  0 lvm  /
└─vda1          252:1    0  200M  0 part /boot/efi

接下来为新的分区新建文件系统

# mkfs.xfs /dev/vda4
meta-data=/dev/vda4              isize=512    agcount=4, agsize=5242943 blks
        =                       sectsz=512   attr=2, projid32bit=1
        =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=20971771, imaxpct=25
        =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=10240, version=2
        =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

查看分区的UUID并挂载到/home/data

# mkdir -p /home/data
# blkid /dev/vda4
/dev/vda4: UUID="f3d146ea-a257-4a5a-8c45-e4e94e839a8e" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="a5cfa242-ecde-4ecf-8bff-b62fee8fb1c1"
# mount  UUID="f3d146ea-a257-4a5a-8c45-e4e94e839a8e" /home/data
# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.7G     0  1.7G   0% /dev
tmpfs                    1.8G     0  1.8G   0% /dev/shm
tmpfs                    1.8G   29M  1.7G   2% /run
tmpfs                    1.8G     0  1.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  3.6G   14G  22% /
/dev/vda2               1014M  145M  870M  15% /boot
/dev/vda1                200M  9.9M  190M   5% /boot/efi
tmpfs                    349M     0  349M   0% /run/user/0
/dev/vda4                 80G   33M   80G   1% /home/data # 可以看到已经挂载

将分区设置为开机自动挂载

# 首先复制vda4的UUID,并在/etc/fstab中添加下面一行并保存
# [挂载分区的UUID]  [挂载点] [文件类型]  [文件系统参数]  [dump] [fsck]
UUID=f3d146ea-a257-4a5a-8c45-e4e94e839a8e /home/data xfs defaults 0 0


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。