コマンドラインでext4パーティションのサイズを変更するには?

コマンドラインでext4パーティションのサイズを変更するには?

コマンドラインでext4パーティション(または方法に応じてすべての種類のパーティション)のサイズを変更する最も簡単な方法は何ですか(おそらく最も少ないコマンドを使用しますが、理解するのが最も簡単です)。

GUIでGpartedなどのツールを使用するのは確かに簡単ですが、コマンドラインではどうですか?技術的にはまだコマンドラインにあるので、テキストベースのGUIも答えと見なされます。簡単なはずです。

パーティションとは、ラップトップなどのパーソナルコンピュータの単一ディスク上の単純なパーティションを意味します。たとえば、サイズを変更したいと思います/dev/sda4。 RAIDはなく、ディスクドライブは1つだけで、ここに複雑なものはありません。単一ディスク (/dev/sda の /dev/sdaX) にある単純なパーティションです。

ベストアンサー1

fdisk実行時にパーティションテーブルを変更できます。引用するLinuxでのリアルタイムサイズ変更ext4ファイルシステム (存在するコードサイレントブログ):

婦人声明: 次のガイドラインに従うと、間違えた場合にデータが簡単に破壊される可能性があります。以下を実行する前に、バックアップした仮想マシンでこれを実行しています。バックアップを取らずにデータが失われた場合は、ここに来て文句を言わないでください。

...

まず、ディスクサイズを増やします。

ESXiでは、仮想ディスクのサイズを増やすのと同じくらい簡単です。これで大きなハードドライブがありますが、まだa)パーティションのサイズを増やし、b)ファイルシステムのサイズを変更する必要があります。

2番目:パーティションのサイズを大きくしてください。

fdisk実行時にパーティションテーブルを変更できます。デフォルトのUbuntuインストールでは、基本パーティション(sda1)、拡張パーティション(sda2)、および1つの論理パーティション(sda5)の3つのパーティションが作成されます。拡張パーティションはスワップ専用なので、データを失うことなく簡単に移動できます。

  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 
    

後で私は起動可能なフラグを設定しなかったことがわかりました。あなたにはあまり必要ありません。

3番目:ファイルシステムを拡張します。

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

マイナーな問題:再起動後にスワップスペースが無効になります。その結果、を実行し、新しいUUIDにmkswap調整してスワップを開く必要があります。/etc/fstab

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古いスワップパーティションのUUIDmkswapを。

おすすめ記事