こちらの手順が最新となります。
GCE環境でのVM初期構築時 /パーティションサイズ拡張手順
以下、旧手順となります。
概要
GCE環境にVM構築する際に/パーティションのサイズは10GB固定となっているため、作成時に拡張する手順を以下に記す。
公式手順は下記リンクであり、GoogleCloudSDKのgcloudコマンドでの手順となっている。
https://developers.google.com/compute/docs/disks?hl=ja#repartitionrootpd
今回記載した手順はGoogleCloudSDKのgcutilコマンドを使用した手順である。
(個人的にはgcutilコマンドの方がzoneを毎回指定する必要もなくレスポンスも早いように思え使い勝手がいいと思っているのだが、公式はgcloudコマンドを推奨している模様。作業の結果はどちらのコマンドでも同じとなる。)
手順
事前準備
- GoogleDevelopersConsoleでのプロジェクトの作成
- GoogleCloudSDKのインストールおよび認証設定(Installation and Quick Start参照)
また、必須ではないがgcutil ssh実行時に毎回プロジェクト名を入力する手間を省く為
% gcloud config set project <your project>
を実行しておく。accountやprojectなど設定した内容を確認するには
% gcloud config list
で確認が出来る。
Diskの作成
拡張したいサイズでDiskを作成し、そのタイミングでimageを選択しOSのインストールもあわせて実施する。
下記は50GB、backports-debian-7-wheezyのOSをinstallしている。
imageは適宜利用したいOS、バージョンを指定する事。
ディスク名はインスタンス名と同じにしておくと管理がしやすいので次の手順で作成するVMインスタンス名と同じにしておく。
% gcutil adddisk <ディスク(VMインスタンス)名> \
--size_gb="50" \
--zone="asia-east1-a" \
--disk_type="pd-standard" \
--source_image="https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/backports-debian-7-wheezy-v20140814"
VMインスタンスの作成
前述の『Diskの作成』で作成したディスクを用いてVMインスタンスを作成する。
zone,machine_network,tagsは適宜作成する用途に準じて設定すること。
下記はdefaultのNetwork環境に外部からhttp,https通信が可能なVMインスタンス構築を実施している。
% gcutil addinstance <VMインスタンス名> \
--zone="asia-east1-a" \
--machine_type="n1-standard-1" \
--network="default" \
--external_ip_address="ephemeral" \
--tags="http-server,https-server" \
--disk="<VMインスタンス名>,deviceName=<ディスク(VMインスタンス)名>,mode=READ_WRITE,boot" \
--auto_delete_boot_disk="false"
作成したVMインスタンスにSSHでログイン
% gcutil ssh <VMインスタンス名>
ディスクパーティションの確認
$ df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 9.8G 477M 8.8G 6% /
udev 10M 0 10M 0% /dev
tmpfs 372M 100K 372M 1% /run
/dev/disk/by-uuid/2795e34f-4e9b-46a9-9e39-e6cc0f1f1478 9.8G 477M 8.8G 6% /
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 743M 0 743M 0% /run/shm
fdiskの実行
$ sudo fdisk /dev/sda
※CentOSは、ディスクのDOS-compatibilityを無効にし、unitではなくsectorで表示させるよう切り替える。
$ sudo fdisk /dev/sda
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
警告: DOS互換モードは廃止予定です。このモード (コマンド 'c') を止めることを
強く推奨します。 and change display units to
sectors (command 'u').
コマンド (m でヘルプ): c
DOS互換フラグは設定されていません
コマンド (m でヘルプ): u
セクタ数 の表示/項目ユニットを変更します
パーティションの確認
今回の環境では『プライマリパーティションの1番目に4096番〜20971519番のセクタが割当て』られている事が確認できる。
ここでstartに記されている値(この場合4096)が、後のパーティション再作成時にも必要なので覚えておく。
可能であれば実行結果をコピーしておく事。
Command (m for help): p
Disk /dev/sda: 53.7 GB, 53687091200 bytes
4 heads, 32 sectors/track, 819200 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000ea150
Device Boot Start End Blocks Id System
/dev/sda1 * 4096 20971519 10483712 83 Linux
パーティションの削除
Command (m for help): d
Selected partition 1
パーティションが削除されている事を確認
パーティションの確認手順で出力されていた
/dev/sda1 * 4096 20971519 10483712 83 Linux
が出力されていない事を確認する。
Command (m for help): p
Disk /dev/sda: 53.7 GB, 53687091200 bytes
4 heads, 32 sectors/track, 819200 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000ea150
Device Boot Start End Blocks Id System
パーティションの再作成
『プライマリパーティションの1番目に4096番〜最大(104857599)のセクタが割当て』るよう設定する。
作成前は『プライマリパーティションの1番目に4096番〜20971519番のセクタが割当て』てあったので
約5倍の領域が割り当てられることが予想でき、10GB->50GBへ拡張できていることが予想できる。
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048): 4096
Last sector, +sectors or +size{K,M,G} (4096-104857599, default 104857599):
Using default value 104857599
設定確認
『プライマリパーティションの1番目に4096番〜104857599番のセクタが割当て』てあることを確認する。
Command (m for help): p
Disk /dev/sda: 53.7 GB, 53687091200 bytes
4 heads, 32 sectors/track, 819200 cylinders, total 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000ea150
Device Boot Start End Blocks Id System
/dev/sda1 4096 104857599 52426752 83 Linux
設定の保存
保存後、fdiskからは自動的に抜けます。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
OS再起動
実行後、VMインスタンスからは自動的にログアウトします。
$ sudo reboot
SSHで再度ログイン(再起動後、SSHが起動する1-2分待ってから)
fdiskの設定に失敗するとOSが起動しない為、SSH接続が行えません。
GCEは現在(2014/8/25時点)SSH以外でコンソールを取る手段がないのでその場合は残念ですがVMを破棄し、手順を1からやり直しましょう。
% gcutil ssh <VMインスタンス名>
※手順をやり直す場合(VMインスタンスとディスクの削除)
% gcutil deleteinstance <VMインスタンス名> --force --delete_boot_pd
現在のディスク状況を確認
まだサイズ拡張は認識されていない。
$ df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 9.8G 477M 8.8G 6% /
udev 10M 0 10M 0% /dev
tmpfs 372M 100K 372M 1% /run
/dev/disk/by-uuid/2795e34f-4e9b-46a9-9e39-e6cc0f1f1478 9.8G 477M 8.8G 6% /
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 743M 0 743M 0% /run/shm
resize2fsの実行
OSにパーティション拡張を認識させる。
$ sudo resize2fs /dev/sda1
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 4
The filesystem on /dev/sda1 is now 13106688 blocks long.
ディスク状況確認
rootfsのSizeが10G->拡張したサイズ(この場合50G)に拡張されていることを確認できればOK。
お疲れさまでした。
$ df -h
Filesystem Size Used Avail Use% Mounted on
rootfs 50G 487M 47G 2% /
udev 10M 0 10M 0% /dev
tmpfs 372M 100K 372M 1% /run
/dev/disk/by-uuid/2795e34f-4e9b-46a9-9e39-e6cc0f1f1478 50G 487M 47G 2% /
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 743M 0 743M 0% /run/shm