Proxmox VE与Cloud-init

认识Cloud-init

Cloud-Init是一个用于初始化虚拟机的工具。使用Cloud-Init可以在虚拟机管理程序端配置网络设备、ssh 密钥以及用户信息等。当 VM 首次启动时,VM 中的 Cloud-Init程序将读取这些设置,并且执行。

官方文档:https://cloudinit.readthedocs.io/

 

Proxmox VE中的Cloud-init

1. cloud-init格式

Proxmox VE会根据虚拟机的OSTYPE属性,设置cloudinit格式。

属于Linux虚拟机,则使用Nocloud格式。属于Windows虚拟机,则使用configdrive2。当然也支持opennebula格式。

如果需要自定义,则可以通过下面命令实现

qm set <vmid> --citype <configdrive2 | nocloud | opennebula>

在虚拟机配置栏中,添加一个Cloud-init驱动器之后,会在磁盘目录生成一个cloudinit磁盘文件。

root@pve1:/mnt/pve/NFS/images/1023# ls -lh
total 1.8G
-rw-r----- 1 nobody 4294967294 4.4M Mar 21 23:41 vm-1023-cloudinit.qcow2
-rw-r----- 1 nobody 4294967294  11G Mar 22 14:31 vm-1023-disk-0.qcow2

将上面的cloudinit磁盘文件挂载,我们可以看到有如下内容。

本文给大家带来3种格式的分别讲解

1.1 opennebula

OpenNebula是一个类似与Openstack的开源云计算平台。

使用opennebula会生成一个context.sh的文件,如下

root@pve1:/1# tree /1
/1
└── context.sh
root@pve1:/1# cat context.sh
USERNAME=root
CRYPTED_PASSWORD_BASE64=JDUkVGpObWZhWkQkdnJYZE1ta2k5N1YwNVpTbnVMMkcwN1VvdExHYi5zL1EyVW5obE5lc09uLg==

SET_HOSTNAME=cit22
DNS="10.13.14.51"
SEARCH_DOMAIN="testest.com"
ETH0_DHCP=YES
ETH0_AUTO6=YES
NETWORK=YES

 1.2 nocloud

nocloud在根目录下有四个文件

  • meta-data
  • network-config
  • user-data
  • vendor-data

 

root@pve1:/# tree /1
/1
├── meta-data
├── network-config
├── user-data
└── vendor-data

0 directories, 4 files

meta-data文件


root@pve1:/1# cat meta-data
instance-id: 2c6a02fe41ae071a19b06c4b9bdf50dafce8adb0

network-config文件

root@pve1:/1# cat network-config
version: 1
config:
    - type: physical
      name: eth0
      mac_address: 'ac:bd:ef:f3:fb:b7'
      subnets:
      - type: dhcp4
      - type: ipv6_slaac
    - type: nameserver
      address:
      - '10.13.14.51'
      search:
      - testtest.com'

user-data文件

root@pve1:/1# cat user-data
#cloud-config
hostname: cit22
manage_etc_hosts: true
user: root
disable_root: False
password: $5$TjNmfaZD$vrXdMmki97V05ZSnuL2G07UotLGb.s/Q2UnhlNesOn.
chpasswd:
expire: False
package_upgrade: true

packages:
- qemu-guest-agent
- nginx
- aria2

vendor-data无数据

1.3 configdrive2

configdrive2依然有4个文件。

root@pve1:/1# tree /1
/1
└── openstack
    ├── content
    │   └── 0000
    └── latest
        ├── meta_data.json
        ├── user_data
        └── vendor_data.json

3 directories, 4 files

openstack/content/0000 定义了网络属性

root@pve1:/1# cat openstack/content/0000
auto lo
iface lo inet loopback

        dns_nameservers 10.13.14.51
        dns_search testest.com
auto eth0
iface eth0 inet dhcp
iface eth0 inet6 auto

openstack/latest/meta_data.json 定义元数据,同时指引程序去哪里读取网络配置

