HDDパーティショニング

パーティショニング

ページ作成日 2016/5/4
ページ更新日 2022/9/27

パーティション設計

Linuxのパーティション作成の基本

まず基本のパーティショニングは、ディスク領域を/(ルート)とスワップ用の2つに分けます。
スワップ用のパーティションは必ず用意します。

Chris Down氏の「スワップの弁護:よくある誤解を解く」から要約を引用します。

/以降を全て1つのパーティションにする

昨今は、/を1つのパーティションに割り当ててしまい、ディレクトリ単位でパーティションをマッピングするというのは、やらなくなりました。
もちろん、/homeを別パーティションにするのはアリです。

gdisk

gdiskとは

UEFI用のパーティショニングでは、gdiskを使います。 gdiskとは、GPT fdiskの略です。

GTPは、従来のMBRに代わる、ハードディスク上のパーティションテーブルの配置に関する標準規格です。

従来、MBRパーティションでは、32bitでセクタ情報を管理するため、1セクタ512Byteで定義すると、512Byte×2^32=2TiBまでしか管理できませんでした。
セクタ情報の管理を64bit化することで、8ZiBまで拡張できるようにしたのが、GUID Partion tableです。
GTPにすると、以下の利点があります。

  1. 容量が2TiB以上のディスクを取り扱える。
  2. パーティションが128個作成できる。MBRの時のプライマリパーティション4個の制限から解放される。プライマリパーティション、拡張パーティションという枠組みから解放される。
  3. プライマリGPTの内容に不整合が生じた時のために、最終論理セクターにバックアップGPTが作成される。チェックサムエラーが生じた際は、バックアップからGPTをコピーしてくれる。

gdiskのコマンド

gdiskを起動します。
gdiskは必ず引数でデバイスファイル名が必要です。
デバイスファイル名を入れないと、以下のように表示されます。


root@slackware:/# gdisk
GPT fdisk (gdisk) version 1.0.8

Type device filename, or press <Enter> to exit: _

Enterキーを押すことで、終了できます。

「インストールの前に」で、事前に自分のPCのストレージがどの種別か、BIOSで確認しましょうと説明しました。
もし、M.2のSSDを使っているなら、gdisk /dev/nvme0n1と入力してみましょう。
古い端末のSSDなら、gdisk /dev/sdaと入力してみましょう。


root@slackware:/# gdisk /dev/nvme0n1
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.

Command (? for help): _

次にhelpを表示させてみましょう。?を入力します。


b           backup 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 talbe
q           quit without saving changes
r           recovery and transformation option (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

Command (? for help): _

自分が正しいデバイスファイルを指定したか、確認しましょう。
pを入力すると、M.2 SSDだと、以下のような出力が表示されます。


Command (? for help): p
Disk /dev/nvme0n1: 1953525168 sectors, 931.5 GiB
Model: Sabrent Rocket 4.0 1TB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 745D34CB-D312-4EFE-AA80-4501A5531CDD
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 1953525134
Partitions will be aligned on 2048-sector boundaries
Total free space is 1953525134 sectors (931.5 GiB)

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

Command (? for help): _

以前、私が使っていたSSDだと、こんな感じで表示されます。


Command (? for help): p
Disk /dev/sda: 1000215216 sectors, 476.9 GiB
Model: INTEL SSDSC2KW51
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 2152F182-D7FA-4B4E-8250-309151B25A07
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 1000215182
Partitions will be aligned on 2048-sector boundaries
Total free space is 1000215149 sectors (476.9 GiB)

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

Command (? for help): _

新しいGPTを作成する

まず最初にGUIDのパーティションテーブルを作成します。
この領域に、これから作成するパーティション情報が保存されます。
oを入力して、Enterキーを押します。


Command (? for help): o
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): Y

「全てのパーティションを削除し、新しい保護用MBRを作成しますか?」と質問されるので、Yを入力し、全てのパーティションを削除し、新しい保護用MBRを作成します。

EFIシステムパーティション

まず最初にEFIシステムパーティションを100MBで作成します。


