在RHEL 7 / CentOS 7中增加/扩展XFS文件系统

本指南将说明如何在不重新启动的情况下在VMWare虚拟机上扩展XFS文件系统。

验证您的服务器是否具有XFS文件系统(您应该在Type列中看到“xfs”):

# df -Th

Filesystem Type Size Used Avail Use% Mounted on

/dev/mapper/cl-root xfs 143G 27G 117G 19% /

devtmpfs devtmpfs 908M 0 908M 0% /dev

tmpfs tmpfs 918M 4.0K 918M 1% /dev/shm

tmpfs tmpfs 918M 90M 828M 10% /run

tmpfs tmpfs 918M 0 918M 0% /sys/fs/cgroup

/dev/sda1 xfs 1014M 184M 831M 19% /boot

/dev/mapper/cl-home xfs 8.0G 33M 8.0G 1% /home

tmpfs tmpfs 184M 0 184M 0% /run/user/0

要增加虚拟机的磁盘大小,您需要执行两个主要步骤:

  1. 首先,您需要在vSphere Client中或通过CLI增加磁盘大小。这将增加虚拟机可以看到的“硬件”磁盘。

  2. 然后,您需要通过分区来利用该额外空间。

步骤1.增加VMWare ESXi主机中的硬件磁盘大小

检查是否可以扩展当前磁盘或是否需要添加新磁盘

这是非常重要的一步,因为已经分区在4个主分区中的磁盘已经无法再扩展。要检查这一点,请登录您的服务器并fdisk -l在命令行运行。

# fdisk -l

Disk /dev/sda: 137.4 GB, 137438953472 bytes, 268435456 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: 0x000aa739

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

/dev/sda2 2099200 268435455 133168128 8e Linux LVM

如果它看起来像只有2个分区,则可以安全地扩展虚拟机中的当前硬盘。

但是,如果它看起来像这样:

# fdisk -l

Disk /dev/sda: 187.9 GB, 187904819200 bytes

255 heads, 63 sectors/track, 22844 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

 

Device Boot Start End Blocks Id System

/dev/sda1 * 1 25 200781 83 Linux

/dev/sda2 26 2636 20972857+ 8e Linux LVM

/dev/sda3 2637 19581 136110712+ 8e Linux LVM

/dev/sda4 19582 22844 26210047+ 8e Linux LVM

它将向您显示系统上已有4个主分区,您需要向虚拟机添加新的虚拟磁盘。您仍然可以使用额外的虚拟磁盘来增加LVM大小,所以不用担心。

将磁盘空间添加到虚拟机

使用VMWare vSphere Client,打开虚拟机的属性并增加预配置大小。

如果“预配置大小”区域(右上角)显示为灰色,请考虑先关闭VM(如果它不允许“热添加”磁盘/大小),并检查是否有任何由该VM构成的快照。只要有可用的快照,就无法增加磁盘大小。

或者,如果您已有4个主分区,则还可以选择“添加...”以将新硬件“虚拟磁盘”添加到VM,并具有所需的额外空间。

步骤2.在虚拟机中扩展分区

对未分配空间进行分区:如果增加了磁盘大小

在VMware中更改磁盘大小后,如果必须将其关闭以增加vSphere中的磁盘大小,请再次启动VM。如果您重新启动了服务器,则无需像启动时那样重新扫描SCSI设备。如果未重新启动服务器,请重新扫描SCSI设备。

首先,检查scsi设备的名称。

# ls /sys/class/scsi_device/

1:0:0:0 2:0:0:0

然后重新扫描scsi总线。在这台机器上,我们有两个设备。执行以下命令重新扫描它们。下面你可以用上一个命令找到的实际scsi总线名替换'1:0:0:0'。每个冒号前面都有一个斜线,这使它看起来很奇怪。

# echo 1 > /sys/class/scsi_device/1\:0\:0\:0/device/rescan

# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan

这将重新扫描当前的scsi总线,并且将显示已更改的磁盘大小。

执行fdisk -l以检查虚拟机是否可见新大小:

# fdisk -l

Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 sectors

对未分配空间进行分区:如果已添加新磁盘

如果您在服务器上添加了新磁盘,则操作类似于上述操作。但是,不必像之前的show那样重新扫描现有的scsi总线,而是必须重新扫描主机,以便在添加新磁盘时检测新的scsi总线。

# ls /sys/class/scsi_host/

total 0

drwxr-xr-x 3 root root 0 Feb 13 02:55 .

drwxr-xr-x 39 root root 0 Feb 13 02:57 ..

drwxr-xr-x 2 root root 0 Feb 13 02:57 host0

您的主机设备被调用host0,重新扫描它:

# echo "- - -" > /sys/class/scsi_host/host0/scan

它不会显示任何输出,但运行fdisk -l将显示新磁盘。

创建新分区

重新扫描完成后(应该只需几秒钟),您可以检查是否可以在磁盘上看到额外的空间。

# fdisk -l

Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 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: 0x000aa739

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

/dev/sda2 2099200 268435455 133168128 8e Linux LVM

使用fdisk,在/dev/sda设备上创建一个新分区。输入n,以创建新分区:

# fdisk /dev/sda

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): n

现在选择p创建一个新的主分区。请注意,您的系统在此磁盘上只能有4个主分区!如果您已达到此限制,请创建扩展分区。

Partition type:

p primary (2 primary, 0 extended, 2 free)

e extended

Select (default p): p

