[Linux服务器] Linux 给根目录扩容

821 0
Honkers 2025-7-20 20:36:37 | 显示全部楼层 |阅读模式

一、如何给Linux服务器扩展根目录

1.添加一块新的硬盘

我这里添加硬盘3,加了一个5G的盘

2.查看当前服务器设备信息

查看新添加的硬盘和磁盘大小

可以看到我新加的盘sdc

root/目录现在是13G

  1. root@10.35.153.99  ~   20:09:39
  2. ❯ lsblk
  3. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  4. sda 8:0 0 15G 0 disk
  5. ├─sda1 8:1 0 1G 0 part /boot
  6. └─sda2 8:2 0 14G 0 part
  7. ├─centos-root 253:0 0 12.5G 0 lvm / #扩容前当前
  8. └─centos-swap 253:1 0 1.5G 0 lvm [SWAP]
  9. sdb 8:16 0 5G 0 disk
  10. └─sdb1 8:17 0 100M 0 part
  11. sdc 8:32 0 5G 0 disk
  12. sr0 11:0 1 4.4G 0 rom
  13. root@10.35.153.99  ~   20:09:45
  14. ❯ df -Th
  15. 文件系统 类型 容量 已用 可用 已用% 挂载点
  16. devtmpfs devtmpfs 470M 0 470M 0% /dev
  17. tmpfs tmpfs 487M 0 487M 0% /dev/shm
  18. tmpfs tmpfs 487M 8.4M 478M 2% /run
  19. tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
  20. /dev/mapper/centos-root xfs 13G 5.1G 7.5G 41% /
  21. /dev/sda1 xfs 1014M 172M 843M 17% /boot
  22. tmpfs tmpfs 98M 12K 98M 1% /run/user/42
  23. tmpfs tmpfs 98M 0 98M 0% /run/user/0
复制代码

3.创建分区

fdisk 创建分区格式MBR分区

我这里创建了一个分区为sdc1的设备

  1. root@10.35.153.99  ~   20:09:59
  2. ❯ fdisk /dev/sdc #fdisk 创建分区格式MBR分区
  3. 欢迎使用 fdisk (util-linux 2.23.2)。
  4. 更改将停留在内存中,直到您决定将更改写入磁盘。
  5. 使用写入命令前请三思。
  6. Device does not contain a recognized partition table
  7. 使用磁盘标识符 0x937a59e3 创建新的 DOS 磁盘标签。
  8. 命令(输入 m 获取帮助):n
  9. Partition type:
  10. p primary (0 primary, 0 extended, 4 free)
  11. e extended
  12. Select (default p): p
  13. 分区号 (1-4,默认 1):
  14. 起始 扇区 (2048-10485759,默认为 2048):
  15. 将使用默认值 2048
  16. Last 扇区, +扇区 or +size{K,M,G} (2048-10485759,默认为 10485759):
  17. 将使用默认值 10485759
  18. 分区 1 已设置为 Linux 类型,大小设为 5 GiB
  19. 命令(输入 m 获取帮助):p
  20. 磁盘 /dev/sdc:5368 MB, 5368709120 字节,10485760 个扇区
  21. Units = 扇区 of 1 * 512 = 512 bytes
  22. 扇区大小(逻辑/物理):512 字节 / 512 字节
  23. I/O 大小(最小/最佳):512 字节 / 512 字节
  24. 磁盘标签类型:dos
  25. 磁盘标识符:0x937a59e3
  26. 设备 Boot Start End Blocks Id System
  27. /dev/sdc1 2048 10485759 5241856 83 Linux
  28. 命令(输入 m 获取帮助):w #保存退出
  29. The partition table has been altered!
  30. Calling ioctl() to re-read partition table.
  31. 正在同步磁盘。
复制代码
4、格式化分区

给新创建的分区创建文件系统

  1. root@10.35.153.99  ~   20:13:11
  2. ❯ mkfs.ext3 /dev/sdc1 #格式化文件系统
  3. mke2fs 1.42.9 (28-Dec-2013)
  4. 文件系统标签=
  5. OS type: Linux
  6. 块大小=4096 (log=2)
  7. 分块大小=4096 (log=2)
  8. Stride=0 blocks, Stripe width=0 blocks
  9. 327680 inodes, 1310464 blocks
  10. 65523 blocks (5.00%) reserved for the super user
  11. 第一个数据块=0
  12. Maximum filesystem blocks=1342177280
  13. 40 block groups
  14. 32768 blocks per group, 32768 fragments per group
  15. 8192 inodes per group
  16. Superblock backups stored on blocks:
  17. 32768, 98304, 163840, 229376, 294912, 819200, 884736
  18. Allocating group tables: 完成
  19. 正在写入inode表: 完成
  20. Creating journal (32768 blocks): 完成
  21. Writing superblocks and filesystem accounting information: 完成
复制代码
做完以上操作后,现在开始扩展根分区

5、给新建的分区创建PV(物理卷)
  1. root@10.35.153.99  ~   20:13:35
  2. ❯ pvcreate /dev/sdc1 #创建PV物理卷
  3. WARNING: ext3 signature detected on /dev/sdc1 at offset 1080. Wipe it? [y/n]: y
  4. Wiping ext3 signature on /dev/sdc1.
  5. Physical volume "/dev/sdc1" successfully created.
复制代码
6、添加到 VG 卷组

