如何從命令列調整 ext4 分割區的大小?

如何從命令列調整 ext4 分割區的大小?

從命令列調整 ext4 分割區(或任何類型分割區,取決於方法)大小的最簡單方法是什麼(可能使用最少的指令,但也是最容易理解的)?

在 GUI 中使用像 Gparted 這樣的工具顯然很容易,但是在命令列中呢?我想基於文字的 GUI 也可以算作答案,因為它在技術上仍然在命令列中。它只需要簡單即可。

我所說的分割區是指個人電腦(例如筆記型電腦)的單一磁碟上的簡單分割區。例如,我想調整大小/dev/sda4。沒有 RAID,只有一個磁碟機,這裡沒有任何複雜的事情。只是單一磁碟上的簡單分割區(/dev/sda 上的 /dev/sdaX)。

答案1

您可以fdisk在執行時變更分區表。參考在 Linux 上即時調整 ext4 檔案系統的大小 (在代碼的沉默部落格):

免責聲明: 如果您犯了錯誤,以下說明很容易毀掉您的資料。我在執行以下操作之前備份的虛擬機器上執行此操作。如果您因未執行備份而丟失數據,請不要過來抱怨。

第一:增加磁碟大小。

在 ESXi 中這很簡單,只需增加虛擬磁碟的大小即可。現在您有了更大的硬碟,但您仍然需要 a) 增加分割區大小和 b) 調整檔案系統大小。

第二:增加分區大小。

您可以fdisk在執行時變更分區表。原生 Ubuntu 安裝創建了 3 個分割區:一個主分割區 (sda1),一個擴充分割區 (sda2),其中有一個邏輯分割區 (sda5)。擴展分區僅用於交換,因此我可以輕鬆移動它而不會丟失任何資料。

  1. 刪除主分割區

  2. 刪除擴充分割區

  3. 建立一個新的主分割區,從與原始分割區相同的磁區開始,只是大小較大(留一些用於交換)

  4. 建立一個新的擴展分區,其中包含邏輯分區以保存交換空間

    me@ubuntu:~$ sudo fdisk /dev/sda
    
    Command (m for help): p
    
    Disk /dev/sda: 268.4 GB, 268435456000 bytes
    255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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
    Disk identifier: 0x000e49fa
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048   192940031    96468992   83  Linux
    /dev/sda2       192942078   209713151     8385537    5  Extended
    /dev/sda5       192942080   209713151     8385536   82  Linux swap / Solaris
    
    Command (m for help): d
    Partition number (1-5): 1
    
    Command (m for help): d
    Partition number (1-5): 2
    
    Command (m for help): n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): p
    Partition number (1-4, default 1):
    Using default value 1
    First sector (2048-524287999, default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-524287999, default 524287999): 507516925
    
    Command (m for help): p
    
    Disk /dev/sda: 268.4 GB, 268435456000 bytes
    255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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
    Disk identifier: 0x000e49fa
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            2048   507516925   253757439   83  Linux
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): e
    Partition number (1-4, default 2): 2
    First sector (507516926-524287999, default 507516926):
    Using default value 507516926
    Last sector, +sectors or +size{K,M,G} (507516926-524287999, default 524287999):
    Using default value 524287999
    
    Command (m for help): p
    
    Disk /dev/sda: 268.4 GB, 268435456000 bytes
    255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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
    Disk identifier: 0x000e49fa
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            2048   507516925   253757439   83  Linux
    /dev/sda2       507516926   524287999     8385537    5  Extended
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)
    Select (default p): l
    Adding logical partition 5
    First sector (507518974-524287999, default 507518974):
    Using default value 507518974
    Last sector, +sectors or +size{K,M,G} (507518974-524287999, default 524287999):
    Using default value 524287999
    
    Command (m for help): p
    
    Disk /dev/sda: 268.4 GB, 268435456000 bytes
    255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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
    Disk identifier: 0x000e49fa
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            2048   507516925   253757439   83  Linux
    /dev/sda2       507516926   524287999     8385537    5  Extended
    /dev/sda5       507518974   524287999     8384513   83  Linux
    
    Command (m for help): t
    Partition number (1-5): 5
    
    Hex code (type L to list codes): 82
    Changed system type of partition 5 to 82 (Linux swap / Solaris)
    
    Command (m for help): p
    
    Disk /dev/sda: 268.4 GB, 268435456000 bytes
    255 heads, 63 sectors/track, 32635 cylinders, total 524288000 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
    Disk identifier: 0x000e49fa
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1            2048   507516925   253757439   83  Linux
    /dev/sda2       507516926   524287999     8385537    5  Extended
    /dev/sda5       507518974   524287999     8384513   82  Linux swap / Solaris
    
    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.
    
    me@ubuntu:~$ sudo reboot 
    

