当前位置首页 > Linux知识

Linux如何扩容物理文件系统分区

阅读次数:400 次  来源:admin  发布时间:

一. 能否扩容?

答:只可扩容disk的最后一个partitio

参考链接:https://access.redhat.com/solutions/29095

How can I extend an existing file-system partition without destroying data?
Solution Verified - Updated September 19 2019 at 8:07 PM - English
Environment

    Red Hat Enterprise Linux (RHEL) 4, 5, 6, 7

Issue

    Need to extend partition size and file system size without destroying any existing data
    How do you extend an existing non-lvm partition without destroy any data?
    I would like to extend a disk partition /dev/sdX1, is there any supported method to accomplish this?
    What are the steps for increasing non lvm disk size for root
    /var utilization is 100%, how to extend the size of partition /dev/sda4 which is mounted on /var?

Resolution

    Disclaimer: The following information has been provided by Red Hat, but is outside the scope of the posted Service Level Agreements and support procedures (Production Support - Red Hat Customer Portal). The information is provided as-is and any configuration settings or installed applications made from the information in this article could make the Operating System unsupported by Red Hat Global Support Services. The intent of this article is to provide information to accomplish the system\'s needs. Use of the information in this article at the user\'s own risk.

    From a technical point of view, resizing filesystems and their underlying partitions (making changes to the existing partition table) can be very dangerous. Also, it is only possible to resize partitions from their end position on the disk. It is not possible to move partitions on the disk or resize them from their beginning. Such operations can result in a corrupted partition table, which will render all data on the disk device inaccessible, leading to data loss.

There are a few points to consider before attempting to resize your volume:

    Storage resize commands are dangerous and can cause a complete loss of data. Backup all important information before attempting to follow the steps in this document.
    This document only covers resizing the last partition on your device. If this is not the last partition, you will need to backup your data, rebuild the disk and partitions, then restore the data.
    This document assumes you\'re using either a GPT partition table, or an msdos partition table using primary partition types.

The commands below use the follow variables that you need to adjust for your devices:

    /mnt is the active mountpoint
    /dev/vdc1 is the partition we are resizing
    /dev/vdc is the device that holds the partition

1. Check the size of your current volume using the df command
Raw

# df -hT /mnt
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vdc1      ext3  5.0G  139M  4.6G   3% /mnt

2. Take note of the existing partition table using parted. Be sure to specify the device, not the partition. Only the highest numbered device can be resized.
Raw

# parted /dev/vdc u s p
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 20971520s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start  End        Size       Type     File system  Flags
 1      63s    10485215s  10485153s  primary  ext3

We can see that the ending sector is 10485215, which is smaller than the full disk size of 20971520. This indicates that the partition has room to grow.

3. Unmount the existing file-system ( all partitions on the disk will need to be unmounted )
Raw

# umount /mnt

4. Delete the existing partition with parted by specifying the partition number listed in step 2.
Raw

# parted /dev/vdc rm 1

5. Recreate the partition with the larger size. Specify the same starting sector as the previous partition, and use 100% to fill the whole disk.
Raw

# parted -s /dev/vdc mkpart primary 63s 100%
Warning: The resulting partition is not properly aligned for best performance.

6. Verify the partition has extended by comparing the End sector to the previous output
Raw

# parted /dev/vdc u s p
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 20971520s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start  End        Size       Type     File system  Flags
 1      63s    20971519s  20971457s  primary  ext3

7a. Run a file-system check against the volume. This is required before a resize.
Raw

# e2fsck /dev/vdc1

7b. If the filesystem is XFS then run
Raw

# xfs_repair /dev/vdc1

8a. For ext2/3/4 file-systems, resize the file-system to the correct size using resize2fs
Raw

# resize2fs -f /dev/vdc1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vdc1 to 2621432 (4k) blocks.
The filesystem on /dev/vdc1 is now 2621432 blocks long.

8b. For XFS file-system, resize using xfs_growfs
Raw

# xfs_growfs /dev/vdc1

9. Mount the file-system
Raw

# mount /dev/vdc1 /mnt

10. Verify the file-system is now larger
Raw

# df -hT /mnt
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vdc1      ext3  9.9G  140M  9.3G   2% /mnt

Root Cause

    Making modifications to the partition table does not affect the underlying data under regular circumstances
    The partition table resides at the beginning of the disk and is a pointer for the OS to find the file-system superblock.

=============================================================================

Q:This method will only work to extend the last disk partition. Is there any way to extend a second last partition.

R: No. You would have to migrate your data first. I would recommend backing up the data, recreating the partitions as you need them, and then restoring the data back. Perhaps you could consider using LVM before the restore.