把做好的PV物理卷 加入到 根目录的VG卷组里实现扩容

  1. root@10.35.153.99  ~   20:14:16
  2. ❯ vgs
  3. VG #PV #LV #SN Attr VSize VFree
  4. centos 1 2 0 wz--n- <14.00g 0 #当前只有一个PV卷
  5. root@10.35.153.99  ~   20:14:31
  6. ❯ vgextend centos /dev/sdc1 #给/dev/sdc1扩容到centos
  7. Volume group "centos" successfully extended
  8. ❯ vgs
  9. VG #PV #LV #SN Attr VSize VFree
  10. centos 2 2 0 wz--n- 18.99g <5.00g #添加后有两个PV卷
复制代码
7、扩展LV(逻辑卷)

查看当前状态

  1. root@10.35.153.99  ~   20:15:39
  2. ❯ lvs
  3. LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
  4. root centos -wi-ao---- <12.50g #当前root卷大小12.5G
  5. swap centos -wi-ao---- 1.50g
  6. #查看位置逻辑卷位置
  7. root@10.35.153.99  ~   20:26:11
  8. ❯ lvdisplay
  9. --- Logical volume ---
  10. LV Path /dev/centos/root #根目录位置是/dev/centos/root
  11. LV Name root
  12. VG Name centos
  13. LV UUID FhLbqi-q4R0-eAT6-KfXj-kQg7-qkgS-EhzfnB
  14. LV Write Access read/write
  15. LV Creation host, time localhost, 2024-12-02 15:46:50 +0800
  16. LV Status available
  17. # open 1
  18. LV Size <12.50 GiB
  19. Current LE 3199
  20. Segments 1
  21. Allocation inherit
  22. Read ahead sectors auto
  23. - currently set to 8192
  24. Block device 253:0
复制代码

扩展逻辑卷

把所有的剩余空间都添加到 dev/centos/root 

  1. root@10.35.153.99  ~   20:26:14
  2. ❯ lvextend -l +100%FREE /dev/centos/root
  3. Size of logical volume centos/root changed from <12.50 GiB (3199 extents) to 17.49 GiB (4478 extents).
  4. Logical volume centos/root successfully resized.
复制代码

查看大小,是否已经添加

  1. root@10.35.153.99  ~   20:27:00
  2. ❯ lvs
  3. LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
  4. root centos -wi-ao---- 17.49g #现在大小17G
  5. swap centos -wi-ao---- 1.50g
复制代码

使用 xfs_growfs命令使系统重新读取大小

  1. root@10.35.153.99  ~   20:28:10
  2. ❯ xfs_growfs /dev/centos/root
  3. meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=818944 blks
  4. = sectsz=512 attr=2, projid32bit=1
  5. = crc=1 finobt=0 spinodes=0
  6. data = bsize=4096 blocks=3275776, imaxpct=25
  7. = sunit=0 swidth=0 blks
  8. naming =version 2 bsize=4096 ascii-ci=0 ftype=1
  9. log =internal bsize=4096 blocks=2560, version=2
  10. = sectsz=512 sunit=0 blks, lazy-count=1
  11. realtime =none extsz=4096 blocks=0, rtextents=0
  12. data blocks changed from 3275776 to 4585472
复制代码

查看扩容前和扩容后对比

  1. #扩容前
  2. root@10.35.153.99  ~   20:09:45
  3. ❯ df -Th
  4. 文件系统 类型 容量 已用 可用 已用% 挂载点
  5. devtmpfs devtmpfs 470M 0 470M 0% /dev
  6. tmpfs tmpfs 487M 0 487M 0% /dev/shm
  7. tmpfs tmpfs 487M 8.4M 478M 2% /run
  8. tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
  9. /dev/mapper/centos-root xfs 13G 5.1G 7.5G 41% / #扩容前13G
  10. /dev/sda1 xfs 1014M 172M 843M 17% /boot
  11. tmpfs tmpfs 98M 12K 98M 1% /run/user/42
  12. tmpfs tmpfs 98M 0 98M 0% /run/user/0
  13. #扩容后
  14. root@10.35.153.99  ~   20:28:30
  15. ❯ df -Th
  16. 文件系统 类型 容量 已用 可用 已用% 挂载点
  17. devtmpfs devtmpfs 470M 0 470M 0% /dev
  18. tmpfs tmpfs 487M 0 487M 0% /dev/shm
  19. tmpfs tmpfs 487M 8.6M 478M 2% /run
  20. tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
  21. /dev/mapper/centos-root xfs 18G 5.7G 12G 33% / #扩容后18G
  22. /dev/sda1 xfs 1014M 172M 843M 17% /boot
  23. tmpfs tmpfs 98M 32K 98M 1% /run/user/0
  24. /dev/sr0 iso9660 4.4G 4.4G 0 100% /run/media/root/CentOS 7 x86_64
  25. root@10.35.153.99  ~   21:06:04
  26. ❯ lsblk
  27. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
  28. sda 8:0 0 15G 0 disk
  29. ├─sda1 8:1 0 1G 0 part /boot
  30. └─sda2 8:2 0 14G 0 part
  31. ├─centos-root 253:0 0 17.5G 0 lvm / #扩容后
  32. └─centos-swap 253:1 0 1.5G 0 lvm [SWAP]
  33. sdb 8:16 0 5G 0 disk
  34. └─sdb1 8:17 0 100M 0 part
  35. sdc 8:32 0 5G 0 disk
  36. └─sdc1 8:33 0 5G 0 part
  37. └─centos-root 253:0 0 17.5G 0 lvm /
  38. sr0 11:0 1 4.4G 0 rom /run/media/root/CentOS 7 x86_64
复制代码

以上就完成了Linux对根目录容量的扩容操作

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行