このページで解説している内容は、以下の YouTube 動画の解説で見ることができます。

【Linux】GPTで使用する:gdiskコマンド

gdiskコマンドの概要

 gdiskコマンドは、GUIDパーティションテーブル (GPT) を操作するためのツールです。GPTは、従来のマスターブートレコード (MBR) よりも多くのパーティションをサポートし、パーティションのサイズ制限が大幅に緩和されています。gdiskは、GPTの作成、変更、および削除などの操作を提供します。

gdiskコマンドの主なオプション

オプション説明
-l指定したディスクのパーティションテーブルをリスト表示します。
コマンドの主なオプション

gdiskコマンドの対話モードにおける主なコマンド

コマンド説明
mメニューを表示します。
p現在のパーティションテーブルを表示します。
n新しいパーティションを作成します。
dパーティションを削除します。
wパーティションテーブルの変更を保存して終了します。
qパーティションテーブルの変更を保存せずに終了します。
対話モードにおける主なコマンド

コマンドの使用例と解説

 以下は、3台目のディスクをgdiskコマンドを使ってパーティションに分割する手順です。fdiskコマンドと同様に、パーティションの操作は対話モードで進めていきます。

1.「su -」コマンドを実行する。

 パーティションの操作はシステム管理のコマンドになるため、rootユーザーに切り替えておく必要があります。パスワードの入力が求められたら、パスワードを入力します。

user01@ubuntu-vm:~$ su -
パスワード:
root@ubuntu-vm:~#

2.メニューを表示します。

・オプションを指定しないで「gdisk /dev/sdc」コマンドを実行します。

 gdiskコマンドを実行し、メニューを表示します。「/dev/sdc」の部分で3台目のディスクを指定しています。

root@ubuntu-vm:~# gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.8

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries in memory.

対話モードに入ったら「m」と入力し、Enterキーを押します。

Command (? for help): m
b	back up GPT data to a file
c	change a partition's name
d	delete a partition
i	show detailed information on a partition
l	list known partition types
n	add a new partition
o	create a new empty GUID partition table (GPT)
p	print the partition table
q	quit without saving changes
r	recovery and transformation options (experts only)
s	sort partitions
t	change a partition's type code
v	verify disk
w	write table to disk and exit
x	extra functionality (experts only)
?	print this menu

3.現在のパーティションテーブルの内容を表示します。

・「p」と入力し、Enterキーを押します。

ディスクがまだ未分割であることを確認します。

Command (? for help): p
Disk /dev/sdc: 20971520 sectors, 10.0 GiB
Model: VBOX HARDDISK   
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 100AD349-D134-4E67-BF0F-243EE6B28A47
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 20971453 sectors (10.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

4.新規パーティションを作成します。

・「n」と入力し、Enterキーを押します。

Command (? for help): n

5.パーティション番号を指定します。

・パーティション番号を「1」に指定し、Enterキーを押します。

Partition number (1-128, default 1): 1

6.容量を1GBに指定します。

・容量を「+1G」と指定し、Enterキーを押します。

Last sector (2048-20971486, default = 20971486) or {+-}size{KMGTP}: +1G
Current type is 8300 (Linux filesystem)

7.パーティションタイプはデフォルトのLinuxファイルシステムにします。

・Enterキーを押します。

Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

8.作成したパーティションを確認します。

・「p」と入力し、Enterキーを押します。

Command (? for help): p
Disk /dev/sdc: 20971520 sectors, 10.0 GiB
Model: VBOX HARDDISK   
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 100AD349-D134-4E67-BF0F-243EE6B28A47
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 20971486
Partitions will be aligned on 2048-sector boundaries
Total free space is 18874301 sectors (9.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

   1            2048         2099199   1024.0 MiB  8300  Linux filesystem

9.パーティションテーブルの変更を保存して終了します。

・「w」と入力し、Enterキーを押します。

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

10.処理を進めるために「Y」と入力し、Enterキーを押します。

・「Y」と入力し、Enterキーを押します。

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.

11.デバイスファイルを確認します

・「ls /dev/sd*」コマンドを実行し、新しいパーティションが作成されていることを確認します。

3台目のディスクに新しいパーティション「/dev/sdc1」が作成されていることが確認できます。

root@ubuntu-vm:~# ls /dev/sd*
/dev/sda   /dev/sda2  /dev/sdb1  /dev/sdc1
/dev/sda1  /dev/sdb   /dev/sdc   /dev/sdd

まとめ

 gdiskコマンドは、GPTディスクのパーティション操作に非常に便利なツールです。主なオプションと対話モードのコマンドを理解することで、効率的にパーティションを管理できます。