linux根目录扩容方法

参考:https://www.jb51.net/article/144291.htm

https://blog.csdn.net/weixin_43139644/article/details/89948983

1. 查看磁盘空间大小,使用df -h 命令

文件系统                 容量  已用  可用 已用% 挂载点
devtmpfs                 1.4G     0  1.4G    0% /dev
tmpfs                    1.4G     0  1.4G    0% /dev/shm
tmpfs                    1.4G   11M  1.4G    1% /run
tmpfs                    1.4G     0  1.4G    0% /sys/fs/cgroup
/dev/mapper/centos-root  7.0G  4.7G  2.4G   67% /
/dev/sda1               1014M  185M  830M   19% /boot
tmpfs                    283M   20K  283M    1% /run/user/1000
/dev/sr0                 4.5G  4.5G     0  100% /run/media/zhujiachun/CentOS 7 x86_64

2. 增加磁盘空间,例如下图使用VM虚拟机增加的方式。

3.使用fdisk -l命令查看磁盘信息。当看到第一行Disk /dev/vda: 161.1 GB与实际df -h显示内容不符时,说明增加磁盘成功了。

root@ubuntu14:/opt# fdisk -l

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
Units = 扇区 of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001a023

  设备 启动   起点     终点   块数  Id 系统
/dev/vda1  *    2048   499711   248832  83 Linux
/dev/vda2     501758  62912511  31205377  5 扩展
/dev/vda5     501760  62912511  31205376  8e Linux LVM

Disk /dev/mapper/ubuntu14--vg-root: 29.8 GB, 29804724224 bytes
255 heads, 63 sectors/track, 3623 cylinders, total 58212352 sectors
Units = 扇区 of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/ubuntu14--vg-root doesn't contain a valid partition table

Disk /dev/mapper/ubuntu14--vg-swap_1: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors
Units = 扇区 of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/ubuntu14--vg-swap_1 doesn't contain a valid partition table

4.使用fdisk /dev/sda, 创建新分区。

注意:不同操作系统的磁盘命名方式不同,有些是/dev/sda。具体使用方式参考第三步中fdisk -l首行显示Disk。如笔者在该系统中首行显示为Disk /dev/vda: 161.1 GB, 161061273600 bytes ,故使用命令fdisk /dev/vda)

按照下面Linux操作进行分区。

root@ubuntu14:/opt# fdisk /dev/vda

命令(输入 m 获取帮助): m
命令操作
  a  toggle a bootable flag
  b  edit bsd disklabel
  c  toggle the dos compatibility flag
  d  delete a partition
  l  list known partition types
  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
  u  change display/entry units
  v  verify the partition table
  w  write table to disk and exit
  x  extra functionality (experts only)

命令(输入 m 获取帮助): n #new 新分区
Partition type:
  p  primary (1 primary, 1 extended, 2 free)
  l  logical (numbered from 5)
Select (default p): p  #选择主分区
分区号 (1-4,默认为 3): 3 #分区序号
起始 sector (499712-314572799,默认为 499712): #分区开始回车默认
将使用默认值 499712
Last sector, +扇区 or +size{K,M,G} (499712-501757,默认为 501757): 
将使用默认值 501757

命令(输入 m 获取帮助): t  #修改分区格式
分区号 (1-5): 4  #修改分区号
Hex code (type L to list codes): 8e #格式选择8e  linux LVM
Changed system type of partition 4 to 8e (Linux LVM)

命令(输入 m 获取帮助): p #显示分区信息

Disk /dev/vda: 161.1 GB, 161061273600 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572800 sectors
Units = 扇区 of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0001a023

  设备 启动   起点     终点   块数  Id 系统
/dev/vda1  *    2048   499711   248832  83 Linux
/dev/vda2     501758  62912511  31205377  5 扩展
/dev/vda3     499712   501757    1023  83 Linux
/dev/vda4    62912512  314572799  125830144  8e Linux LVM
/dev/vda5     501760  62912511  31205376  8e Linux LVM
Partition table entries are not in disk order
命令(输入 m 获取帮助): w #保存信息
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

5. 重启Linux操作系统,使用reboot命令。

6. 创建物理卷,使用pvcreate /dev/sda3命令。

root@ubuntu14:~# pvcreate /dev/sda3
 Physical volume "/dev/vda4" successfully created

7.查看新建的物理卷和大小,使用pvdisplay命令。

root@ubuntu14:~# pvdisplay
 --- Physical volume ---
 PV Name        ubuntu14-vg
 PV Size        29.76 GiB / not usable 2.00 MiB
 Allocatable      yes (but full)
 PE Size        4.00 MiB
 Total PE       7618
 Free PE        0
 Allocated PE     7618
 PV UUID        XR32TY-aRQC-IQC6-oCx7-aa9X-KdJe-bHmaBd
 "/dev/vda4"        /dev/vda5
 VG Name is a new physical volume of "120.00 GiB"
 --- NEW Physical volume ---
 PV Name        /dev/vda4
 VG Name        
 PV Size        120.00 GiB
 Allocatable      NO
 PE Size        0  
 Total PE       0
 Free PE        0
 Allocated PE     0
 PV UUID        MVK0IT-LDgP-eWwZ-fzQM-tdyY-kRQ-nbk122

8. 将添加新的物理卷,加载到centos卷组,使用vgextend centos /dev/sda3命令。

vgextend centos /dev/sdb1

9. 逻辑卷扩容

lvextend -l +511 /dev/mapper/centos-root

10.接下来扩容文件系统
注意:如果文件系统是xfs格式用 xfs_growfs ,ext4格式用resize2fs

xfs_growfs /dev/mapper/centos-root

11.df -h 查看结果

扩容完成!

原文地址:https://www.cnblogs.com/erchun/p/14634615.html