File:  [NetBSD Developer Wiki] / wikisrc / amazon_ec2.mdwn
Revision 1.32: download - view: text, annotated - select for diffs
Wed Feb 23 21:57:05 2011 UTC (12 years, 3 months ago) by wiki
Branches: MAIN
CVS tags: HEAD
web commit by jym: Revert previous. It's a script, hence not a command string... oops.


# Introduction

This tutorial aims at showing how you can build, setup, upload and launch NetBSD under the [Amazon EC2]( service. We will first give some brief explanations on how you can obtain an AWS account, and what you will need to run NetBSD under EC2. Then, at your convenience, you will be able to start with pre-made images, or roll-out your own.

# Subscribe to AWS (Amazon Web Services)

If you already have an account for [Amazon Web Services](, and you are a registered user for EC2 service, you can directly jump to section [What do you need to know](#index2h2). If not, keep reading.

## Quick overview

Before you can start playing with Amazon EC2, you have to create an account on Amazon Web Services, of which EC2, the Elastic Compute Cloud, is part. This is fairly straightforward, and done in two steps:

1. you "sign-up" directly on [Amazon Web Services]( home-page. This is where you enter your credentials, and confirm your AWS account registration.
1. you sign-up to EC2 through [EC2 AWS home-page]( You will be asked some more information, like a credit card (for billing), and a phone-number, for account validation.

## What do you need to know?

EC2 uses different types of credentials. In addition to your login and password, you need an access key, a X.509 certificate (with its private key), and a pair of RSA keys, for remote SSH access.

These can be created through the [Security Credentials]( page (also accessible from the [Account]( page):

1. create the access key. Keep a secured copy of the ID and its associated secret value. These will be used by various scripts later on to perform certain EC2 actions.
1. note down your account number (different from your access key ID!). This identifier can usually be obtained in the right top part of the page; it is a serie of numbers, separated with dashes: XXXX-XXXX-XXXX.
1. create, or upload, a X.509 certificate, in PEM format. Keep the private key in a safe place.
1. lastly, generate Amazon EC2 key pairs that will be used for SSH access. This step will be performed through the [Amazon Management Console]( Note down the SSH Key Pair Name you chose.

### Keep your credentials!

The different credentials created above will be used in various places of EC2, and by a myriad of commands. You are advised to keep them easily accessible, while still reasonably secure regarding their access. Most EC2 tools expect them to be find through a set of environment variables.

For convenience, you could store them under a *.ec2* directory inside your *$HOME*:

[[!template id=programlisting text="""
$ ls .ec2/                                                                
cert-SOMERANDOMKEY.pem # the X.509 certificate
id_rsa.ec2             # private RSA SSH key         # public RSA SSH key
pk-SOMERANDOMKEY.pem   # the private key associated to the certificate

then set the environment accordingly:

[[!template id=programlisting text="""
export EC2_CERT=$HOME/.ec2/cert-SOMERANDOMKEY.pem
export EC2_SSH_KEY=$HOME/.ec2/id_rsa.ec2
export EC2_SSH_KEYNAME=<your_ssh_key_pair_name>

Please note that the rest of the tutorial will assume that these variables are set.

## Installing EC2 API tools

NetBSD provides EC2 API tools, to ease EC2 account management a little bit. The package is found inside [pkgsrc](, under [[!template id=pkg category=misc name=ec2-api-tools]].

[[!template id=programlisting text="""
cd /usr/pkgsrc/misc/ec2-api-tools
make ACCEPTABLE_LICENSES+=amazon-software-license install

Package depends on Java, so build will take some time to finish. While it builds, just continue reading.

## EC2 vocabulary -- last notes

Before starting to play with EC2, you need to be familiar with the EC2 vocabulary used throughout this tutorial.

Briefly said, EC2 uses [Xen]( as virtualization solution. So, in essence, all operating systems that support Xen para-virtualization can theoretically run inside EC2, as a domU. This is the case for NetBSD; however, please note that only amd64 is currently supported. Work is on-going to support 32 bits for EC2.

All operating systems are run as *instances*, which are, as their name implies, the instantiation of a specific AMI, or *Amazon Machine Image*. An AMI is an image built from specific *snapshots* of *volumes*. The volumes are part of [Elastic Block Storage]( (or EBS for short), which is another service offered by AWS, distinct from EC2.

These instances are tied to a *region* (a geographical location; typically US East, US West, Europe West, etc.). Each region has *availability zones*, which can be compared to a sub-region, each one being physically distinct from another. Regions are identified by a name, like *us-east-1*, *eu-west-1*. Same goes for availability zones, usually with the region's name as prefix: *us-east-1a*, *us-east-1b*, and so forth. Note that resources are **not** shared between zones, so if you transfer data from one zone to another, you will be charged for it.

AKI, or *Amazon Kernel Image*, are a specific type of image. It represents the Xen guest para-virtualized kernel, as used by an AMI. Certain AKIs are allowed to boot customized operating systems, e.g. those that are still not officially supported by Amazon. Thanks to [PyGrub](, it can boot a kernel that resides inside an AMI's snapshot.

# Using pre-made AMIs


# Build-up your NetBSD system

## Fetch and build NetBSD

EC2 does not provide direct access to console. As a consequence, we cannot rely on it for installation, especially via [[!template id=man name=sysinst section=8]]. We must therefore build and install NetBSD in a separate directory, and configure it manually, before upload.

This tutorial assumes that you will build the system under */mnt/ec2*.

/!\Please note that you will need the [[!template id=man name=makefs section=8]] tool later in the process, so you can build a file system image that can be uploaded to Amazon EC2. You are therefore advised to perform the installation directly under a living NetBSD system, or in case your are not, to fetch the *src* tree to build the toolchain, which will contain the **nbmakefs** utility.

[Details regarding on how you can fetch *src* are given in the NetBSD's guide]( Here are the basic commands you should type to build and install NetBSD under */mnt/ec2*:

[[!template id=programlisting text="""
cd /usr/
# grab a recent src.tgz file (use curl(1), ftp(1), wget(1), ...)
ftp -a ''
# Decompress
tar -xzpf src.tar.gz
cd src
# build distribution and kernel
./ -O ../obj -T ../tools -D ../dest -R ../release -m amd64 -U distribution
./ -O ../obj -T ../tools -m amd64 kernel=XEN3_DOMU
# install distribution in /mnt/ec2
su root ./ -O ../obj -T ../tools -D ../dest -R ../release -U -V INSTALLSETS="base etc" install=/mnt/ec2

# Configuration of your NetBSD EC2 tree

/!\This part assumes that you have a non-configured NetBSD system extracted under */mnt/ec2*; that is, it should have not been modified through [[!template id=man name=sysinst section=8]], nor by you.

Under */mnt/ec2*, edit the files to add (or modify) these lines:

[[!template id=filecontent name=etc/rc.conf text="""

sshd=YES # for remote shell access to instance

[[!template id=filecontent name=etc/ssh/sshd_config text="""
# Allows root to login via authentication keys
PermitRootLogin without-password

This file is needed if you want to login via the EC2 SSH key pair created previously:

[[!template id=filecontent name=etc/rc.d/ec2_init text="""
# PROVIDE: ec2_init

$_rc_subr_loaded . /etc/rc.subr




        umask 022
        # fetch the key pair from Amazon Web Services
        EC2_SSH_KEY=$(ftp -o - "${METADATA_URL}${SSH_KEY_URL}")

        if [ -n "$EC2_SSH_KEY" ]; then
                # A key pair is associated with this instance, add it
                # to root 'authorized_keys' file
                mkdir -p $(dirname "$SSH_KEY_FILE")
                touch "$SSH_KEY_FILE"
                cd $(dirname "$SSH_KEY_FILE")

                grep -q "$EC2_SSH_KEY" "$SSH_KEY_FILE"
                if [ $? -ne 0 ]; then
                        echo "Setting EC2 SSH key pair: ${EC2_SSH_KEY##* }"
                        echo "$EC2_SSH_KEY" >> "$SSH_KEY_FILE"

        # set hostname
        HOSTNAME=$(ftp -o - "${METADATA_URL}${HOSTNAME_URL}")
        echo "Setting EC2 hostname: ${HOSTNAME}"
        echo "$HOSTNAME" > /etc/myname
        hostname "$HOSTNAME"

load_rc_config $name
run_rc_command "$1"

Create various files and directories:

[[!template id=programlisting text="""
cd /mnt/ec2
# Add proc and kern directories
mkdir grub kern proc
# EC2 network configuration, via DHCP
echo "dhcp" > etc/ifconfig.xennet0
# Basic fstab entries
cat > etc/fstab << EOF
/dev/xbd1a /        ffs    rw 1 1
/dev/xbd0a /grub    ext2fs rw 2 2
kernfs     /kern    kernfs rw
ptyfs      /dev/pts ptyfs  rw
procfs     /proc    procfs rw
# EC2 startup script (if you installed it)
if [ -f etc/rc.d/ec2_init ]; then
    chmod 755 etc/rc.d/ec2_init

You can then proceed to modifying the system living under */mnt/ec2*, so it can fit your needs (adding custom binaries, packages, etc). When done, build the *NetBSD-AMI.img.gz* ffs image, via [[!template id=man name=makefs section=8]], or **nbmakefs**, from the [toolchain](

[[!template id=programlisting text="""
$ makefs -t ffs -B le -s 256m -N /mnt/ec2/etc/ -o density=32k NetBSD-AMI.img /mnt/ec2/ 
Calculated size of `NetBSD-AMI.img': 268435456 bytes, 7345 inodes
Extent size set to 8192
NetBSD-AMI.img: 256.0MB (524288 sectors) block size 8192, fragment size 1024
        using 5 cylinder groups of 53.88MB, 6896 blks, 1728 inodes.
super-block backups (for fsck -b #) at:
     32, 110368, 220704, 331040, 441376,
Populating `NetBSD-AMI.img'
Image `NetBSD-AMI.img' complete
$ gzip -9n NetBSD-AMI.img

# Upload NetBSD to EC2

We must now upload our NetBSD system to EC2. For that, we will have to create a minimalist EC2 instance, to which we will copy our files to construct our snapshots. We will use an Amazon Linux AMI instance.

EC2 being localized in geographical regions, you have to carefully choose the AMI identifier you want to use there. This depends on where you want to execute your instance. Amazon Linux AMI IDs are listed on [the main page]( of the project, by regions. Choose ones backed by EBS.

The examples listed here assume that the instances run in **US East**, within the **c** zone (e.g. **us-east-1c**). To have a list of EC2 regions, you can use the command **ec2-describe-regions**, and **ec2-describe-availability-zones** for availability zones.

## Create an Amazon Linux instance

Creating an instance is straightforward. Amazon provides [different types of instances](, with varying levels of billing and reliability. We will use a [*micro* instance](; its pricing is almost free.

[[!template id=programlisting text="""
$ ec2-run-instances ami-74f0061d -t t1.micro -z us-east-1c -k $EC2_SSH_KEYNAME
RESERVATION     r-1ab61377      983624114127    default
INSTANCE        <strong>i-5babe737</strong>      ami-74f0061d                    pending &lt;your_ssh_key_pair_name&gt;  0               t1.micro        2011-02-17T23:15:04+0000        us-east-1c      aki-427d952b                    monitoring-disabled                                     ebs                                     paravirtual     xen     

Use the instance identifier **i-XXXXXXX** to query the instance state via **ec2-describe-instances**. It will take some time to launch:

[[!template id=programlisting text="""
$ sleep 5 && ec2-describe-instances i-5babe737 | grep running
$ sleep 5 && ec2-describe-instances i-5babe737 | grep running
INSTANCE        i-5babe737      ami-74f0061d    <strong></strong>       ip-10-99-86-193.ec2.internal    running &lt;your_ssh_key_pair_name&gt;  0               t1.micro        2011-02-17T23:22:37+0000        us-east-1c      aki-427d952b                    monitoring-disabled                    ebs

## Create and attach your NetBSD volumes

We will have to create and attach two EBS volumes:

1. one to contain the Grub *menu.lst* config file, as well as the NetBSD kernel.
1. the other one will contain the root file-system.

[[!template id=programlisting text="""
<strong>ec2-create-volume -s 1 -z us-east-1c</strong> # 1GiB -- will be used for Grub and kernel
VOLUME  vol-24f88d4c    1               us-east-1c      creating        2011-02-18T00:06:21+0000
<strong>ec2-create-volume -s 5 -z us-east-1c</strong> # 5GiB -- will contain the root file-system
VOLUME  vol-36f88d5e    5               us-east-1c      creating        2011-02-18T00:06:32+0000
*** Wait until both volumes are marked as "available" ***
<strong>ec2-describe-volumes vol-24f88d4c vol-36f88d5e</strong>
VOLUME  vol-36f88d5e    5               us-east-1c      available       2011-02-18T00:06:32+0000
VOLUME  vol-24f88d4c    1               us-east-1c      available       2011-02-18T00:06:21+0000
# Attach them under /dev/sdf and /dev/sdg respectively
<strong>ec2-attach-volume vol-36f88d5e -i i-5babe737 -d "/dev/sdf"</strong> # root file-system
ATTACHMENT      vol-36f88d5e    i-5babe737      /dev/sdf        attaching       2011-02-18T00:13:53+0000
<strong>ec2-attach-volume vol-24f88d4c -i i-5babe737 -d "/dev/sdg"</strong> # Grub and kernel
ATTACHMENT      vol-24f88d4c    i-5babe737      /dev/sdg        attaching       2011-02-18T00:14:02+0000
*** Wait until both volumes are "attached" ***
<strong>ec2-describe-volumes vol-24f88d4c vol-36f88d5e</strong>
VOLUME  vol-36f88d5e    5               us-east-1c      in-use  2011-02-18T00:06:32+0000
ATTACHMENT      vol-36f88d5e    i-5babe737      /dev/sdf        attached        2011-02-18T00:14:00+0000
VOLUME  vol-24f88d4c    1               us-east-1c      in-use  2011-02-18T00:06:21+0000
ATTACHMENT      vol-24f88d4c    i-5babe737      /dev/sdg        attached        2011-02-18T00:14:10+0000

## Snapshots!

Before we can connect to our brand new instance, we have to allow connections on SSH port (22) through the AWS EC2 firewall:

[[!template id=programlisting text="""
$ ec2-authorize default -p 22
GROUP           default 
PERMISSION              default ALLOWS  tcp     22      22      FROM    CIDR

We can now upload the kernel and the NetBSD disk image created earlier, *NetBSD-AMI.img.gz*, to our instance host:

[[!template id=programlisting text="""
# Upload kernel to Linux AMI
rsync -aPv -e "ssh -i $EC2_SSH_KEY" /usr/obj/sys/arch/amd64/compile/XEN3_DOMU/netbsd \
# Upload disk image
rsync -aPv -e "ssh -i $EC2_SSH_KEY" NetBSD-AMI.img.gz \

Then, log in to the instance, via its name. We will format and mount the Grub partition, create the *menu.lst* file, then copy files to their respective partitions.

[[!template id=programlisting text="""
$ ec2-describe-instances i-5babe737
INSTANCE        i-5babe737      ami-74f0061d    <strong></strong>       ip-10-99-86-193.ec2.internal    running &lt;your_ssh_key_pair_name&gt;  0               t1.micro        2011-02-17T23:22:37+0000        us-east-1c      aki-427d952b                    monitoring-disabled                    ebs
$ ssh -i "$EC2_SSH_KEY"
[ec2-user@ip-10-99-86-193 ~]$ sudo su
[root@ip-10-99-86-193 ec2-user]# mkdir /mnt/grub
[root@ip-10-99-86-193 ec2-user]# mkfs.ext2 /dev/sdg
[root@ip-10-99-86-193 ec2-user]# mount /dev/sdg /mnt/grub/
[root@ip-10-99-86-193 ec2-user]# mkdir -p /mnt/grub/boot/grub/
[root@ip-10-99-86-193 ec2-user]# cat > /mnt/grub/boot/grub/menu.lst << EOF

title NetBSD AMI
root (hd0)
kernel /boot/netbsd root=xbd1
[root@ip-10-99-86-193 ec2-user]# mv netbsd /mnt/grub/boot/
[root@ip-10-99-86-193 ec2-user]# umount /dev/sdg
[root@ip-10-99-86-193 ec2-user]# gunzip < NetBSD-AMI.img.gz | dd of=/dev/sdf bs=32k
[root@ip-10-99-86-193 ec2-user]# sync

## Shutdown the Linux instance

We now have to detach volumes, snapshot them, then we shutdown the Linux instance.

[[!template id=programlisting text="""
# ec2-detach-volume vol-36f88d5e
ATTACHMENT      vol-36f88d5e    i-5babe737      /dev/sdf        detaching       2011-02-18T00:14:00+0000
# ec2-detach-volume vol-24f88d4c
ATTACHMENT      vol-24f88d4c    i-5babe737      /dev/sdg        detaching       2011-02-18T00:14:10+0000
# ec2-create-snapshot vol-36f88d5e
SNAPSHOT        <strong>snap-deef2bb2</strong>   vol-36f88d5e    pending 2011-02-18T01:17:59+0000                983624114127    5
# ec2-create-snapshot vol-24f88d4c
SNAPSHOT        <strong>snap-8aef2be6</strong>   vol-24f88d4c    pending 2011-02-18T01:18:10+0000                983624114127    1
# ec2-terminate-instances i-5babe737
INSTANCE        i-5babe737      running shutting-down

# Playing with your first NetBSD instance

## Create your first NetBSD AMI

An AMI requires multiples components to be registered: the snapshots IDs we made in the previous chapter, as well as a specific AKI: the one that can chain-load Xenified kernels through PyGrub.

/!\ AKIs are entitled to the same conditions as AMIs: their IDs are region-specific. So choose one carefully, or you will not be able to launch your NetBSD instance later!

The list of AKIs that suits our situation can be obtained with the following command:

[[!template id=programlisting text="""
# Obtain all kernel images (AKI) for region US East, for which manifest location contains pv-grub (for PyGrub)
# ec2-describe-images -a --region=us-east-1 -F image-type=kernel -F manifest-location=*pv-grub*
IMAGE   aki-407d9529    ec2-public-images/pv-grub-hd0-V1.01-i386.gz.manifest.xml        amazon  available       public          i386    kernel                          instance-store  paravirtual     xen
<strong>IMAGE   aki-427d952b    ec2-public-images/pv-grub-hd0-V1.01-x86_64.gz.manifest.xml      amazon  available       public          x86_64  kernel                          instance-store  paravirtual     xen</strong>
IMAGE   aki-4c7d9525    ec2-public-images/pv-grub-hd00-V1.01-i386.gz.manifest.xml       amazon  available       public          i386    kernel                          instance-store  paravirtual     xen
IMAGE   aki-4e7d9527    ec2-public-images/pv-grub-hd00-V1.01-x86_64.gz.manifest.xml     amazon  available       public          x86_64  kernel                          instance-store  paravirtual     xen

Pick the one with the correct architecture (x86_64 here). **hd0** are for AMIs where the snapshot contains no partition (where the volume is itself the whole partition), while **hd00** are for snapshots partitioned in a classical way (via MBR). Choose **hd0** AKIs. In this case, that will be **aki-427d952b**.

We can proceed to the creation of our AMI, with:

1. */dev/sda1* as Grub partition (*/dev/sdg*, snapshot **snap-8aef2be6** of volume **vol-24f88d4c**)
1. */dev/sda2* as root file-system (*/dev/sdf*, snapshot **snap-deef2bb2** of volume **vol-36f88d5e**)

[[!template id=programlisting text="""
$ ec2-register -a x86_64 --kernel aki-427d952b --region us-east-1 \
    -b "/dev/sda1=snap-8aef2be6" -b "/dev/sda2=snap-deef2bb2" -n "NetBSD-x86_64-current" \
    -d "&lt;add your own description here&gt;
IMAGE   <strong>ami-74d0231d</strong>

## Launch your first instance

You can now start your own NetBSD instance, via:

[[!template id=programlisting text="""
$ ec2-run-instances ami-74d0231d -t t1.micro -z us-east-1c -k $EC2_SSH_KEYNAME
RESERVATION     r-08218465      983624114127    default
INSTANCE        <strong>i-953d72f9</strong>      ami-74d0231d                    pending         0               t1.micro        2011-02-18T02:05:46+0000        us-east-1c      aki-4e7d9527                    monitoring-disabled
*** Wait a few minutes, micro instances take time to start ***
# Query console output for your new instance
$ ec2-get-console-output i-953d72f9
Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    2006, 2007, 2008, 2009, 2010, 2011
    The NetBSD Foundation, Inc.  All rights reserved.
Copyright (c) 1982, 1986, 1989, 1991, 1993
    The Regents of the University of California.  All rights reserved.

NetBSD 5.99.45 (XEN3_DOMU) #9: Wed Feb 16 21:14:49 CET 2011
NetBSD/amd64 (ip-10-112-58-223.ec2.internal) (console)


## Connect to your NetBSD instance

Connection is similar to the one you used for the Amazon Linux instance, except that you login as "root" instead of "ec2-user":

[[!template id=programlisting text="""
$ ec2-describe-instances i-953d72f9
RESERVATION     r-da8021b7      983624114127    default
INSTANCE        i-953d72f9      ami-74d0231d    <strong></strong>  ip-10-112-58-223.ec2.internal   running &lt;your_ssh_key_pair_name&gt;  0               t1.micro        2011-02-19T04:01:03+0000        us-east-1c      aki-427d952b                    monitoring-disabled                   ebs                                     paravirtual     xen     
BLOCKDEVICE     /dev/sda1       vol-ec3c4a84    2011-02-19T04:01:31.000Z        
BLOCKDEVICE     /dev/sda2       vol-ee3c4a86    2011-02-19T04:01:31.000Z        
$ ssh -i "$EC2_SSH_KEY"
The authenticity of host ' (' can't be established.
Thank you for helping us test and improve NetBSD.

Terminal type is xterm.
We recommend that you create a non-root account and use su(1) for root access.
ip-10-112-58-223# uname -a
NetBSD ip-10-112-58-223.ec2.internal 5.99.45 NetBSD 5.99.45 (XEN3_DOMU) #9: Wed Feb 16 21:14:49 CET 2011  jym@paris:/home/jym/cvs/obj/sys/arch/amd64/compile/XEN3_DOMU amd64


## And now?

Well, you got a NetBSD instance that is in almost every part similar to what a NetBSD domU can be. You can use this domU to host Internet services, run a database, extend your build farm, or use it as a sandbox. The AMI being built around snapshots, you can play and break your instance in every way you want; just restart one anew if you need to. Don't forget that Amazon will charge acccordingly :)

Remember, you can query information regarding your AWS account through [[!template id=pkg category=misc name=ec2-api-tools]] package. It is quite easy to use these tools for scripting; for a more elaborate, graphical interface, use the [Amazon Management Console](

CVSweb for NetBSD wikisrc <> software: FreeBSD-CVSweb