root@pve1:/1# cat openstack/latest/meta_data.json
{
     "uuid": "efdc255b780dfa1bfe8e6e30102d97f2a8e42642",
     "network_config": { "content_path": "/content/0000" }
}

openstack/latest/user_data 用户配置文件

root@pve1:/1# cat openstack/latest/user_data
#cloud-config
hostname: cit22
manage_etc_hosts: true
user: root
disable_root: False
password: $5$TjNmfaZD$vrXdMmki97V05ZSnuL2G07UotLGb.s/Q2UnhlNesOn.
chpasswd:
  expire: False
package_upgrade: true


packages:
  - qemu-guest-agent
  - nginx
  - aria2

openstack/latest/vendor_data.json为空

cloudinit的3种格式会更具ostype自动选择,一般不需要改变。

2. cloudinit磁盘

PVE会把网页上可配置的信息,储存在vm config文件中,如下,密码是会打码。

root@pve1:~# qm config 1023
agent: 1,fstrim_cloned_disks=1
boot: order=scsi0
cipassword: **********
citype: nocloud
ciuser: root
ide2: NFS:1023/vm-1023-cloudinit.qcow2,media=cdrom
ipconfig0: ip=dhcp,ip6=auto
meta: creation-qemu=6.1.1,ctime=1647790662
name: cit22
net0: virtio=AC:BD:EF:F3:FB:B7,bridge=vmbr0,firewall=1
ostype: l26
scsi0: NFS:1023/vm-1023-disk-0.qcow2,size=10G
scsihw: virtio-scsi-single
serial0: socket
smbios1: uuid=1f11325b-924b-42dc-aa4a-96b32b07186e
vmgenid: 93d2e067-c660-4015-a6c8-5208d3e088e5

接着会根据citype和conf信息,生成或者更新相应的类型的qcow2磁盘。如

ide2: NFS:1023/vm-1023-cloudinit.qcow2,media=cdrom

使用命令qm showcmd <vmid>可以看到有如下行,这个磁盘作为cdrom挂载给VM

-drive 'file=/mnt/pve/NFS/images/1023/vm-1023-cloudinit.qcow2,if=none,id=drive-ide2,media=cdrom,aio=io_uring'

3. 虚拟机启动

这里截取了一段虚拟机启动的输出,从输出可以看到cloud-init的初始化过程。

Welcome to Ubuntu 20.04.4 LTS!
#开始设置hostname
[    3.503089] systemd[1]: Set hostname to <cit22>.           
[    3.872818] systemd[1]: Created slice system-modprobe.slice.
[  OK  ] Created slice system-modprobe.slice.
[    3.874739] systemd[1]: Created slice system-serial\x2dgetty.slice.
[  OK  ] Created slice system-serial\x2dgetty.slice.
[    3.877511] systemd[1]: Created slice system-systemd\x2dfsck.slice.
[  OK  ] Created slice system-systemd\x2dfsck.slice.
[    3.879692] systemd[1]: Created slice User and Session Slice.
[  OK  ] Created slice User and Session Slice.
[    3.881410] systemd[1]: Started Forward Password Requests to Wall Directory Watch.
[  OK  ] Started Forward Password R…uests to Wall Directory Watch.
[    3.883699] systemd[1]: Set up automount Arbitrary Executable File Formats File System Automount Point.
[  OK  ] Set up automount Arbitrary…s File System Automount Point.
[    3.885721] systemd[1]: Reached target User and Group Name Lookups.
[  OK  ] Reached target User and Group Name Lookups.
[    3.887231] systemd[1]: Reached target Slices.
[  OK  ] Reached target Slices.
[    3.889099] systemd[1]: Reached target Swap.
[  OK  ] Reached target Swap.
[    3.890403] systemd[1]: Listening on Device-mapper event daemon FIFOs.
[  OK  ] Listening on Device-mapper event daemon FIFOs.
[    3.892177] systemd[1]: Listening on LVM2 poll daemon socket.
[  OK  ] Listening on LVM2 poll daemon socket.
[    3.893710] systemd[1]: Listening on multipathd control socket.
[  OK  ] Listening on multipathd control socket.
[    3.895299] systemd[1]: Listening on Syslog Socket.
[  OK  ] Listening on Syslog Socket.
[    3.896670] systemd[1]: Listening on fsck to fsckd communication Socket.
[  OK  ] Listening on fsck to fsckd communication Socket.
[    3.898411] systemd[1]: Listening on initctl Compatibility Named Pipe.
[  OK  ] Listening on initctl Compatibility Named Pipe.
[    3.900176] systemd[1]: Listening on Journal Audit Socket.
[  OK  ] Listening on Journal Audit Socket.
[    3.901633] systemd[1]: Listening on Journal Socket (/dev/log).
[  OK  ] Listening on Journal Socket (/dev/log).
[    3.903120] systemd[1]: Listening on Journal Socket.
[  OK  ] Listening on Journal Socket.
[    3.905281] systemd[1]: Listening on Network Service Netlink Socket.
[  OK  ] Listening on Network Service Netlink Socket.
[    3.907714] systemd[1]: Listening on udev Control Socket.
[  OK  ] Listening on udev Control Socket.
[    3.909923] systemd[1]: Listening on udev Kernel Socket.
[  OK  ] Listening on udev Kernel Socket.
[    3.915361] systemd[1]: Mounting Huge Pages File System...
         Mounting Huge Pages File System...
