Linux——系统扩容根目录磁盘空间、磁盘扩容的操作方法

简介: Linux根目录磁盘空间不够用了,当修改了虚拟机模版增加磁盘大小或者插入了一块新硬盘,但是发现系统里的大小还是没改变。产生的原因是没有给磁盘格式化,没有增加分区。

一、使用背景

Linux根目录磁盘空间不够用了,当修改了虚拟机模版增加磁盘大小或者插入了一块新硬盘,但是发现系统里的大小还是没改变。

产生的原因是没有给磁盘格式化,没有增加分区。


二、操作方法

1.查看磁盘空间大小,使用df -h 命令,发现挂载根目录节点的/dev/mapper/vg_node003-lv_root 只有7.1G容量。

[root@node-002 ~]# df -h
Filesystem                                              Size  Used Avail Use% Mounted on
/dev/mapper/vg_node003-lv_root                          18G   9.3G  7.1G  57% /
tmpfs                                                   491M     0  491M   0% /dev/shm
/dev/sda1                                               477M   42M  411M  10% /boot

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FQqZ4mMX-1660472750103)(https://img-community.csdnimg.cn/images/24d2bb9ba56947d1b7addf25e4eff81a.png “#left”)]


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

[root@node-002 ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00092cf6
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM
Disk /dev/mapper/vg_node003-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/vg_node003-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

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

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

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

[root@node-002 ~]# fdisk /dev/sda

命令(输入 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 (2 primary, 1 extended, 1 free)
  l  logical (numbered from 5)
Select (default p): p #选择主分区
Selected partition 4  #分区序号
起始 sector (62912512-314572799,默认为 62912512): #分区开始回车默认
将使用默认值 62912512
Last sector, +扇区 or +size{K,M,G} (62912512-314572799,默认为 314572799): 
将使用默认值 314572799
 命令(输入 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/sda: 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/sda1  *    2048   499711   248832  83 Linux
/dev/sda2     501758  62912511  31205377  5 扩展
/dev/sda3     499712   501757    1023  83 Linux
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.


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


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


[root@node-002 dev]# pvcreate /dev/sda3 
Physical volume "/dev/sda3" successfully created

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

[root@node-002 dev]# pvdisplay
 --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <15.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3839
  Free PE               0
  Allocated PE          3839
  PV UUID               pHtcZN-k0Ne-POHc-CZME-P5yD-N8Vr-DGE9uh
 "/dev/sda3" is a new physical volume of "84.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               84.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Jtewe4-172I-WooF-J1Yz-mGon-3pGv-AptNh0


5.将添加新的物理卷,加载到vg_node003卷组,使用vgextend centos /dev/sda3命令。这里的centos 是在上面查明的VG Name

[root@node-002 dev]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended


查看卷组信息,使用vgdisplay命令。

[root@node-002 dev]# vgdisplay
 --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               98.99 GiB
  PE Size               4.00 MiB
  Total PE              25342
  Alloc PE / Size       3839 / <15.00 GiB
  Free  PE / Size       21503 / <84.00 GiB
  VG UUID               RWIfZm-m8Kg-sePL-wHRC-KDso-IRsd-cDlQYX


6.增加 centos 大小,增加84G。使用lvresize -L +84G /dev/mapper/centos-root命令。

注:centos-root在/dev/mapper里面查看 ==这里我在使用过程中路径是/dev/centos/root,/dev/mapper下虽然也有但是用了会提示找不到逻辑卷 这里 用了提示找不到逻辑卷的原因是我是用的是centos,而不是 centos-root

[root@master ~]# lvresize -L +84G /dev/centos/root 
  Insufficient free space: 21504 extents needed, but only 21503 available
[root@master ~]# lvresize -L +83.99G /dev/centos/root 
  Rounding size to boundary between physical extents: 83.99 GiB.
  Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to <97.39 GiB (24931 extents).
  Logical volume centos/root successfully resized.


7.重新识别centos-root大小,使用resize2fs /dev/mapper/centos-root命令。

[root@node-002 mapper]# resize2fs /dev/mapper/centos-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn't find valid filesystem superblock.


8.如出现下列情况


resize2fs: Bad magic number in super-block 当尝试打开 /dev/mapper/centos-root 时 找不到有效的文件系统超级块.


9.请使用xfs_growfs /dev/mapper/centos-root


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


10.查看扩容后的大小 ,使用df -h命令。

[root@node-002 mapper]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   98G   11G   88G  11% /
devtmpfs                 2.9G     0  2.9G   0% /dev
tmpfs                    2.9G     0  2.9G   0% /dev/shm
tmpfs                    2.9G  8.6M  2.9G   1% /run
tmpfs                    2.9G     0  2.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M  15% /boot
tmpfs                    581M     0  581M   0% /run/user/0


11.当看到/dev/mapper/vg_node003-lv_root 已经更新到新的大小时,则表示增加成功。

目录
相关文章
|
1天前
|
Ubuntu Linux 数据库
【Linux】深入了解Linux磁盘配额:限制用户磁盘空间的利器
【Linux】深入了解Linux磁盘配额:限制用户磁盘空间的利器
|
1天前
|
SQL 监控 架构师
linux系统性能分析的目的
【4月更文挑战第19天】在Linux系统中,找到性能瓶颈是关键,涉及应用程序、操作系统、硬件和网络的全面排查。优化方案通常针对应用程序和操作系统,而硬件和网络问题较易定位。目标是平衡资源使用,确保系统响应和稳定性。系统管理员、架构设计人员和开发人员共同参与,通过监控硬件、网络、配置和代码来优化性能。流程包括管理员初步判断,架构师处理结构问题,开发人员优化代码,实现系统资源的均衡利用。
6 1
|
3天前
|
Ubuntu Linux
Linux(Ubuntu)系统临时IP以及静态IP配置(关闭、启动网卡等操作)
请注意,以上步骤是在临时基础上进行配置的。如果要永久保存静态IP地址,通常还需要修改 `/etc/network/interfaces`文件,以便在系统重启后保持配置。同时,确保备份相关配置文件以防止出现问题。
16 1
|
4天前
|
Linux 数据安全/隐私保护
Linux系统忘记密码的三种解决办法
这篇博客介绍了三种在Linux忘记密码时重置登录密码的方法:1) 使用恢复模式,通过控制台界面以管理员权限更改密码;2) 利用Linux Live CD/USB启动,挂载硬盘分区并使用终端更改密码;3) 进入单用户模式,自动以管理员身份登录后重置密码。每个方法都提供了详细步骤,提醒用户在操作前备份重要数据。
|
4天前
|
JSON Unix Linux
Linux系统之jq工具的基本使用
Linux系统之jq工具的基本使用
32 2
|
4天前
|
数据采集 监控 安全
linux系统被×××后处理经历
linux系统被×××后处理经历
|
4天前
|
监控 安全 Linux
Linux系统之安装ServerBee服务器监控工具
【4月更文挑战第22天】Linux系统之安装ServerBee服务器监控工具
43 2
|
4天前
|
缓存 Linux
linux系统缓存机制
linux系统缓存机制
|
5天前
|
存储 Linux Android开发
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
20 0
|
5天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
7 0