Linux boot和根目录扩容

1.背景

安装Centos7.8的时候,boot分区大小分配了200M,现准备升级到Centos7.9,报错,boot目录不足。

2.删除分区

如果装完系统后,磁盘所有空间都被分配出去了,此时需要删除一个分区来挤出空间

本例使用Data目录,因为里面的东西都不重要,因此决定卸载这个目录并删除其对应的分区

2.1.顺利卸载

# 卸载/data分区
umount /Data
# 使用parted查看删除分区
parted

image-20240402165526640

# 修改fstab,注释/data条目
vi /etc/fstab

image-20240402165645311

2.2.无法顺利卸载

如果无法顺利卸载,那么直接修改fstab,注释/data所在条目,直接重启

3.新增一个分区

# parted新增分区
parted
print
mkpart DiskExtension xfs 250GB 252GB

image-20240402170026937

4.格式化

mkfs.xfs -f /dev/sda8
mount /dev/sda8 /mnt

5.复制

cp -r /boot/* /mnt
# blkid查看sda8的uuid
blkid
# 注释掉原来的boot并将sda8挂载到boot
vi /etc/fstab

image-20240402171242283

6.重启

此时umount /boot大概率报错,因为/boot正在被使用,经过第5步后,直接重启,重启后,/boot就是新的分区

重启后,grub依旧使用原来的boot分区,需要重新配置到新的分区上

# 查看是传统模式还是UEFI模式启动
[ -d /sys/firmware/efi ] && echo UEFI || echo BIOS
# 如果是UEFI,使用下面的命令配置
grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
# 如果是传统模式
grub2-mkconfig -o /boot/grub2/grub.cfg

7.其他

到第6步已经完成了boot目录的扩容,下面是针对扩容的一些扩展

如果boot安装到了另一块磁盘,参考:linux下boot标准分区如何扩容及注意事项

参考文章的分区格式是DOS模式的,DOS模式可以添加boot标签,GPT格式经过测试,无需添加boot标签

参考文章的启动模式是传统模式,对于UEFI,grup2-install可能会报错

image-20240408151113367

# 解决方法
yum install grub2-efi-x64-modules.noarch
grub2-install /dev/sda

参考:Linux启动流程和grub详解
Linux启动之grub详解,故障排除
第 6 章 对 GRUB 菜单进行临时更改

8.Linux根目录扩展(非LVM)

8.1.根目录分区后边腾出空间

一般扩容有两种方案。第一种,直接把目录卸载,然后划分一个更大的分区,并把目录挂载到新的大分区上。第二种,在原有分区大小的基础上,扩充分区大小,之后,扩充文件系统大小。针对根目录,本次采用第二种方法。

扩容需要有连续的空间,因此,需要在根目录分区的后边腾出一部分空闲空间,供根目录扩充。

df -h命令来看,我们需要把、dev/sda4分区删除,删除前,请备份好分区对应的目录的数据。

8.2.扩展分区

针对GPT格式的分区,使用gdisk来进行分区的扩展,不要使用parted

[root@test ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.3

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/sda: 2344114096 sectors, 1.1 TiB
Model: LOGICAL VOLUME
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 7D0EA7B0-AC41-4201-99F6-FAFC6F2BE60D
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 2344114062
Partitions will be aligned on 2048-sector boundaries
Total free space is 209719149 sectors (100.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048         2099199   1024.0 MiB  EF00  EFI System Partition
   2         2099200         4196351   1024.0 MiB  8300
   3         4196352       213911551   100.0 GiB   8300
   5       423626752       457181183   16.0 GiB    8200
   6       457181184       490735615   16.0 GiB    8200
   7       490735616      1539311615   500.0 GiB   8300
   8      1539311616      2344112127   383.8 GiB   8300

Command (? for help): d
Partition number (1-8): 3

Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-2344114062, default = 4196352) or {+-}size{KMGTP}: 4196352
Last sector (4196352-423626751, default = 423626751) or {+-}size{KMGTP}: 423626751
Current type is 'Linux filesystem'
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/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
partprobe /dev/sda
# 扩容文件系统
xfs_growfs /dev/sda3

8.3.查看扩容结果

参考:linux非lvm管理的根目录扩容

posted @ 2024-04-08 15:21  monkey6  阅读(312)  评论(0编辑  收藏  举报