後來我注意到我沒有設置可啟動標誌,但顯然你並不真的需要它

第三:擴大文件系統。

您可以resize2fs在已安裝的分割區上線上執行此操作。

me@ubuntu:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        91G   86G   12M 100% /
udev            3.9G  4.0K  3.9G   1% /dev
tmpfs           1.6G  696K  1.6G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            3.9G  144K  3.9G   1% /run/shm
none            100M   16K  100M   1% /run/user

me@ubuntu:~$ sudo resize2fs /dev/sda1
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 6, new_desc_blocks = 16
The filesystem on /dev/sda1 is now 63439359 blocks long.

me@ubuntu:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       239G   86G  142G  38% /
udev            3.9G   12K  3.9G   1% /dev
tmpfs           1.6G  696K  1.6G   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            3.9G  152K  3.9G   1% /run/shm
none            100M   36K  100M   1% /run/user

輕微的問題:重新啟動後,交換空間不活動。結果你需要運行mkswap,調整/etc/fstab到新的 UUID 並打開交換

me@ubuntu:~$ sudo mkswap /dev/sda5 
Setting up swapspace version 1, size = 8384508 KiB
no label, UUID=141d401a-b49d-4a96-9b85-c130cb0de40a

me@ubuntu:~$ sudo swapon --all --verbose
swapon on /dev/sda5
swapon: /dev/sda5: found swap signature: version 1, page-size 4, same byte order
swapon: /dev/sda5: pagesize=4096, swapsize=8585740288, devsize=8585741312

編輯/etc/fstab以將舊交換分割區的 UUID 替換為mkswap.

答案2

我回答這個問題是為了防止你遇到和我類似的情況。我必須在以 Guest 身分執行 Ubuntu 18.0.4 LTS 的動態磁碟上調整檔案系統 (Ext4) 的大小。我設法從主機調整磁碟大小,然後調整檔案系統大小。

無法運行常用命令。例如:

$ resize2fs /dev/sda2
resize2fs 1.44.1 (24-Mar-2018)
The filesystem is already 13180160 (4k) blocks long.  Nothing to do!

而且fdisk相當複雜,可能我還必須執行 LiveCD 才能在未安裝的磁碟上運作。

這兩個命令輕鬆快速地解決了我的問題:

$ growpart /dev/sda 2
CHANGED: partition=2 start=4096 old: size=105441280 end=105445376 new: size=409595871,end=409599967

$ resize2fs /dev/sda2
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 7, new_desc_blocks = 25
The filesystem on /dev/sda2 is now 51199483 (4k) blocks long.

注意:2第一個指令中表示設備上的分區號碼/dev/sda

答案3

如果要增加分割區和檔案系統的大小,請先執行fdisk以增加分割區的大小,然後執行resize2fs以增加檔案系統的大小。

如果您要縮小兩者的大小,請先運行resize2fs,然後運行fdisk

答案4

諾阿洛的解決方案顯然是最好的;比其他的簡單得多:

迴聲“-+”| sfdisk-N<零件號碼>/dev/sdX

關於其工作原理的簡短說明:-N意味著sfdisk將在單一分區上工作。當像這樣開始時,sfdisk將是互動的並等待開始位置、大小和可選的分割區類型。 「-」表示起始位置不改變,「+」表示結束位置將使用盡可能多的空間。之後您將必須使用printwrite命令。領先者echo "- +" |在一行中做到了這一點。

它的限制是必須有空閒空間緊接著。如果分割區調整大小後,存在交換分割區(即裡面沒有真實資料),也可以透過 輕鬆解決sfdisk。使用上面的語法,假設要更改的分割區是 onsda1且 swap on sda2,我們有一個 128 G 的磁碟,我們需要 6 G 的交換: 第一步是將交換分割區移到末尾:

echo "122G +" | sfdisk -N2 /dev/sda

現在展開第一個分割區:

echo "- +"    | sfdisk -N1 /dev/sda

並使用 調整其大小resize2fs。您可能必須確保您的新交換得到認可。請參閱swapoffmkswapswapon文件fstab。這不在這個問題的討論範圍之內。

關於命令sda2不要以這種方式移動帶有資料的分區!  您將丟失所有資料。該命令不移動資料;只是分區邊界。用於 sda1 ( ) 的命令沒有危險,- +因為分區的起始位置沒有改變;這就是-用於單一分區時「 」的意思。

謝謝你@noalo

相關內容