2
4

More than 1 year has passed since last update.

Ubuntu 18.04 でも Kickstart で OS 自動インストール

Last updated at Posted at 2019-03-16

Kickstart は RedHat でよく使用される OS の自動インストールツールです。Ubuntu で OS の自動インストールと言えばは preseed がよく使用されますが、Kickstart も Ubuntu 18.04 で動きます。RedHat に比べると使えるオプションが少なく、LVM が使えないなど制限はありますがテスト環境作り程度なら Kickstart で十分ではないでしょうか。

テンプレートは以下のようなものを使用します。TIMEZONE (e.g. Asia/Tokyo), USERNAME, FULLNAME, PASSWORD, IP_ADDR, NETMASK, GATEWAY, HOSTNAME, DNS をそれぞれ自分の環境に合わせて書き換えてください。本作業は KVM 上で行っています。


#Use CDROM installation media
cdrom
# System language
lang en_US
# Keyboard layouts
keyboard us

# System timezone
timezone TIMEZONE --isUtc --nontp
# Root password
rootpw --disabled
# Initial user
user USERNAME --fullname "FULLNAME" --password PASSWORD

# Reboot after installation
reboot
# Use text mode install
text
# Install OS instead of upgrade
install

#System bootloader configuration
bootloader --location=mbr

# Clear the Master Boot Record
zerombr yes
#Partition clearing information
clearpart --all --initlabel
# Basic disk partition
# According to this document, No LVM functionality for Ubuntu
# https://help.ubuntu.com/community/KickstartCompatibility
part / --fstype ext4 --size 1 --grow --asprimary
part swap --size 1024
part /boot --fstype ext4 --size 512 --asprimary

#System authorization information
auth  --useshadow  --enablemd5

# Network information
# It seems that the order of these options are important and we can't define multiple NICs...
network --bootproto=static --ip=IP_ADDR --netmask=NETMASK --gateway=GATEWAY --hostname=HOSTNAME --nameserver=DNS --device=ens3
#network --bootproto=static --ip=IP_ADDR2 --netmask=255.255.255.0 --device=ens4 #This doesn't work.

%packages
ubuntu-minimal
python
openssh-server

編集したファイルを引数にして Ubuntu を起動してください。例えば以下のようになります。下記ではインストール元の ISO ファイルが /mnt/datastore1/isos/linux/ubuntu-18.04.1-server-amd64.iso でインストール先のディスクが /mnt/datastore1/images/k50h.qcow2 となっています。


$ cat ./ubuntu-ks.cfg
#Use CDROM installation media
cdrom
# System language
lang en_US
# Keyboard layouts
keyboard us

# System timezone
timezone Asia/Tokyo --isUtc --nontp
# Root password
rootpw --disabled
# Initial user
user user1 --fullname "FULL NAME" --password user12345

# Reboot after installation
reboot
# Use text mode install
text
# Install OS instead of upgrade
install

#System bootloader configuration
bootloader --location=mbr

# Clear the Master Boot Record
zerombr yes
#Partition clearing information
clearpart --all --initlabel
# Basic disk partition
# According to this document, No LVM functionality for Ubuntu
# https://help.ubuntu.com/community/KickstartCompatibility
part / --fstype ext4 --size 1 --grow --asprimary
part swap --size 1024
part /boot --fstype ext4 --size 512 --asprimary

#System authorization information
auth  --useshadow  --enablemd5

# Network information
# It seems that the order of these options are important and we can't define multiple NICs...
network --bootproto=static --ip=192.168.1.50 --netmask=255.255.255.0 --gateway=192.168.1.1 --hostname=k50h.local --nameserver=8.8.8.8 --device=ens3
#network --bootproto=static --ip=IP_ADDR2 --netmask=255.255.255.0 --device=ens4 #This doesn't work.

%packages
ubuntu-minimal
python
openssh-server
$ sudo virt-install \
>   --virt-type kvm \
>   --location /mnt/datastore1/isos/linux/ubuntu-18.04.1-server-amd64.iso \
>   --os-type linux \
>   --os-variant ubuntu18.04 \
>   --ram 4096 \
>   --vcpus 2 \
>   --disk /mnt/datastore1/images/k50h.qcow2,size=10 \
>   --name k50h.local \
>   --network bridge=br0 \
>   --network network=virbr1 \
>   --network network=virbr2 \
>   --hvm \
>   --noautoconsole \
>   --initrd-inject ./ubuntu-ks.cfg \
>   --extra-args \
>     "ks=file:/ubuntu-ks.cfg"

Starting install...
Retrieving file .treeinfo...                                                                                                                            |    0 B  00:00:00
Retrieving file content...                                                                                                                              |    0 B  00:00:00
Retrieving file info...                                                                                                                                 |   68 B  00:00:00
Retrieving file vmlinuz...                                                                                                                              | 7.9 MB  00:00:00
Retrieving file initrd.gz...                                                                                                                            |  15 MB  00:00:00
Allocating 'k50h.qcow2'                                                                                                                                 |  10 GB  00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

virt-manager などでインストールが自動で進んでいく様子が確認できます。最後何故か VM が落ちたままとなってしまっている場合は、virsh start コマンドなどで OS を起動してください。ssh などでインストールした OS にアクセスできます。ここまで来れば Ansible などでどうにでも設定はできるようになります。


$ ssh -l user1 192.168.1.50
user1@192.168.1.50's password: user12345

後は簡単なスクリプトで回せば何台でもリソースが許す限りインストールできます。


$ git clone https://github.com/tkarube/ubuntu_kickstart_os_install
Cloning into 'ubuntu_kickstart_os_install'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
$ cd ubuntu_kickstart_os_install/
/ubuntu_kickstart_os_install$ ./run_me.sh
$ cd ubuntu_kickstart_os_install
# README.md に従い run_me.sh と install_ubuntu.sh を編集
$ ./run_me.sh
$ virsh list | grep k5
 35    k50h                           running
 36    k51h                           running
 37    k52h                           running
 38    k53h                           running

2020/9/15:

Ubuntu 20.04 にアップデートしてから試したら動かなくなってた。。これかも。後で(いつとは言ってないw)試してみる。
https://askubuntu.com/questions/1237165/cant-install-ubuntu-20-04-kvm-guest

2
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4