解决linux磁盘扩容大全:新增磁盘原磁盘扩容home分区root分区扩容

Posted 太阳花先生可爱多

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决linux磁盘扩容大全:新增磁盘原磁盘扩容home分区root分区扩容相关的知识,希望对你有一定的参考价值。

一、背景

        linux使用中经常遇到磁盘空间不足,需要对磁盘进行扩容。根据自己的经验进行归纳整理,多种场景,帮助需要者和备查

本文执行的命令,均在centos7 环境下运行的,其他环境思路和相似,命令基本一致。

分区情况说明:

sda是单独的一块磁盘总大小50G,sda1、sda2是两个分区,sda2下面有三个逻辑分区centos-root 、centos-home、centos-swap (df命令时可以看到的挂载点); 

centos-root 对应的挂载点是 根目录

centos-home对应的挂载点是 /home

卷组名称为centos (通过pvdisplay查看

[root@vm-210 templates]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part 
  ├─centos-root 253:0    0   40G  0 lvm  /
  ├─centos-home 253:1    0    7G  0 lvm  /home
  └─centos-swap 253:2    0    2G  0 lvm  [SWAP]
[root@vm-210 templates]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  #卷组名称  
  VG Name               centos
  PV Size               <49.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               1
  Allocated PE          12542
  PV UUID               1Fpioh-HkxD-f4Pd-78An-on4A-vGS1-B9UXr4

二、场景

出现以下问题参考文章:http://t.csdn.cn/cbViW

扩容磁盘不显示扩容后的磁盘或者磁盘大小两种情况:
                情况一:修改原来磁盘大小
                情况二:新增磁盘

场景一:将centos-home分区移动到根分区centos-root

        由于分区时,centos-home分区空间非常大,centos-root分区非常小,需要删除centos-home分区,扩容到centos-root分区

操作思路括号中是用到的命令):

  1. 先备份/home下文件
  2. 卸载(umount) /home (注意:卸载不掉时,请将所有使用/home的进程关闭  参考http://t.csdn.cn/cbViW
  3. 从逻辑卷中删除(lvremove)  home 分区
  4. 扩展:将腾出的空间扩展(lvextend)到 centos-root分区下
  5. 使扩展生效(xfs_growfs
  6. 注释掉fstab中的启动挂载项
  7. 恢复备份数据到/home 下

详细代码如下:

#!/bin/bash
#【功能】 将centos-home分区的空间移动到根centos-root分区下

#1.先备份 /home下文件: 将/home下的所有内容备份到/backup下,下面的命令自动创建/backup目录
mkdir /backup && mv /home/*  /backup

# 2.卸载 centos-home 
umount /home 
   ##  如提示失败则用下面的命令终止/home 分区下的进程 再进行卸载
   ##  fuser 安装方法  yum install -y psmisc
   ##  fuser -m -v -i -k /home
   ##  判断是哪个进程占用了此目录,请kill掉那个进程也可以

# 3.从逻辑卷中删除(lvremove)  centos-home 分区
echo y|lvremove /dev/mapper/centos-home 

# 4.扩展:将腾出的空间扩展(lvextend)到 centos-root分区下
lvextend -l+100%FREE /dev/mapper/centos-root 

# 5.使扩展生效(xfs_growfs)
xfs_growfs /dev/mapper/centos-root 

# 6.注释掉fstab中的启动挂载项
sed -i 's;/dev/mapper/centos-home;#/dev/mapper/centos-home;g' /etc/fstab 

# 7.恢复 原来home下的数据 并删除/backup
mv /backup/* /home  && rm -rf /backup

# 8.查看
df -H

说明:

        思路是一样的,如果是其他分区,请修改里面的分区目录即可

场景二:新增磁盘扩容到根目录或者home下

        磁盘空间不足,新增加了一块硬盘的场景。此处举例新增的一块磁盘名称为/dev/sdb

操作思路括号中是用到的命令):

  1. 查看磁盘情况(lsblk、fdisk -l、pvdisplay)
  2. 创建物理卷(pvcreate
  3. 将磁盘扩展到卷组(vgextend
  4. 扩展逻辑分区(lvextend
  5. 使扩展生效(xfs_growfs
  6. 查看(df -H
# 1.查看磁盘情况(lsblk、fdisk -l、pvdisplay)
lsblk

# 2.创建物理卷(pvcreate)
pvcreate /dev/sdb

# 3.将磁盘扩展到卷组(vgextend)
vgextend centos /dev/sdb

# 4.扩展逻辑分区(lvextend)
#   根据自己的情况选择扩容到centos-root
#                      还是centos-home逻辑分区,请修改注释
# 100%是将所有的磁盘空间扩容到指定分区,也可以根据需要进行调整比例
#      也支持指定大小比如10G等等(指定固定值时参数-l需要修改为-L)
lvextend -l+100%FREE /dev/mapper/centos-root
#lvextend -l+100%FREE /dev/mapper/centos-home

# 5.使扩展生效(xfs_growfs)根据扩容的分区进行选择使用
# xfs_growfs /dev/mapper/centos-home
xfs_growfs /dev/mapper/centos-root

# 6.查看(df -H)
df -H

    场景三:在原始磁盘上增加空间(常在虚拟化环境中)

        虚拟化或者超融合环境中、或者VMware虚拟机中;磁盘空间不足,增加了已有磁盘的大小的场景,把增加的空间扩容到根目录或者centos-home下。

操作思路括号中是用到的命令):

方法一:fdisk进行分区,操作步骤与场景二基本一样,多了一步分区

  1. 查看磁盘情况(lsblk、fdisk -l、pvdisplay)
  2. 创建分区(fdisk 磁盘名称)
  3. 创建物理卷(pvcreate
  4. 将磁盘扩展到卷组(vgextend
  5. 扩展逻辑分区(lvextend
  6. 使扩展生效(xfs_growfs
  7. 查看(df -H

方法二:用parted命令调整大小

  1. 查看磁盘情况(parted /dev/sda  print free)
  2. 分配剩余的可用空间(parted /dev/sda resizepart 2 100%
  3. 刷新物理卷(pvresize /dev/sda2
  4. 扩展逻辑分区(lvextend
  5. 使扩展生效(xfs_growfs
  6. 查看(df -H

 方法一:fdisk进行分区,操作步骤与场景二基本一样,多了一步分区

# 1.创建分区(fdisk)
fdisk  /dev/sda
#在进入命令环境中,按n键  一路按回车,最后一步按w键保存,最后按q键退出

# 2.创建物理卷(pvcreate) sda3根据第一步中创建的分区名称进行替换
pvcreate /dev/sda3

# 3.将磁盘扩展到卷组(vgextend)  sda3根据第一步中创建的分区名称进行替换
vgextend centos /dev/sda3

# 4.扩展逻辑分区(lvextend)
#   根据自己的情况选择扩容到centos-root
#                      还是centos-home逻辑分区,请修改注释
# 100%是将所有的磁盘空间扩容到指定分区,也可以根据需要进行调整比例
#      也支持指定大小比如10G等等(指定固定值时参数-l需要修改为-L)
lvextend -l+100%FREE /dev/mapper/centos-root
#lvextend -l+100%FREE /dev/mapper/centos-home

# 5.使扩展生效(xfs_growfs)根据扩容的分区进行选择使用
# xfs_growfs /dev/mapper/centos-home
xfs_growfs /dev/mapper/centos-root

# 6.查看(df -H)
df -H

   方法二:用parted命令调整大小

举例:系统只有一个磁盘,2个分区:需要将新扩容的磁盘空间扩容到分区2上,下图中number 为2的,实际上对应的是/dev/sda2

[root@vm-210 templates]# parted /dev/sda  print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  53.7GB  52.6GB  primary               lvm
方法二:用parted命令进行操作

#1. 查看磁盘情况:打印可用空间
parted /dev/sda  print free

#2.分配剩余的可用空间
#将剩余的可用空间分配到 /dev/sda2,根据实际修改磁盘
# resizepart 中的2 只的是第二个分区即:/dev/sda2 
#             100% 将所有的空闲空间分配给/dev/sda2,也可以用单位和百分比
parted /dev/sda resizepart 2 100%

# 3.刷新物理卷
#分区的空间修改了,也要刷新一下pv物理卷的大小,这样pv才能识别变动的空间
pvresize /dev/sda2

# 4.查看物理卷是否变化
#查看pv中free 项中是否显示了可用的空间
pvdisplay

# 5.扩展逻辑分区
#开始增加到逻辑卷中
lvextend -l+100%FREE /dev/mapper/centos-root

# 6.使扩展生效(xfs_growfs)
xfs_growfs /dev/mapper/centos-root

# 7.查看(df -H)
df -H

三、总结

        自己在工作中遇到磁盘扩容的问题进行了汇总。按照我的命令可以一步一步执行,如果还想进行了解更深,需要了解什么是物理卷(pv)、逻辑卷、逻辑分组、逻辑分区等等。
       

Linux下非逻辑卷扩容之新增盘扩容

导语

我们都知道使用lv逻辑卷可以方便的扩容。
但是当原盘不是逻辑卷且其上有数据的时候,如何扩容呢?
因为扩容只能是磁盘type为逻辑卷lvm才可以。
新增数据盘创建逻辑卷分区后在将原分区创建逻辑卷之后再合并。
我们今天就来测试一下。

添加硬盘

  • 添加两块磁盘

查看分区情况

  • 我们看到新添加的两块盘已经可以用了
[root@C8-196 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  200G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0  199G  0 part 
  ├─cl-root 253:0    0   70G  0 lvm  /
  ├─cl-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cl-home 253:2    0  127G  0 lvm  /home
sdb           8:16   0    5G  0 disk 
sdc           8:32   0    5G  0 disk 

对sdb进行传统分区并挂载数据目录

  • 使用fdisk对/dev/sdb进行分区,
  • 创建一个主分区并格式化为xfs
  • 挂载数据目录/data
[root@C8-196 ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x72dee268.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Last sector, +sectors or +sizeK,M,G,T,P (2048-10485759, default 10485759): 

Created a new partition 1 of type 'Linux' and of size 5 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@C8-196 ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327616 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1310464, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@C8-196 ~]# mount /dev/sdb1 /data
[root@C8-196 ~]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  876M     0  876M   0% /dev
tmpfs               tmpfs     896M     0  896M   0% /dev/shm
tmpfs               tmpfs     896M   17M  879M   2% /run
tmpfs               tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home xfs       127G  939M  126G   1% /home
/dev/sda1           xfs      1014M  197M  818M  20% /boot
tmpfs               tmpfs     180M     0  180M   0% /run/user/0
/dev/sdb1           xfs       5.0G   68M  5.0G   2% /data

写入数据模拟数据盘写100个文件到磁盘上做数据测试

  • 写100个文件到磁盘上做数据测试
    for i in 1..100;do $(time dd if=/dev/zero of=/data/testw$i.db bs=4k count=10000);done&&ll /data&&df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  876M     0  876M   0% /dev
tmpfs               tmpfs     896M     0  896M   0% /dev/shm
tmpfs               tmpfs     896M   17M  879M   2% /run
tmpfs               tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home xfs       127G  939M  126G   1% /home
/dev/sda1           xfs      1014M  197M  818M  20% /boot
tmpfs               tmpfs     180M     0  180M   0% /run/user/0
/dev/sdb1           xfs       5.0G  3.9G  1.2G  78% /data

在sdc上创建逻辑卷lvm模拟新增盘扩容

  • 不分区直接对sdc整盘创建逻辑卷
    pvcreate /dev/sdc&&vgcreate datavg /dev/sdc&&lvcreate -l 100%FREE -n datalv datavg&&lsblk
[root@C8-196 ~]# pvcreate /dev/sdc&&vgcreate datavg /dev/sdc&&lvcreate -l 100%FREE -n datalv datavg&&lsblk
  Physical volume "/dev/sdc" successfully created.
  Volume group "datavg" successfully created
  Logical volume "datalv" created.
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─cl-root     253:0    0   70G  0 lvm  /
  ├─cl-swap     253:1    0    2G  0 lvm  [SWAP]
  └─cl-home     253:2    0  127G  0 lvm  /home
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    5G  0 part /data
sdc               8:32   0    5G  0 disk 
└─datavg-datalv 253:3    0    5G  0 lvm  

尝试直接使用dd命令拷贝sdb1上的数据到sdc逻辑卷

  • 据说dd有格式化的功能
  • 尝试逻辑卷不创建文件系统直接dd
  • 结果dd了个寂寞
[root@C8-196 ~]# time dd if=/dev/sdb1 of=/dev/mapper/datavg-datalv
dd: writing to '/dev/mapper/datavg-datalv': No space left on device
10477569+0 records in
10477568+0 records out
5364514816 bytes (5.4 GB, 5.0 GiB) copied, 63.8898 s, 84.0 MB/s

real	1m3.891s
user	0m0.823s
sys	0m22.107s
[root@C8-196 ~]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  876M     0  876M   0% /dev
tmpfs               tmpfs     896M     0  896M   0% /dev/shm
tmpfs               tmpfs     896M   17M  879M   2% /run
tmpfs               tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home xfs       127G  939M  126G   1% /home
/dev/sda1           xfs      1014M  197M  818M  20% /boot
tmpfs               tmpfs     180M     0  180M   0% /run/user/0
/dev/sdb1           xfs       5.0G  3.9G  1.2G  78% /data
[root@C8-196 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─cl-root     253:0    0   70G  0 lvm  /
  ├─cl-swap     253:1    0    2G  0 lvm  [SWAP]
  └─cl-home     253:2    0  127G  0 lvm  /home
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    5G  0 part /data
sdc               8:32   0    5G  0 disk 
└─datavg-datalv 253:3    0    5G  0 lvm  
[root@C8-196 ~]# mount /dev/mapper/datavg-datalv /data
mount: /data: wrong fs type, bad option, bad superblock on /dev/mapper/datavg-datalv, missing codepage or helper program, or other error.

尝试创建文件系统后再dd

  • 创建文件系统的时候告诉我已经有了
  • 难道是惊喜?告诉我什么是TMD惊喜?
[root@C8-196 ~]# mkfs.xfs /dev/mapper/datavg-datalv
mkfs.xfs: /dev/mapper/datavg-datalv appears to contain an existing filesystem (xfs).
mkfs.xfs: Use the -f option to force overwrite.

尝试先解挂载后再挂载新创建的逻辑卷

  • 之前dd后没有umount就直接挂载,提示出错了
  • 这回试试先umount以后,再挂载同一个目录同一个梦想
[root@C8-196 ~]# umount /dev/sdb1
[root@C8-196 ~]# df -TH
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  919M     0  919M   0% /dev
tmpfs               tmpfs     939M     0  939M   0% /dev/shm
tmpfs               tmpfs     939M   18M  922M   2% /run
tmpfs               tmpfs     939M     0  939M   0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        76G  2.5G   73G   4% /
/dev/mapper/cl-home xfs       137G  984M  136G   1% /home
/dev/sda1           xfs       1.1G  207M  857M  20% /boot
tmpfs               tmpfs     188M     0  188M   0% /run/user/0
[root@C8-196 ~]# mount -t xfs /dev/mapper/datavg-datalv /data
mount: /data: can't read superblock on /dev/mapper/datavg-datalv.
  • 白兴奋了,还是挂了个寂寞

尝试创建文件系统后再dd

  • 先把目录挂回来,保持挂载状态,看看dd有啥效果
[root@C8-196 ~]# mount /dev/sdb1 /data&&ls /data
testw100.db  testw25.db  testw40.db  testw56.db  testw71.db  testw87.db
testw10.db   testw26.db  testw41.db  testw57.db  testw72.db  testw88.db
testw11.db   testw27.db  testw42.db  testw58.db  testw73.db  testw89.db
testw12.db   testw28.db  testw43.db  testw59.db  testw74.db  testw8.db
testw13.db   testw29.db  testw44.db  testw5.db   testw75.db  testw90.db
testw14.db   testw2.db   testw45.db  testw60.db  testw76.db  testw91.db
testw15.db   testw30.db  testw46.db  testw61.db  testw77.db  testw92.db
testw16.db   testw31.db  testw47.db  testw62.db  testw78.db  testw93.db
testw17.db   testw32.db  testw48.db  testw63.db  testw79.db  testw94.db
testw18.db   testw33.db  testw49.db  testw64.db  testw7.db   testw95.db
testw19.db   testw34.db  testw4.db   testw65.db  testw80.db  testw96.db
testw1.db    testw35.db  testw50.db  testw66.db  testw81.db  testw97.db
testw20.db   testw36.db  testw51.db  testw67.db  testw82.db  testw98.db
testw21.db   testw37.db  testw52.db  testw68.db  testw83.db  testw99.db
testw22.db   testw38.db  testw53.db  testw69.db  testw84.db  testw9.db
testw23.db   testw39.db  testw54.db  testw6.db   testw85.db
testw24.db   testw3.db   testw55.db  testw70.db  testw86.db
  • 强制创建文件系统在逻辑卷上
[root@C8-196 ~]# mkfs.xfs /dev/mapper/datavg-datalv -f
meta-data=/dev/mapper/datavg-datalv isize=512    agcount=4, agsize=327424 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1309696, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
  • 再次dd,哈哈哈!
[root@C8-196 ~]# time dd if=/dev/sdb1 of=/dev/mapper/datavg-datalv
dd: writing to '/dev/mapper/datavg-datalv': No space left on device
10477569+0 records in
10477568+0 records out
5364514816 bytes (5.4 GB, 5.0 GiB) copied, 59.9753 s, 89.4 MB/s

real	0m59.977s
user	0m0.669s
sys	0m19.805s
  • 估计还是挂不上
[root@C8-196 ~]# mount /dev/mapper/datavg-datalv /data
mount: /data: wrong fs type, bad option, bad superblock on /dev/mapper/datavg-datalv, missing codepage or helper program, or other error.
[root@C8-196 ~]# umount /dev/sdb1
[root@C8-196 ~]# df -Th
Filesystem          Type      Size  Used Avail Use% Mounted on
devtmpfs            devtmpfs  876M     0  876M   0% /dev
tmpfs               tmpfs     896M     0  896M   0% /dev/shm
tmpfs               tmpfs     896M   17M  879M   2% /run
tmpfs               tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home xfs       127G  939M  126G   1% /home
/dev/sda1           xfs      1014M  197M  818M  20% /boot
tmpfs               tmpfs     180M     0  180M   0% /run/user/0
[root@C8-196 ~]# mount /dev/mapper/datavg-datalv /data
mount: /data: can't read superblock on /dev/mapper/datavg-datalv.
  • 同样的结果,再次出现,看来dd果然是带有格式化功能呢

正经挂载逻辑卷吧

  • 按照正常步骤来是肯定能挂上的
[root@C8-196 ~]# mkfs.xfs /dev/mapper/datavg-datalv -f
meta-data=/dev/mapper/datavg-datalv isize=512    agcount=4, agsize=327424 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1309696, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@C8-196 ~]# mount /dev/mapper/datavg-datalv /data
[root@C8-196 ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  876M     0  876M   0% /dev
tmpfs                     tmpfs     896M     0  896M   0% /dev/shm
tmpfs                     tmpfs     896M   17M  879M   2% /run
tmpfs                     tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root       xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home       xfs       127G  939M  126G   1% /home
/dev/sda1                 xfs      1014M  197M  818M  20% /boot
tmpfs                     tmpfs     180M     0  180M   0% /run/user/0
/dev/mapper/datavg-datalv xfs       5.0G   68M  5.0G   2% /data
[root@C8-196 ~]# ls /data
[root@C8-196 ~]# ll /data
total 0

创建一个新目录挂原数据盘

  • 创建新的目录用来挂载原来的数据盘,数据都在里边
[root@C8-196 ~]# mkdir /datasrc
[root@C8-196 ~]# mount /dev/sdb1 /datasrc/&&ls /datasrc&&df -Th
testw100.db  testw25.db  testw40.db  testw56.db  testw71.db  testw87.db
testw10.db   testw26.db  testw41.db  testw57.db  testw72.db  testw88.db
testw11.db   testw27.db  testw42.db  testw58.db  testw73.db  testw89.db
testw12.db   testw28.db  testw43.db  testw59.db  testw74.db  testw8.db
testw13.db   testw29.db  testw44.db  testw5.db   testw75.db  testw90.db
testw14.db   testw2.db   testw45.db  testw60.db  testw76.db  testw91.db
testw15.db   testw30.db  testw46.db  testw61.db  testw77.db  testw92.db
testw16.db   testw31.db  testw47.db  testw62.db  testw78.db  testw93.db
testw17.db   testw32.db  testw48.db  testw63.db  testw79.db  testw94.db
testw18.db   testw33.db  testw49.db  testw64.db  testw7.db   testw95.db
testw19.db   testw34.db  testw4.db   testw65.db  testw80.db  testw96.db
testw1.db    testw35.db  testw50.db  testw66.db  testw81.db  testw97.db
testw20.db   testw36.db  testw51.db  testw67.db  testw82.db  testw98.db
testw21.db   testw37.db  testw52.db  testw68.db  testw83.db  testw99.db
testw22.db   testw38.db  testw53.db  testw69.db  testw84.db  testw9.db
testw23.db   testw39.db  testw54.db  testw6.db   testw85.db
testw24.db   testw3.db   testw55.db  testw70.db  testw86.db
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  876M     0  876M   0% /dev
tmpfs                     tmpfs     896M     0  896M   0% /dev/shm
tmpfs                     tmpfs     896M   17M  879M   2% /run
tmpfs                     tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root       xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home       xfs       127G  939M  126G   1% /home
/dev/sda1                 xfs      1014M  197M  818M  20% /boot
tmpfs                     tmpfs     180M     0  180M   0% /run/user/0
/dev/mapper/datavg-datalv xfs       5.0G   68M  5.0G   2% /data
/dev/sdb1                 xfs       5.0G  3.9G  1.2G  78% /datasrc

用dd直接拷贝目录吧

  • 想的太美了,dd不能拷贝目录
[root@C8-196 ~]# time dd if=/datasrc of=/data
dd: failed to open '/data': Is a directory

real	0m0.001s
user	0m0.000s
sys	0m0.001s

老老实实的用CP吧

time cp -av /datasrc /data

[root@C8-196 ~]# time cp -av /datasrc /data
'/datasrc' -> '/data/datasrc'
'/datasrc/testw1.db' -> '/data/datasrc/testw1.db'
'/datasrc/testw2.db' -> '/data/datasrc/testw2.db'
'/datasrc/testw3.db' -> '/data/datasrc/testw3.db'
## :
## 中间省略好多行
## :
'/datasrc/testw99.db' -> '/data/datasrc/testw99.db'
'/datasrc/testw100.db' -> '/data/datasrc/testw100.db'

real	0m2.541s
user	0m0.006s
sys	0m2.155s

数据迁移完了就搞原来的数据盘弄成逻辑卷就行了

[root@C8-196 ~]# umount /datasrc 
[root@C8-196 ~]# pvcreate /dev/sdb
  Cannot use /dev/sdb: device is partitioned
[root@C8-196 ~]# pvcreate /dev/sdv1
  No device found for /dev/sdv1.
[root@C8-196 ~]# pvcreate /dev/sdb1
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: n
  Aborted wiping of xfs.
  1 existing signature left on the device.
[root@C8-196 ~]# pvcreate /dev/sdb1&&vgcreate datavg /dev/sdb1&&lvextend -rl 100%FREE /dev/mapper/datavg-datalv
WARNING: xfs signature detected on /dev/sdb1 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
  /dev/datavg: already exists in filesystem
  Run `vgcreate --help' for more information.
[root@C8-196 ~]# vgex
vgexport  vgextend  
[root@C8-196 ~]# vgextend 
cl      datavg  
[root@C8-196 ~]# vgextend datavg /dev/sdb1&&lvextend -rl 100%FREE /dev/mapper/datavg-datalv&&lsblk&&df -Th
  Volume group "datavg" successfully extended
  Size of logical volume datavg/datalv unchanged from <5.00 GiB (1279 extents).
  Logical volume datavg/datalv successfully resized.
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─cl-root     253:0    0   70G  0 lvm  /
  ├─cl-swap     253:1    0    2G  0 lvm  [SWAP]
  └─cl-home     253:2    0  127G  0 lvm  /home
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sdc               8:32   0    5G  0 disk 
└─datavg-datalv 253:3    0    5G  0 lvm  /data
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  876M     0  876M   0% /dev
tmpfs                     tmpfs     896M     0  896M   0% /dev/shm
tmpfs                     tmpfs     896M   17M  879M   2% /run
tmpfs                     tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root       xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home       xfs       127G  939M  126G   1% /home
/dev/sda1                 xfs      1014M  197M  818M  20% /boot
tmpfs                     tmpfs     180M     0  180M   0% /run/user/0
/dev/mapper/datavg-datalv xfs       5.0G  3.9G  1.2G  78% /data
  • sdb1怎么还没变? /data目录的空间也没变大啊!

查看当前逻辑卷的状态

[root@C8-196 ~]# pvs
  PV         VG     Fmt  Attr PSize    PFree 
  /dev/sda2  cl     lvm2 a--  <199.00g     0 
  /dev/sdb1  datavg lvm2 a--    <5.00g <5.00g
  /dev/sdc   datavg lvm2 a--    <5.00g     0 
[root@C8-196 ~]# vgs
  VG     #PV #LV #SN Attr   VSize    VFree 
  cl       1   3   0 wz--n- <199.00g     0 
  datavg   2   1   0 wz--n-    9.99g <5.00g
[root@C8-196 ~]# lvs
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home   cl     -wi-ao---- 126.96g                                                    
  root   cl     -wi-ao----  70.00g                                                    
  swap   cl     -wi-ao----   2.03g                                                    
  datalv datavg -wi-ao----  <5.00g        

再搞一次扩展

[root@C8-196 ~]# lvextend -r -l 100%FREE /dev/mapper/datavg-datalv
Size of logical volume datavg/datalv unchanged from <5.00 GiB (1279 extents).
Logical volume datavg/datalv successfully resized.
[root@C8-196 ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  876M     0  876M   0% /dev
tmpfs                     tmpfs     896M     0  896M   0% /dev/shm
tmpfs                     tmpfs     896M   17M  879M   2% /run
tmpfs                     tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root       xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home       xfs       127G  939M  126G   1% /home
/dev/sda1                 xfs      1014M  197M  818M  20% /boot
tmpfs                     tmpfs     180M     0  180M   0% /run/user/0
/dev/mapper/datavg-datalv xfs       5.0G  3.9G  1.2G  78% /data
[root@C8-196 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
├─cl-root     253:0    0   70G  0 lvm  /
├─cl-swap     253:1    0    2G  0 lvm  [SWAP]
└─cl-home     253:2    0  127G  0 lvm  /home
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sdc               8:32   0    5G  0 disk 
└─datavg-datalv 253:3    0    5G  0 lvm  /data
[root@C8-196 ~]# 
  • 结果还是没变???
  • 所以lvextend不好用,哈哈哈!

一步到位的逻辑卷扩容大法来了!

[root@C8-196 ~]# lvresize -r -l 100%FREE /dev/mapper/datavg-datalv&&df -Th &&lsblk
  Size of logical volume datavg/datalv unchanged from <5.00 GiB (1279 extents).
  Logical volume datavg/datalv successfully resized.
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  876M     0  876M   0% /dev
tmpfs                     tmpfs     896M     0  896M   0% /dev/shm
tmpfs                     tmpfs     896M   17M  879M   2% /run
tmpfs                     tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root       xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home       xfs       127G  939M  126G   1% /home
/dev/sda1                 xfs      1014M  197M  818M  20% /boot
tmpfs                     tmpfs     180M     0  180M   0% /run/user/0
/dev/mapper/datavg-datalv xfs       5.0G  3.9G  1.2G  78% /data
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─cl-root     253:0    0   70G  0 lvm  /
  ├─cl-swap     253:1    0    2G  0 lvm  [SWAP]
  └─cl-home     253:2    0  127G  0 lvm  /home
sdb               8:16   0    5G  0 disk 
└─sdb1            8:17   0    5G  0 part 
sdc               8:32   0    5G  0 disk 
└─datavg-datalv 253:3    0    5G  0 lvm  /data
  • 结果还是一样的,那个sdb1就有问题在那
  • 看他不爽,搞掉他
[root@C8-196 ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@C8-196 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─cl-root     253:0    0   70G  0 lvm  /
  ├─cl-swap     253:1    0    2G  0 lvm  [SWAP]
  └─cl-home     253:2    0  127G  0 lvm  /home
sdb               8:16   0    5G  0 disk 
sdc               8:32   0    5G  0 disk 
└─datavg-datalv 253:3    0    5G  0 lvm  /data

逻辑分区再次搞起!

[root@C8-196 ~]# pvcreate /dev/sdb&&vgextend datavg /dev/sdb&&lvresize -r -l +100%FREE /dev/mapper/datavg-datalv&&df -Th &&lsblk
WARNING: dos signature detected on /dev/sdb at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdb.
  Physical volume "/dev/sdb" successfully created.
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  WARNING: VG datavg is missing PV l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV (last written to /dev/sdb1).
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  Volume group "datavg" successfully extended
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  WARNING: VG datavg is missing PV l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV (last written to [unknown]).
  Cannot change VG datavg while PVs are missing.
  See vgreduce --removemissing and vgextend --restoremissing.
  Cannot process volume group datavg
[root@C8-196 ~]# pvs
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  WARNING: VG datavg is missing PV l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV (last written to [unknown]).
  PV         VG     Fmt  Attr PSize    PFree 
  /dev/sda2  cl     lvm2 a--  <199.00g     0 
  /dev/sdb   datavg lvm2 a--    <5.00g <5.00g
  /dev/sdc   datavg lvm2 a--    <5.00g     0 
  [unknown]  datavg lvm2 a-m    <5.00g <5.00g
[root@C8-196 ~]# vgs
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  WARNING: VG datavg is missing PV l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV (last written to [unknown]).
  VG     #PV #LV #SN Attr   VSize    VFree
  cl       1   3   0 wz--n- <199.00g    0 
  datavg   3   1   0 wz-pn-  <14.99g 9.99g
  • 结果搞挂了!哈哈哈!
  • 忘了把原来的vg拿掉了
[root@C8-196 ~]# pvs
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  WARNING: VG datavg is missing PV l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV (last written to [unknown]).
  PV         VG     Fmt  Attr PSize    PFree 
  /dev/sda2  cl     lvm2 a--  <199.00g     0 
  /dev/sdb   datavg lvm2 a--    <5.00g <5.00g
  /dev/sdc   datavg lvm2 a--    <5.00g     0 
  [unknown]  datavg lvm2 a-m    <5.00g <5.00g
[root@C8-196 ~]# vgreduce --removemissing datavg
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  WARNING: VG datavg is missing PV l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV (last written to [unknown]).
  WARNING: Couldn't find device with uuid l7TGdc-G8Zg-ZqCJ-Ef7X-wF2R-den5-njtWyV.
  Wrote out consistent volume group datavg.
[root@C8-196 ~]# vgs
  VG     #PV #LV #SN Attr   VSize    VFree 
  cl       1   3   0 wz--n- <199.00g     0 
  datavg   2   1   0 wz--n-    9.99g <5.00g
[root@C8-196 ~]# pvs
  PV         VG     Fmt  Attr PSize    PFree 
  /dev/sda2  cl     lvm2 a--  <199.00g     0 
  /dev/sdb   datavg lvm2 a--    <5.00g <5.00g
  /dev/sdc   datavg lvm2 a--    <5.00g    

华丽丽的成功了!哈哈哈!

lvresize -r -l +100%FREE /dev/mapper/datavg-datalv&&df -Th &&lsblk

[root@C8-196 ~]# vgextend datavg /dev/sdb&&lvresize -r -l +100%FREE /dev/mapper/datavg-datalv&&df -Th &&lsblk
  Physical volume '/dev/sdb' is already in volume group 'datavg'
  Unable to add physical volume '/dev/sdb' to volume group 'datavg'
  /dev/sdb: physical volume not initialized.
[root@C8-196 ~]# lvresize -r -l +100%FREE /dev/mapper/datavg-datalv&&df -Th &&lsblk
  Size of logical volume datavg/datalv changed from <5.00 GiB (1279 extents) to 9.99 GiB (2558 extents).
  Logical volume datavg/datalv successfully resized.
meta-data=/dev/mapper/datavg-datalv isize=512    agcount=4, agsize=327424 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=1309696, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           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 1309696 to 2619392
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  876M     0  876M   0% /dev
tmpfs                     tmpfs     896M     0  896M   0% /dev/shm
tmpfs                     tmpfs     896M   17M  879M   2% /run
tmpfs                     tmpfs     896M     0  896M   0% /sys/fs/cgroup
/dev/mapper/cl-root       xfs        70G  2.3G   68G   4% /
/dev/mapper/cl-home       xfs       127G  939M  126G   1% /home
/dev/sda1                 xfs      1014M  197M  818M  20% /boot
tmpfs                     tmpfs     180M     0  180M   0% /run/user/0
/dev/mapper/datavg-datalv xfs        10G  4.0G  6.1G  40% /data
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─cl-root     253:0    0   70G  0 lvm  /
  ├─cl-swap     253:1    0    2G  0 lvm  [SWAP]
  └─cl-home     253:2    0  127G  0 lvm  /home
sdb               8:16   0    5G  0 disk 
└─datavg-datalv 253:3    0   10G  0 lvm  /data
sdc               8:32   0    5G  0 disk 
└─datavg-datalv 253:3    0   10G  0 lvm  /data

总结

  • 总体思路是
  1. 先将新挂载盘做成逻辑卷,
  2. 接着老老实实用cp -av拷贝文件
  3. 拷贝完文件踏踏实实把原来的分区干掉
  4. 在原分区或磁盘上创建新的逻辑分区
  5. 扩展逻辑分区命令lvresize -r -l +100%FREE /dev/mapper/datavg-datalv
  • 上边踩过的坑
  1. 不要试图使用dd命令拷贝文件夹,逻辑卷和物理分区完全不是一回事
  2. 最好对原盘的分区彻底搞一下,如果是独立盘,要把分区删掉最好
  3. 逻辑卷往下摘物理盘是有顺序的

从卷组里剔除物理卷

  1. 先要从卷组中将被占用的空间挪走 将被占用的pe挪走
    挪之前pe的个数要小于整个vg剩余的pe数
    pvmove /dev/sdc 将pv上被占用的pe挪到同vg上的其他pv上
  2. 挪完以后从vg中删掉pv vgreduce vof /dev/sdc
  3. 再删除pv属性 pvremove /dev/sdc

以上是关于解决linux磁盘扩容大全:新增磁盘原磁盘扩容home分区root分区扩容的主要内容,如果未能解决你的问题,请参考以下文章

Linux虚拟机磁盘扩容

Linux操作系统系列:通过新增磁盘对LVM文件系统扩容

Linux下非逻辑卷扩容之新增盘扩容

Linux下非逻辑卷扩容之新增盘扩容

Linux下非逻辑卷扩容之新增盘扩容

SQL Server 磁盘空间告急(磁盘扩容)转载