Rocky Linux 9

さくらのVPSで提供中の標準OS「Rocky Linux 9(x86_64)」のディスク拡張に関するページです。

本手順書の対象は標準OSの Rocky Linux 9 です。カスタムOSにてインストールされた Rocky Linux 9 は対象外となります。
仮想ディスクパーティション構成を初期提供時から変更している場合は、本手順書の対象外となります。

本手順書で想定している仮想サーバーのスペックは以下の通りです。実際のスペックは、お客様の実環境に合わせてお読み替えください。

プラン

仮想CPU

仮想メモリ容量

仮想ディスク容量

スケールアップ前

SSD 4G

4CPU

4096MB

200GiB

スケールアップ後

SSD 8G

6CPU

8192MB

400GiB

スケールアップを実施する

スケールアップは「コントロールパネル」からおこないます。
手順の詳細については、 スケールアップ をご覧ください。

サーバーの起動とログイン

スケールアップ完了後、VPSコントロールパネルの「電源操作」から「起動する」を押して仮想サーバーを起動します。

サーバの起動方法

VNCコンソールまたはSSH経由で仮想サーバーへログインし、rootユーザーに切り替えます。

スケールアップ後のディスク空き容量を利用する為には、次の方法が有ります。 お客様の環境及びご利用方法に合わせて、選択ください。

新しいパーティションを追加する

スケールアップで拡張されたディスクの空き領域に、新しいパーティションを作成し、マウントポイント /data にマウントします。

ディスクの状態を確認

現在のディスクの状況を確認します。

