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有益提醒你,你必须在继续之前重新加载分区表。最安全的方法是简单地重新启动;但您也可以使用partprobe或kpartx (更多信息)。
步骤 3.
调整分区大小并重新加载分区表后,resize2fs在文件系统上运行就很简单了,即使它作为根分区挂载也可以执行此操作。
例子:
$ sudo resize2fs /dev/sda1
Run Code Online (Sandbox Code Playgroud)
小智 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)
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) 以防万一。
vaa*_*aab 16
是的,您可以在不重新启动的情况下缩小/移动/增长在线根分区(也不是 livecd,也不是 usbkey):请参阅此答案。它写得很好,很容易理解,虽然很长而且有点冒险。因此,如果您只想扩大您的 ext4 分区,您可以坚持使用传统的工作resize2fs解决方案。
例如,我使用的通用解决方案适用于任何类型的专用或 VPS 解决方案。
TLDR;这种解决方案意味着要pivot_root对tmpfs这样你就可以umount放心地根分区的生活和摆弄它。完成后,您将pivot_root回到新的根分区。
这几乎允许对根文件系统进行任何操作(移动它、更改文件系统、更改它的物理设备......)。
没有重新启动是必需的过程中,这允许绕过限制的resize2fs 不能够收缩 ext4分区。
我个人使用过这个,它在 debian 系统上也运行得很好,所以它应该在 Ubuntu 上运行。我很惊讶没有看到这个深入的解决方案与处理相同问题的 stackexchange 网站中的许多问题有更多的联系。
注意:当然,如果你想扩大你的分区,一个简单的resize2fs就足够了,如许多地方和其他答案中所述。
我想对@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)
现在一切都很好,您可以毫无问题地重新启动。
刚刚在安装根目录时在实时系统上调整了 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)
| 归档时间: |
|
| 查看次数: |
1043789 次 |
| 最近记录: |