二. 常规扩容步骤

推荐使用LVM,可新加磁盘做成LVM,再用rsync迁移原有的物理文件系统分区数据到新建LV中,如将650G的物理磁盘扩容到700G,(第一个分区增加50G):

/haha/data  250G  ----->  300G

/hana/log  150G  ----->  150G

/hana/shared  250G  ----->  250G

直接物理扩容第一个分区是不可行的,这会导致损毁第一个分区之后的分区内的数据,因此建议新建一个目标大小的磁盘再做数据同步,为了方便以后的扩缩容,新磁盘建议使用LVM。

1.备份好数据

2.添加一个700G的新磁盘

3.重启识别新硬盘(也可通过以下方法在不重启机器的情况下识别新硬盘)

https://www.hostarr.com/centos-scanning-disk/

在服务器新加 SCSI 硬盘后,不重启系统情况下可能识别不了,需要用下面命令手动刷新一下。

echo "- - -" >  /sys/class/scsi_host/host0/scan
上面host0是 SCSI 接口,服务器通常有多个这样接口,用 ls /sys/class/scsi_host 命令查看。

[root@test ~]# ls /sys/class/scsi_host
host0  host1  host2  host3  host4  host5  host6  host7
那么如何查询使用的是哪个接口呢?运行 udevadm info -a -p /sys/class/scsi_generic/sg0 查看打印信息。其中下面这两段是我们要找的,可以看到笔者这台服务器 scsi_host 值是 host0。

  looking at parent device \'/devices/pci0000:00/0000:00:17.0/host0/target0:0:0\':
    KERNELS=="target0:0:0"
    SUBSYSTEMS=="scsi"
    DRIVERS==""

  looking at parent device \'/devices/pci0000:00/0000:00:17.0/host0\':
    KERNELS=="host0"
    SUBSYSTEMS=="scsi"
    DRIVERS==""
确认 scsi_host 值后,按实际情况修改上面刷新命令。若要确认命令运行情况,可 tail -20 /var/log/messages 查看最近 20 条系统日志,之后再 fdisk -l 查看硬盘是否已经被识别。

关于查询 scsi_host 值网上还流传有另一个命令(如下),但笔者测试无效。

grep mpt /sys/class/scsi_host/host?/proc_name

4.新磁盘配置LVM

pvcreate /dev/sdd
vgcreate vgdata0 /dev/sdd

lvcreate -L 300G -n lvdata vgdata0
lvcreate -L 150G -n lvlog vgdata0
lvcreate -L 250G -n lvshared vgdata0 (if can not create)
lvcreate -l XXX -n lvshared vgdata0

mkfs.xfs /dev/vgdata0/lvdata
mkfs.xfs /dev/vgdata0/lvlog
mkfs.xfs /dev/vgdata0/lvshared

mkdir /hana/data1
mkdir /hana/log1
mkdir /hana/shared1

mount /dev/vgdata0/lvdata /hana/data1
mount /dev/vgdata0/lvlog /hana/log1
mount /dev/vgdata0/lvshared /hana/shared1

5.同步旧磁盘数据到新磁盘rsync -avPH /hana/data/* /hana/data1 > outfile1 2>&1 &am

rsync -avPH /hana/log/* /hana/log1 > outfile2 2>&1 &
rsync -avPH /hana/shared/* /hana/shared1 > outfile3 2>&1 &

#对比新旧文件夹差异
#https://m.linuxidc.com/Linux/2016-02/128307.htm
rsync --dry-run -rcnC --out-format="%n" data1/ data/ | grep -v "/$"
rsync --dry-run -rcnC --out-format="%n" log1/ log/ | grep -v "/$"
rsync --dry-run -rcnC --out-format="%n" shared1/ shared/ | grep -v "/$"

umount /hana/data
umount /hana/log
umount /hana/shared

umount /hana/data1
umount /hana/log1
umount /hana/shared1

#根据需要设置目录权限
chmod 777 /hana/data
chmod 777 /hana/log
chmod 777 /hana/shared

#配置fstab
Comment out the mount information for the old disk in /etc/fstab and add the mount information for the new disk.

#New disk mount information
/dev/vgdata0/lvdata  /hana/data xfs defaults 1 2
/dev/vgdata0/lvlog  /hana/log xfs defaults 1 2
/dev/vgdata0/lvshared  /hana/shared xfs defaults 1 2

mount -a

verify the new directory whether can be used.

#删除临时目录/hana/{data1,log1,shared1}

Linux如何扩容物理文件系统分区

上一篇:SSM框架运行报错:org.mybatis.spring.MyBatisSystemException:nestedexcept
下一篇:DebianLinux下的Python学习——控制流