使用qemu-system-x86_64和cloud-init修改qcow2镜像密码

方法来自于:Core tutorial with QEMU

依次执行下面的命令

sudo apt install qemu-system-x86

mkdir temp
cd temp

# 以此镜像为例
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img

cat << EOF > user-data
#cloud-config
password: 123
chpasswd:
  expire: False

EOF

cat << EOF > meta-data
instance-id: test

EOF

touch vendor-data

开启新窗口执行命令:

cd temp
python3 -m http.server --directory .

切回原来的窗口

qemu-system-x86_64                                            \
    -net nic                                                    \
    -net user                                                   \
    -machine accel=kvm:tcg                                      \
    -cpu host                                                   \
    -m 512                                                      \
    -nographic                                                  \
    -hda jammy-server-cloudimg-amd64.img                        \
    -smbios type=1,serial=ds='nocloud;s=http://10.0.2.2:8000/'

如果虚拟机正常起来了,使用 ubuntu/123 可以登录系统,此时这个镜像的ubuntu密码就修改成功

或者使用命令:

virt-customize -a al2023-kvm-2023.4.20240429.0-kernel-6.1-arm64.xfs.gpt.qcow2 --root-password password:123456
posted @ 2024-05-15 17:27  武平宁  阅读(11)  评论(0编辑  收藏  举报