[    3.918050] systemd[1]: Mounting POSIX Message Queue File System...
         Mounting POSIX Message Queue File System...
[    3.924530] systemd[1]: Mounting Kernel Debug File System...
         Mounting Kernel Debug File System...
[    3.927263] systemd[1]: Mounting Kernel Trace File System...
         Mounting Kernel Trace File System...
[    3.933970] systemd[1]: Starting Journal Service...
         Starting Journal Service...
[    3.936664] systemd[1]: Starting Set the console keyboard layout...
         Starting Set the console keyboard layout...
[    3.945959] systemd[1]: Starting Create list of static device nodes for the current kernel...
         Starting Create list of stodes for the current kernel...
[    3.952008] systemd[1]: Starting Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling...
         Starting Monitoring of LVM…meventd or progress polling...
[    3.958350] systemd[1]: Starting Load Kernel Module drm...
         Starting Load Kernel Module drm...
[    3.961961] systemd[1]: Condition check resulted in OpenVSwitch configuration for cleanup being skipped.
[    3.965423] systemd[1]: Condition check resulted in Set Up Additional Binary Formats being skipped.
[    3.966742] systemd[1]: Condition check resulted in File System Check on Root Device being skipped.
[    3.973440] systemd[1]: Starting Load Kernel Modules...
         Starting Load Kernel Modules...
[    3.980812] systemd[1]: Starting Remount Root and Kernel File Systems...
         Starting Remount Root and Kernel File Systems...
[    3.985727] systemd[1]: Starting udev Coldplug all Devices...
         Starting udev Coldplug all Devices...
[    3.991607] systemd[1]: Starting Uncomplicated firewall...
         Starting Uncomplicated firewall...
