Cloud Images with KVM (cloud-init)

最後更新: 2019-06-11

介紹

 

原理:

VM Image 預裝了cloud-init, 我們將 config 放到 iso 內

在 VM 第一次啟動時讓 cloud-init 去設定它

cloud-init 的 Boot Stages

Generator   

# 決定是否行 cloud-init

 - A file exists: /etc/cloud/cloud-init.disabled

 - /proc/cmdline contains cloud-init=disabled

Local           

# cloud-init-local.service

# As soon as possible with / mounted read-write

 - locate “local” data sources

 - apply networking configuration to the system

Network     

# cloud-init.service

# After local stage and configured networking is up

# runs modules:  disk_setup, mounts, bootcmd ...

Config         

# cloud-config.service

# After network stage

 - runs config modules only (cloud_config_modules in /etc/cloud/cloud.cfg)

Final             

# cloud-final.service

此 step 會執行 user-scripts (including runcmd)

 


1. Download Cloud Image

 

# C7

mkdir /ssd/cloudimg

cd /ssd/cloudimg

wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qc...

unxz -k CentOS-7-x86_64-GenericCloud.qcow2.xz

Remark

  • CentOS-7-x86_64-GenericCloud.qcow2.xz --> Last version image #  它已是最 update img
  • .xz 比 .tar.gz 有更好壓縮比, 所以使用它

 


2. resize image & create swap file

 

# resize image

qemu-img resize cloudimg.qcow2 20G

qemu-img info cloudimg.qcow2

# create swap

fallocate -l 1G swap.raw

 


3. 建立設定檔

 

cloud-config.txt

#cloud-config
hostname: cloudimg.local
user: root
password: MyPassword
chpasswd: { expire: False }
ssh_pwauth: True
growpart:
  mode: auto
  devices: ['/']
locale: en_HK.UTF-8
timezone: Asia/Hong_Kong
write_files:
-   encoding: b64
    content: U0VMSU5VWD1kaXNhYmxlZApTRUxJTlVYVFlQRT10YXJnZXRlZCAK
    owner: root:root
    path: /etc/selinux/config
    permissions: '0644'
disk_setup:
   /dev/vdb:
       table_type: 'mbr'
       layout: True
       overwrite: True
fs_setup:
   - label: 'swap'
     filesystem: 'swap'
     device: '/dev/vdb1'
mounts:
 - [ '/dev/vdb1', 'none', 'swap', 'sw', '0', '0' ]
bootcmd:
 - ifdown eth0
 - ifup eth0
package_upgrade: true
packages:
 - pwgen
 - wget
 - screen
 - vim
runcmd:
  - [ yum, -y, remove, cloud-init ]
  - [ yum, -y, remove, cloud-utils-growpart ]
power_state:
  delay: "+1"
  mode: reboot
  message: Initial configuration done by cloud-init, forcing reboot to apply changes.

meta-data.txt

network-interfaces: |
  iface eth0 inet static
  address 192.168.123.44
  network 192.168.123.0
  netmask 255.255.255.0
  broadcast 192.168.123.255
  gateway 192.168.123.1
  dns-nameservers 8.8.8.8
  dns-nameservers 8.8.4.4
  dns-search local

 


4. Install 設定工貝 - cloud-localds

 

安裝 tool

apt install cloud-image-utils

Usage

cloud-localds [ options ] output user-data [meta-data]

NoCloud

The data source NoCloud allows the user to provide user-data and meta-data to the instance without running a network service

(or even without having a network at all)

You can provide meta-data and user-data to a local vm boot via files on a vfat or iso9660 filesystem.

The filesystem volume label must be cidata or CIDATA

Example

cloud-localds cloud-config.img cloud-config.txt meta-data.txt

 


5. 用設定檔去安裝 VM

 

apt-get install virtinst

virt-install --connect=qemu:///system \
 --name cloudimg \
 --ram 512 \
 --vcpus=2 \
 --virt-type kvm \
 --os-type=linux \
 --os-variant=centos7.0 \
 --disk cloudimg.qcow2,device=disk,bus=virtio \
 --disk swap.raw,device=disk,bus=virtio \
 --disk cloud-config.img,device=cdrom \
 --network bridge=br0,model=virtio \
 --graphics vnc,listen=0.0.0.0,port=5944,password=foobar \
 --import

Final

# 離開

Ctrl + ]

Remark

 * 當 "--graphics none"時, cmd 後會 auto 進入了 console

 * swap 放在分配好空間的 raw 檔內

# 彈出設定檔的 iso

virsh change-media cloudimg hda --eject --config

 


Doc

 

https://cloudinit.readthedocs.io/en/latest/