gdisk コマンド:Linux のパーティション設定と GPT パーティションの作成、削除

テスト環境RHEL系AlmaLinux 9.8rootアカウント
ディスク・マウントコマンドgdisk 1.0.7場所/usr/sbin/gdisk

ディスクを GPT 方式で分けるときは、gdisk コマンドでパーティションを作成・削除します。gdisk の後ろにデバイスファイル名を付けて実行すると対話画面が開き、パーティション一覧の確認は p、削除は d のように 1 文字のオプションを入力して Linux のパーティション設定を進めます。

gdisk disk.img

記事に載せた gdisk の出力は、truncate で作った 3 GiB の練習用イメージファイル disk.img で実行した結果です。実際のディスクでは、disk.img の部分に /dev/sdb のようなデバイスファイル名を入れます。

マウントするまでの作業手順は以下のとおりです。

  • Linuxパーティション設定の手順
  • パーティション情報を編集したいデバイスファイル名を指定してコマンドを実行
  • パーティション一覧を表示
  • 不要なパーティションを削除
  • 新しいパーティションを作成
  • パーティション情報を保存(w)して終了

w で保存するまでは、パーティションの作業内容はディスクに反映されません。

1️⃣ gdisk コマンドで Linux のパーティションを設定する

gdisk は GPT パーティションテーブルを扱うパーティション管理ユーティリティです。MBR 方式では 2TiB を超えるディスクを使い切れないため、2TB 以上の大容量ディスクは GPT で分け、このとき gdisk や parted でパーティション作業を行います。

gdisk -[オプション] [対象 デバイス ファイル]

設定されたパーティション情報は、 gdisk または fdisk コマンドに -l オプションを付けて確認できます。

gdisk -l disk.img
GPT fdisk (gdisk) version 1.0.7

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

Found valid GPT with protective MBR; using GPT.
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): ABA1DC8A-EB0E-4342-86D5-EEF52D68CD23
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       code  name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200         3147775   512.0 MiB   8200  Linux swap
   3         3147776         6291422   1.5 GiB     8300  Linux filesystem

1. gdisk の実行画面

パーティションテーブルのない空の disk.img で gdisk を実行したときに表示される基本画面です。

gdisk disk.img
GPT fdisk (gdisk) version 1.0.7

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

Creating new GPT entries in memory.

Command (? for help):

2. gdisk p オプション:パーティション一覧の確認

以下は、同じ disk.img にパーティションを 3 つ(1 GiB、swap 512 MiB、残りの 1.5 GiB)作って保存したあと、p で表示した一覧です。

Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): ABA1DC8A-EB0E-4342-86D5-EEF52D68CD23
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       code  name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200         3147775   512.0 MiB   8200  Linux swap
   3         3147776         6291422   1.5 GiB     8300  Linux filesystem
p の出力項目説明
DiskSectors とディスクの総容量
Sector size論理セクター 1 つのサイズ(この記事では 512 bytes)
Disk identifierGUID情報を表示します。
Numberパーティション番号
Start (sector)パーティションの開始セクター
End (sector)パーティションの終了セクター
Sizeパーティションの容量
Codeパーティションの種類を番号で表示
Nameパーティションの用途。cコマンドで名前を変更できます。

3. gdiskでパーティションを作成

n を入力すると、パーティション番号、開始セクター、終了セクターを順に尋ねられます。以下は、1 番パーティションだけがある disk.img で、番号と開始セクターはデフォルトのまま、終了セクターに +512M を入れて 2 番パーティションを作った例です。

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)

パーティションの種類はLコマンドで確認できます。

Hex code or GUID (L to show codes, Enter = 8300): L
Type search string, or <Enter> to show all codes: 
0700 Microsoft basic data                0701 Microsoft Storage Replica         
0702 ArcaOS Type 1                       0c01 Microsoft reserved                
2700 Windows RE                          3000 ONIE boot                         
3001 ONIE config                         3900 Plan 9                            
4100 PowerPC PReP boot                   4200 Windows LDM data                  
4201 Windows LDM metadata                4202 Windows Storage Spaces            
7501 IBM GPFS                            7f00 ChromeOS kernel                   
7f01 ChromeOS root                       7f02 ChromeOS reserved                 
8200 Linux swap                          8300 Linux filesystem                  
8301 Linux reserved                      8302 Linux /home                       
8303 Linux x86 root (/)                  8304 Linux x86-64 root (/)             
8305 Linux ARM64 root (/)                8306 Linux /srv                        
8307 Linux ARM32 root (/)                8308 Linux dm-crypt                    
8309 Linux LUKS                          830a Linux IA-64 root (/)              
830b Linux x86 root verity               830c Linux x86-64 root verity          
830d Linux ARM32 root verity             830e Linux ARM64 root verity           
830f Linux IA-64 root verity             8310 Linux /var                        
8311 Linux /var/tmp                      8312 Linux user's home                 
8313 Linux x86 /usr                      8314 Linux x86-64 /usr                 
8315 Linux ARM32 /usr                    8316 Linux ARM64 /usr                  
8317 Linux IA-64 /usr                    8318 Linux x86 /usr verity             
Press the <Enter> key to see more codes, q to quit: 

