0
点赞
收藏
分享

微信扫一扫

PVE内Debian系统调整磁盘大小后扩容空间

北溟有渔夫 2024-03-18 阅读 10

操作前请务必打快照和备份处理!!!!!!

操作前请务必打快照和备份处理!!!!!!

操作前请务必打快照和备份处理!!!!!!

操作前请务必打快照和备份处理!!!!!!

操作前请务必打快照和备份处理!!!!!!

操作前请务必打快照和备份处理!!!!!!

操作前请务必打快照和备份处理!!!!!!

背景

在PVE创建系统是硬盘设置空间过少,如何在增加空间后对 debain11 已分区的磁盘进行分区扩容?

PVE内Debian系统调整磁盘大小后扩容空间_PVE

系统介绍

查看debain版本

PVE内Debian系统调整磁盘大小后扩容空间_Debian_02

查看磁盘信息

通过 df -h 输出当前磁盘分区大小 。

root@bogon:~$ sudo df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.9G     0  7.9G   0% /dev
tmpfs           1.6G  616K  1.6G   1% /run
/dev/sda1       40G    10G   30G  25% /
tmpfs           7.9G     0  7.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.6G     0  1.6G   0% /run/user/1000

通过 fdisk -l 输出所有的磁盘和磁盘分区的详细信息

root@bogon:~# sudo fdisk -l
Disk /dev/sda: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual disk    
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: 0x500f2a57

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 81885183 81883136   39G 83 Linux
/dev/sda2       81887230 83884031  1996802  975M  5 Extended
/dev/sda5       81887232 83884031  1996800  975M 82 Linux swap / Solaris

修改交换分区

删除交换分区

运行 fdisk 指定磁盘 /dev/sda fdisk /dev/sda

root@bogon:~# sudo fdisk /dev/sda

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

注意: fdisk需要小心使,要知道,在保存更改之前,您始终可以退出而不会产生任何更改。

我们输入p查看分区列表:

Command (m for help): p
Disk /dev/sda: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual disk    
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: 0x500f2a57

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 81885183 81883136   39G 83 Linux
/dev/sda2       81887230 83884031  1996802  975M  5 Extended
/dev/sda5       81887232 83884031  1996800  975M 82 Linux swap / Solaris

这里我们对磁盘上的扇区总数(2147483648)和要交换的扇区数(1996800)感兴趣。请注意分区/dev/sda2(它是自动创建的)没有里面文件我们将删除它。

删除交换分区5和逻辑分区2(命令d):

Command (m for help): d
Partition number (1,2,5, default 5): 5

Partition 5 has been deleted.

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

然后执行p查看结果,此时只剩下一个分区。

Command (m for help): p
Disk /dev/sda: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual disk    
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: 0x500f2a57

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 81885183 81883136  39G 83 Linux

新建交换分区

现在,让我们在磁盘末尾重新创建交换分区。使用n创建新分区,如果你的内存够大可以考虑不启用交换分区,可以跳过此步骤:

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Using default response p.

您将被要求为创建的 /dev/sda2 选择分区类型。选择逻辑 e:

指定分区2的序列号:

Partition number (2-4, default 2): 2

接下来,您需要指示第一个扇区。如果我们想保持交换大小不变,那么从磁盘/dev/sda的最后一个扇区(2147483648)减去分区/dev/sda2的扇区大小(1996800)为默认值,得到 2145486848:

First sector (81885184-2147483647, default 81885184): 2145486848

指定磁盘的最后一个扇区一般默认就行:

Last sector, +/-sectors or +/-size{K,M,G,T,P} (2145486848-2147483647, default 2147483647): 2147483647

Created a new partition 2 of type 'Linux' and of size 975 MiB.

现在我们通过按 t 来更改其类型,指定分区 2 和代码 82 进行转换分区类型:

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

转换分区类型后看到的结果:

Command (m for help): p
Disk /dev/sda: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual disk    
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: 0x500f2a57

Device     Boot      Start        End  Sectors  Size Id Type
/dev/sda1  *          2048   81885183 81883136   39G 83 Linux
/dev/sda2       2145486848 2147483647  1996800  975M 82 Linux swap / Solaris

保存所做的更改:

Command (m for help): w
The partition table has been altered.
Syncing disks.

在这之后,我们可以创建一个交换(指定您的新交换分区),通过如下命令获取盘符的UUID。

root@bogon:~# sudo mkswap /dev/sda2
Setting up swapspace version 1, size = 975 MiB (1022357504 bytes)
no label, UUID=28131458-4fd7-4a59-b7c9-4d5dc01707cc

复制上一步获取的UUID,更新 /etc/fstab 文件为交换分区设置新的UUID:

sudo vi /etc/fstab

PVE内Debian系统调整磁盘大小后扩容空间_PVE_03

启动交换分区

sudo swapon /dev/sda2

扩展分区

进入 fdisk /dev/sda 输入 p 查看磁盘分配

root@bogon:~# sudo fdisk /dev/sda

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

ommand (m for help): p
Disk /dev/sda: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual disk    
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: 0x500f2a57

Device     Boot      Start        End  Sectors  Size Id Type
/dev/sda1  *          2048   81885183 81883136   39G 83 Linux
/dev/sda2       2145486848 2147483647  1996800  975M 82 Linux swap / Solaris

您可以有其他分区。主要的是在我们想要扩展的分区之后有空闲扇区。为了做到这一点,在上一节中,我们将交换分区移动到磁盘的末尾。我们得到sda1在扇区81885183处结束,下一个分区sda2在52145486848处开始,它们之间有空闲扇区。如果磁盘上有一个分区或者是最后一个分区,那么您需要查看分区(81883136)和磁盘(2145486848)的最后一个扇区。

要扩展分区,必须首先删除有关它的信息。为此,输入d并指定分区(1 /dev/sda1):

Command (m for help): d
Partition number (1-2, default 2): 1

Partition 1 has been deleted.

创建新分区

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1,3,4, default 1): 1
First sector (2048-2145486847, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2145486847, default 2145486847): 

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

系统会询问是否删除当前文件选择 N 不删除,然后输入 w 保存更改。

# 此处不要选 y 
Do you want to remove the signature? [Y]es/[N]o: n

Command (m for help): w

The partition table has been altered.
Syncing disks.

重启系统,此处也可以不操作,有条件建议重启一下。

sudo reboot

调整 ext4 文件系统的大小 sudo resize2fs /dev/sda1

root@bogon:~$ sudo resize2fs /dev/sda1
-bash: resize2fs: command not found
root@bogon:~$ sudo resize2fs /dev/sda1
resize2fs 1.46.2 (28-Feb-2021)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 128
The filesystem on /dev/sda1 is now 268185600 (4k) blocks long.

通过 df -h 可以验证磁盘已经扩展成功

root@bogon:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            7.9G     0  7.9G   0% /dev
tmpfs           1.6G  616K  1.6G   1% /run
/dev/sda1      1007G  4.2G  962G   1% /
tmpfs           7.9G     0  7.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           1.6G     0  1.6G   0% /run/user/1000

举报

相关推荐

0 条评论