如何在运行时调整 ext 根分区的大小?

Bon*_*ngo 315 partitioning ext4

如何在运行时增加系统根分区的大小?

我有一个在根分区(也是 ext4)之后未分配的分区,如何将未分配的空间添加到分配给根分区的空间而不必关闭服务器?

Sør*_*org 279

GUI(Ubuntu 14.04 及更高版本):GParted v0.17 及更高版本为此提供了一个很好的 GUI。(旧版本将拒绝调整已安装分区的大小)。

命令行(任何 Ubuntu 版本): 这需要三个步骤。

步骤 1. 必须首先调整分区大小。如果您使用 LVM,这很容易,而且您大概知道如何继续。如果您使用的是经典分区,则有点复杂,并且可能需要重新启动(尽管您永远不必启动另一个系统或 Live CD)。

我是这样做的:fdisk先删除分区(这个想法是磁盘上的数据会被保留),然后在同一位置用更大的大小小心地重新创建它。

例子:

$ sudo fdisk /dev/sda

Command (m for help): p

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     9437183     4717568   83  Linux

Command (m for help): d
Selected partition 1

Command (m for help): p

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4, default 1): 1
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759

Command (m for help): p

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    10485759     5241856   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.
Run Code Online (Sandbox Code Playgroud)

同样,新分区与旧分区从同一块开始是至关重要的。Id 也应该匹配(Linux 系统为 83)。准备好在最轻微的错误中丢失所有数据。

为了安全起见,您还可以通过按 来恢复启动标志(根据维基百科的说法,某些计算机仍然需要它)a

如果您的交换分区妨碍了您的操作,请参阅评论部分。

到目前为止,人们推荐使用 live CD 的原因应该很明显了。;-)

第2步: 作为fdisk有益提醒你,你必须在继续之前重新加载分区表。最安全的方法是简单地重新启动;但您也可以使用partprobekpartx (更多信息)

步骤 3. 调整分区大小并重新加载分区表后,resize2fs在文件系统上运行就很简单了,即使它作为根分区挂载也可以执行此操作。

例子:

