在Linux中扩展/缩小主分区大小的2种简单方法
如何在Linux中调整主分区的大小。
如何扩展非lvm根分区。
如何在Linux中使用parted和fdisk更改分区大小而又不破坏数据。
在RHEL/CentOS 7和8 Linux中扩展分区的步骤。
在CentOS中执行磁盘管理。
在Linux中,如何使用未分配的空间来更改磁盘中分区的大小。
如何在Linux中的LVM上调整根分区的大小。
分步指南以调整主分区的大小。
如何在Linux中使用示例扩展分区。
centos磁盘管理。
rhel 7扩展了非lvm根分区。
centos 7调整根分区的大小。
调整主分区大小以扩展Linux中非lvm根分区的步骤。
如何在Linux中将未分配的磁盘空间添加到分区。
centos缩小或者扩展非lvm根分区。
为分区添加空间。
fdisk扩展分区。
调整启动分区的大小。
在Linux中扩展非lvm根分区。
更改分区的大小。
如何扩展分区。
centos调整主分区的大小。
rhel更改分区大小。
早些时候,我分享了在Linux中创建文件系统和检查文件系统类型的步骤。
现在,在本文中,我将分享调整主分区大小的步骤,其中我们将扩展非lvm根分区。
使用LVM,在Linux中更改卷组中分区的大小要容易得多,风险也更低。
警告:
本文介绍了调整主分区(非lvm)大小的步骤,这很危险,可能会使Linux系统处于无法使用的损坏状态。
在尝试更改分区大小之前,请备份内容,这一点很重要。我们只能在具有未分配空间的存储设备上的最后一个分区上更改分区(非lvm)的大小。
如果相应的分区不是最后一个分区,那么扩展分区的唯一方法是备份数据,重建磁盘和分区,然后还原数据。
在这种情况下,没有任何工具(例如gparted,parted或者fdisk)可以更改分区的大小。
我们必须在相关设备中具有一些未分配的空间或者可用空间,才能扩展分区。
在某些虚拟环境中,我们可以选择更改存储设备的大小,但是在物理节点上,如果没有足够的未分配空间,则无法调整主分区(扩展非lvm根分区)的大小本文假定我们正在使用GPT分区表或者使用主分区类型的msdos分区表来扩展非lvm根分区。
说明:
我将仅演示扩展非lvm根分区的步骤,但是使用相同的方法,我们还可以缩小分区并将Linux的大小更改为较低的值。
但是请务必备份数据。
在Linux中调整主分区(RHEL/CentOS 7/8)大小的实验室环境
我已经在Linux服务器上安装的Oracle VirtualBox上运行的虚拟机上执行了大小调整主分区操作。
我的VM正在CentOS 8上运行,但我也已在RHEL/CentOS 7和RHEL 8 Linux上验证了这些步骤。
其中我的VM安装在/dev/sda
设备上,其中/dev/sda1
是引导分区,/dev/sda2
是根目录,而/dev/sda3
用于交换。
/dev/sda中的可用空间用于本文的演示,以扩展非lvm根分区。
方法1:使用部分CLI实用程序更改分区的大小
我们可以使用gparted(GUI实用程序)或者parted(CLI实用程序)来更改Linux中分区的大小。
其中我们计划调整主分区的大小,在本例中为/dev/sda。
列出可用的分区
要列出/dev/sda
中的可用分区,我们将执行以下命令
[root@centos-8 ~]# parted -l /dev/sda Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 16.1GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags <--- This is my partition layout 1 1049kB 538MB 537MB primary ext4 boot 2 538MB 11.3GB 10.7GB primary ext4 3 11.3GB 12.3GB 1074MB primary linux-swap(v1)
如我在免责声明部分所述,我们只能在设备的最后一个分区上更改分区的大小,但是其中根分区(/dev/sda2
)不是最后一个分区,而交换分区(/dev/sda3
)是我的最后一个分区。
因此,要扩展根分区,我必须删除交换设备,以便能够使用未分配的空间并扩展非lvm根分区。
删除交换后,根分区将成为/dev/sda
上的最后一个分区,之后我们可以调整主分区的大小。
禁用交换分区
目前我有大约1GB的空间用于交换分区
[root@centos-8 ~]# free -m total used free shared buff/cache available Mem: 3781 164 3245 8 371 3389 Swap: 1023 0 1023
我将关闭交换(禁用交换分区)并使用此空间在Linux中扩展非lvm根分区
[root@centos-8 ~]# swapoff -a
验证交换分区空间,因为我们看不到它为0
[root@centos-8 ~]# free -m total used free shared buff/cache available Mem: 3781 164 3241 8 375 3389 Swap: 0 0 0
更新/etc/fstab
以确保在启动阶段未安装交换分区。
[root@centos-8 ~]# cat /etc/fstab UUID=b3ba77fc-86a7-40dc-a93e-402d8e3987ab/ ext4 defaults 1 1 UUID=621d26e4-4bcc-441d-b336-a9bae280343d /boot ext4 defaults 1 2 #UUID=3816d191-31fd-4e20-874d-b18360133aca swap swap defaults 0 0
删除交换并扩展分区
现在要调整主分区/dev/sda2
的大小并将分区扩展到新值,我们必须首先使用parted实用程序删除交换分区。
因为我们需要根分区成为/dev/sda的最后一个分区,然后再扩展分区。
[root@centos-8 ~]# parted /dev/sda GNU Parted 3.2 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 16.1GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 538MB 537MB primary ext4 boot 2 538MB 11.3GB 10.7GB primary ext4 3 11.3GB 12.3GB 1074MB primary linux-swap(v1) (parted) rm 3 <-- First we need to delete swap partition (parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 16.1GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 538MB 537MB primary ext4 boot 2 538MB 11.3GB 10.7GB primary ext4 (parted) u <-- Change the unit type to sector to avoid any risk of loosing data when you resize root partition Unit? [compact]? s (parted) p <-- Print the available partition table Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 31457280s Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 2048s 1050623s 1048576s primary ext4 boot 2 1050624s 22022143s 20971520s primary ext4 <--- Note down the start sector number, this will be used in next step (parted) rm 2 <-- We will delete the root partition's entry. This will not impact the content of root partition and only partition table is modified. Warning: Partition /dev/sda2 is being used. Are you sure you want to continue? Yes/No? Yes Error: Partition(s) 2 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes. Ignore/Cancel? Ignore <-- If you reboot your server at this stage then you Jan end up with a broken node so don't reboot your node at this stage. (parted) p <-- Print the current partition table Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 31457280s Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 2048s 1050623s 1048576s primary ext4 boot (parted) mkpart <-- Now we will create root partition with new size Partition type? primary/extended? primary File system type? [ext2]? ext4 Start? 1050624s <-- Here give the start sector as it was earlier for root partition End? 24022143s <-- Give the new end sector higher than the earlier value to resize root partition (parted) p <-- Print the new partition table after you extend non lvm root partition Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 31457280s Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 2048s 1050623s 1048576s primary ext4 boot 2 1050624s 24022143s 22971520s primary ext4 lba (parted) quit <-- We are all done here Information: You Jan need to update /etc/fstab.
但是我们的根分区显示的大小与之前的大小相同,即约10GB
[root@centos-8 ~]# df -h /dev/sda2 Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.8G 1.2G 8.1G 13% /
要完成调整非lvm根分区大小的步骤,执行resizefs
扩展分区并刷新更改
[root@centos-8 ~]# resize2fs /dev/sda2 resize2fs 1.44.3 (10-July-2016) Filesystem at /dev/sda2 is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 2 The filesystem on /dev/sda2 is now 2871440 (4k) blocks long.
重新验证根分区的新大小,以确保我们扩展非lvm根分区的步骤成功完成。
[root@centos-8 ~]# df -h /dev/sda2 Filesystem Size Used Avail Use% Mounted on /dev/sda2 11G 1.3G 9.0G 12% /
重新创建交换分区
现在,我们必须创建在本文前面已删除的Swap分区。
我们将再次使用parted实用程序来创建交换分区
[root@centos-8 ~]# parted /dev/sda GNU Parted 3.2 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mkpart Partition type? primary/extended? primary File system type? [ext2]? linux-swap <-- To create swap partition Start? Start? 24022144s End? 98% Warning: The resulting partition is not properly aligned for best performance: 24022144s % 2048s != 0s Ignore/Cancel? Ignore (parted) p Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 16.1GB Sector size (logical/physical): 512B/512B Partition Table: msdos Disk Flags: Number Start End Size Type File system Flags 1 1049kB 538MB 537MB primary ext4 boot 2 538MB 12.3GB 11.8GB primary ext4 3 12.3GB 12.8GB 512MB primary linux-swap(v1) lba <-- Now we have a swap partition (parted) quit Information: You Jan need to update /etc/fstab.
现在分开了,我们只创建了一个文件系统类型为swap的分区。
使用mkswap
将这个分区变成交换分区。
[root@centos-8 ~]# mkswap /dev/sda3 Setting up swapspace version 1, size = 488.3 MiB (511995904 bytes) no label, UUID=decb7df5-8d7a-41b4-bfd7-bee076006ec9
接下来,我们必须使用新的交换分区的UUID更新/etc/fstab
。
获取交换分区的新UUID
[root@centos-8 ~]# blkid /dev/sda3 /dev/sda3: UUID="decb7df5-8d7a-41b4-bfd7-bee076006ec9" TYPE="swap" PARTUUID="5290bf38-03"
如下所示在/etc/fstab
中更新此UUID
[root@centos-8 ~]# cat /etc/fstab UUID=b3ba77fc-86a7-40dc-a93e-402d8e3987ab/ ext4 defaults 1 1 UUID=621d26e4-4bcc-441d-b336-a9bae280343d /boot ext4 defaults 1 2 UUID=3816d191-31fd-4e20-874d-b18360133aca swap swap defaults 0 0
最后打开新的交换分区
[root@centos-8 ~]# swapon /dev/sda3
验证新的交换分区
[root@centos-8 ~]# free -m total used free shared buff/cache available Mem: 3781 155 3390 8 235 3407 Swap: 488 0 488
现在,我们可以重新启动Linux服务器,以确保一切正常,并且成功调整了主分区的大小。
方法2:使用fdisk实用程序更改分区的大小
与parted
命令类似,我们也可以使用fdisk实用程序来调整主分区的大小并扩展非lvm根分区。
列出可用的分区
在调整主分区的大小之前,让我们列出可用分区
[root@centos-8 ~]# fdisk -l /dev/sda Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 Disklabel type: dos Disk identifier: 0x5290bf38 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1050623 1048576 512M 83 Linux /dev/sda2 1050624 22022143 20971520 10G 83 Linux <-- This is our root partition /dev/sda3 22022144 24119295 2097152 1G 82 Linux swap/Solaris
现在,如我们所见,我现有的根分区/dev/sda2
的大小为~10G
。
其中我们将使用/dev/sda
中未分配的磁盘空间将分区扩展为+ 1GB。
[root@centos-8 ~]# df -h /dev/sda2 Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.8G 1.3G 8.1G 14% /
删除交换分区
目前,我的交换分区已启用,并且是/dev/sda
的最后一个分区。
因此,我们必须先删除交换分区,然后再更改分区大小,因为我们需要root成为/dev/sda
中的最后一个分区。
[root@centos-8 ~]# free -m total used free shared buff/cache available Mem: 3781 165 3243 8 372 3388 Swap: 1023 0 1023
因此,我们将在禁用交换分区之前将其关闭
[root@centos-8 ~]# swapoff -a
现在是时候使用fdisk
实用程序来调整分区大小了
[root@centos-8 ~]# fdisk /dev/sda 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): p <-- Print the partition table Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 Disklabel type: dos Disk identifier: 0x5290bf38 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1050623 1048576 512M 83 Linux /dev/sda2 1050624 22022143 20971520 10G 83 Linux /dev/sda3 22022144 24119295 2097152 1G 82 Linux swap/Solaris Command (m for help): d <-- To delete a partition ude 'd' Partition number (1-3, default 3): 3 <-- Provide the partition number which you wish to delete. Here our swap partition number is 3 Partition 3 has been deleted. Command (m for help): p <-- Next we will print the new partition table again Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 Disklabel type: dos Disk identifier: 0x5290bf38 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1050623 1048576 512M 83 Linux /dev/sda2 1050624 22022143 20971520 10G 83 Linux
第1部分-调整根分区的大小
我们将继续执行在同一fdisk
会话中扩展非lvm根分区的步骤。
在扩展分区之前,请注意根分区的起始扇区。
Command (m for help): d <-- Now we need to delete root partition before we extend non lvm root partition. This will not impact the content stored in root partition and will only clear the partition table. Partition number (1,2, default 2): 2 <-- provide the root's partition number Partition 2 has been deleted. Command (m for help): p <-- Print the partition table with the new changes Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 Disklabel type: dos Disk identifier: 0x5290bf38 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1050623 1048576 512M 83 Linux <-- Now we only have a boot partition Command (m for help): n <-- Next we will create and expand partition for root Partition type p primary (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): p <-- This will be a primary partition Partition number (2-4, default 2): 2 <-- For root partition we know the partition number was 2 so we will use the same as it is important to use the same start sector First sector (1050624-31457279, default 1050624): 1050624 <-- Use the same first sector as it was earlier to resize root partition Last sector, +sectors or +size{K,M,G,T,P} (1050624-31457279, default 31457279): +11G <-- Here you can define the new size to resize root partition and expand partition Created a new partition 2 of type 'Linux' and of size 11 GiB. Partition #2 contains a ext4 signature. Do you want to remove the signature? [Y]es/[N]o: N <-- Since we want to retain ext4 signature on our root partition we will use "N" Command (m for help): p <-- Now print the new partition table once resize root partition is successful Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 Disklabel type: dos Disk identifier: 0x5290bf38 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1050623 1048576 512M 83 Linux /dev/sda2 1050624 24119295 23068672 11G 83 Linux <-- We have our new root partition with 11G as new size
创建交换分区
调整主分区的大小之后,在同一fdisk
会话中,我们还将创建一个新的交换分区,该分区最初是使用新的开始和结束扇区删除的
Command (m for help): n <-- Now we will also create a new swap partition Partition type p primary (2 primary, 0 extended, 2 free) e extended (container for logical partitions) Select (default p): p <-- This also will be a primary partition Partition number (3,4, default 3): 3 <-- We can give any partition number here but we will use 3 as earlier First sector (24119296-31457279, default 24119296): <-- This can be the same as default option Last sector, +sectors or +size{K,M,G,T,P} (24119296-31457279, default 31457279): +512M <--- My new swap partition size will be 512MB Created a new partition 3 of type 'Linux' and of size 512 MiB. Command (m for help): p <-- Print the new partition table Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 Disklabel type: dos Disk identifier: 0x5290bf38 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1050623 1048576 512M 83 Linux /dev/sda2 1050624 24119295 23068672 11G 83 Linux /dev/sda3 24119296 25167871 1048576 512M 83 Linux <-- Now resize root partition was successful and we have all the partitions with us Command (m for help): t <-- We must also change the filesystem type of our swap partition Partition number (1-3, default 3): 3 <-- This is our swap partition Hex code (type L to list all codes): 82 <-- 82 is the code for Linux swap filesystem type Changed type of partition 'Linux' to 'Linux swap/Solaris'. Command (m for help): p <-- Print the partition table to verify the new changes Disk /dev/sda: 15 GiB, 16106127360 bytes, 31457280 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 Disklabel type: dos Disk identifier: 0x5290bf38 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 1050623 1048576 512M 83 Linux /dev/sda2 1050624 24119295 23068672 11G 83 Linux /dev/sda3 24119296 25167871 1048576 512M 82 Linux swap/Solaris Command (m for help): w <-- Save the new partition table with the changes to expand partition The partition table has been altered. Syncing disks.
更新内核,以了解我们最近所做的更改分区大小的更改
[root@centos-8 ~]# partprobe /dev/sda
但是我们的交换分区仍然显示为0
[root@centos-8 ~]# free -m total used free shared buff/cache available Mem: 3781 164 3238 8 378 3389 Swap: 0 0 0
现在,让我们完成使用mkswap
创建交换分区的步骤。
[root@centos-8 ~]# mkswap /dev/sda3 Setting up swapspace version 1, size = 512 MiB (536866816 bytes) no label, UUID=f34150e6-6d37-448c-b81f-69ecdcb13ed5
接下来使用新交换分区的UUID更新/etc/fstab
。
我们可以使用blkid
来获取UUID
[root@centos-8 ~]# blkid /dev/sda3 /dev/sda3: UUID="f34150e6-6d37-448c-b81f-69ecdcb13ed5" TYPE="swap" PARTUUID="5290bf38-03"
如下所示在/etc/fstab
中进行更新
UUID=f34150e6-6d37-448c-b81f-69ecdcb13ed5 swap swap defaults 0 0
现在我们可以打开交换分区
[root@centos-8 ~]# swapon -a
使用free
命令验证相同
[root@centos-8 ~]# free -m total used free shared buff/cache available Mem: 3781 164 3237 8 378 3388 Swap: 511 0 511
第2部分-调整根分区的大小
如果上述步骤尚未完成,则退出fdisk实用程序后,请根据我们最近所做的更改内核的更新来更改分区大小
[root@centos-8 ~]# partprobe /dev/sda
调整主分区的大小还没有完成,因为df
命令仍然显示根分区的旧分区大小
[root@centos-8 ~]# df -h /dev/sda2 Filesystem Size Used Avail Use% Mounted on /dev/sda2 9.8G 1.3G 8.1G 14% /
执行resize2fs
扩展/dev/sda2
上的分区,并进行新的更改
[root@centos-8 ~]# resize2fs /dev/sda2 resize2fs 1.44.3 (10-July-2016) Filesystem at /dev/sda2 is mounted on /; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 2 The filesystem on /dev/sda2 is now 2883584 (4k) blocks long.
现在我们可以验证根分区的新大小
[root@centos-8 ~]# df -h /dev/sda2 Filesystem Size Used Avail Use% Mounted on /dev/sda2 11G 1.3G 9.0G 12% /