第 4 章 Creating a virtual machine with cloud-init
To create a new virtual machine (VM) that includes cloud-init, create a meta-data file and a user-data file.
-
The
meta-datafile includes instance details. -
The
user-datafile includes information to create a user and grant access.
Include these files in a new ISO image, and attach the ISO file to a new VM created from a KVM Guest Image. In this scenario, the datasource is NoCloud.
Procedure
Create a
cloudinitisodirectory and set it as your working directory:$ mkdir cloudinitiso $ cd cloudinitisoEdit the
meta-datafile:$ vi meta-data instance-id: citest local-hostname: citest-1Edit the
user-datafile:$ vi user-data #cloud-config password: cilogon chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - <ssh-rsa AAA...fhHQ== sample@example.com>注意You can find your SSH public keys in the
~/.ssh/id_rsa.pubfile.Create an ISO image that includes
user-dataandmeta-data:# genisoimage -output ciiso.iso -volid cidata -joliet -rock user-data meta-data I: -input-charset not specified, using utf-8 (detected in locale settings) Total translation table size: 0 Total rockridge attributes bytes: 331 Total directory bytes: 0 Path table size(bytes): 10 Max brk space used 0 183 extents written (0 MB)-
Download a KVM Guest Image from the Red Hat Customer Portal to the
/var/lib/libvirt/imagesdirectory. Create a new VM from the KVM Guest Image using the
virt-installutility and attach the downloaded image to the existing image:# virt-install \ --memory 4096 \ --vcpus 4 \ --name mytestcivm \ --disk /var/lib/libvirt/images/rhel-8.1-x86_64-kvm.qcow2,device=disk,bus=virtio,format=qcow2 \ --disk /home/sample/cloudinitiso/ciiso.iso,device=cdrom \ --os-type Linux \ --os-variant rhel10.0 \ --virt-type kvm \ --graphics none \ --importLog on to your image with the default username
cloud-userand default passwordcilogon:citest-1 login: cloud-user Password: [cloud-user@citest-1 ~]$
Verification
Check the status of the
cloud-initservice to confirm that the utility has completed its defined tasks:[cloud-user@citest-1 instance]$ cloud-init status status: doneThe
cloud-initutility creates thecloud-initdirectory layout under/var/lib/cloudwhen it runs, and it updates or changes certain directory contents based upon the directives you have specified.For example, you can confirm that the datasource is
NoCloudby checking the datasource file.$ cd /var/lib/cloud/instance $ cat datasource DataSourceNoCloud: DataSourceNoCloud [seed=/dev/sr0][dsmode=net]cloud-initcopies user-data into/var/lib/cloud/instance/user-data.txt:$ cat user-data.txt #cloud-config password: cilogon chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...fhHQ== sample@redhat.com
For OpenStack, the Creating and managing instances includes information for configuring an instance using cloud-init. See Creating a customized instance for specific procedures.