[    3.999573] systemd[1]: Mounted Huge Pages File System.
[  OK  ] Mounted Huge Pages File System.
[    4.006549] systemd[1]: Mounted POSIX Message Queue File System.
[  OK  ] Mounted POSIX Message Queue File System.
[    4.012949] systemd[1]: Mounted Kernel Debug File System.
[  OK  ] Mounted Kernel Debug File System.
[    4.015188] systemd[1]: Mounted Kernel Trace File System.
[  OK  ] Mounted Kernel Trace File System.
[    4.020671] systemd[1]: Finished Create list of static device nodes for the current kernel.
[  OK  ] Finished Create list of st… nodes for the current kernel.
[    4.029260] systemd[1]: Finished Uncomplicated firewall.
[  OK  ] Finished Uncomplicated firewall.
[    4.044876] EXT4-fs (sda1): re-mounted. Opts: (null)
[    4.050101] IPMI message handler: version 39.2
[    4.053614] systemd[1]: Finished Remount Root and Kernel File Systems.
[  OK  ] Finished Remount Root and Kernel File Systems.
[    4.057375] systemd[1]: Condition check resulted in Rebuild Hardware Database being skipped.
[    4.058588] systemd[1]: Condition check resulted in Platform Persistent Storage Archival being skipped.
[    4.062594] ipmi device interface
[    4.063305] systemd[1]: Starting Load/Save Random Seed...
         Starting Load/Save Random Seed...
[    4.070159] systemd[1]: Starting Create System Users...
         Starting Create System Users...
[    4.074369] systemd[1]: modprobe@drm.service: Succeeded.
[    4.079589] systemd[1]: Finished Load Kernel Module drm.
[  OK  ] Finished Load Kernel Module drm.
[    4.087037] systemd[1]: Finished Load Kernel Modules.
[  OK  ] Finished Load Kernel Modules.
[    4.092847] systemd[1]: Mounting FUSE Control File System...
         Mounting FUSE Control File System...
[    4.096808] systemd[1]: Mounting Kernel Configuration File System...
         Mounting Kernel Configuration File System...
[    4.105370] systemd[1]: Starting Apply Kernel Variables...
         Starting Apply Kernel Variables...
[    4.111405] systemd[1]: Mounted FUSE Control File System.
[  OK  ] Mounted FUSE Control File System.
[    4.119657] systemd[1]: Mounted Kernel Configuration File System.
[  OK  ] Mounted Kernel Configuration File System.
[    4.124678] systemd[1]: Finished Load/Save Random Seed.
[  OK  ] Finished Load/Save Random Seed.
[    4.135160] systemd[1]: Finished Monitoring of LVM2 mirrors, snapshots etc. using dmeventd or progress polling.
[  OK  ] Finished Monitoring of LVM dmeventd or progress polling.
[    4.143898] systemd[1]: Finished Create System Users.
[  OK  ] Finished Create System Users.
[    4.149956] systemd[1]: Starting Create Static Device Nodes in /dev...
         Starting Create Static Device Nodes in /dev...
[  OK  ] Finished Apply Kernel Variables.
[  OK  ] Finished Create Static Device Nodes in /dev.
         Starting udev Kernel Device Manager...
[  OK  ] Started Journal Service.
         Starting Flush Journal to Persistent Storage...
[  OK  ] Started udev Kernel Device Manager.
[  OK  ] Finished udev Coldplug all Devices.
         Starting udev Wait for Complete Device Initialization...
[  OK  ] Finished Set the console keyboard layout.
[  OK  ] Started Dispatch Password …ts to Console Directory Watch.
[  OK  ] Reached target Local Encrypted Volumes.
[  OK  ] Finished Flush Journal to Persistent Storage.
[  OK  ] Found device /dev/ttyS0.
[  OK  ] Listening on Load/Save RF …itch Status /dev/rfkill Watch.
[  OK  ] Found device QEMU_HARDDISK UEFI.
[  OK  ] Finished udev Wait for Complete Device Initialization.
         Starting Device-Mapper Multipath Device Controller...
[  OK  ] Started Device-Mapper Multipath Device Controller.
[  OK  ] Reached target Local File Systems (Pre).
         Mounting Mount unit for core20, revision 1270...
         Mounting Mount unit for core20, revision 1376...
         Mounting Mount unit for lxd, revision 21835...
         Mounting Mount unit for lxd, revision 22526...
         Mounting Mount unit for snapd, revision 15177...
         Starting File System Check on /dev/disk/by-label/UEFI...
