KVM のゲスト OS を作成するのに、preseed より手軽な方法がないか探していたら、ubuntu-vm-builder というのを見つけたので試してみる。
前提
今回は次のような前提で話を進める。
- ホスト、ゲストの OS はともに Ubuntu 12.04 Server amd64
- すでにホスト OS で KVM やブリッジは設定済み
準備
必要となるパッケージを入れておく。
$ sudo apt-get install ubuntu-vm-builder
手順
最初はゲスト OS が何もない状態。
$ virsh list --all
Id Name State
----------------------------------
作成するゲスト OS が、初回起動時に実行するスクリプト (firstboot) を用意しておく。使いドコロは kickstart で言うところの %post と似たような感じだと思う。
$ vi firstboot.sh
# シリアルコンソールの有効化
echo 'start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
respawn
exec /sbin/getty -L 115200 ttyS0 xterm' > /etc/init/ttyS0.conf
echo 'GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
GRUB_TERMINAL=serial
GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' > /etc/default/grub
update-grub
いよいよ ubuntu-vm-builder を使って precise (12.04) のゲスト OS を作成する。引数は適宜調整する (すべてのオプションは、この記事の下部で紹介する)。
$ GUEST_NAME=tmpl_ubuntu1204
$ sudo ubuntu-vm-builder kvm precise \
--mem 512 \
--destdir=${GUEST_NAME} \
--libvirt qemu:///system \
--install-mirror=http://jp.archive.ubuntu.com/ubuntu \
--hostname=${GUEST_NAME} \
--timezone=Asia/Tokyo \
--bridge=br0 \
--ip=172.16.100.255 \
--mask=255.255.0.0 \
--gw=172.16.0.2 \
--dns=172.16.0.2 \
--addpkg acpid \
--addpkg openssh-server \
--firstboot ${PWD}/firstboot.sh
しばらく待つと完了する。ちなみにローカルに apt-cacher を建ててそれを参照するようにしたところ、2回目以降は8分弱で済むようになった。
2014-02-01 19:25:49,130 INFO : Calling hook: preflight_check
2014-02-01 19:25:49,133 INFO : Calling hook: set_defaults
2014-02-01 19:25:49,133 INFO : Calling hook: bootstrap\
:
2014-02-01 19:33:14,393 INFO : Calling hook: fix_ownership
2014-02-01 19:33:14,394 INFO : Calling hook: deploy
ゲスト OS が作られたか確認する。
$ virsh list --all
Id Name State
----------------------------------
- tmpl_ubuntu1204 shut off
KVM のゲスト OS の設定に、コンソールに関する記述を追記する。
$ virsh edit tmpl_ubuntu1204
:
<!-- ↓ここから -->
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<!-- ↑ここまで -->
</devices>
</domain>
ゲスト OS を起動する。
$ virsh start tmpl_ubuntu1204
Domain tmpl_ubuntu1204 started
KVM のコンソールで接続する。User/Pass は ubuntu/ubuntu でログインできる。
$ virsh console tmpl_ubuntu1204
Connected to domain tmpl_ubuntu1204
Escape character is ^]
Ubuntu 12.04.4 LTS (none) ttyS0
(none) login:
以上、コマンド操作だけで手軽にゲスト OS が作成できた。
あとは ssh の鍵を置いたり追加のパッケージを入れたりして、好きなようにカスタマイズする。ゲストを clone したら、virt-config でネットワーク設定を更新すれば良い。
- akagisho / virt-config – GitHub
オプション
ubuntu-vm-builder のすべてのオプションは、次のコマンドで確認できる。
$ ubuntu-vm-builder <hypervisor> <distro> --help
precise の場合は次のように表示される。
$ ubuntu-vm-builder kvm precise --help
Usage: ubuntu-vm-builder hypervisor suite [options]
Options:
-h, --help show this help message and exit
--version Show version information
Build options:
--debug Show debug information
-v, --verbose Show progress information
-q, --quiet Silent operation
-o, --overwrite Configuration file
-c CONFIG, --config=CONFIG
Configuration file
--templates=DIR Prepend DIR to template search path.
-d DESTDIR, --destdir=DESTDIR
Destination directory
--only-chroot Only build the chroot. Don't install it on disk images
or anything.
--chroot-dir=CHROOT_DIR
Build the chroot in directory.
--existing-chroot=EXISTING_CHROOT
Use existing chroot.
-t DIR, --tmp=DIR Use TMP as temporary working space for image
generation. Defaults to $TMPDIR if it is defined or
/tmp otherwise. [default: /tmp]
--tmpfs=SIZE Use a tmpfs as the working directory, specifying its
size or "-" to use tmpfs default (suid,dev,size=1G).
Disk:
--rootsize=SIZE Size (in MB) of the root filesystem [default: 4096]
--optsize=SIZE Size (in MB) of the /opt filesystem. If not set, no
/opt filesystem will be added.
--swapsize=SIZE Size (in MB) of the swap partition [default: 1024]
--raw=PATH Specify a file (or block device) to use as first disk
image (can be specified multiple times).
--part=PATH Specify a partition table in PATH. Each line of
partfile should specify (root first): mountpoint
size one per line, separated by space, where size is
in megabytes. You can have up to 4 virtual disks, a
new disk starts on a line containing only '---'. ie:
root 2000 /boot 512 swap 1000 ---
/var 8000 /var/log 2000
Settings for the initial user:
--user=USER Username of initial user [default: ubuntu]
--name=NAME Full name of initial user [default: Ubuntu]
--pass=PASS Password of initial user [default: ubuntu]
--rootpass=ROOTPASS
Initial root password (WARNING: this has strong
security implications).
--uid=UID Initial UID value.
--gid=GID Initial GID value.
--lock-user Lock the initial user [default: none]
Other options:
--ssh-key=PATH Add PATH to root's ~/.ssh/authorized_keys (WARNING:
this has strong security implications).
--ssh-user-key=SSH_USER_KEY
Add PATH to the user's ~/.ssh/authorized_keys.
--manifest=PATH If passed, a manifest will be written to PATH
General OS options:
-a ARCH, --arch=ARCH
Specify the target architecture. Valid options: amd64
i386 lpia (defaults to host arch)
--hostname=HOSTNAME
Set NAME as the hostname of the guest. Default:
ubuntu. Also uses this name as the VM name.
Scripts:
--firstboot=PATH Specify a script that will be copied into the guest
and executed the first time the machine boots. This
script must not be interactive.
--firstlogin=PATH Specify a script that will be copied into the guest
and will be executed the first time the user logs in.
This script can be interactive.
Post install actions:
--copy=FILE Read 'source dest' lines from FILE, copying source
files from host to dest in the guest's file system.
--execscript=SCRIPT, --exec=SCRIPT
Run SCRIPT after distro installation finishes. Script
will be called with the guest's chroot as first
argument, so you can use 'chroot $1 <cmd>' to run code
in the virtual machine.
Package options:
--addpkg=PKG Install PKG into the guest (can be specified multiple
times).
--removepkg=PKG Remove PKG from the guest (can be specified multiple
times)
--seedfile=SEEDFILE
Seed the debconf database with the contents of this
seed file before installing packages
Network:
--domain=DOMAIN Set DOMAIN as the domain name of the guest [default:
host.example.com].
Installation options:
--suite=SUITE Suite to install. Valid options: dapper gutsy hardy
intrepid jaunty karmic lucid maverick natty oneiric
precise quantal raring saucy [default: lucid]
--flavour=FLAVOUR, --kernel-flavour=FLAVOUR
Kernel flavour to use. Default and valid options
depend on architecture and suite
--variant=VARIANT Passed to debootstrap --variant flag; use minbase,
buildd, or fakechroot.
--iso=PATH Use an iso image as the source for installation of
file. Full path to the iso must be provided. If
--mirror is also provided, it will be used in the
final sources.list of the vm. This requires suite and
kernel parameter to match what is available on the
iso, obviously.
--mirror=URL Use Ubuntu mirror at URL instead of the default, which
is http://archive.ubuntu.com/ubuntu for official
arches and http://ports.ubuntu.com/ubuntu-ports
otherwise
--proxy=URL Use proxy at URL for cached packages
--install-mirror=URL
Use Ubuntu mirror at URL for the installation only.
Apt's sources.list will still use default or URL set
by --mirror
--security-mirror=URL
Use Ubuntu security mirror at URL instead of the
default, which is http://security.ubuntu.com/ubuntu
for official arches and http://ports.ubuntu.com
/ubuntu-ports otherwise.
--install-security-mirror=URL
Use the security mirror at URL for installation only.
Apt's sources.list will still use default or URL set
by --security-mirror
--components=COMPS A comma seperated list of distro components to include
(e.g. main,universe).
--ppa=PPA Add ppa belonging to PPA to the vm's sources.list.
--lang=LANG Set the locale to LANG [default: en_US.UTF-8]
--timezone=TZ Set the timezone to TZ in the vm. [default: UTC]
libvirt integration:
--libvirt=URI Add VM to given URI
--bridge=BRIDGE Set up bridged network connected to BRIDGE.
--network=NETWORK Set up a network connection to virtual network
NETWORK.
Network:
--ip=ADDRESS IP address in dotted form [default: dhcp].
--mac=MAC MAC address of the guest [default: random].
--mask=VALUE IP mask in dotted form [default: based on ip setting].
Ignored if ip is not specified.
--net=ADDRESS IP net address in dotted form [default: based on ip
setting]. Ignored if ip is not specified.
--bcast=VALUE IP broadcast in dotted form [default: based on ip
setting]. Ignored if ip is not specified.
--gw=ADDRESS Gateway (router) address in dotted form [default:
based on ip setting (first valid address in the
network)]. Ignored if ip is not specified.
--dns=ADDRESS DNS address in dotted form [default: based on ip
setting (first valid address in the network)] Ignored
if ip is not specified.
VM settings:
-m MEM, --mem=MEM Assign MEM megabytes of memory to the guest vm.
[default: 128]
--cpus=CPUS Assign NUM cpus to the guest vm. [default: 1]
参考ページ
- KVM/CreateGuests – ubuntu documentation
- DraftKVMSerialConsole – ubuntu documentation
- ubuntu-vm-builder – ubuntu manuals
- ubuntu-vm-builder で仮想マシンを作る – May the Source be with you
コメント