virt-install实现cloudinit

蒜香大龙虾 2024-08-08 16:35:54
Categories: Tags:

下载cloudinit镜像

此处下载rocky9的GenericCloud-Base镜像。

传送门

创建虚拟机

拷贝镜像到libvirt目录

默认位置为/var/lib/libvirt/images/

1
cp Rocky-9-GenericCloud-Base-9.4-20240609.1.x86_64.qcow2 /var/lib/libvirt/images/linux1.qcow2

使用virt-install创建

--cloud-init的子选项如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root-password-generate=on
Generate a new root password for the VM. When used, virt-install will print the generated password to the console, and pause for 10 seconds to give the user a chance to notice it and copy it.

disable=on
Disable cloud-init in the VM for subsequent boots. Without this, cloud-init may reset auth on each boot.

root-password-file=
A file used to set the VM root password from. This option can be used either as "root-password-file=/path/to/password-file" or as "root-password-file=/dev/fd/n", being n the file descriptor of the password-file. Note that only the first line of the file will be considered, including any whitespace characters and excluding new-line.

meta-data=
Specify a cloud-init meta-data file to add directly to the iso. All other meta-data configuration options on the --cloud-init command line are ignored.

user-data=
Specify a cloud-init user-data file to add directly to the iso. All other user-data configuration options on the --cloud-init command line are ignored.

root-ssh-key=
Specify a public key to inject into the guest, providing ssh access to the root account. Example: root-ssh-key=/home/user/.ssh/id_rsa.pub

clouduser-ssh-key
Specify a public key to inject into the guest, providing ssh access to the default cloud-init user account. The account name is different per distro cloud image. Some common ones are documented here: https://docs.openstack.org/image-guide/obtain-images.html

network-config=
Specify a cloud-init network-config file to add directly to the iso.

大致翻译概括:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root-password-generate=on
生成虚拟机的新 root 密码。使用此选项时,virt-install 会将生成的密码打印到控制台,并暂停 10 秒,以便用户有机会注意到并复制密码。

disable=on
在虚拟机的后续启动中禁用 cloud-init。如果不这样做,cloud-init 可能会在每次启动时重置认证。

root-password-file=
使用文件设置虚拟机的 root 密码。此选项可以这样使用:"root-password-file=/path/to/password-file" 或者 "root-password-file=/dev/fd/n",其中 n 是密码文件的文件描述符。请注意,只有文件的第一行会被考虑,包括任何空白字符,并且不包括新行。

meta-data=
指定一个 cloud-init 元数据文件直接添加到 ISO 中。所有其他在 --cloud-init 命令行上的元数据配置选项都将被忽略。

user-data=
指定一个 cloud-init 用户数据文件直接添加到 ISO 中。所有其他在 --cloud-init 命令行上的用户数据配置选项都将被忽略。

root-ssh-key=
指定一个公钥注入到客户机中,提供对 root 账户的 SSH 访问权限。示例:root-ssh-key=/home/user/.ssh/id_rsa.pub

clouduser-ssh-key
指定一个公钥注入到客户机中,提供对默认的 cloud-init 用户账户的 SSH 访问权限。账户名称因发行版的云镜像而异。

network-config=
指定一个 cloud-init 网络配置文件直接添加到 ISO 中。

生成一个虚拟机,并把本机的公钥传入

1
2
3
4
5
6
7
8
virt-install \
--name cloud-init-linux1 \
--memory 2048 \
--vcpus 2 \
--disk=size=40,backing_store="$(pwd)/Rocky-9-GenericCloud-Base-9.4-20240609.1.x86_64.qcow2" \
--osinfo rocky9 \
--cloud-init root-ssh-key=/home/cshrimp/.ssh/id_ed25519.pub \
--noautoconsole

生成一个虚拟机,并把本机的公钥传入,再生成root密码

1
2
3
4
5
6
7
8
virt-install \
--name cloud-init-linux2 \
--memory 2048 \
--vcpus 2 \
--disk=size=40,backing_store="$(pwd)/Rocky-9-GenericCloud-Base-9.4-20240609.1.x86_64.qcow2" \
--osinfo rocky9 \
--cloud-init root-ssh-key=/home/cshrimp/.ssh/id_ed25519.pub,root-password-generate=on \
--noautoconsole

反馈如下

1
2
3
4
5
6
7
[cshrimp-a300 ~]# virt-install --name cloud-init-linux2 --memory 2048 --vcpus 2 --disk=size=40,backing_store="$(pwd)/Rocky-9-GenericCloud-Base-9.4-20240609.1.x86_64.qcow2" --osinfo rocky9 --cloud-init root-ssh-key=/home/cshrimp/.ssh/id_ed25519.pub,root-password-generate=on --noautoconsole 

Starting install...
Password for first root login is: b5lqFbPwLNQT5vKI <-----------------密码在这
Allocating 'cloud-init-linux2.qcow2' | 0 B 00:00:00 ...
Creating domain... | 0 B 00:00:00
Domain creation completed.

尝试登陆cloud-init-linux2

1
2
3
4
5
6
7
8
9
10
11
[cshrimp-a300 ~]# virsh console cloud-init-linux2
Connected to domain 'cloud-init-linux2'
Escape character is ^] (Ctrl + ])

localhost login: root
Password: b5lqFbPwLNQT5vKI
You are required to change your password immediately (administrator enforced).
Current password: b5lqFbPwLNQT5vKI
New password: your_new_password
Retype new password: your_new_password
[root@localhost ~]#

查看虚拟机ip

1
2
3
4
[cshrimp-a300 ~]# virsh domifaddr cloud-init-linux2
Name MAC address Protocol Address
-------------------------------------------------------------------------------
vnet4 52:54:00:a3:0e:a1 ipv4 192.168.122.108/24

尝试用ssh登陆

1
2
3
4
5
6
7
8
9
10
[cshrimp@cshrimp-a300 ~]$ ssh [email protected]
The authenticity of host '192.168.122.108 (192.168.122.108)' can't be established.
ED25519 key fingerprint is SHA256:r2B7xMRj2qNgUlsywg2QosnNcXeBykdYHkMa2L1A3Js.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.122.108' (ED25519) to the list of known hosts.
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Thu Aug 8 09:31:21 2024
[root@localhost ~]#

无需输入密码即可连接,证明ssh成功使用证书登陆。