平台介绍

Win11 + WSL2 Ubunut 24.04

步骤

安装 QEMU 和镜像配置工具:

1
2
sudo apt install qemu-system
sudo apt install cloud-image-utils

由于 WSL 无图形输出,这里选择使用 Ubuntu Cloud Image 进行无图形化版本安装。

下载 Ubuntu Cloud Image,这里选择 24.04/release-20240725 版本。

下载 UEFI 固件镜像 QEMU_EFI.fd,这里选择 linaro/16.02/qemu64 版本。

创建配置镜像文件(配置 ssh 等,ubuntu cloud image 初次只能使用 ssh 登录),将以下内容写入到 cloud.txt

1
2
3
4
5
6
7
8
#cloud-config
users:
- name: your_username
ssh-authorized-keys:
- ssh-rsa your_ssh_pub_key
sudo: ['ALL=(ALL) NOPASSWD:ALL']
groups: sudo
shell: /bin/bash

构建配置镜像:

1
cloud-localds --disk-format qcow2 cloud.img cloud.txt

启动 ubuntu:

1
qemu-system-aarch64 -smp 4 -m 8G -M virt -cpu cortex-a76 -nographic -device virtio-blk-device,drive=image -drive if=none,id=image,file=ubuntu-24.04-server-cloudimg-arm64.img -device virtio-blk-device,drive=cloud -drive if=none,id=cloud,file=cloud.img -bios QEMU_EFI.fd -netdev user,id=user0 -net user,hostfwd=tcp::2222-:22 -net nic

参数解释:

  • qemu-system-aarch64:启动QEMU仿真器并指定使用aarch64(64位ARM架构)系统。
  • -smp 4:配置虚拟机使用4个虚拟CPU。
  • -m 8G:配置虚拟机使用8GB内存。
  • -M virt:使用名为 virt 的虚拟机器类型,这是一种用于ARM虚拟化的通用机器类型。
  • -cpu cortex-a76:指定虚拟机的CPU型号为Cortex-A76,这是一种常见的ARM CPU型号。
  • -nographic:禁用图形输出,使虚拟机只使用命令行模式(无图形界面)。
  • -device virtio-blk-device,drive=image:使用 virtio 块设备,关联到后续指定的 image 驱动。
  • -drive if=none,id=image,file=ubuntu-24.04-server-cloudimg-arm64.img:配置一个驱动,不连接到标准接口(if=none),给它一个 ID 为 image,并指定映像文件为 ubuntu-24.04-server-cloudimg-arm64.img。这通常是操作系统的镜像文件。
  • -device virtio-blk-device,drive=cloud:使用另一个 virtio 块设备,关联到后续指定的 cloud 驱动。
  • -drive if=none,id=cloud,file=cloud.img:配置另一个驱动,不连接到标准接口(if=none),给它一个 ID 为 cloud,并指定映像文件为 cloud.img。这通常是云初始化驱动器映像,用于提供元数据和用户数据。
  • -net user,hostfwd=tcp::2222-:22:使用用户模式网络,配置主机端口 2222 转发到虚拟机端口 22。这样您可以通过 ssh -p 2222 user@localhost 来访问虚拟机的 SSH 服务。
  • -net nic:创建一个虚拟的网卡,使虚拟机能够使用前面配置的用户模式网络。

启动后,使用当前用户(your_username)以及公钥(your_ssh_pub_key)连接 ubuntu:

1
ssh localhost -p 2222

参考资料

QEMU arm64 cloud server emulation