【Linux】Linux根文件系统扩容
  TEZNKK3IfmPf 2023年11月13日 41 0

场景:根文件系统需要至少100GB的剩余空间,但是目前就剩余91GB。因此,我们需要对根文件系统进行扩容。

# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 3.9G     0  3.9G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G   14M  3.9G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/centos-root   98G  7.2G   91G    8% /

现在我们开始根文件系统的扩容……

首先,我们为虚拟机新创建一块50G的硬盘。在添加硬件向导中,选择硬件类型为:硬盘

【Linux】Linux根文件系统扩容

这一步默认即可

【Linux】Linux根文件系统扩容

这一步同样默认

【Linux】Linux根文件系统扩容

这一步,指定磁盘的最大容量,存储为单个文件,一般不要立即分配所有磁盘空间。

【Linux】Linux根文件系统扩容

磁盘文件名默认即可

【Linux】Linux根文件系统扩容

OK,至此我们创建一块新的硬盘工作已经完成!

【Linux】Linux根文件系统扩容

如果在虚拟机没有关机的情况下创建的新硬盘,则可能需要重启虚拟机系统,才能使用fdisk命令看到新加的硬盘。

# fdisk -l

磁盘 /dev/sdb:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/sda:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b215d

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   209715199   103808000   8e  Linux LVM

磁盘 /dev/mapper/centos-root:105.2 GB, 105222504448 字节,205512704 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-home:1073 MB, 1073741824 字节,2097152 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

现在,我们对这块新加的硬盘/dev/sdb进行分区创建、格式化、pv创建等操作。

# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

Device does not contain a recognized partition table
使用磁盘标识符 0x98e6b88a 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-104857599,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-104857599,默认为 104857599):
将使用默认值 104857599
分区 1 已设置为 Linux 类型,大小设为 50 GiB

命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。
# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3276800 inodes, 13106944 blocks
655347 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2162163712
400 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成   
# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.

使用pvdisplay命令查看根分区的PV、VG等信息,可以看到根分区的VG名称为centos。

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <99.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              25343
  Free PE               0
  Allocated PE          25343
  PV UUID               87kZBR-DJxS-GAdy-ggJJ-wLoa-Jd9V-6T5vvQ
   
  "/dev/sdb1" is a new physical volume of "<50.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <50.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               5VD2eC-0crC-E7nq-VW1n-K8SP-sb1H-zEnQCf

将/dev/sdb1物理卷添加到根分区的卷组设备中。

# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

将/dev/sdb1的空间扩展到根分区。

# lvextend -V +49.9G /dev/mapper/centos-root /dev/sdb1
lvextend:无效选项 -- V
  Error during parsing of command line.
[root@k8s-master ~]# lvextend -L +49.9G /dev/mapper/centos-root /dev/sdb1
  Rounding size to boundary between physical extents: 49.90 GiB.
  Size of logical volume centos/root changed from <98.00 GiB (25087 extents) to <147.90 GiB (37862 extents).
  Logical volume centos/root successfully resized.

使用xfs_growfs来增加根文件系统的大小,前提是根文件系统必须是使用 xfs格式的文件系统。

# xfs_growfs  /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=6422272 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=25689088, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=12543, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 25689088 to 38770688

# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 3.9G     0  3.9G    0% /dev
tmpfs                    3.9G     0  3.9G    0% /dev/shm
tmpfs                    3.9G   14M  3.9G    1% /run
tmpfs                    3.9G     0  3.9G    0% /sys/fs/cgroup
/dev/mapper/centos-root  148G  7.2G  141G    5% /

OK,至此Linux的根文件系统的扩容工作成功完成。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   52   0   0 linux服务器
  TEZNKK3IfmPf   2024年05月31日   30   0   0 linux服务器centos
  TEZNKK3IfmPf   2024年05月31日   29   0   0 linuxbind
  TEZNKK3IfmPf   2024年05月31日   40   0   0 linuxshell
TEZNKK3IfmPf