M1 Mac 通过 QEMU 安装 ubuntu 虚拟机。为了更好地模拟、测试 i386,特意选择了最老的系统版本。

VirtualBox 目前在 M1 还是开发预览版,无法正常模拟系统,所以先放弃了。

Ubuntu 下载

通过 ubuntu 官网的 Other images and mirrors,选择一个离你较近的中国镜像,比如:清华大学

为了减少体积,选择服务器版本 ubuntu-16.04.6-server-i386.iso 进行下载。

ubuntu-14.04.6-server-i386.iso 在安装完成后,apt-get install gcc 会出现问题。

安装系统

参考 how-to-run-ubuntu-desktop-on-qemu

qcow2 文件

# 创建 10 个 GB 的磁盘
$ qemu-img create -f qcow2 "ubuntu16.img.qcow2" 10G

# 安装系统
#   指定磁盘、系统光盘
#   内存设置为 2G,默认 128M,不够用
$ qemu-system-i386 \
  -cdrom ./ubuntu-16.04.6-server-i386.iso \
  -drive "file=./ubuntu16.img.qcow2,format=qcow2" \
  -m 2G

# 启动系统
#   开启端口映射,方便 SSH,自带的窗口会捕获鼠标等,不是特别友好
#     使用 SSH 自己的终端,就能复制粘贴什么的,都很方便,参考 how-to-ssh-from-host-to-guest-using-qemu
#   开启 monitor,方便查看系统信息
#     寄存器啥的,参考 https://wiki.osdev.org/Qemu_Monitor
$ qemu-system-i386 \
  -drive "file=./ubuntu16.img.qcow2,format=qcow2" \
  -m 2G \
  -nic user,hostfwd=tcp::10022-:22 \
  -monitor stdio



$ qemu-system-x86_64 \
  -drive "file=./ubuntu22.img.qcow2,format=qcow2" \
  -m 2G \
  -nic user,hostfwd=tcp::10022-:22 \
  -monitor stdio

QEMU Monitor 指令

参考 Qemu_Monitor

# 显示寄存器
(qemu) info registers

# 读取内存
(qemu) x/32x 0xf67c400

QEMU 命令

# 使用 qemu-img 转 virtualbox 能使用的格式
$ qemu-img convert -O vdi ubuntu.img.qcow2 ubuntu.vdi

连接虚拟机

# 当使用上述命令开启 22 之后,就能正常连接
$ qemu-system-i386 ... -nic user,hostfwd=tcp::10022-:22 ...

# 安装 ssh
#   ubuntu 22 版本有自带的版本,14 版本需要自己安装一下
#   参考:https://linuxize.com/post/how-to-enable-ssh-on-ubuntu-20-04/
#   apt-get 加速可使用中科大,参考:https://mirrors.ustc.edu.cn/help/ubuntu.html
$ sudo apt update
$ sudo apt install openssh-server

# 常用工具
$ sudo apt install gcc
$ sudo apt install nasm

# 使用 ssh 连接
$ ssh xxx@localhost -p10022

References