パーティションの種類コードは swap パーティションが 8200、Linux ファイルシステムが 8300 で、このコードを入力して種類を設定します。

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

作ったパーティションは w でディスクに書き込み、続行するか尋ねられたら Y を入力します。

Command (? for help): w

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

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to disk.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

4. gdiskでパーティションを削除

[d] オプションでパーティションを削除します。番号を尋ねられたら削除するパーティションの番号を入力します。パーティションが 1 つしかない場合は、番号を尋ねずにすぐ削除されます。

Command (? for help): d
Partition number (1-3): 3

🔲 gdisk コマンドのオプション

オプションコピー説明
-lパーティション一覧を出力してすぐ終了
pディスクのパーティション一覧を表示
n新しいパーティションを作成(番号、開始セクター、終了セクターの順に入力)
L種類コードを尋ねる欄でパーティションの種類一覧を表示
8200パーティションの種類コードを入力(n、t の種類欄)
wパーティション情報をディスクに保存して終了
dパーティションを削除
オプションコピー説明
cパーティション名を変更
lパーティションの種類を一覧表示
qパーティション情報を保存せずに終了
tパーティションの種類を設定(既存パーティションの種類変更を含む)
sパーティションを並べ替え
?操作コマンドを一覧表示

🔲 gdisk コマンドシミュレーター

下のシミュレーターで gdisk disk.img を実行し、オプション表の p、n、d、w をチップかキーボードで入力すると、保存前のメモリ欄とディスク欄がどう変わるかを確認できます。

gdisk シミュレーター練習用ディスク disk.img · 3 GiB
[root@server ~]#
ブラウザ内だけで動作し、実際のディスクには何も書き込みません。w で保存するまで変更はメモリ上にだけあり、q で終了すると消えます。

2️⃣ gdisk コマンドの実務例

  • ↪️ 練習用イメージで試したパーティション作業とミス

まず truncate で練習用イメージを 2 つ作りました。この章の出力は、新しく作った 3 GiB のイメージ disk.img と 1 GiB のイメージ data.img で取得したもので、同じ手順を gdisk コマンドシミュレーターで直接入力して試せます。

truncate -s 3G disk.img
truncate -s 1G data.img

🔲 gdisk のパーティション作成と削除の例

空の disk.img で gdisk を実行し、n で 1 GiB のパーティションと 512 MiB の swap パーティションを順に作りました。番号と開始セクターは Enter でデフォルト値を受け入れ、終了セクターに +1G と +512M を、2 つ目のパーティションの種類コードに 8200 を入力しました。

gdisk disk.img
GPT fdisk (gdisk) version 1.0.7

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

Creating new GPT entries in memory.

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-6291422, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-6291422, default = 6291422) or {+-}size{KMGTP}: +1G
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'

w を入力する前に、まず p で一覧を確認しました。2 つのパーティションは入力したサイズと種類コードで作られ、空き領域は 1.5 GiB と表示されました。

Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 3145661 sectors (1.5 GiB)

Number  Start (sector)    End (sector)  Size       code  name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200         3147775   512.0 MiB   8200  Linux swap

一覧を確認したあと、w と Y を入力してディスクに書き込みました。

Command (? for help): w

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

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to disk.img.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.

削除も同じ手順で行いました。gdisk をもう一度実行して d に 2 を入力し、p で 1 番パーティションだけが残ったことを確認してから w で保存しました。

Command (? for help): d
Partition number (1-2): 2

Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 4194237 sectors (2.0 GiB)

Number  Start (sector)    End (sector)  Size       code  name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem

🔲 gdisk でよくあるミス

