汇总Linux系统常见磁盘扩容操作

一、准备工作:获取目标磁盘信息

(1)确认分区表格式和文件系统

运行以下命令确认数据盘的分区表格式



[root@staros ~]#fdisk -lu /dev/sda
Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 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: 0x9277b47b
 
Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 83 Linux

注意:Disk label type 的值为 dos 则分区是 MBR,若该结果的值为 gpt 则分区是 GPT

运行以下命令确认已有分区的文件系统类型



[root@staros ~]# blkid /dev/vdb1
/dev/vdb1: UUID="e97bf1e2-fc84-4c11-9652-73********24" TYPE="ext4"

注意:TYPE 的值为 ext4 则文件系统类型为 ext4,若该结果的值为 xfs 则文件系统类型为 xfs

ext*文件系统使用 resize2fs 命令扩容文件系统 xfs 文件系统使用 xfs_growfs 命令扩容文件系统

说明

为了防止数据丢失,不建议扩容已挂载的分区和文件系统。请先取消挂载(umount)分区,完成扩容并正常使用后,重新挂载(mount)。针对不同的 Linux 内核版本,推荐以下操作方式:

实例内核版本小于 3.6:先取消挂载该分区,再修改分区表,最后扩容文件系统。

实例内核版本大于等于 3.6:先修改对应分区表,再通知内核更新分区表,最后扩容文件系统。

二、扩容已有 MBR 分区

fdisk 命令主要用于 MBR 分区表的操作

MBR: MBR 分区表支持最多四个主分区,其中一个可以是扩展分区,该扩展分区可以包含多个逻辑分区。由于 MBR 使用 32 位的扇区偏移值来寻址,因此 MBR 分区表的最大容量为 2 TB

(1)分区后扩容

/dev/sdb 有 10G,只有一个分区/dev/sdb1。将其扩大至 20G,将新增的 10G 划分至已有的/dev/sdb1 内

ext 为例



