EBSスナップショットから準仮想AMIに登録する方法


毎回やり方を思い出すのに時間がかかるので、忘備録としてメモ

## 下準備
pv-grub用の最新のkernel idを確認する

```:shell
$ aws ec2 describe-images --owner amazon --filters ¥
 Name=name,Values=pv-grub*gz Name=architecture,Values=x86_64

{
    "Images": [
        {
            "VirtualizationType": "paravirtual",
            "Name": "pv-grub-hd0_1.04-x86_64.gz",
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "ImageId": "aki-176bf516",
            "State": "available",
            "BlockDeviceMappings": [],
            "Architecture": "x86_64",
            "ImageLocation": "amzn-ami-ap-northeast-1/pv-grub-hd0_1.04-x86_64.gz.manifest.xml",
            "RootDeviceType": "instance-store",
            "OwnerId": "137112412989",
            "CreationDate": "2013-10-01T22:34:48.000Z",
            "Public": true,
            "ImageType": "kernel",
            "Description": "PV-GRUB release 1.04, 64-bit, configured for (hd0)/boot/grub/menu.lst"
        },
        {
            "VirtualizationType": "paravirtual",
            "Name": "pv-grub-hd00_1.04-x86_64.gz",
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "ImageId": "aki-1f6bf51e",
            "State": "available",
            "BlockDeviceMappings": [],
            "Architecture": "x86_64",
            "ImageLocation": "amzn-ami-ap-northeast-1/pv-grub-hd00_1.04-x86_64.gz.manifest.xml",
            "RootDeviceType": "instance-store",
            "OwnerId": "137112412989",
            "CreationDate": "2013-10-01T22:39:08.000Z",
            "Public": true,
            "ImageType": "kernel",
            "Description": "PV-GRUB release 1.04, 64-bit, configured for (hd0,0)/boot/grub/menu.lst"
        },
        {
            "VirtualizationType": "paravirtual",
            "Name": "pv-grub-hd00_1.03-x86_64.gz",
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "ImageId": "aki-40992841",
            "State": "available",
            "BlockDeviceMappings": [],
            "Architecture": "x86_64",
            "ImageLocation": "ec2-public-images-ap-northeast-1/pv-grub-hd00_1.03-x86_64.gz.manifest.xml",
            "RootDeviceType": "instance-store",
            "OwnerId": "206029621532",
            "CreationDate": "2012-03-18T22:57:01.000Z",
            "Public": true,
            "ImageType": "kernel",
            "Description": "PV-GRUB release 1.03, 64-bit, configured for (hd0,0)/boot/grub/menu.lst"
        },
        {
            "VirtualizationType": "paravirtual",
            "Name": "pv-grub-hd0_1.03-x86_64.gz",
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "ImageId": "aki-44992845",
            "State": "available",
            "BlockDeviceMappings": [],
            "Architecture": "x86_64",
            "ImageLocation": "ec2-public-images-ap-northeast-1/pv-grub-hd0_1.03-x86_64.gz.manifest.xml",
            "RootDeviceType": "instance-store",
            "OwnerId": "206029621532",
            "CreationDate": "2012-03-18T22:57:30.000Z",
            "Public": true,
            "ImageType": "kernel",
            "Description": "PV-GRUB release 1.03, 64-bit, configured for (hd0)/boot/grub/menu.lst"
        }
    ]
}

```

2015年3月時点での最新版は`aki-176bf516`

(hd0)用と(hd0,0)用の2種類あるが、1.04からは(hd0)を使えとのこと。
`/boot/grub/menu.lst`がDISKの何処に存在するかで使い分けるが現在は、
いくつかのパーテションを探しに行く仕様になった様だ。

参考：[AWSのドキュメント](http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UserProvidedKernels.html)


## aws-api-toolsの場合

以下を実行。ephemralの指定は好みで。rootボリュームにgp2を指定。

```:shell
$ ec2-register --aws-access-key <access key> ¥
--aws-secret-key <secret key> ¥
--region ap-northeast-1 ¥
--architecture x86_64 ¥
--block-device-mapping '/dev/sda=snap-XXXXXXXX:8:true:gp2' ¥
--block-device-mapping '/dev/sdb=ephemeral0' ¥
--description <string> ¥
--name <string> ¥
--kernel aki-176bf516 ¥
--root-device-name '/dev/sda' ¥
--virtualization-type paravirtual
```

## awscliの場合

`--generate-cli-skeleton`を利用してコンフィグのサンプルを取得

```:shell
$ aws ec2 register-image --generate-cli-skeleton > sample_config.json
```

スケルトンを元に各種パラメータを記載する

```:json
$ vim sample_config.json
{
    "DryRun": true,
    "Name": "<string>",
    "Description": "<string>",
    "Architecture": "x86_64",
    "KernelId": "aki-176bf516",
    "RootDeviceName": "/dev/sda",
    "BlockDeviceMappings": [
        {
            "DeviceName": "/dev/sda",
            "Ebs": {
                "SnapshotId": "snap-XXXXXX",
                "VolumeSize": 8,
                "DeleteOnTermination": true,
                "VolumeType": "gp2"
            }
        },
        {
            "VirtualName": "ephemeral0",
            "DeviceName": "/dev/sdb"
        }
    ],
    "VirtualizationType": "paravirtual"
}
```

作成したコンフィグファイルを指定して登録(DryRunは削除)

```:shell
$ aws ec2 register-image --cli-input-json file://sample_config.json
```