[  OK  ] Started File System Check Daemon to report status.
[  OK  ] Mounted Mount unit for lxd, revision 21835.
[  OK  ] Mounted Mount unit for core20, revision 1376.
[  OK  ] Mounted Mount unit for snapd, revision 15177.
[  OK  ] Mounted Mount unit for core20, revision 1270.
[  OK  ] Mounted Mount unit for lxd, revision 22526.
[  OK  ] Finished File System Check on /dev/disk/by-label/UEFI.
         Mounting /boot/efi...
[  OK  ] Mounted /boot/efi.
[  OK  ] Reached target Local File Systems.
         Starting Load AppArmor profiles...
         Starting Set console font and keymap...
         Starting Create final runt…dir for shutdown pivot root...
         Starting Tell Plymouth To Write Out Runtime Data...
         Starting Create Volatile Files and Directories...
[  OK  ] Finished Create final runt…e dir for shutdown pivot root.
[  OK  ] Finished Set console font and keymap.
[  OK  ] Finished Tell Plymouth To Write Out Runtime Data.
[  OK  ] Finished Create Volatile Files and Directories.
         Starting Network Time Synchronization...
         Starting Update UTMP about System Boot/Shutdown...
[  OK  ] Finished Update UTMP about System Boot/Shutdown.
[  OK  ] Finished Load AppArmor profiles.
         Starting Load AppArmor pro…managed internally by snapd...
#初始化网络
         Starting Initial cloud-init job (pre-networking)...
[  OK  ] Finished Load AppArmor pro…s managed internally by snapd.
[  OK  ] Started Network Time Synchronization.
[  OK  ] Reached target System Time Set.
[  OK  ] Reached target System Time Synchronized.
[    7.044738] cloud-init[521]: Cloud-init v. 21.4-0ubuntu1~20.04.1 running 'init-local' at Tue, 22 Mar 2022 08:06:54 +0000. Up 6.87 seconds.
[  OK  ] Finished Initial cloud-init job (pre-networking).
[  OK  ] Reached target Network (Pre).
         Starting Network Service...
[  OK  ] Started Network Service.
         Starting Wait for Network to be Configured...
         Starting Network Name Resolution...
[  OK  ] Started Network Name Resolution.
[  OK  ] Reached target Network.
[  OK  ] Reached target Host and Network Name Lookups.
[  OK  ] Finished Wait for Network to be Configured.
         Starting Initial cloud-ini… (metadata service crawler)...