# gdisk -l /dev/vda
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 /dev/vda: 838860800 sectors, 400.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5D60C06B-A5B5-4E19-9988-38B44B8E1655
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 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 4413 sectors (2.2 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            4095   1024.0 KiB  EF02  BIOS
   2            4096       419427967   200.0 GiB   8300  Linux

gdisk コマンドの結果から「/dev/vda」のサイズは 400GB に拡張されていることは確認できますが、OS 側からの認識状況では2つのパーティションとして認識している容量の合計は 200GB です。

# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1 1024M  0 rom
vda    253:0    0  400G  0 disk
├─vda1 253:1    0    1M  0 part
└─vda2 253:2    0  200G  0 part /

また、lsblk コマンドの結果から、標準OSでインストールした際に作成された2つのパーティション「vda1」「vda2」が確認できます。

パーティションをソートする

gdisk コマンドの実行結果から、Total free space (合計空き容量)が正しく認識されていないことが分かります。
「4413 sectors (2.2 MiB)」が表示されていることから空き容量が 2MB 程度ですが、本来は 200GB あります。
これを整理(ソート)して正しい情報にするには、以下のコマンドを実行します。
# sgdisk -s /dev/vda
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 コマンドでディスクの状況を確認します。

# gdisk -l /dev/vda
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.
[root@tk2-201-10043 ~]# gdisk -l /dev/vda
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 /dev/vda: 838860800 sectors, 400.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5D60C06B-A5B5-4E19-9988-38B44B8E1655
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 838860766
Partitions will be aligned on 2048-sector boundaries
Total free space is 419434813 sectors (200.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            4095   1024.0 KiB  EF02  BIOS
   2            4096       419427967   200.0 GiB   8300  Linux

「Total free space is 419434813 sectors (200.0 GiB)」と表示されていることから、空き容量を正常に認識しました。

新しいパーティションを作成する

gdisk コマンドで新しいディスクパーティションを作成します。

# gdisk /dev/vda
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.

*[ n ] コマンドを入力します。新しいパーティションを作るという意味です。*
Command (? for help): n

*Partition number はデフォルトの [ 3 ] にしますので、そのまま [ Enter ] キーを押します。*
Partition number (3-128, default 3):

*Fisrt sector/Last sector もデフォルトの数値にするので、そのまま [ Enter ] キーを押します。*
First sector (34-838860766, default = 419428352) or {+-}size{KMGTP}:
Last sector (419428352-838860766, default = 838860766) or {+-}size{KMGTP}:

*Partition Type もデフォルトの [ Linux filesystem ] で問題がなければ、そのまま [ Enter ] キーを押します。*
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

*作成完了したので、 [ p ] コマンドでパーティションテーブルの確認をします。*
Command (? for help): p
Disk /dev/vda: 838860800 sectors, 400.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5D60C06B-A5B5-4E19-9988-38B44B8E1655
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 838860766
Partitions will be aligned on 2048-sector boundaries
Total free space is 2398 sectors (1.2 MiB)

*表示されている新しいパーティションテーブルにサイズなどの問題がないことを確認します。*
Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            4095   1024.0 KiB  EF02  BIOS
   2            4096       419427967   200.0 GiB   8300  Linux
   3       419428352       838860766   200.0 GiB   8300  Linux filesystem

*問題がなければ [ w ] コマンドを入力します。*
Command (? for help): w

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

*もう一度確認をして問題がなければ [ Y ]を入力して、 [ Enter ] キーを押します。*
*このコマンドを実行すると、ディスクの書き換えが発生するので、必ず確認した上で実行しましょう。*
Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/vda.
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.

ファイルシステムを作る

新規作成したパーティションを利用するには、フォーマットしてからマウントをする必要があります。
以降の例ではファイルシステムを ext4 形式でフォーマットし、 /data にマウントする手順を説明します。

新しいディスクパーティションがOS上にデバイスとして認識されているか確認します。
# ls /dev/vda3

ls: cannot access /dev/vda3: No such file or directory

新しく作成した vda3 がデバイスとして認識されていない場合は、一度OSを再起動します。

# reboot

ヒント

再起動が困難な場合は、以下のコマンドで強制的に認識させることも可能です。

# partx -a /dev/vda

vda3 がOSに認識されたら、 mkfs.ext4 コマンドでフォーマットを実行します。

# mkfs.ext4 /dev/vda3
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 52428800 4k blocks and 13132800 inodes
Filesystem UUID: 68bf186d-5206-4e64-823c-14fe299a4c31
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

新しいパーティションをマウントする

初期化した領域を使うため、マウントポイントを作成します。

# mkdir /data

マウントポイントの作成後、 /dev/vda3 を /data にマウントします。

# mount /dev/vda3 /data

正常にマウントがおこなえたか確認します。

# mount | grep vda3
/dev/vda3 on /data type ext4 (rw,relatime)
# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1 1024M  0 rom
vda    253:0    0  400G  0 disk
├─vda1 253:1    0    1M  0 part
├─vda2 253:2    0  200G  0 part /
└─vda3 253:3    0  200G  0 part /data
200GBのサイズで /data にマウントされていることが確認できます。
最後にOSを再起動しても自動マウントされるように、 /etc/fstab に設定を追加します。
# id=$(blkid -o value -s UUID /dev/vda3)
# echo "UUID=${id} /data ext4 defaults 0 2" >> /etc/fstab

サーバーの再起動

拡張したディスクが正しく動作するか確認するために、サーバーを再起動します。

# reboot

サーバーを再起動した後に、 df コマンドなどで作成した領域がマウントされていることを確認できれば作業は完了です。

ルートパーティションを拡張する

スケールアップで拡張されたディスクの空き領域を利用して、既存の / パーティションを拡張します。 / パーティションの後ろに拡張の為の空き容量が存在する必要が有ります。

注意

記載の内容は、ご利用のOSやバージョンなどによっては動作しない、または動作が異なる場合があり、弊社が公式にサポートする内容ではございません。 ファイルシステムの操作のため、作業に失敗するとデータが破損する場合があります。 作業前に必ずバックアップを取得してください。

ディスクの状態を確認

現在のディスクの状況を確認します。

# gdisk -l /dev/vda
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 /dev/vda: 838860800 sectors, 400.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5D60C06B-A5B5-4E19-9988-38B44B8E1655
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 419430366
Partitions will be aligned on 2048-sector boundaries
Total free space is 4413 sectors (2.2 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048            4095   1024.0 KiB  EF02  BIOS
   2            4096       419427967   200.0 GiB   8300  Linux
# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2       200G  3.8G  187G   2% /

gdisk コマンドの結果から /dev/vda のサイズは 400GB に拡張されていることが確認できます。 しかし、/パーティションが存在する /dev/vda2 は 約200GB のままで有る事が確認できます。

パーティションを拡張する

growpart コマンドを利用するとパーティションを拡張できます。 次のコマンドでインストールが可能です。

# dnf install cloud-utils-growpart

growpart コマンドには、対象ブロックデバイスと、gdiskコマンドで確認したパーティション番号を順に指定します。 今回の例では、 /dev/vda の パーティション 2番ですので、次のように実行します。

# growpart /dev/vda 2

実行後、再び gdisk コマンドを実行し、パーティションサイズが正常に拡張されている事を確認します。

# gdisk -l /dev/vda
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 /dev/vda: 838860800 sectors, 400.0 GiB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 5D60C06B-A5B5-4E19-9988-38B44B8E1655
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 838860766
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            4095   1024.0 KiB  EF02  BIOS
   2            4096       838860766   400.0 GiB   8300  Linux

ファイルシステムをリサイズする

パーティションの拡張後、ファイルシステムをリサイズしなければ実際に利用できないので、 / パーティションをリサイズします。

# lsblk -f
NAME   FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sr0
vda
├─vda1
└─vda2 ext4   1.0         cd2e5791-18fa-40b3-a4ba-11b4da284c7c    186G     2% /

lsblk コマンドに -f オプションを用いて、/ パーティションで利用しているファイルシステムを確認します。 今回の例では、 ext4 ですので、resize2fs コマンドを用いてファイルシステムをリサイズします。 resize2fs には、Linuxにより認識されているブロックデバイスを引数として渡します。

# resize2fs /dev/vda2

最後に、正常に拡張されたか df コマンド等を用いて確認します。

# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2       394G  780M  375G   1% /