829 views
Liunx-服务安装

Linux磁盘扩容

文章目录

Linux磁盘扩容基本步骤

1. 先查看服务器上的磁盘信息df -h,fdisk -l

注意:我们要扩展的是home目录

[root@ftp ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   10G  2.2G  7.9G  22% /
devtmpfs               1.9G     0  1.9G   0% /dev
tmpfs                  1.9G     0  1.9G   0% /dev/shm
tmpfs                  1.9G  193M  1.7G  11% /run
tmpfs                  1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-home  106G   90G   16G  85% /home
/dev/sda1              497M  124M  373M  25% /boot
tmpfs                  380M     0  380M   0% /run/user/0
[root@ftp ~]# fdisk -l

Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000fc9e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   251658239   125316096   8e  Linux LVM

Disk /dev/mapper/rhel-root: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-swap: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-home: 113.3 GB, 113283956736 bytes, 221257728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

2. 添加服务器磁盘

- 如果是虚拟机,就直接添加一块硬盘
- 如果是磁盘阵列,就直接映射
- 如果是云主机,也直接加一块硬盘

在线加完硬盘之后,需要重新扫描磁盘才能看到新增的那块磁盘,命令如下:
echo "- - -" > /sys/class/scsi_host/host1/scan 

注意:/sys/class/scsi_host/目录下可能会有多个host文件,如果扫描其中一个没有生效,
     可以继续扫描其他的,直至生效为止

下面是加了一个100G硬盘之后的:
[root@ftp ~]# fdisk -l

Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000fc9e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048   251658239   125316096   8e  Linux LVM

Disk /dev/mapper/rhel-root: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-swap: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/rhel-home: 113.3 GB, 113283956736 bytes, 221257728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

# 这一块硬盘是新增的/dev/sdb,虽然新增的是100G,这里显示107.4G,这是系统和虚拟机的计算方式问题,没关系
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

3.开始扩展

1. 将/dev/sdb 创建新的分区
[root@ftp ~]# fdisk /dev/sdb
# 新建分区
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
# 直接Enter 使用默认选项    
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set
# 写入并保存退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@ftp ~]# fdisk -l

....
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x647aa105
# 会看到这里有个新增的分区
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   209715199   104856576   83  Linux

2. 创建pv
[root@ftp ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
[root@ftp ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree  
  /dev/sda2  rhel lvm2 a--  119.51g   4.00m
  /dev/sdb1       lvm2 ---  100.00g 100.00g # 新增的  

3. 扩展vg
[root@ftp ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  rhel   1   3   0 wz--n- 119.51g 4.00m  # 之前的大小
[root@ftp ~]# vgextend rhel /dev/sdb1
  Volume group "rhel" successfully extended
[root@ftp ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree  
  rhel   2   3   0 wz--n- 219.50g 100.00g #扩展后的大小

4. 扩展lv
[root@ftp ~]# lvs
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home rhel -wi-ao---- 105.50g     #之前的大小                                               
  root rhel -wi-ao----  10.00g                                                    
  swap rhel -wi-ao----   4.00g                                                    
[root@ftp ~]# lvextend -L+95G /dev/rhel/home
  Size of logical volume rhel/home changed from 105.50 GiB (27009 extents) to 200.50 GiB (51329 extents).
  Logical volume home successfully resized.
[root@ftp ~]# lvs
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home rhel -wi-ao---- 200.50g     #扩展后的大小                                               
  root rhel -wi-ao----  10.00g                                                    
  swap rhel -wi-ao----   4.00g                                                    

5. 扩展后,不能直接使用,需要重新检查再调整磁盘大小
[root@ftp ~]# e2fsck -f /dev/rhel/home 
e2fsck 1.42.9 (28-Dec-2013)
/dev/rhel/home is mounted.
e2fsck: Cannot continue, aborting.

[root@ftp ~]# resize2fs /dev/rhel/home 
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/rhel/home
Couldn't find valid filesystem superblock.
# 使用上面两个命令会发现有问题,原因是/dev/mapper/rhel-home分区不是ex格式的,需要使用下面的方法
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
[root@ftp ~]# cat /etc/fstab | grep home
/dev/mapper/rhel-home   /home                   xfs     defaults        0 0
[root@ftp ~]# xfs_growfs /dev/rhel/home
meta-data=/dev/mapper/rhel-home  isize=256    agcount=4, agsize=6914304 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=27657216, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=13504, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 27657216 to 52560896
[root@ftp ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   10G  2.2G  7.9G  22% /
devtmpfs               1.9G     0  1.9G   0% /dev
tmpfs                  1.9G     0  1.9G   0% /dev/shm
tmpfs                  1.9G  193M  1.7G  11% /run
tmpfs                  1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/rhel-home  201G   90G  111G  45% /home   # 扩展磁盘成功
/dev/sda1              497M  124M  373M  25% /boot
tmpfs                  380M     0  380M   0% /run/user/0

4.总结所有命令

echo "- - -" > /sys/class/scsi_host/host1/scan 
fdisk /dev/sdb → n → 一直回车,最后w保存
pvcreate /dev/sdb1
vgextend rhel /dev/sdb1
lvextend -L+95G /dev/rhel/home
xfs_growfs /dev/rhel/home  # xfs格式的
resize2fs /dev/rhel/home   # ext格式的
df -h # 查看最终结果
影子专属博客 赣ICP备17013143号