[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb1               ext4      9.8G   37M  9.2G   1% /data

执行以下命令,安装 growpart 扩容工具



yum install cloud-utils-growpart -y
 
[root@staros ~]# echo 1 > /sys/block/sdb/device/rescan #如果数据盘扩容后容量大小未变执行此命令让内核扫描并重新识别连接到 sdb 磁盘的所有磁盘驱动器
[root@staros ~]# growpart /dev/sdb 1
CHANGED: partition=1 start=2048 old: size=20969472 end=20971520 new: size=41940959 end=41943007
[root@staros ~]# resize2fs /dev/sdb1 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/sdb1 is mounted on /data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/sdb1 is now 5242619 blocks long.



[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb1               ext4       20G   44M   19G   1% /data

如果不使用 growpart 命令 手动扩容如下



[root@staros ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
 
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
 
 
Command (m for help): p  #查看分区
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x2e714d05
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux
 
Command (m for help): d   #删除当前 1 分区 切记不要 w 保存退出 会造成数据丢失
Selected partition 1
Partition 1 is deleted
 
Command (m for help): n #选择分区类型
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p  #选择主分区
Partition number (1-4, default 1):    
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
 
Command (m for help): p
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x2e714d05
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux
 
Command (m for help): w  #以上操作保存退出 如果中途命令输入错误 ctrl+c 回退所有命令  重头再开始
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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.
[root@staros ~]# partprobe
[root@staros ~]# resize2fs /dev/sdb1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/sdb1 is mounted on /data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/sdb1 is now 5242624 blocks long.
 
[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb1               ext4       20G   44M   19G   1% /data

xfs 为例



[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb1               xfs       9.8G   37M  9.2G   1% /data


执行以下命令,安装 growpart 扩容工具



yum install cloud-utils-growpart -y
[root@staros ~]# echo 1 > /sys/block/sdb/device/rescan #如果数据盘扩容后容量大小未变执行此命令让内核扫描并重新识别连接到 sdb 磁盘的所有磁盘驱动器
[root@staros ~]# growpart /dev/sdb 1
CHANGED: partition=1 start=2048 old: size=20969472 end=20971520 new: size=41940959 end=41943007
[root@staros ~]# xfs_growfs /dev/sdb1 
[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb1               xfs        20G   44M   19G   1% /data
 

如果不使用 growpart 命令 手动扩容如下



[root@staros ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
 
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
 
 
Command (m for help): p #打印分区表
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x2e714d05
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   83  Linux
 
Command (m for help): d  #删除分区   
Selected partition 1
Partition 1 is deleted
 
Command (m for help): n #选择主分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p  #默认选择 primary
Partition number (1-4, default 1):    #默认选择 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
 
Command (m for help): p
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x2e714d05
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux
 
Command (m for help): w  #保存修改并退出
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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.
[root@staros ~]# partprobe
[root@staros ~]# xfs_growfs /dev/sdb1
meta-data=/dev/sdf               isize=512    agcount=4, agsize=655360 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2621440, 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
[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb1               xfs       20G   44M   19G   1% /data


三、扩容已有 GPT 分区

parted 命令主要用于 GPT 分区表的操作

GPT: GPT 分区表支持更大的硬盘容量。它使用 64 位的逻辑块地址,因此可以支持超过 2 TB 的硬盘容量

(1)分区后扩容

ext 为例

/dev/sdf1 有 10G。将数据盘容量扩大至 20G,将新增的 10G 划分至已有的/dev/sdf1 内




[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   20M  1.9G   2% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata ext4      689G   69M  658G   1% /data
/dev/sdf1                 ext4      9.8G   37M  9.2G   1% /aaa
[root@staros ~]# parted /dev/sdf
GNU Parted 3.1
Using /dev/sdf
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: The backup GPT table is not at the end of the disk, as it should be.  This might mean that another operating system believes the
disk is smaller.  Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? Fix   #若界面提示 Fix/Ignore/Cancel?和 Fix/Ignore?,均输入 Fix 即可
Warning: Not all of the space available to /dev/sdf appears to be used, you can fix the GPT to use all of the space (an extra 20971520
blocks) or continue with the current setting?
Fix/Ignore? Fix
Model: VMware Virtual disk (scsi)
Disk /dev/sdf: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
 
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  10.7GB  10.7GB  ext4         primary
 
(parted) rm 1  #删除 1 分区
(parted) mkpart primary 1049kB 100%   #原分区的起始扇区为 1049kB,且要将扩容后的总容量全部分配给该分区
(parted) print 
Model: VMware Virtual disk (scsi)
Disk /dev/sdf: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
 
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  21.5GB  21.5GB  ext4         primary
(parted) quit #退出
Information: You may need to update /etc/fstab.
 
[root@staros ~]# resize2fs /dev/sdf1
resize2fs 1.42.9 (28-Dec-2013)
Please run 'e2fsck -f /dev/sdf1' first.
 
[root@staros ~]# e2fsck -f /dev/sdf1  #检测磁盘
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdf1: 11/655360 files (0.0% non-contiguous), 83137/2620928 blocks
[root@staros ~]# resize2fs /dev/sdf1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/sdf1 to 5242368 (4k) blocks.
The filesystem on /dev/sdf1 is now 5242368 blocks long.
 
[root@staros ~]# mount /dev/sdf1  /aaa
[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   20M  1.9G   2% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata ext4      689G   69M  658G   1% /data
/dev/sdf1                 ext4       20G   44M   19G   1% /aaa

xfs 为例



[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   20M  1.9G   2% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata ext4      689G   69M  658G   1% /data
/dev/sdf1                 xfs        10G   33M   10G   1% /aaa
[root@staros ~]# parted /dev/sdf
GNU Parted 3.1
Using /dev/sdf
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Error: The backup GPT table is not at the end of the disk, as it should be.  This might mean that another operating system believes the
disk is smaller.  Fix, by moving the backup to the end (and removing the old backup)?
Fix/Ignore/Cancel? fix
Warning: Not all of the space available to /dev/sdf appears to be used, you can fix the GPT to use all of the space (an extra 20971520
blocks) or continue with the current setting?
Fix/Ignore? fix
Model: VMware Virtual disk (scsi)
Disk /dev/sdf: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
 
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  10.7GB  10.7GB  xfs          primary
 
(parted) rm 1
(parted) mkpart primary 1049kB 100%
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sdf: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
 
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  21.5GB  21.5GB  xfs          primary
 
(parted) quit
Information: You may need to update /etc/fstab.
[root@staros ~]# mount /dev/sdf1 /aaa
[root@staros ~]# xfs_growfs /aaa
meta-data=/dev/sdf1              isize=512    agcount=4, agsize=655232 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2620928, 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 2620928 to 5242368
[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   20M  1.9G   2% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata ext4      689G   69M  658G   1% /data
/dev/sdf1                 xfs        20G   33M   20G   1% /aaa
[root@staros ~]#


四、未分区扩容

/dev/sdb 有 10G,未分区直接挂载。将数据盘容量扩大至 20G,将新增的 10G 划分至已有的/dev/sdb 内

ext 为例



[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb                ext4      9.8G   37M  9.2G   1% /data
[root@staros ~]# echo 1 > /sys/block/sdb/device/rescan
[root@staros ~]# resize2fs /dev/sdb
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/sdb is mounted on /data; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/sdb is now 5242880 blocks long.
[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb                ext4       20G   44M   19G   1% /data


xfs 为例



[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb                xfs      9.8G   37M  9.2G   1% /data
[root@staros ~]# echo 1 > /sys/block/sdb/device/rescan
[root@staros ~]# xfs_growfs /dev/sdb
[root@staros ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                   tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        53G  3.2G   50G   6% /
/dev/sda1               xfs       797M  151M  647M  19% /boot
tmpfs                   tmpfs     378M     0  378M   0% /run/user/0
/dev/sdb                xfs        20G   44M   19G   1% /data



五、LVM

(1)通过 LVM 创建逻辑卷

三块大小为 100G 的磁盘,通过 LVM 创建逻辑卷并挂载目录

ext 为例



[root@staros ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   55G  0 disk
├─sda1            8:1    0  800M  0 part /boot
├─sda2            8:2    0 39.2G  0 part
│ ├─centos-root 253:0    0 52.2G  0 lvm  /
│ └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
├─sda3            8:3    0    5G  0 part
│ └─centos-root 253:0    0 52.2G  0 lvm  /
└─sda4            8:4    0   10G  0 part
  └─centos-root 253:0    0 52.2G  0 lvm  /
sdb               8:16   0  100G  0 disk
sdc               8:32   0  100G  0 disk
sdd               8:48   0  100G  0 disk
sr0              11:0    1 1024M  0 rom
[root@staros ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
[root@staros ~]# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.
[root@staros ~]# pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created.
[root@staros ~]# vgcreate vgdata /dev/sdb
  Volume group "vgdata" successfully created
[root@staros ~]# vgs
  VG      #PV #LV #SN Attr   VSize    VFree
  centos    3   2   0 wz--n-  <54.21g       0
  vgdata   1   0   0 wz--n- <100.00g <100.00g
[root@staros ~]# vgextend vgdata /dev/sdc
  Volume group "vgdata" successfully extended
[root@staros ~]# vgextend vgdata /dev/sdd
  Volume group "vgdata" successfully extended
[root@staros ~]# vgs
  VG      #PV #LV #SN Attr   VSize    VFree
  centos    3   2   0 wz--n-  <54.21g       0
  vgdata    3   0   0 wz--n-  <299.99g <299.99g
[root@staros ~]# lvcreate -l 100%FREE -n lvdata vgdata
  Logical volume "lvdata" created.
[root@staros ~]# lvs
  LV      VG      Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root    centos  -wi-ao----  <52.21g
  swap    centos  -wi-ao----    2.00g
  lvdata vgdata   -wi-a----- <299.99g
[root@staros ~]# mkfs -t ext4 /dev/vgdata/lvdata
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
19660800 inodes, 78640128 blocks
3932006 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2227175424
2400 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, 71663616
 
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 
[root@staros ~]# mount /dev/vgdata/lvdata /data
[root@staros ~]# df -Th
Filesystem                    Type      Size  Used Avail Use% Mounted on
devtmpfs                      devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                         tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                         tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                         tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root       xfs        53G  3.2G   50G   6% /
/dev/sda1                     xfs       797M  151M  647M  19% /boot
tmpfs                         tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata     ext4      296G   65M  281G   1% /data
[root@staros ~]# vim /etc/fstab  #末尾添加
/dev/mapper/vgdata-lvdata  /data  ext4  defaults        0  0

xfs 为例



[root@staros ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   55G  0 disk
├─sda1            8:1    0  800M  0 part /boot
├─sda2            8:2    0 39.2G  0 part
│ ├─centos-root 253:0    0 52.2G  0 lvm  /
│ └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
├─sda3            8:3    0    5G  0 part
│ └─centos-root 253:0    0 52.2G  0 lvm  /
└─sda4            8:4    0   10G  0 part
  └─centos-root 253:0    0 52.2G  0 lvm  /
sdb               8:16   0  100G  0 disk
sdc               8:32   0  100G  0 disk
sdd               8:48   0  100G  0 disk
sr0              11:0    1 1024M  0 rom
[root@staros ~]# pvcreate /dev/sdb
  Physical volume "/dev/sdb" successfully created.
[root@staros ~]# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.
[root@staros ~]# pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created.
[root@staros ~]# vgcreate vgdata /dev/sdb
  Volume group "vgdata" successfully created
[root@staros ~]# vgs
  VG      #PV #LV #SN Attr   VSize    VFree
  centos    3   2   0 wz--n-  <54.21g       0
  vgdata   1   0   0 wz--n- <100.00g <100.00g
[root@staros ~]# vgextend vgdata /dev/sdc
  Volume group "vgdata" successfully extended
[root@staros ~]# vgextend vgdata /dev/sdd
  Volume group "vgdata" successfully extended
[root@staros ~]# vgs
  VG      #PV #LV #SN Attr   VSize    VFree
  centos    3   2   0 wz--n-  <54.21g       0
  vgdata    3   0   0 wz--n-  <299.99g <299.99g
[root@staros ~]# lvcreate -l 100%FREE -n lvdata vgdata
  Logical volume "lvdata" created.
[root@staros ~]# lvs
  LV      VG      Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root    centos  -wi-ao----  <52.21g
  swap    centos  -wi-ao----    2.00g
  lvdata vgdata   -wi-a----- <299.99g
[root@staros ~]# mkfs -t xfs /dev/vgdata/lvdata
[root@staros ~]# mount /dev/vgdata/lvdata /data
[root@staros ~]# df -Th
Filesystem                    Type      Size  Used Avail Use% Mounted on
devtmpfs                      devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                         tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                         tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                         tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root       xfs        53G  3.2G   50G   6% /
/dev/sda1                     xfs       797M  151M  647M  19% /boot
tmpfs                         tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata     xfs       296G   65M  281G   1% /data
[root@staros ~]# vim /etc/fstab  #末尾添加
/dev/mapper/vgdata-lvdata  /data  xfs  defaults        0  0


(2)扩容逻辑卷

方式一:扩容磁盘以扩容逻辑卷容量

其中一块磁盘扩容到 200G 如何扩容逻辑卷

ext 为例



[root@staros ~]# pvresize /dev/sdb
  Physical volume "/dev/sdb" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@staros ~]# lvextend -l +100%FREE /dev/vgdata/lvdata
  Size of logical volume vgdata/lvdata changed from <299.99 GiB (76797 extents) to <399.99 GiB (102397 extents).
  Logical volume vgdata/lvdata successfully resized.
[root@staros ~]# resize2fs /dev/vgdata/lvdata
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vgdata/lvdata is mounted on /data; on-line resizing required
old_desc_blocks = 38, new_desc_blocks = 50
The filesystem on /dev/vgdata/lvdata is now 104854528 blocks long.
[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata ext4      394G   72M  375G   1% /data=


xfs 为例



[root@staros ~]# pvresize /dev/sdb
  Physical volume "/dev/sdb" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
[root@staros ~]# lvextend -l +100%FREE /dev/vgdata/lvdata
  Size of logical volume vgdata/lvdata changed from <299.99 GiB (76797 extents) to <399.99 GiB (102397 extents).
  Logical volume vgdata/lvdata successfully resized.
[root@staros ~]# xfs_growfs /dev/vgdata/lvdata
[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   12M  1.9G   1% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata xfs       394G   72M  375G   1% /data


方式二:新增磁盘以扩容逻辑卷容量

新增一块 200G 磁盘 sde 如何增加到逻辑卷

ext 为例



[root@staros ~]# pvcreate /dev/sde
  Physical volume "/dev/sde" successfully created.
[root@staros ~]# vgextend vgdata /dev/sde
  Volume group "vgdata" successfully extended
[root@staros ~]# lvextend -l +100%FREE /dev/mapper/vgdata-lvdata
  Size of logical volume vgdata/lvdata changed from <499.99 GiB (127997 extents) to 699.98 GiB (179196 extents).
  Logical volume vgdata/lvdata successfully resized.
[root@staros ~]# resize2fs /dev/mapper/vgdata-lvdata
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vgdata-lvdata is mounted on /data; on-line resizing required
old_desc_blocks = 63, new_desc_blocks = 88
The filesystem on /dev/mapper/vgdata-lvdata is now 183496704 blocks long.
[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   20M  1.9G   2% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata ext4      689G   69M  658G   1% /data

xfs 为例



[root@staros ~]# pvcreate /dev/sde
  Physical volume "/dev/sde" successfully created.
[root@staros ~]# vgextend vgdata /dev/sde
  Volume group "vgdata" successfully extended
[root@staros ~]# lvextend -l +100%FREE /dev/mapper/vgdata-lvdata
  Size of logical volume vgdata/lvdata changed from <499.99 GiB (127997 extents) to 699.98 GiB (179196 extents).
  Logical volume vgdata/lvdata successfully resized.
[root@staros ~]# xfs_growfs /dev/mapper/vgdata-lvdata
[root@staros ~]# df -Th
Filesystem                Type      Size  Used Avail Use% Mounted on
devtmpfs                  devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                     tmpfs     1.9G     0  1.9G   0% /dev/shm
tmpfs                     tmpfs     1.9G   20M  1.9G   2% /run
tmpfs                     tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root   xfs        53G  3.2G   50G   6% /
/dev/sda1                 xfs       797M  151M  647M  19% /boot
tmpfs                     tmpfs     378M     0  378M   0% /run/user/0
/dev/mapper/vgdata-lvdata xfs       689G   69M  658G   1% /data


© 版权声明

☆ END ☆
喜欢就点个赞吧
点赞0 分享
评论 抢沙发
  • 武穆逸仙

    昵称

  • 取消回复

    请填写用户信息:

图片正在生成中,请稍后...