GentooのAMIを作る

in  AWS

GentooをAWS上で動かすために、イメージを作ってみました。 クラウドに使えるstage4ファイルを利用すれば、簡単にできてしまいます。

追加ボリュームにgentooをインストール後、snapshot作成、AMI作成という手順でいきました。

追加ディスクにGentooをインストールする。

何でもいいんで、LinuxベースのAMIを立ち上げ、 EBSボリュームを追加であタッチするところから始めます。

まずはディスクの用意から。/dev/xvdb が追加ディスクです。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
parted -a optimal /dev/xvdb
(parted) mklabel gpt
(parted) unit mib
(parted) mkpart primary 1 3
(parted) name 1 grub
(parted) set 1 bios_grub on
(parted) mkpart primary 3 9000
(parted) name 2 cloudimg-rootfs
(parted) mkpart primary 9000 -1
(parted) name 3 swap
(parted) print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdb: 10240MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start    End       Size     File system     Name             Flags
 1      1.00MiB  3.00MiB   2.00MiB  ext4            grub             bios_grub
 2      3.00MiB  9000MiB   8997MiB                  cloudimg-rootfs
 3      9000MiB  10239MiB  1239MiB  linux-swap(v1)  swap
(parted) quit

ファイルシステムを構築します。

1
2
3
4
5
6
$ mkfs.ext4 /dev/xvdb2
$ e2label /dev/xvdb2 cloudimg-rootfs
$ mkswap --label swap /dev/xvdb3
$ mkdir -p /mnt/gentoo
$ mount /dev/xvdb2 /mnt/gentoo
$ swapon /dev/xvdb3

stageファイルを展開します。

1
2
3
$ cd /mnt/gentoo
$ wget http://distfiles.gentoo.org/releases/amd64/autobuilds/current-stage3-amd64/stage4-amd64-cloud-20161201.tar.bz2
$ tar xjf stage4-amd64-cloud-20161201.tar.bz2

もろもろmountして、

1
2
3
4
5
$ mount -t proc proc /mnt/gentoo/proc
$ mount --rbind /sys /mnt/gentoo/sys
$ mount --rbind /dev /mnt/gentoo/dev
$ chroot /mnt/gentoo/ /bin/bash
$ source /etc/profile

portageツリーを最新化。

1
2
$ emerge --sync
$ emerge --ask --update --deep --newuse @world

ローケールを設定します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$ echo "Asia/Tokyo" > /etc/timezone
$  emerge --config sys-libs/timezone-data


Configuring pkg...

 * Updating /etc/localtime with /usr/share/zoneinfo/Asia/Tokyo

$ vi /etc/locale.gen
ja_JP.UTF-8 UTF-8

$ locale-gen
 * Generating 3 locales (this might take a while) with 1 jobs
 *  (1/3) Generating en_US.ISO-8859-1 ...                                                                                                    [ ok ]
 *  (2/3) Generating en_US.UTF-8 ...                                                                                                         [ ok ]
 *  (3/3) Generating ja_JP.UTF-8 ...                                                                                                         [ ok ]
 * Generation complete

$ eselect locale list
  [1]   C
  [2]   en_US
  [3]   en_US.iso88591
  [4]   en_US.utf8 *
  [5]   ja_JP.utf8
  [6]   POSIX
  [ ]   (free form)

$ eselect locale set 5
Setting LANG to ja_JP.utf8 ...
Run ". /etc/profile" to update the variable in your shell.
$ env-update && source /etc/profile
>>> Regenerating /etc/ld.so.cache...

/etc/fstabを編集します。

1
2
LABEL=cloudimg-rootfs / ext4 defaults 0 0
LABEL=swap            none swap sw    0 0

/etc/conf.d/netを編集します。

1
config_eth0="dhcp"

ネットワークを設定します。

1
2
3
$ cd /etc/init.d/
$ ln -s net.lo net.eth0
$ rc-update add net.eth0 default

Grubを設定します。

1
2
3
4
5
6
7
8
9
$ grub-install /dev/xvdb
Installing for i386-pc platform.
Installation finished. No error reported.

$ grub-mkconfig -o /boot/grub/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/kernel-genkernel-x86_64-4.4.26-gentoo-openstack
Found initrd image: /boot/initramfs-genkernel-x86_64-4.4.26-gentoo-openstack
done

/mnt/gentoo/etc/cloud/cloud.cfgに下記を追記

1
datasource_list: [ Ec2, None ]

SnapshotからGentooインスタンスを立ち上げる

ここまで来たら、作るためにsnapshotを取ります。 作ったSnapshotからAMIを作ってインスタンスを起動するTerraformの設定は下記になります。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
resource "aws_ami" "gentoo-base" {
    name = "gentoo-base-20161124"
    virtualization_type = "hvm"
    root_device_name = "/dev/sda1"

    ebs_block_device {
        device_name = "/dev/sda1"
        snapshot_id = "snap-1c6e4993"
        volume_size = 10
    }
}

resource "aws_instance" "gside-gentoo" {
    ami                         = "${aws_ami.gentoo-base.id}"
    availability_zone           = "${aws_subnet.main.availability_zone}"
    ebs_optimized               = false
    associate_public_ip_address = false
    instance_type               = "t2.nano"
    monitoring                  = false
    key_name                    = "${aws_key_pair.gside-key.key_name}"
    vpc_security_group_ids      = ["${aws_security_group.basic.id}"]
    associate_public_ip_address = true
    disable_api_termination     = "true"
    source_dest_check           = "false"
    subnet_id 			= "${aws_subnet.main.id}"

    root_block_device {
        volume_type           = "gp2"
        volume_size           = 10
        delete_on_termination = true
    }

    tags {
        "Name" = "gside-gentoo"
    }
}

起動後はgentooユーザーでsshログインできます。

まとめ

CloudフレンドリーなStage4ファイルが有るおかげで、色々楽ちんでした。 中身は下記が詳しいです。

https://mthode.org/posts/2016/Jan/stage4-tarballs-minimal-and-cloud/

stage4の中身についてもここからたどれます。


Share