Centos7扩容根目录

 

查询信息

查询当前主机可用的容量

目前根目录只有44G可用,要对根目录进行扩容,添加100G空间

[root@centos ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 989M     0  989M    0% /dev
tmpfs                   1000M     0 1000M    0% /dev/shm
tmpfs                   1000M  8.6M  991M    1% /run
tmpfs                   1000M     0 1000M    0% /sys/fs/cgroup
/dev/mapper/centos-root   46G  1.8G   44G    4% /
/dev/vda1               1014M  190M  825M   19% /boot
tmpfs                    200M     0  200M    0% /run/user/0

查询目前主机已经连接到的磁盘

可以看到一共有两块磁盘/dev/vda /dev/vdb vda是系统默认盘,装操作系统时分配。vdb是后装的磁盘,目标就是把这100g添加到根目录,完成扩容。

[root@centos ~]# fdisk -l

磁盘 /dev/vda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00045c8b

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857599    51379200   8e  Linux LVM

磁盘 /dev/vdb:107.4 GB, 107374182400 字节,209715200 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


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


磁盘 /dev/mapper/centos-swap:4160 MB, 4160749568 字节,8126464 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
fdisk -l

将vdb硬盘分区

[root@centos ~]# fdisk /dev/vdb
欢迎使用 fdisk (util-linux 2.23.2)。

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

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

# 输入m查看帮助
命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag  #指定启动分区
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition  #删除一个存在的分区
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types  #显示分区类型的id号
   m   print this menu
   n   add a new partition  #创建新分区
   o   create a new empty DOS partition table
   p   print the partition table  #打印显示分区列表
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition\'s system id  #修改分区的系统id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit  #保存变更并退出
   x   extra functionality (experts only)

# 输入n创建新分区
命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
# 输入p创建主分区(e是扩展分区)
Select (default p): p
# 分区号默认选1,直接enter
分区号 (1-4,默认 1)# 扇区号默认值就可以 直接enter
起始 扇区 (2048-209715199,默认为 2048):
将使用默认值 2048
# 最后一个扇区号  如果这块磁盘要分多个分区就输入+20G/+30M/+100G,指定此分区的大小。 
# 如果全部都用来分一个分区,直接默认enter就可以。
Last 扇区, +扇区 or +size{K,M,G} (2048-209715199,默认为 209715199):
将使用默认值 209715199
分区 1 已设置为 Linux 类型,大小设为 100 GiB

# 输入w保存变更并退出
命令(输入 m 获取帮助):w
The partition table has been altered!

Calling ioctl() to re-read partition table.
正在同步磁盘。

再次查询分区,可以看到已经多出了/dev/vdb1这个新分区

[root@centos ~]# fdisk -l

磁盘 /dev/vda:53.7 GB, 53687091200 字节,104857600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00045c8b

   设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857599    51379200   8e  Linux LVM

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

   设备 Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   209715199   104856576   83  Linux

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


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

格式化分区

[root@centos ~]# mkfs.ext4 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214144 blocks
1310707 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2174746624
800 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, 20480000, 23887872

Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

这里如果不扩容根目录的话,可以直接mount到其他位置,直接使用。

mount

mount /dev/vdb1 /data

centos7开机自动挂载

echo "/dev/vdb1 /data xfs defaults  0 0" >> /etc/fstab

Linux(CentOS)挂载数据盘并设置开机自动挂载

修改逻辑卷

查看卷组信息

[root@centos ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                6XRB41-vTIu-CB10-aQzl-hOTj-Yd1k-lnRNrb
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-08-22 09:51:42 +0800
  LV Status              available
  # open                 2
  LV Size                <3.88 GiB
  Current LE             992
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                8MH51r-SImM-0YRq-9FAz-90I7-JMt5-oO7I8d
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-08-22 09:51:43 +0800
  LV Status              available
  # open                 1
  LV Size                <45.12 GiB
  Current LE             11550
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

将这个分区变成pv

[root@centos ~]# pvcreate /dev/vdb1
WARNING: ext4 signature detected on /dev/vdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/vdb1.
  Physical volume "/dev/vdb1" successfully created.

加入到根目录所在的centos卷组

[root@centos ~]# vgextend centos /dev/vdb1
  Volume group "centos" successfully extended

扩展逻辑卷大小

[root@centos ~]# lvextend -L +100G /dev/centos/root
  Size of logical volume centos/root changed from <45.12 GiB (11550 extents) to <145.12 GiB (37150 extents).
  Logical volume centos/root successfully resized.

lvextend -l +100%FREE /dev/centos/root

重新加载逻辑卷大小

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

查看根目录容量

[root@centos ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 989M     0  989M    0% /dev
tmpfs                   1000M     0 1000M    0% /dev/shm
tmpfs                   1000M  8.6M  991M    1% /run
tmpfs                   1000M     0 1000M    0% /sys/fs/cgroup
/dev/mapper/centos-root  146G  1.8G  144G    2% /
/dev/vda1               1014M  190M  825M   19% /boot
tmpfs                    200M     0  200M    0% /run/user/0

重启