Command (? for help): n <- nを入力して、Enterキーを押す
Partition number (1-128, default 1): <- 何も入力せず、Enterキーを押す
First sector (34-1953525134, default = 2048) or {+-}size{KMGTP}: <- 何も入力せず、Enterキーを押す
Last sector (2048-1953525134, default = 1953525134) or {+-}size{KMGTP}: +100M <- 100MBをEFIシステム領域として確保
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): EF00 <- EF00を入力して、Enterキーを押す
Change type of partition to 'EFI System'

Command (? for help): _

スワップ用パーティションの作成

スワップ領域は必ず作成します。
物理メモリの倍ぐらいで作りましょうというのが昔からの目安でしたが、現在は、もう少し計算式が複雑です。

"How Much Swap Should You Use in Linux?"では、RedHatやCentOSの推奨計算式を紹介しつつ、以下のようにまとめています。

物理メモリとOSの用途によるスワップサイズの目安
RAMの容量スワップのサイズ(ハイバネーションを使わないサーバ用途の場合)スワップのサイズ(ハイバネーションを使うデスクトップ用途の場合)
256MB256MB512MB
512MB512MB1GB
1GB1GB2GB
2GB1GB3GB
3GB2GB5GB
4GB2GB6GB
6GB2GB8GB
8GB3GB11GB
12GB3GB15GB
16GB4GB20GB
24GB5GB29GB
32GB6GB38GB
64GB8GB72GB
128GB11GB139GB

以下の例では、物理メモリ64GBのPCをデスクトップ用途で使うために、72GBのディスク領域をパーティションで切り、スワップ領域として作成しています。


Command (? for help): n <- nを入力して、Enterキーを押す
Partition number (2-128, default 2): <- 何も入力せず、Enterキーを押す
First sector (34-1953525134, default = 206848) or {+-}size{KMGTP}: <- 何も入力せず、Enterキーを押す
Last sector (206848-1953525134, default = 1953525134) or {+-}size{KMGTP}: +72G <- この例では72GBをスワップ領域として確保
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8200 <- 8200を入力して、Enterキーを押す
Change type of partition to 'Linux swap'

Command (? for help): _

スワップ領域用パーティションは、8200というコードになります。

/(ルート)用パーティションの作成

次に/(ルート)の領域を確保します。
30GBあれば十分ですが、余裕があるのであれば、100GBぐらいまで増やしても良いでしょう。
/varの領域は、ログを記録に、/tmpは一時的なファイルの書き込みやパッケージ作成などに使うので、ある程度余裕があった方が良いです。

以下の例では、残りの全領域をルート用パーティションとして作成しています。


Command (? for help): n <- nを入力して、Enterキーを押す
Partition number (3-128, default 3): <- 何も入力せず、Enterキーを押す
First sector (34-1953525134, default = 151201792) or {+-}size{KMGTP}: <- 何も入力せず、Enterキーを押す
Last sector (151201792-1953525134, default = 1953525134) or {+-}size{KMGTP}: <- 何も入力せず、Enterキーを押す
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): <- 何も入力せず、Enterキーを押す
Change type of partition to 'Linux filesystems'

Command (? for help): _

作成したパーティションの確認と確定

最後にpを押して、作ったパーティションを確認します。


Command (? for help): p
Disk /dev/nvme0n1: 1953525134 sectors, 931.5 GiB
Model: Sabrent Rocket 4.0 1TB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 745D34CB-D312-4EFE-AA80-4501A5531CDD
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 1953525134
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          206847   100.0 MiB   EF00  EFI system partition
   2          206848       151201791   72.0 GiB    8200  Linux swap
   3       151201792      1953525134   859.4 GiB   8300  Linux filesystem

Command (? for help): 

これでパーティションを確定させます。
wを入力して、パーティションテーブルをディスクに書き込み、gdiskを終了させます。


Command (? for help): w

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

Do you want to proceed? (Y/N): Y <- Yを入力して、Enterキーを押す
OK; writing new GUID partition table (GPT) to /dev/nvme0n1.
The operation has completed successfully.
root@slackware:/# _

複数台の記憶装置を使う場合

もしも、複数台のSSDやHDD、M.2 SSDを搭載したPCを使い、別のデバイスに/homeや/varなどを割り当てるのであれば、gdisk /dev/sdbのように入力して、同じような作業を行います。
2台目以降はブート用デバイスではなく、データ保存専用デバイスとなるため、EFIシステムパーティションを作る必要はありません。

次は、パッケージの選択とインストールに進みます。