3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Oracle Cloud] 仮想インスタンスのRootファイルシステムを自動拡張

Last updated at Posted at 2019-08-15

はじめに

OCI で作成する仮想インスタンスのRootファイルシステムを拡張するために、cloud-init を利用すると便利に拡張を行うことが可能です。
これは、OCIに限らず、AWS, Azure, GCP, Alibaba, OpenStack といったクラウド環境で標準的に利用可能なものになっています。

各クラウドによって、細かな方法が違うためご注意ください。今回の記事では、OCI上での設定方法をご紹介します。
今回の記事では、CentOS7 をサンプルにして記載します。

下の2個のケースを想定しています。

  • 新規インスタンスを作成する場合
  • 既存インスタンスを拡張したい場合

cloud-init について

cloud-init とは、クラウドのインスタンスを初期化や再起動をしたときに、必要となる設定を行うためのソフトウェアです。
例えば、新規作成時に自動的に仮想インスタンス名を使用して hostname を設定したり、任意の shell script を実行することが可能です。

Rootファイルシステムの拡張についても、cloud-init で自動実行が可能となっています。
cloud-init で Root ファイルシステムを拡張するときには、growpart という module を有効にする必要があります。

具体的には、以下の設定値を変更すると growpart を有効にすることが出来、インスタンスの構築時や再起動時に自動的にRootファイルシステムを拡張することが可能です。

vim /etc/cloud/cloud.cfg
# The growpart module is disabled by default. To enable automatic boot volume resizing, uncomment 
# the below entry for '- growpart' and reboot. All the dependent packages for the growpart
# module to work such as cloud-utils-growpart and gdisk are already included in the image.
# - growpart   <============== ここのコメントアウトを外すと良い

上記の growpart は、Public Cloud はデフォルトで有効になっていることが多いですが、OCI はデフォルトで無効になっているため、有効にする必要があります。

新規インスタンスの場合

OCI で新規にインスタンスを構成する際に、CentOS 7 を選択します。

1565861861452.png

Default では、46.6 GB となっていますが、Custom boot volume sizeを200GBに変更します

1565861947963.png

cloud-init を使用して、以下の作業を自動設定をします

  • cloud-init用の設定ファイルをsedで書き換えて、growpart を有効
  • 必要なパッケージをインストール
  • インスタンスを再起動

1565870508402.png

コピペ用

# !/bin/bash
sudo sed -i -e 's/#- growpart/ - growpart/g' /etc/cloud/cloud.cfg
sudo yum install -y gdisk cloud-utils-growpart
sudo shutdown -r now

作成完了後、SSHログインすると、以下のように Root ファイルシステムが拡張されていることが確認できます

[root@rootextend-4501 ~]# df -hT 
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sda3      xfs       192G  1.7G  190G   1% / <===================== ここが拡張されている
devtmpfs       devtmpfs  7.3G     0  7.3G   0% /dev
tmpfs          tmpfs     7.3G     0  7.3G   0% /dev/shm
tmpfs          tmpfs     7.3G  8.6M  7.3G   1% /run
tmpfs          tmpfs     7.3G     0  7.3G   0% /sys/fs/cgroup
/dev/sda1      vfat      512M   12M  501M   3% /boot/efi
tmpfs          tmpfs     1.5G     0  1.5G   0% /run/user/1000

既存インスタンスの場合

既存のRootファイルシステムを拡張したい場合は、以下の手順が必要です。

  • cloud-init の設定変更して、growpart を有効化
  • 必要なパッケージのインストール
  • Bootボリュームのデタッチ
  • Bootボリュームの拡張
  • 拡張後のBootボリュームを使用してインスタンスを起動

手動で cloud-init の設定ファイルを変更し、growpart を有効にします。

vim /etc/cloud/cloud.cfg
# The growpart module is disabled by default. To enable automatic boot volume resizing, uncomment 
# the below entry for '- growpart' and reboot. All the dependent packages for the growpart
# module to work such as cloud-utils-growpart and gdisk are already included in the image.
# - growpart

# The growpart module is disabled by default. To enable automatic boot volume resizing, uncomment 
# the below entry for '- growpart' and reboot. All the dependent packages for the growpart
# module to work such as cloud-utils-growpart and gdisk are already included in the image.
 - growpart

必要なパッケージを導入します。cloud-init の設定ファイルのコメントには、すでに included していると記載がありますが、自分の環境では正しく動作しませんでした。

sudo yum install -y gdisk cloud-utils-growpart

インストール完了後、Boot ボリュームを拡張するために、一度 Compute Instance を Stop します。

1575508589482.png

Boot Volume を拡張するために、インスタンスの詳細画面へ移動します

1575508650296.png

Boot Volume メニューから、Detach を選択します

1575508714820.png

一度 Compute Instance の一覧画面まで戻り、そこに表示されている Boot Volume メニューに移動し、Resizeを行います。

1575508861619.png

47GB から、300GBへ Resize をします。

1575508888446.png

Provisioning となって、数秒後にAvailable となります。

1575508913407.png

Compute Instance の詳細画面へ移動し、Detach した Boot Volume を Attach します

1575508977588.png

Attach が完了したら、Compute Instance を起動します

1575509072011.png

起動完了後、SSHログインしてファイルシステムを確認すると、拡張されていることが確認できます

[opc@tokyo01 ~]$ df -hT 
Filesystem     Type      Size  Used Avail Use% Mounted on 
devtmpfs       devtmpfs  7.3G     0  7.3G   0% /dev
tmpfs          tmpfs     7.3G     0  7.3G   0% /dev/shm
tmpfs          tmpfs     7.3G  8.6M  7.3G   1% /run
tmpfs          tmpfs     7.3G     0  7.3G   0% /sys/fs/cgroup
/dev/sda3      xfs       292G  1.9G  290G   1% / <========= 拡張されている
/dev/sda1      vfat      512M   12M  501M   3% /boot/efi
tmpfs          tmpfs     1.5G     0  1.5G   0% /run/user/1000

参考URL

Using cloud-init to extend the Root Partition on a new Linux Instance on OCI
https://medium.com/oracledevs/automated-process-to-extend-the-root-partition-on-a-new-linux-instance-using-cloud-init-on-oci-21c75a8e552a

OCI Computeでディスクの空き容量が足りなくなったときの追加方法
https://qiita.com/yamada-hakase/items/13885b7664afa7383df0
https://qiita.com/yamada-hakase/items/3f2fb3d0e67b2753ab0e

Service Request の回答
https://support.oracle.com/knowledge/Oracle%20Cloud/2438212_1.html

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?