选择您的分区号码。既然我们已经有了/dev/sda1和/dev/sda2,逻辑数量会3。

Partition number (3,4, default 3): 3

选择新分区的第一个和最后一个扇区,如果按Enter键,则默认情况下新分区将使用所有可用磁盘空间。

First sector (268435456-335544319, default 268435456): <ENTER>

Using default value 268435456

Last sector, +sectors or +size{K,M,G} (268435456-335544319, default 335544319): <ENTER>

Using default value 335544319

Partition 3 of type Linux and of size 32 GiB is set

现在键入t以更改分区类型。出现提示时,输入您刚刚在前面的步骤中创建的分区的编号。当您被要求输入“十六进制代码”时,请输入8e,然后按Enter键确认。

Command (m for help): t

Partition number (1-3, default 3): 3

Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'

在fdisk中返回主命令后,键入w将分区写入磁盘。您将收到有关仍在使用旧分区表的内核的消息,并重新启动以使用新表。不需要重新启动,因为您还可以使用重新扫描这些分区partprobe。

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.

运行以下命令以扫描新创建的分区:

# partprobe -s

/dev/sda: msdos partitions 1 2 3

如果这对您不起作用,您可以尝试使用“partx”重新扫描设备并添加新分区。在下面的命令中,将/ dev / sda更改为刚刚添加新分区的磁盘。

# partx -v -a /dev/sda

如果仍然没有显示您要使用的新创建的分区,则必须重新启动服务器。然后,您可以使用fdisk查看新创建的分区。

# fdisk -l

Disk /dev/sda: 171.8 GB, 171798691840 bytes, 335544320 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: 0x000aa739

 

Device Boot Start End Blocks Id System

/dev/sda1 * 2048 2099199 1048576 83 Linux

/dev/sda2 2099200 268435455 133168128 8e Linux LVM

/dev/sda3 268435456 335544319 33554432 8e Linux LVM

使用新分区扩展逻辑卷

现在,创建物理卷作为LVM的基础。请将/ dev / sda3替换为新创建的分区。

# pvcreate /dev/sda3

Physical volume "/dev/sda3" successfully created

# pvdisplay

现在了解您的卷组的调用方式。在我们的示例中,它具有名称cl

# vgdisplay

--- Volume group ---

VG Name cl

...

让我们通过向其添加新创建的物理卷来扩展该卷组。

# vgextend cl /dev/sda3

Volume group "cl" successfully extended

有了pvscan,我们可以看到我们新增的物理卷和可用空间(在这种情况下为32GB)。

# pvscan

PV /dev/sda2 VG cl lvm2 [127.00 GiB / 4.00 MiB free]

PV /dev/sda3 VG cl lvm2 [32.00 GiB / 32.00 GiB free]

Total: 2 [158.99 GiB] / in use: 2 [158.99 GiB] / in no VG: 0 [0 ]

现在我们可以扩展逻辑卷(而不是我们之前添加到组中的物理卷)。

首先,检查system using命令上可用的逻辑卷ls /dev/VolGroupName(在我们的示例卷组名称中cl):

# ls /dev/cl

home root swap

我们有home,root和swap逻辑卷。要扩展逻辑卷root,请执行以下命令:

# lvextend /dev/cl/root /dev/sda3

Size of logical volume cl/root changed from 111.00 GiB (28415 extents) to 142.99 GiB (36606 extents).

Logical volume cl/root successfully resized.

# lvdisplay

现在剩下的就是将文件系统的大小调整为卷组,因此我们可以使用该空间。执行xfs_growfs如下所示的命令(替换cl-root为系统上的卷组名称)。

# xfs_growfs /dev/mapper/cl-root

meta-data=/dev/mapper/cl-root isize=512 agcount=4, agsize=7274240 blks

= sectsz=512 attr=2, projid32bit=1

= crc=1 finobt=0 spinodes=0

data = bsize=4096 blocks=29096960, imaxpct=25

= sunit=0 swidth=0 blks

naming =version 2 bsize=4096 ascii-ci=0 ftype=1

log =internal bsize=4096 blocks=14207, version=2

= sectsz=512 sunit=0 blks, lazy-count=1

realtime =none extsz=4096 blocks=0, rtextents=0

data blocks changed from 29096960 to 37484544

 

针对RHEL6 扩展ext4 

现在剩下的就是它将文件系统的大小调整为卷组,因此我们可以使用该空间。使用系统上的卷组名称替换正确/ dev设备的路径。

# resize2fs /dev/vg_miarec/lv_root

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/vg_miarec/lv_root is mounted on /; on-line resizing required

old desc_blocks = 4, new_desc_blocks = 6

Performing an on-line resize of /dev/vg_miarec/lv_root to 21493760 (4k) blocks.

The filesystem on /dev/vg_miarec/lv_root is now 21493760 blocks long.

 

执行df -h以确认虚拟机可以使用新磁盘大小。

# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/cl-root 143G 27G 117G 19% /

devtmpfs 908M 0 908M 0% /dev

tmpfs 918M 4.0K 918M 1% /dev/shm

tmpfs 918M 89M 830M 10% /run

tmpfs 918M 0 918M 0% /sys/fs/cgroup

/dev/sda1 1014M 184M 831M 19% /boot

/dev/mapper/cl-home 8.0G 33M 8.0G 1% /home

tmpfs 184M 0 184M 0% /run/user/0

转载于https://www.linuxtechi.com/create-extend-xfs-filesystem-on-lvm/

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值