$ sudo resize2fs /dev/sda1
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说非常有效。但是,我还确保保持的引导标志是原始状态。 (18认同)
  • @jbo5112:正如`fdisk` 所说,`partprobe` 或`kpartx` 可以代替重启工作;另见[这个问题](http://serverfault.com/questions/36038/reread-partition-table-without-rebooting)。即使您重新启动,当涉及到停机时间时,该解决方案仍然比使用 Live CD 更可取,其中虚拟机的简单重新启动可以少于 10 秒。它的操作时间也更快,这就是我通常自己使用这种方法的原因。:) (4认同)
  • 缩水了怎么办? (2认同)

小智 131

可以在线调整 ext4 文件系统的大小,即使它是您的根分区。使用resize2fs命令。

sudo resize2fs /dev/sda1
Run Code Online (Sandbox Code Playgroud)

编辑:不允许在线收缩:

root@brunojcm-htpc:/home# resize2fs /dev/sda5 2654693
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/sda5 is mounted on /; on-line resizing required
resize2fs: On-line shrinking not supported
Run Code Online (Sandbox Code Playgroud)

  • 来自 [`man resize2fs`](http://manpages.ubuntu.com/manpages/precise/en/man8/resize2fs.8.html):`resize2fs 程序不操作分区的大小。如果你想扩大一个文件系统,你必须首先确保你可以扩大底层分区的大小。如果您使用的是逻辑卷管理器 lvm(8),则可以使用 fdisk(8) 通过删除分区并重新创建更大的分区或使用 lvextend(8) 来完成。这个问题是关于调整 * 分区的大小*,而不是*文件系统*。这种区别很微妙,但非常重要。 (59认同)
  • 我发现 [resize2fs 手册页](http://manpages.ubuntu.com/manpages/precise/en/man8/resize2fs.8.html) 的第一段对于最初的问题最有趣:`resize2fs program will resize ext2 、ext3 或 ext4 文件系统。它可用于放大或缩小位于设备上的未安装文件系统。如果文件系统已挂载,则可用于扩展已挂载文件系统的大小,假设内核支持在线调整大小。(在撰写本文时,Linux 2.6 内核支持对使用 ext3 和 ext4 挂载的文件系统进行在线调整大小。)。 (10认同)
  • 请不要使用 `fdisk`,因为 `growpart` 会很容易为你做到这一点。 (10认同)
  • 您可以使用 fdisk 删除根分区,然后在相同的起始块处重新创建它。fdisk 将写出更改,但要在重新启动后才会生效。重新启动后,您可以使用 resize2fs 程序发送磁盘以填充分区。 (8认同)
  • 我刚刚在线调整了 ext4 根分区的大小。因此我可以确认这是可能的。但不是将 /dev/sda* 作为参数传递给 resize2fs,您需要传递逻辑卷名称。 (3认同)

STR*_*RML 123

一个更简单的解决方案 - 使用growpart <device> <partition>

growpart /dev/xvda 1  # Grows the partition; note the space
resize2fs /dev/xvda1  # Grows the filesystem
Run Code Online (Sandbox Code Playgroud)

与往常一样,备份您的分区表 ( sfdisk -d /dev/xvda > partition_bak.dmp) 以防万一。

  • growpart 是 cloud-utils 的一部分。如果你还没有安装,你可以使用 ```apt-get install cloud-utils``` 安装 (12认同)
  • 缩水了怎么办? (5认同)
  • 这非常适合调整我的 AWS 虚拟机的根分区和文件系统的大小。干杯。 (3认同)
  • @klor:来自 cloud-guest-utils 包 (2认同)
  • 这会出现“resize2fs:尝试打开 /dev/sda 时设备或资源繁忙”的错误 (2认同)
  • @NickODell - 我会说“专为云设计”部分的主要原因是......在使用物理主机和物理硬盘时看到硬盘改变大小是不典型的(顶部没有抽象层,如LVM )。但我看不出为什么它不能在非虚拟化机器上工作。 (2认同)
  • 这太棒了! (2认同)
  • gruppart 很棒、安全、快速,为我工作! (2认同)

vaa*_*aab 16

是的,您可以在不重新启动的情况下缩小/移动/增长在线根分区(也不是 livecd,也不是 usbkey):请参阅此答案。它写得很好,很容易理解,虽然很长而且有点冒险。因此,如果您只想扩大您的 ext4 分区,您可以坚持使用传统的工作resize2fs解决方案。

例如,我使用的通用解决方案适用于任何类型的专用或 VPS 解决方案。

TLDR;这种解决方案意味着要pivot_roottmpfs这样你就可以umount放心地根分区的生活和摆弄它。完成后,您将pivot_root回到新的根分区。

这几乎允许对根文件系统进行任何操作(移动它、更改文件系统、更改它的物理设备......)。

没有重新启动是必需的过程中,这允许绕过限制resize2fs 不能够收缩 ext4分区。

我个人使用过这个,它在 debian 系统上也运行得很好,所以它应该在 Ubuntu 上运行。我很惊讶没有看到这个深入的解决方案与处理相同问题的 stackexchange 网站中的许多问题有更多的联系。

注意:当然,如果你想扩大你的分区,一个简单的resize2fs就足够了,如许多地方和其他答案中所述。

  • 我认为对大多数人来说,一旦您停止了访问根分区的所有程序和服务,您还不如重新启动机器。对于缩小/移动,这可能仍然比使用 live CD 更快,但是对于增长(迄今为止最常见的任务,以及 OP 询问的内容),有些方法不涉及大多数系统的临时关闭. (5认同)
  • @SørenLøvborg:您可以在执行完整过程的同时重新启动需要连续生产的核心服务。有许多配置您不能放置 LiveCD(VPS 实例、专用服务器...),或者您希望避免出于特定原因重新启动的情况。原始问题的标题提到了“调整大小”,这吸引了人们在网上寻找缩小分区。**没有其他解决方案允许在线收缩 ext4。**该解决方案有风险、复杂,但最强大,它填补了其他解决方案的缺点。 (2认同)

小智 11

您也可以使用GParted - 只要您调整大小的分区不是您启动的分区 - 否则我建议新手使用live CD选项更容易一些。

GParted基本上完成了所有步骤 - 仅基于前端的 GUI。


wou*_*205 9

我想对@Søren Løvborg 的答案进行扩展:使用交换分区扩展分区。

首先是磁盘扩展后的布局:

$sudo parted /dev/sda 'unit s print' free
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 14336000s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start      End        Size       Type      File system     Flags
        63s        2047s      1985s                Free Space
 1      2048s      10485759s  10483712s  primary   ext4            boot
        10485760s  10487805s  2046s                Free Space
 2      10487806s  12580863s  2093058s   extended
 5      10487808s  12580863s  2093056s   logical   linux-swap(v1)
        12580864s  14335999s  1755136s             Free Space
Run Code Online (Sandbox Code Playgroud)

所以sda1需要用磁盘末尾的空闲空间进行扩展,但是交换分区在它们之间。你可以这样做:

首先我们需要禁用交换。检查它的使用量以及是否可以将其关闭。

$ free -h
              total        used        free      shared  buff/cache   available
Mem:           992M         52M        464M        3.2M        475M        784M
Swap:          1.0G          0B        1.0G
Run Code Online (Sandbox Code Playgroud)

交换在此处未使用,因此我们可以将其关闭

$sudo swapoff /dev/sda5
Run Code Online (Sandbox Code Playgroud)

现在我们将更改分区表:

$sudo fdisk /dev/sda
Run Code Online (Sandbox Code Playgroud)

(注意:如果您的第一个分区从扇区 63 而不是 2048 开始,则需要添加该选项-c=dos

Welcome to fdisk (util-linux 2.27.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
Disk /dev/sda: 6.9 GiB, 7340032000 bytes, 14336000 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: 0x9e11c6df

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1  *        2048 10485759 10483712    5G 83 Linux
/dev/sda2       10487806 12580863  2093058 1022M  5 Extended
/dev/sda5       10487808 12580863  2093056 1022M 82 Linux swap / Solaris

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

Partition 2 has been deleted.

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-14335999, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-14335999, default 14335999): 12242941

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

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (12242942-14335999, default 12242944):
Last sector, +sectors or +size{K,M,G,T,P} (12242944-14335999, default 14335999):

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

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

The bootable flag on partition 1 is enabled now.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).
Run Code Online (Sandbox Code Playgroud)

注意:sda1 的大小是扇区总数减去交换分区的扇区大小:14335999-2093058=12242941

正如 fdisk 所提到的:内核仍在使用旧的分区表,所以我们需要重新加载它。

$partprobe
Run Code Online (Sandbox Code Playgroud)

现在我们需要在 sda1 上运行 resize2fs(不要忘记这个!)

$resize2fs /dev/sda1
resize2fs 1.42.12 (29-Aug-2014)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 10
The filesystem on /dev/sda1 is now 38833617 (4k) blocks long.
Run Code Online (Sandbox Code Playgroud)

现在,事情还没有结束。您可能已经注意到 sda2 被分区为 Linux (Ext4) 类型。出于某种原因,在 fdisk 中无法选择类型。所以我们必须交替使用cfdisk

$ sudo cfdisk
Run Code Online (Sandbox Code Playgroud)

选择 sda2 并将类型更改为82 Linux swap / Solaris并确保写入(输入 yes 确认)

现在我们可以重新激活交换

$mkswap /dev/sda2
/dev/sda2
UUID=d58bf1cb-d27a-487d-b337-056767fd5ad6 none swap sw 0 0
Run Code Online (Sandbox Code Playgroud)

最后打开它:

$swapon /dev/sda2
Run Code Online (Sandbox Code Playgroud)

我们唯一需要做的就是更新 fstab 以在启动时自动挂载交换分区

$sudo nano /etc/fstab
Run Code Online (Sandbox Code Playgroud)

并将交换分区的 UUID 更改为上面的输出:

# swap was on /dev/sda5 during installation
UUID=d58bf1cb-d27a-487d-b337-056767fd5ad6 none            swap    sw              0       0
Run Code Online (Sandbox Code Playgroud)

现在一切都很好,您可以毫无问题地重新启动。


CDR*_*CDR 5

刚刚在安装根目录时在实时系统上调整了 ext4 根分区的大小。

[root@habib i686]# resize2fs /dev/vg_habib/lv_root
resize2fs 1.42 (29-Nov-2011)
Filesystem at /dev/vg_habib/lv_root is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 10
Performing an on-line resize of /dev/vg_habib/lv_root to 38427648 (4k) blocks.
The filesystem on /dev/vg_habib/lv_root is now 38427648 blocks long.

[root@habib i686]# 
Run Code Online (Sandbox Code Playgroud)