Articles

10 fdisk Commands to Manage Linux Disk Partitions

fdisk は、Linux/Unix システムで最も一般的に使用されている、コマンドライン ベースのディスク操作ユーティリティです。

このツールは、新しいパーティション用のスペースの作成、新しいドライブ用のスペースの整理、古いドライブの再整理、新しいディスクへのデータのコピーまたは移動などの点で非常に便利です。 システムに搭載されているハードディスクのサイズに基づいて、最大4つの新しいプライマリ パーティションといくつかの論理 (拡張) パーティションを作成することができます。

ディスクパーティションを管理するfdiskコマンド

この記事では、Linuxベースのシステムでパーティションテーブルを管理する10個の基本的なfdiskコマンドを説明します。

Caution – Don’t Create, Delete or Modify Partitions.

View all Disk Partitions in Linux

以下の基本的なコマンドは、システム上に存在するすべてのディスクパーティションを一覧表示します。 Linuxで利用可能なすべてのパーティションを表示するには、fdiskコマンドで「-l」引数を使用します(listing all partitions)。 パーティションはデバイスの名前で表示されます。 例えば、以下のようになります。

# fdisk -lDisk /dev/sda: 637.8 GB, 637802643456 bytes255 heads, 63 sectors/track, 77541 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System/dev/sda1 * 1 13 104391 83 Linux/dev/sda2 14 2624 20972857+ 83 Linux/dev/sda3 2625 4582 15727635 83 Linux/dev/sda4 4583 77541 586043167+ 5 Extended/dev/sda5 4583 5887 10482381 83 Linux/dev/sda6 5888 7192 10482381 83 Linux/dev/sda7 7193 7845 5245191 83 Linux/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris/dev/sda9 8368 77541 555640123+ 8e Linux LVM

View Specific Disk Partition in Linux

特定のハードディスクのすべてのパーティションを表示するには、オプション ‘-l’ とデバイス名を使用します。 例えば、次のコマンドは、デバイス /dev/sda のすべてのディスクパーティションを表示します。 デバイス名が異なる場合は、デバイス名を /dev/sdb または /dev/sdc と書いてください。

# fdisk -l /dev/sdaDisk /dev/sda: 637.8 GB, 637802643456 bytes255 heads, 63 sectors/track, 77541 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System/dev/sda1 * 1 13 104391 83 Linux/dev/sda2 14 2624 20972857+ 83 Linux/dev/sda3 2625 4582 15727635 83 Linux/dev/sda4 4583 77541 586043167+ 5 Extended/dev/sda5 4583 5887 10482381 83 Linux/dev/sda6 5888 7192 10482381 83 Linux/dev/sda7 7193 7845 5245191 83 Linux/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris/dev/sda9 8368 77541 555640123+ 8e Linux LVM

Check all Available fdisk Commands

fdiskで利用可能なすべてのコマンドを表示したい場合は、以下のコマンドを使用してください。 以下のように、/dev/sdaのようなハードディスク名を指定して、以下のコマンドを使用してください。

‘m’を入力すると、/dev/sdaのハードディスクで操作できるfdiskのすべての利用可能なコマンドのリストが表示されます。

‘m’を入力すると、/dev/sdaのハードディスク上で操作できるfdiskのすべての利用可能なオプションが表示されます。

# fdisk /dev/sdaWARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u').Command (m for help): mCommand action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)Command (m for help):

Print all Partition Table in Linux

ハードディスクのすべてのパーティションテーブルを表示するには、特定のハードディスク (/dev/sda) のコマンドモードにいる必要があります。

Command (m for help): pDisk /dev/sda: 637.8 GB, 637802643456 bytes255 heads, 63 sectors/track, 77541 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System/dev/sda1 * 1 13 104391 83 Linux/dev/sda2 14 2624 20972857+ 83 Linux/dev/sda3 2625 4582 15727635 83 Linux/dev/sda4 4583 77541 586043167+ 5 Extended/dev/sda5 4583 5887 10482381 83 Linux/dev/sda6 5888 7192 10482381 83 Linux/dev/sda7 7193 7845 5245191 83 Linux/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris/dev/sda9 8368 77541 555640123+ 8e Linux LVMCommand (m for help):

Linuxでパーティションを削除する方法

特定のハードディスク(/dev/sda)から特定のパーティション(/dev/sda9など)を削除したい場合、fdiskコマンドを実行する必要があります。

# fdisk /dev/sda

次に、任意のパーティション名をシステムから削除するには、’d’を入力します。 d」を入力すると、/dev/sdaのハードディスクから削除したいパーティション番号を入力するように促されます。 例えば、「4」を入力すると、パーティション番号「4」(つまり/dev/sda4)のディスクが削除され、パーティションテーブルに空き領域が表示されます。 w」を入力すると、テーブルをディスクに書き込み、パーティションテーブルに新たな変更を加えた後に終了します。 新しい変更は、次回のシステムの再起動後にのみ行われます。

