パーティションの作成、編集、削除を行う fdisk
何かしらの環境を構築するときに必要になったコマンドも記録し行きたいと思います。
まずは、パーティションの作成、編集、削除などを行うコマンド fdisk についてまとめていきます。
fdisk コマンド
fdisk コマンドは、ディスクパーティションを作成、表示、編集、削除するためのコマンドです。
例えば、 Ubuntu に物理的に接続されているディスクを確認するには「fdisk -l」を使います。
$ sudo fdisk -l
上記コマンドを実行すると次のような結果が出力されます。
今回は、外付けのハードディスクを接続した状態で実行しており、「/dev/sda」が接続した USB ハードディスクになります。
Disk /dev/nvme0n1: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: WD PC SN740 SDDPNQD-256G-1006
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: gpt
Disk identifier: 51707EE4-051F-41CC-BD51-B9B884EC73D7
Device Start End Sectors Size Type
/dev/nvme0n1p1 2048 2203647 2201600 1G EFI System
/dev/nvme0n1p2 2203648 6397951 4194304 2G Linux filesystem
/dev/nvme0n1p3 6397952 500115455 493717504 235.4G Linux filesystem
Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 100 GiB, 107374182400 bytes, 209715200 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 /dev/sda: 3.64 TiB, 4000787030016 bytes, 7814037168 sectors
Disk model: HD-LE-B
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: FC2C2DE6-82E8-4FDA-98D9-0144F4796910
Device Start End Sectors Size Type
/dev/sda1 1024 7814036479 7814035456 3.6T Microsoft basic data
ディスクに対して対話的に fdisk を実行する
次のようにデバイスに対するコマンドを実行すると、対話モードでの操作になります
sudo fdisk /dev/sda
fdisk の対話モードでは、次のコマンドを使用して操作を行います。
コマンド | 説明 |
---|---|
m | ヘルプメニューを表示 |
p | 現在のパーティションテーブルを表示 |
n | 新しいパーティションを作成 |
d | パーティションを削除 |
t | パーティションのタイプを変更 |
a | ブートフラグを設定またはクリア |
w | 変更を保存して終了 |
q | 変更を保存せずに終了 |
コマンドの実行例
$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.39.3).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help):
「p」コマンドでパーティションを確認します。
Command (m for help): p
Disk /dev/sda: 3.64 TiB, 4000787030016 bytes, 7814037168 sectors
Disk model: HD-LE-B
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: FC2C2DE6-82E8-4FDA-98D9-0144F4796910
Device Start End Sectors Size Type
/dev/sda1 1024 7814036479 7814035456 3.6T Microsoft basic data
「d」コマンドでパーティションを削除します。
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
「n」コマンドでパーティションを作成します。
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (34-7814037134, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-7814037134, default 7814035455):
Created a new partition 1 of type 'Linux filesystem' and of size 3.6 TiB.
「w」コマンドでパーティション情報を保存して終了します。
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
fdisk で確認すると、パーティションが作成されていることを確認できます。
Disk /dev/sda: 3.64 TiB, 4000787030016 bytes, 7814037168 sectors
Disk model: HD-LE-B
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: FC2C2DE6-82E8-4FDA-98D9-0144F4796910
Device Start End Sectors Size Type
/dev/sda1 2048 7814035455 7814033408 3.6T Linux filesystem
実際に使えるようにするには、何らかの形式でフォーマットします。 例えば、 ext4 でフォーマットするには次のように 「 mkfs 」 コマンドを実行します。
$ sudo mkfs.ext4 /dev/sda1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 976754176 4k blocks and 244195328 inodes
Filesystem UUID: dbf8fc4a-100e-4eb1-b487-34f8d7a72d34
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000, 550731776, 644972544
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
fdisk コマンドのオプション
オプション | 説明 |
---|---|
-l | パーティションテーブルを表示します。 |
-b <サイズ> | セクターサイズを指定します。 |
-u <単位> | ディスプレイ単位を変更します(c はシリンダ、s はセクター)。 |
-c= | DOS 互換モードを無効にします。 |
-n | 対話モードを使用せずに新しいパーティションを作成します。 |
-s | パーティションをリストします(古い形式)。 |