[    9.645809] cloud-init[541]: Cloud-init v. 21.4-0ubuntu1~20.04.1 running 'init' at Tue, 22 Mar 2022 08:06:56 +0000. Up 9.53 seconds.
[    9.647124] cloud-init[541]: ci-info: +++++++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++++++
[    9.648486] cloud-init[541]: ci-info: +--------+------+------------------------------+---------------+--------+-------------------+
[    9.652580] cloud-init[541]: ci-info: | Device |  Up  |           Address            |      Mask     | Scope  |     Hw-Address    |
[    9.653702] cloud-init[541]: ci-info: +--------+------+------------------------------+---------------+--------+-------------------+
[    9.655227] cloud-init[541]: ci-info: |  eth0  | True |         10.13.14.107         | 255.255.255.0 | global | ac:bd:ef:f3:fb:b7 |
[    9.656795] cloud-init[541]: ci-info: |  eth0  | True | fe80::aebd:efff:fef3:fbb7/64 |       .       |  link  | ac:bd:ef:f3:fb:b7 |
[    9.658218] cloud-init[541]: ci-info: |   lo   | True |          127.0.0.1           |   255.0.0.0   |  host  |         .         |
[    9.660497] cloud-init[541]: ci-info: |   lo   | True |           ::1/128            |       .       |  host  |         .         |
[    9.661837] cloud-init[541]: ci-info: +--------+------+------------------------------+---------------+--------+-------------------+
[    9.664209] cloud-init[541]: ci-info: ++++++++++++++++++++++++++++++Route IPv4 info++++++++++++++++++++++++++++++
[    9.667326] cloud-init[541]: ci-info: +-------+-------------+-------------+-----------------+-----------+-------+
[    9.671211] cloud-init[541]: ci-info: | Route | Destination |   Gateway   |     Genmask     | Interface | Flags |
[    9.673538] cloud-init[541]: ci-info: +-------+-------------+-------------+-----------------+-----------+-------+
[    9.675636] cloud-init[541]: ci-info: |   0   |   0.0.0.0   | 10.13.14.99 |     0.0.0.0     |    eth0   |   UG  |
[    9.678032] cloud-init[541]: ci-info: |   1   |  10.13.14.0 |   0.0.0.0   |  255.255.255.0  |    eth0   |   U   |
[    9.681857] cloud-init[541]: ci-info: |   2   | 10.13.14.99 |   0.0.0.0   | 255.255.255.255 |    eth0   |   UH  |
[    9.685593] cloud-init[541]: ci-info: +-------+-------------+-------------+-----------------+-----------+-------+
[    9.687086] cloud-init[541]: ci-info: +++++++++++++++++++Route IPv6 info+++++++++++++++++++
[    9.689285] cloud-init[541]: ci-info: +-------+-------------+---------+-----------+-------+
[    9.692577] cloud-init[541]: ci-info: | Route | Destination | Gateway | Interface | Flags |
[    9.693515] cloud-init[541]: ci-info: +-------+-------------+---------+-----------+-------+
[    9.695989] cloud-init[541]: ci-info: |   1   |  fe80::/64  |    ::   |    eth0   |   U   |
[    9.698265] cloud-init[541]: ci-info: |   3   |    local    |    ::   |    eth0   |   U   |
[    9.699175] cloud-init[541]: ci-info: |   4   |  multicast  |    ::   |    eth0   |   U   |
[    9.700140] cloud-init[541]: ci-info: +-------+-------------+---------+-----------+-------+
[  OK  ] Finished Initial cloud-ini…ob (metadata service crawler).
[  OK  ] Reached target Cloud-config availability.
[  OK  ] Reached target Network is Online.
[  OK  ] Reached target System Initialization.
[  OK  ] Started Trigger to poll fo…y enabled on GCP LTS non-pro).
[  OK  ] Started Daily apt download activities.
[  OK  ] Started Daily apt upgrade and clean activities.
[  OK  ] Started Periodic ext4 Onli…ata Check for All Filesystems.
[  OK  ] Started Discard unused blocks once a week.
[  OK  ] Started Refresh fwupd metadata regularly.
[  OK  ] Started Daily rotation of log files.
[  OK  ] Started Daily man-db regeneration.
[  OK  ] Started Message of the Day.
[  OK  ] Started Daily Cleanup of Temporary Directories.
[  OK  ] Started Ubuntu Advantage Timer for running repeated jobs.
[  OK  ] Reached target Paths.
[  OK  ] Reached target Timers.
[  OK  ] Listening on cloud-init hotplug hook socket.
[  OK  ] Listening on D-Bus System Message Bus Socket.
[  OK  ] Listening on Open-iSCSI iscsid Socket.
[  OK  ] Listening on Socket unix for snap application lxd.daemon.
         Starting Socket activation for snappy daemon.
[  OK  ] Listening on UUID daemon activation socket.
[  OK  ] Reached target Remote File Systems (Pre).
[  OK  ] Reached target Remote File Systems.
         Starting Availability of block devices...
[  OK  ] Listening on Socket activation for snappy daemon.
[  OK  ] Reached target Sockets.
[  OK  ] Reached target Basic System.
         Starting Accounts Service...
         Starting LSB: automatic crash report generation...
         Starting Deferred execution scheduler...
[  OK  ] Started Regular background program processing daemon.
[  OK  ] Started D-Bus System Message Bus.
[  OK  ] Started Save initial kernel messages after boot.
         Starting Remove Stale Onli…t4 Metadata Check Snapshots...
         Starting Record successful boot for GRUB...