初めて使うときにしやすいミスは、w を入力せずに q で終了してしまうことです。1 番パーティションだけがある disk.img に n で swap パーティションを追加し、p で 2 番パーティションまで確認したあと、w を入力せずに q で終了してみました。

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-6291422, default = 2099200) or {+-}size{KMGTP}:
Last sector (2099200-6291422, default = 6291422) or {+-}size{KMGTP}: +512M
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'

Command (? for help): p
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 3145661 sectors (1.5 GiB)

Number  Start (sector)    End (sector)  Size       code  name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem
   2         2099200         3147775   512.0 MiB   8200  Linux swap

Command (? for help): q

もう一度 [gdisk -l] で確認すると、2 番パーティションはなく、1 番パーティションだけが残っていました。p の一覧に出たパーティションも、w で保存しないとディスクには書き込まれません。

gdisk -l disk.img
GPT fdisk (gdisk) version 1.0.7

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

Found valid GPT with protective MBR; using GPT.
Disk disk.img: 6291456 sectors, 3.0 GiB
Sector size (logical): 512 bytes
Disk identifier (GUID): 85C84C15-686C-4505-BE51-CF9025D04020
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 6291422
Partitions will be aligned on 2048-sector boundaries
Total free space is 4194237 sectors (2.0 GiB)

Number  Start (sector)    End (sector)  Size       code  name
   1            2048         2099199   1024.0 MiB  8300  Linux filesystem

別のディスクを指定してしまうミスは、作業の前に [gdisk -l] でサイズとパーティション一覧を先に確認すれば防げます。以下は 1 GiB のイメージ data.img に同じコマンドを実行した出力で、[Disk data.img: 2097152 sectors, 1024.0 MiB] の行と空のパーティション一覧で disk.img と区別できました。

gdisk -l data.img
GPT fdisk (gdisk) version 1.0.7

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

Creating new GPT entries in memory.
Disk data.img: 2097152 sectors, 1024.0 MiB
Sector size (logical): 512 bytes
Disk identifier (GUID): E8F9E93B-4299-41F3-8985-37973CBAEFFF
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 2097118
Partitions will be aligned on 2048-sector boundaries
Total free space is 2097085 sectors (1024.0 MiB)

Number  Start (sector)    End (sector)  Size       code  name

🔲 gdisk の出力の変化を確認

パーティションを作成または削除したあとは、p の出力でパーティション一覧の行数と [Total free space] の行が一緒に変わります。パーティションを 2 つ作った直後は [Total free space is 3145661 sectors (1.5 GiB)] で、512 MiB の 2 番パーティションを削除したあとは [Total free space is 4194237 sectors (2.0 GiB)] に増えました。

Code 欄には入力した種類コードがそのまま表示されました。Enter でデフォルト値を受け入れた 1 番パーティションは 8300、8200 を入力した 2 番パーティションは 8200 でした。

保存したかどうかは、gdisk の最初の画面の [Partition table scan] の下でも見分けられました。保存前の空のイメージでは [GPT: not present] で、w で保存したあとは [GPT: present] と [Found valid GPT with protective MBR; using GPT.] の行が表示されました。

🔢 FAQ & おすすめコンテンツ

q を入力すると、変更内容をディスクに書き込まずに gdisk を終了します。n や d で変更した内容は w を入力するまでメモリにだけあり、w を入力したときにディスクに書き込まれるので、間違えたときは q で終了してからやり直します。

w の出力の [Warning: The kernel is still using the old partition table.] の行のとおり、保存した直後もカーネルは以前のパーティションテーブルを使い続けます。新しいテーブルは、再起動するか partprobe または kpartx を実行したあとに反映されます。

gdisk はパーティションの開始位置を 2048 セクター単位でそろえます。p の出力の [Partitions will be aligned on 2048-sector boundaries] の行がこの設定です。そのため 34〜2047 セクターは空いており、パーティションを 3 つ作った disk.img の空き領域 2014 セクターがこの部分です。

ℹ️ 提携について
当サイトのコンテンツにはアフィリエイトリンクが含まれています。訪問者がこのリンクを通じて商品やサービスを購入すると、当サイトは販売元から手数料を受け取ります。この過程において、購入者が支払う金額(イベント割引適用で金額が下がります↓)は上昇しません。掲載されている価格・割引・在庫情報は作成時点のものであり、実際とは異なる場合がありますので、ご購入前に販売元にて最終確認をお願いいたします。商品の選定および評価は独自の基準に基づいて行われており、手数料の支払いの有無は紹介順や評価内容に影響を与えません。

コメントを残す

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

人間であることを証明してください: 4   +   5   =