「cloud-init」- 安装、使用

  CREATED BY JENKINSBOT

通过 Cloud Image 部署

linux – How do I set a password on an Ubuntu cloud image? – Server Fault
How to set root password with cloud-config? · Issue #659 · vmware/photon · GitHub
12.04 – Default username/password for Ubuntu Cloud image? – Ask Ubuntu
Cloud-init To Enable Cloud Image Root Login – TCC Consulting Limited
mcwhirter.com.au/craige/blog/2015/Enable Root Login Over SSH With Cloud-Init on OpenStack
Creating a VM using Libvirt, Cloud Image and Cloud-Init | Sumit’s Dreams of Electric Sheeps

第一步、创建镜像

下载 Cloud Image 文件:https://cloud-images.ubuntu.com/

# .img 文件为 QCOW2 格式
wget http://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img

# 创建磁盘文件 
qemu-img convert -f qcow2 -O raw focal-server-cloudimg-amd64.img focal-server-cloudimg-amd64.raw
qemu-img resize focal-server-cloudimg-amd64.raw 100G

# 创建磁盘文件:此方式使用 QCOW2 的写时复制特性,hal9000.img 引用 focal-server-cloudimg-amd64.img 文件,空间占用小
qemu-img create -b focal-server-cloudimg-amd64.img -f qcow2 -F qcow2 hal9000.img 10G

第二步、创建配置

apt-get install whois cloud-image-utils

mkpasswd -m sha512crypt 123456 -S "12345342"

cat > user-data <<EOF
#cloud-config

hostname: focal-server
manage_etc_hosts: localhost

users:
  - name: root
    lock_passwd: false
    hashed_passwd: '<the output of mkpassword...>'
    ssh_authorized_keys:
      - ssh-rsa AAAAB3NzaC1y...

# SSH
ssh_pwauth: True
disable_root: false
EOF

cloud-localds user-data.iso user-data

第三步、创建虚拟机

然后,在虚拟机中同时挂载 focal-server-cloudimg-amd64.raw 与 user-data.iso 文件;

virt-install              \
    --vcpus=4             \
    --ram=8192            \
    --import              \
    --os-variant=ubuntu20.04      \
    --network network=cluster-network,model=virtio                \
    --graphics vnc,listen=0.0.0.0 \
    --noautoconsole               \
    --disk path=/srv/isos/user-data.iso,device=cdrom              \
    --disk path=/srv/image/focal-server-cloudimg-amd64.raw,format=qcow2   \
    --name=focal-server

# --network type=direct,source=enp6s0f0,source_mode=bridge,model=virtio
# --vnc
# https://gist.github.com/smurugap/163b3e2be7676a46c835339f8ba0710f

当开机启动完成后,便可通过 root/123456 进行登录;

相关资源

libvirtd + cloud-init 快速创建虚拟机:./libvirtd-guest-setup.sh

参考文献

cloud-init Documentation — cloud-init 22.1 documentation
12.04 – Default username/password for Ubuntu Cloud image? – Ask Ubuntu
Networking Config Version 2 — cloud-init 22.1 documentation
KVM: Testing cloud-init locally using KVM for an Ubuntu cloud image