[  OK  ] Started irqbalance daemon.
         Starting Dispatcher daemon for systemd-networkd...
         Starting A high performanc… and a reverse proxy server...
         Starting Authorization Manager...
[  OK  ] Started QEMU Guest Agent.
         Starting System Logging Service...
         Starting Service for snap application lxd.activate...
         Starting Snap Daemon...
         Starting OpenBSD Secure Shell server...
         Starting Login Service...
         Starting Permit User Sessions...
         Starting Disk Manager...
[  OK  ] Finished Availability of block devices.
[  OK  ] Started Deferred execution scheduler.
[  OK  ] Finished Permit User Sessions.
         Starting Hold until boot process finishes up...
         Starting Terminate Plymouth Boot Screen...
[  OK  ] Finished Hold until boot process finishes up.
[  OK  ] Started Serial Getty on ttyS0.
         Starting Set console scheme...
[  OK  ] Finished Terminate Plymouth Boot Screen.
[  OK  ] Finished Set console scheme.
[  OK  ] Created slice system-getty.slice.
[  OK  ] Started Getty on tty1.
[  OK  ] Reached target Login Prompts.
[  OK  ] Started System Logging Service.
[  OK  ] Finished Record successful boot for GRUB.
         Starting GRUB failed boot detection...
[  OK  ] Finished GRUB failed boot detection.
[  OK  ] Finished Remove Stale Onli…ext4 Metadata Check Snapshots.
[  OK  ] Started LSB: automatic crash report generation.
[  OK  ] Started Authorization Manager.
         Starting Modem Manager...
[  OK  ] Started OpenBSD Secure Shell server.
[  OK  ] Started A high performanceer and a reverse proxy server.
[  OK  ] Started Accounts Service.
[  OK  ] Started Modem Manager.
[  OK  ] Started Login Service.
[  OK  ] Started Unattended Upgrades Shutdown.
[  OK  ] Started Disk Manager.
[  OK  ] Started Dispatcher daemon for systemd-networkd.
[  OK  ] Started Snap Daemon.
         Starting Wait until snapd is fully seeded...
         Starting Time & Date Service...
[  OK  ] Started Time & Date Service.
[  OK  ] Finished Wait until snapd is fully seeded.
         Starting Apply the settings specified in cloud-config...
[  OK  ] Finished Service for snap application lxd.activate.
[  OK  ] Reached target Multi-User System.
[  OK  ] Reached target Graphical Interface.
         Starting Update UTMP about System Runlevel Changes...
[  OK  ] Finished Update UTMP about System Runlevel Changes.
[   13.785415] cloud-init[780]: Cloud-init v. 21.4-0ubuntu1~20.04.1 running 'modules:config' at Tue, 22 Mar 2022 08:07:01 +0000. Up 13.68 seconds.
[  OK  ] Finished Apply the settings specified in cloud-config.
         Starting Execute cloud user/final scripts...
[   14.274127] cloud-init[785]: Cloud-init v. 21.4-0ubuntu1~20.04.1 running 'modules:final' at Tue, 22 Mar 2022 08:07:01 +0000. Up 14.16 seconds.
#cloud-init完成
[   14.275588] cloud-init[785]: Cloud-init v. 21.4-0ubuntu1~20.04.1 finished at Tue, 22 Mar 2022 08:07:01 +0000. Datasource DataSourceNoCloud [seed=/dev/sr0][dsmode=net].  Up 14.26 seconds
[  OK  ] Finished Execute cloud user/final scripts.
[  OK  ] Reached target Cloud-init target.

Ubuntu 20.04.4 LTS cit22 ttyS0

cit22 login: 

 

4. 使用Cloud-init

 

如何使用cloud-init可以参考

https://pve.proxmox.com/wiki/Cloud-Init_Support

在Proxmox VE pve里使用cloud-init 构建(centos\ubuntu\debian)cloud images (buduanwang.vip)

5. Cloud-init自定义

