cloud-init

Cloud init guide

ref: https://cloudinit.readthedocs.io/en/latest/topics/examples.html

1. Install tools on the hypervisor machine

1.1 Install kvm qemu virt

1
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst

1.2 Install cloud init tools

1
sudo apt install -y cloud-utils

download a cloud image

1
wget https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.img

create a vm image

1
2
qemu-img create -f qcow2 -b ubuntu-20.04-server-cloudimg-amd64.img client.qcow2
qemu-img resize client.qcow2 +10G

create a cloud init file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cat > cloud-init.yaml << EOF
#cloud-config
hostname: cloudimg.local
user: root
password: lab123
chpasswd: { expire: False }
ssh_pwauth: True
users:
- name: lab
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
groups: root, sudo
plain_text_passwd: 'lab123'
lock_passwd: false
growpart:
mode: auto
devices: ['/']
locale: en_US.UTF-8
timezone: Asia/Shanghai
EOF

create network config file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cat > network.yaml << EOF  
ethernets:
enp1s0:
dhcp4: false
addresses:
- 11.1.10.12/24
gateway4: 11.1.10.1
enp2s0:
addresses: [ 192.168.122.110/24 ]
#gateway4: 192.168.122.1
#nameservers:
# addresses: [114.114.114.114]
version: 2
EOF

create the cloud-init iso file

1
2
cloud-localds -m local -N network.yaml cloud-init.iso cloud-init.yaml

boot a vm by virt-install

1
2
3
4
5
6
7
8
9
10
11
12
virt-install \
--name vm1 \
--ram=8192 \
--vcpus 4 \
--os-type linux \
--os-variant ubuntu20.04 \
--graphics none \
--disk /home/lab/cloud/client.qcow2,device=disk,bus=virtio \
--disk /home/lab/cloud/cloud-init.iso,device=cdrom \
--network bridge=br10,model=virtio \
--network bridge=virbr0,model=virtio \
--import