Warning : このステップを実行する際には、オプション ‘d’ を使用するとシステムからパーティションが完全に削除され、パーティション内のすべてのデータが失われる可能性があるため、注意が必要です。

How to Create a New Partition in Linux

デバイスの 1 つ (例えば /dev/sda) に空き領域があり、その下に新しいパーティションを作成したい場合、fdisk を使用する必要があります。 その場合、/dev/sdaのfdiskコマンドモードになっている必要があります。

# fdisk /dev/sda

コマンドモードに入ったら、今度は「n」コマンドを押して、/dev/sdaの下に特定のサイズの新しいパーティションを作成します。

新しいパーティションを作成している間、「extended」または「primary」パーティション作成の2つのオプションを尋ねてきます。 e」を押すと拡張パーティション、「p」を押すとプライマリパーティションとなります。

  1. 作成するパーティションの最初のシリンダー番号
  2. 作成するパーティションの最後のシリンダー番号 (Last cylinder, +cylinders or +size).

最後のシリンダーに「+5000M」を追加することで、シリンダーのサイズを入力できます。 ここで、「+」は追加を意味し、5000Mは新しいパーティションのサイズ(つまり5000MB)を意味します。

Command (m for help): wThe 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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.

How to Format a Partition in Linux

新しいパーティションを作成した後は、’mkfs’コマンドを使ってフォーマットしてください。 ターミナルで以下のコマンドを入力して、パーティションをフォーマットします。

# mkfs.ext4 /dev/sda4

Linuxでパーティションのサイズを確認する方法

新しいパーティションをフォーマットした後、fdiskコマンドでフラグ「s」(サイズをブロック単位で表示)を使ってパーティションのサイズを確認します。

# fdisk -s /dev/sda25194304

How to Fix Partition Table Order

論理パーティションを削除した後に再度作成した場合、「パーティションが順番になっていない」という問題や、「パーティション テーブル エントリがディスクの順番になっていません」といったエラーメッセージに気づくかもしれません。

例えば、3つの論理パーティション(sda4, sda5, sda6)を削除して、新しいパーティションを作成した場合、新しいパーティション名はsda4になると思うかもしれません。 しかし、システムはそれをsda5として作成します。

このようなパーティションの順番の問題を解決し、新しく作成されたパーティションにsda4を割り当てるには、「x」を発行して追加機能セクションに入り、次に「f」エキスパートコマンドを入力して、以下のようにパーティションテーブルの順番を修正します。

# fdisk /dev/sdaWARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u').Command (m for help): xExpert command (m for help): fDone.Expert command (m for help): wThe 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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.

「f」コマンドを実行した後、「w」コマンドを実行して保存し、fdiskコマンドモードを終了することを忘れないでください。

パーティションのブートフラグ (*) を無効にする方法

デフォルトでは、fdiskコマンドは各パーティションにブートフラグ (例: ‘*’) のシンボルを表示します。

# fdisk /dev/sda

「p」コマンドを押して現在のパーティションテーブルを表示すると、下図のように /dev/sda1 ディスクにブートフラグ (オレンジ色のアスタリスク (*) シンボル) があることがわかります。

次にブートフラグを無効にするコマンド「a」を入力し、パーティション番号「1」を入力します。 これにより、パーティション /dev/sda1 のブートフラグが無効になります。

Command (m for help): aPartition number (1-9): 1Command (m for help): pDisk /dev/sda: 637.8 GB, 637802643456 bytes255 heads, 63 sectors/track, 77541 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System/dev/sda1 1 13 104391 83 Linux/dev/sda2 14 2624 20972857+ 83 Linux/dev/sda3 2625 4582 15727635 83 Linux/dev/sda4 4583 77541 586043167+ 5 Extended/dev/sda5 4583 5887 10482381 83 Linux/dev/sda6 5888 7192 10482381 83 Linux/dev/sda7 7193 7845 5245191 83 Linux/dev/sda8 7846 8367 4192933+ 82 Linux swap / Solaris/dev/sda9 8368 77541 555640123+ 8e Linux LVMCommand (m for help):

fdiskコマンドの基本的なコマンドをほぼすべて含めるように努力しましたが、fdiskには他にもさまざまな専門的なコマンドがあり、’x’を入力することでそれらを使用することができます。 より詳しい情報は、ターミナルから「man fdisk」コマンドをチェックしてみてください。

Read Also :

  1. 12 “df” Commands to Check Disk Space in Linux
  2. 10 Useful “du” Commands to Find Disk Usage of Files and Directories

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です