PVE本身的Cloud-init功能比较基本。对Linux虚拟机支持比较好。在Windows支持上还比较弱。

从上文中,可以看到Cloud-init有4个组件

  • meta-data
  • network-config
  • user-data
  • vendor-data

我们可以使用命令

qm cloudinit dump <vmid> <user | network | meta>

来获取VM的默认配置,如userdata

root@pve1:~#  qm cloudinit dump 1023 user
#cloud-config
hostname: cit22
manage_etc_hosts: true
user: root
disable_root: False
password: $5$TjNmfaZD$vrXdMmki97V05ZSnuL2G07UotLGb.s/Q2UnhlNesOn.
chpasswd:
  expire: False
package_upgrade: true

如network

root@pve1:~#  qm cloudinit dump 1023 network
version: 1
config:
    - type: physical
      name: eth0
      mac_address: 'ac:bd:ef:f3:fb:b7'
      subnets:
      - type: dhcp4
      - type: ipv6_slaac
    - type: nameserver
      address:
      - '10.13.14.51'
      search:
      - 'testtest.com'

如meta-data

root@pve1:~#  qm cloudinit dump 1023 meta
instance-id: 76ee35d56704bb4d168e99e3eba8af8b32aa8674

有时候官方的默认配置无法满足要求。可以自定义元数据。如下面

qm set 9000 --cicustom "user=<volume>,network=<volume>,meta=<volume>"

自定义配置文件,需要放置到snippets文件夹下,如

qm set 9000 --cicustom "user=local:snippets/userconfig.yaml" 

自定义配置文件在PVE上有如下特性:

  • 自定义数据会覆盖默认数据
  • 未配置时使用默认数据

比如。自定义了userconfig.yaml数据。那么VM配置文件中user信息将不可用。

但其他的如network、meta信息仍然可用。

这种方式比较适用于程序环境,小型环境显而易见比较繁琐。

有个简单的方法,就是修改PVE的Cloud-init的生成代码。

编辑/usr/share/perl5/PVE/QemuServer/Cloudinit.pm 文件(注意!请备份后再修改)

如下面的片段就是一个userdata

sub cloudinit_userdata {
    my ($conf, $vmid) = @_;

    my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);

    my $content = "#cloud-config\n";

    $content .= "hostname: $hostname\n";
    $content .= "manage_etc_hosts: true\n";
    $content .= "fqdn: $fqdn\n" if defined($fqdn);

    my $username = $conf->{ciuser};
    my $password = $conf->{cipassword};

    $content .= "user: $username\n" if defined($username);
    $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
    $content .= "password: $password\n" if defined($password);

    if (defined(my $keys = $conf->{sshkeys})) {
        $keys = URI::Escape::uri_unescape($keys);
        $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)];
        $keys = [grep { /\S/ } @$keys];
        $content .= "ssh_authorized_keys:\n";
        foreach my $k (@$keys) {
            $content .= "  - $k\n";
        }
    }
    $content .= "chpasswd:\n";
    $content .= "  expire: False\n";

    if (!defined($username) || $username ne 'root') {
        $content .= "users:\n";
        $content .= "  - default\n";
    }

    $content .= "package_upgrade: true\n";

    return $content;
}

修改之后,执行systemctl restart pvedaemon

若要对Windows虚拟机配置Cloud-init,请参考Windows系统初始化工具Cloudbase-init

版权声明:
作者:佛西
链接:https://foxi.buduanwang.vip/virtualization/pve/1636.html/
文章版权归作者所有,未经允许请勿转载
如需获得支持,请点击网页右上角
THE END
分享
二维码
海报
Proxmox VE与Cloud-init
认识Cloud-init Cloud-Init是一个用于初始化虚拟机的工具。使用Cloud-Init可以在虚拟机管理程序端配置网络设备、ssh 密钥以及用户信息等。当 VM 首次启动时,V……
<<上一篇
下一篇>>
文章目录
关闭
目 录