EC2のディスクサイズを拡張するのにCentOSだとfdiskがいる話

EC2のEBSボリュームを拡張しようと思ったらマニュアル通りにはいかなかった。
というかCentOSでは1Step必要だった。

AWSでの操作は[公式のマニュアル](http://docs.aws.amazon.com/ja_jp/AWSEC2/latest/UserGuide/ebs-expand-volume.html)通りなので割愛。

流れは↓
 
* 既存Volume の snapshot 作成
 
* 新規Volume作成
 
* 旧Volume Detach

* 新規VolumeのAtach

***

公式では起動後、以下を実行と書いてあるがCentOSではこれだけではディスク拡張はされない。
`resize2fs /dev/DEVICE`

fdiskでパーティションを切り直してあげる。

* 確認

```
root@ec2:~ # df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvdx1       40G   23G   16G  60% /
root@ec2:~ # 
root@ec2:~ # fdisk -ul

Disk /dev/xvdx: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00098461

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdx1            2048    83886079    41942016   83  Linux
root@ec2:~ # 

```

* 変更

```
root@ec2:~ # fdisk -u /dev/xvdx
Command (m for help): p [現在のパーティション表示]

Disk /dev/xvdx: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00098461

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdx1            2048    83886079    41942016   83  Linux

Command (m for help): d [削除]
Selected partition 1

Command (m for help): p [現在のパーティション表示]

Disk /dev/xvdx: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00098461

    Device Boot      Start         End      Blocks   Id  System

Command (m for help): n  [新規パーティション作成]
Command action
   e   extended
   p   primary partition (1-4)
p    [primaryを選択]
Partition number (1-4): 1
First sector (63-167772159, default 63): 2048 [確認時に見たものと同じ数値]
Last sector, +sectors or +size{K,M,G} (2048-167772159, default 167772159): [何もせずにEnter]
Using default value 167772159

Command (m for help): p [現在のパーティション表示]

Disk /dev/xvdx: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 cylinders, total 167772160 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00098461

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdx1            2048   167772159    83885056   83  Linux

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.
root@ec2:~ #
root@ec2:~ # reboot [再起動で反映]
```

* ファイルシステムのリサイズ

```
root@ec2:~ # resize2fs /dev/xvdx1
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/xvdx1 is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 5
Performing an on-line resize of /dev/xvdx1 to 20971264 (4k) blocks.
The filesystem on /dev/xvdx1 is now 20971264 blocks long.

root@ec2:~ # df
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvdx1       79G   23G   53G  30% /  増えた
root@ec2:~ #
```