
EC2でOS別EBSストレージを拡張する方法まとめ
この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
はじめに
くどうです。
EC2で容量を拡張したいときなど、パーティションをリサイズすることがあると思います。
 
OS別に拡張方法をまとめておきたいと思います。
Amazon Linux AMI 2014.03.1
Amazon Linux AMI の場合は最初に設定したストレージ容量が起動時に反映されるためresize2fsの必要はありません。
 
さらに、スナップショットからボリュームサイズを拡張して作成した場合でもresize2fsにいよる拡張が必要はありません。
 
例20GB→30GBへの拡張
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       30G  1.1G   29G   4% /
devtmpfs        282M   12K  282M   1% /dev
tmpfs           297M     0  297M   0% /dev/shm
[/Bash]
[Bash]
Disk /dev/xvda1: 32.2 GB, 32212254720 bytes, 62914560 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
[/Bash]
なぜ勝手に拡張されるかというとCloud-initが勝手にやってます。
 
[Bash]
May 12 16:08:34 cloud-init[1094]: helpers.py[DEBUG]: Running config-resizefs using lock (
Red Hat Enterprise Linux 6.5 (PV)
Red Hat Enterprise Linux 6.5 (PV) の場合は最初に設定したストレージ容量が起動時に反映されるためresize2fsの必要はありません。
 
さらに、スナップショットからボリュームサイズを拡張して作成した場合でもresize2fsにいよる拡張が必要はありません。
 
例20GB→30GBへの拡張
 
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       30G  2.0G   27G   7% /
tmpfs           296M     0  296M   0% /dev/shm
[/Bash]
[Bash]
Disk /dev/xvda1: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[/Bash]
Amazon Linux AMIと同様にCloud-initが勝手にやってます。
 
[Bash]
May 12 11:57:28 ip-172-31-6-75 [CLOUDINIT] cc_resizefs.py[DEBUG]: Resizing took 12.619 seconds
May 12 11:57:28 ip-172-31-6-75 [CLOUDINIT] cc_resizefs.py[DEBUG]: Resized root filesystem (type=ext4, val=True)
[/Bash]
SuSE Linux Enterprise Server 11 sp3 (PV)
SuSE Linux Enterprise Server 11 sp3 (PV) の場合は最初に設定した拡張したストレージ容量は起動時に反映されません。resize2fsの必要があります。
 
例20GBに拡張して起動
 
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
rootfs          9.9G  1.2G  8.2G  13% /
udev            315M   48K  315M   1% /dev
tmpfs           315M     0  315M   0% /dev/shm
/dev/sda1       9.9G  1.2G  8.2G  13% /
[/Bash]
[Bash]
Disk /dev/sda1: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x00000000
Disk /dev/sda1 doesn't contain a valid partition table
[/Bash]
resize2fsを行ってみましょう。
[Bash]
# resize2fs /dev/sda1
resize2fs 1.41.9 (22-Aug-2009)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/sda1 to 7864320 (4k) blocks.
The filesystem on /dev/sda1 is now 7864320 blocks long.
[/Bash]
resize2fsの後の容量を確認してみましょう。
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
rootfs           30G  1.2G   27G   5% /
udev            315M   48K  315M   1% /dev
tmpfs           315M     0  315M   0% /dev/shm
/dev/sda1        30G  1.2G   27G   5% /
[/Bash]
Ubuntu Server 14.04 LTS (PV)
Ubuntu Server 14.04 LTS (PV) の場合は最初に設定したストレージ容量が起動時に反映されるためresize2fsの必要はありません。
 
例20GB→30GBへの拡張
 
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       30G  764M   28G   3% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            284M   12K  284M   1% /dev
tmpfs            59M  184K   59M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            295M     0  295M   0% /run/shm
none            100M     0  100M   0% /run/user
[/Bash]
[Bash]
Disk /dev/xvda1: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x00000000
Disk /dev/xvda1 doesn't contain a valid partition table
[/Bash]
Amazon Linux AMIと同様にCloud-initが勝手にやってます。
 
[Bash]
2014-05-12 16:56:16,923 - cc_resizefs.py[DEBUG]: Resizing / (ext4) using resize2fs /dev/disk/by-label/cloudimg-rootfs
2014-05-12 16:56:16,923 - util.py[DEBUG]: Running command ('resize2fs', '/dev/disk/by-label/cloudimg-rootfs') with allowed return codes [0] (shell=False, capture=True)
2014-05-12 16:56:17,877 - util.py[DEBUG]: Resizing took 0.954 seconds
2014-05-12 16:56:17,878 - cc_resizefs.py[DEBUG]: Resized root filesystem (type=ext4, val=True)
[/Bash]
Microsoft Windows Server 2012 Base
Microsoft Windows Server 2012 Base の場合は最初に設定したストレージ容量が起動時に反映されるため設定の必要はありません。
 
ただし、EBSの容量を拡張した場合にはDisk Managementで容量を拡張する必要があります。
 
例50GB→70GBへの拡張 
Microsoft Windows Server 2008 R2 Base
Microsoft Windows Server 2008 R2 Base の場合は最初に設定したストレージ容量が起動時に反映されるため設定の必要はありません。
 
ただし、EBSの容量を拡張した場合にはDisk Managementで容量を拡張する必要があります。
 
例50GB→70GBへの拡張 
Microsoft Windows Server 2008 R2 Baseについては、ほぼ同じなので割愛します。
Microsoft Windows Server 2003 R2 Base
Microsoft Windows Server 2003 R2 Base の場合は最初に設定したストレージ容量が起動時に反映されるため設定の必要はありません。
 
ただし、EBSを拡張した場合、標準ツールではシステムパーティションの拡張は行えません。
 
フリーウェアなど利用して行う必要があります。
 

CentOS 6.5 (x86_64) - Release Media
CentOS 6.5 (x86_64) - Release Mediaの場合は最初に設定した拡張したストレージ容量は起動時に反映されません。resize2fsの必要があります。
 
例20GBに拡張して起動
 
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvde       7.9G  650M  6.9G   9% /
tmpfs           296M     0  296M   0% /dev/shm
[/Bash]
[Bash]
Disk /dev/xvde: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[/Bash]
resize2fsを行ってみましょう。
[Bash]
# resize2fs /dev/xvde
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/xvde is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/xvde to 5242880 (4k) blocks.
The filesystem on /dev/xvde is now 5242880 blocks long.
[/Bash]
resize2fsの後の容量を確認してみましょう。
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvde        20G  654M   19G   4% /
tmpfs           296M     0  296M   0% /dev/shm
[/Bash]
Debian GNU/Linux
Debian GNU/Linuxの場合は最初に設定したストレージ容量が起動時に反映されるためresize2fsの必要はありません。
 
さらに、スナップショットからボリュームサイズを拡張して作成した場合でもresize2fsにいよる拡張が必要はありません。
 
例20GB→30GBへの拡張
 
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
rootfs           30G  522M   28G   2% /
udev             10M     0   10M   0% /dev
tmpfs            60M   64K   60M   1% /run
/dev/xvda        30G  522M   28G   2% /
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           120M     0  120M   0% /run/shm
[/Bash]
[Bash]
Disk /dev/xvda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders, total 62914560 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: 0x00000000
Disk /dev/xvda doesn't contain a valid partition table
[/Bash]
Amazon Linux AMIと同様にCloud-initが勝手にやってます。
 
[Bash]
May 12 18:09:19 ip-172-31-7-45 [CLOUDINIT] cc_resizefs.py[DEBUG]: Resizing took 24.148 seconds
May 12 18:09:19 ip-172-31-7-45 [CLOUDINIT] cc_resizefs.py[DEBUG]: Resized root filesystem (type=ext4, val=True)
[/Bash]
CentOS 6 (x86_64) - with Updates
CentOS 6 (x86_64) - with Updates の場合は最初に設定した拡張したストレージ容量は起動時に反映されません。resize2fsの必要があります。
 
例20GBに拡張して起動
 
[Bash]
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvde             7.9G  635M  6.9G   9% /
tmpfs                 296M     0  296M   0% /dev/shm
[/Bash]
[Bash]
Disk /dev/xvde: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[/Bash]
resize2fsを行ってみましょう。
[Bash]
# resize2fs /dev/xvde
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/xvde is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/xvde to 5242880 (4k) blocks.
The filesystem on /dev/xvde is now 5242880 blocks long.
[/Bash]
resize2fsの後の容量を確認してみましょう。
[Bash]
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvde              20G  639M   19G   4% /
tmpfs                 296M     0  296M   0% /dev/shm
[/Bash]
Fedora-x86_64-20-20140407-sda
Fedora-x86_64-20-20140407-sda の場合は最初に設定したストレージ容量が起動時に反映されるためresize2fsの必要はありません。
 
例20GBに拡張して起動
 
[Bash]
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       20G  565M   20G   3% /
devtmpfs        275M     0  275M   0% /dev
tmpfs           296M     0  296M   0% /dev/shm
tmpfs           296M  8.2M  287M   3% /run
tmpfs           296M     0  296M   0% /sys/fs/cgroup
[/Bash]
[Bash]
Disk /dev/xvda1: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0x20ac7dda
This doesn't look like a partition table. Probably you selected the wrong device.
Device Boot Start End Blocks Id System /dev/xvda1p1 ? 3224498923 3657370039 216435558+ 7 HPFS/NTFS/exFAT /dev/xvda1p2 ? 3272020941 5225480974 976730017 16 Hidden FAT16 /dev/xvda1p3 ? 0 0 0 6f Unknown /dev/xvda1p4 50200576 974536369 462167897 0 Empty
Partition table entries are not in disk order. [/Bash]
番外編 古いCentOS5.1(ami-1da6d61c)
最初に設定したストレージ容量が起動時に反映されません。
resize2fsも行えません。そのためfdiskでパーティションの切りなおしが必要となります。 
例20GBに拡張して起動
 
[Bash]
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             9.9G  1.2G  8.2G  13% /
none                  308M     0  308M   0% /dev/shm
[/Bash]
[Bash]
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System /dev/sda1 1 1305 10482381 83 Linux /dev/sda2 1306 1566 2096482+ 82 Linux swap / Solaris [/Bash] resize2fsでは下記のような表示がされます。 [Bash] # resize2fs /dev/sda1 resize2fs 1.39 (29-May-2006) The filesystem is already 2620595 blocks long. Nothing to do! [/Bash] 最初に、fdiskでパーティションを切りなおします。 [Bash] # fdisk /dev/sda
The number of cylinders for this disk is set to 2610. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help):p←パーティション表示
Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System /dev/sda1 1 1305 10482381 83 Linux /dev/sda2 1306 1566 2096482+ 82 Linux swap / Solaris
[/Bash]
ここでスタートとエンドをメモっておきます。
また、シリンダー数もメモっておきます。
次に、パーティションを切りなおすためにSwap領域の計算します。
/dev/sda2 Swap領域=1566-1306=260
2610-260=2350となります。
これを元に下記のようになるよう設定します。
[Bash]
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        2349    xxxxxx   83  Linux
/dev/sda2            2350        2610     xxxxxx+  82  Linux swap / Solaris
[/Bash]
fdiskで設定していきます。
[Bash]
Command (m for help): d ←パーティション削除
Partition number (1-4): 1
Command (m for help): p
Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System /dev/sda2 1306 1566 2096482+ 82 Linux swap / Solaris
Command (m for help): d←パーティション削除 Selected partition 2
Command (m for help): p←パーティション表示
Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
Command (m for help): n←パーティション作成 Command action e extended p primary partition (1-4) p←プライマリパーティション作成 Partition number (1-4): 1 First cylinder (1-2610, default 1): 1←最初のシリンダ Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): 2349←計算したシリンダ
Command (m for help): n←パーティション作成 Command action e extended p primary partition (1-4) p←プライマリパーティション作成 Partition number (1-4): 2 First cylinder (2350-2610, default 2350): 2350←計算したシリンダ Last cylinder or +size or +sizeM or +sizeK (2350-2610, default 2610): 2610←最後のシリンダ
Command (m for help): t←システムIDを変更します Partition number (1-4): 2←/dev/sda2 Hex code (type L to list codes): 82←Swap領域 Changed system type of partition 2 to 82 (Linux swap / Solaris)
Command (m for help): p
Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System /dev/sda1 1 2349 18868311 83 Linux /dev/sda2 2350 2610 2096482+ 82 Linux swap / Solaris
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.
Syncing disks.
[/Bash]
リブートすると設定が反映されます。
起動後swapの設定を行います。
[Bash]
# mkswap -c /dev/sda2
Setting up swapspace version 1, size = 2146791 kB
[/Bash]
Swapの確認をします。
[Bash]
# free
             total       used       free     shared    buffers     cached
Mem:        629760      79692     550068          0        120       6344
-/+ buffers/cache:      73228     556532
Swap:            0          0          0
[/Bash]
SwapをONしてみます。
[Bash]
# swapon /dev/sda2
[/Bash]
Swapの確認をします。有効になったことを確認します。
fstabの確認もしておきましょう。
[Bash]
# free
             total       used       free     shared    buffers     cached
Mem:        629760      80856     548904          0        204       6700
-/+ buffers/cache:      73952     555808
Swap:      2096472          0    2096472
# cat /etc/fstab /dev/sda1 / ext3 defaults 0 0 none /dev/pts devpts gid=5,mode=620 0 0 none /dev/shm tmpfs defaults 0 0 none /proc proc defaults 0 0 none /sys sysfs defaults 0 0 /dev/sda2 swap swap defaults 0 0 [/Bash] 再起動してfreeコマンドでSwapが機能していれば問題ないです。
まとめ
以上、長々と書いてきましたが、EC2に限った話しではなくLinux、Windowsの基本操作の一部になります。
覚えておいて損は無いと思います。











