qemu: aarch64 + ubuntu image

Working example

0.Prepare

创建临时目录,后续所有操作在此目录进行

mkdir -p ~/tmp/qemu-test
cd ~/tmp/qemu-test

1.安装qemu

ubuntu可以使用apt install.
通用的方式从源代码进行编译:

wget https://download.qemu.org/qemu-7.0.0.tar.xz
tar xvJf qemu-7.0.0.tar.xz
cd qemu-7.0.0
# 如果出现python相关错误,使用./configure --python=/usr/local/bin/python3
./configure
make

# 将编译好的文件复制到~/installed/qemu-7.0.0/bin
mkdir -p ~/installed/qemu-7.0.0/bin
cp -R ~/tmp/qemu-test/qemu-7.0.0/build ~/installed/qemu-7.0.0/bin

export PATH=$PATH:~/installed/qemu-7.0.0/bin

https://www.qemu.org/download/

2.下载资源

cd ~/tmp/qemu-test

curl -sLo QEMU_EFI_2.fd https://github.com/rohan-qemu/firmware/raw/master/bios/QEMU_EFI.fd
wget https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-arm64.img

# 备份ubuntu-22.04-server-cloudimg-arm64.img
# 后面会被qemu修改
cp ubuntu-22.04-server-cloudimg-arm64.img ubuntu-22.04-server-cloudimg-arm64.img.orig

bios
https://futurewei-cloud.github.io/ARM-Datacenter/qemu/how-to-launch-aarch64-vm/

# download QEMU_EFI.fd
# borrow from https://gist.github.com/oznu/ac9efae7c24fd1f37f1d933254587aa4
wget https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/QEMU_EFI.fd

3.生成img

qemu-img create ubuntu-image.img 20G

生成user-data image
https://gist.github.com/oznu/ac9efae7c24fd1f37f1d933254587aa4

cat > cloud.txt <<'EOF'
#cloud-config
users:
  - name: oznu
    ssh-authorized-keys:
      - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC...
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
    groups: sudo
    shell: /bin/bash
EOF

# cloud-localds: apt install cloud-image-utils
cloud-localds --disk-format qcow2 user-data-qcow2.img cloud.txt

4.使用virt-customize更改img的password

注意:如果你没有ubuntu环境,你可以使用virtualbox安装ubuntu虚拟机之后执行这个命令

https://serverascode.com/2018/06/26/using-cloud-images.html

# 安装: sudo apt install guestfs-tools
sudo virt-customize -a ubuntu-22.04-server-cloudimg-arm64.img --root-password password:1111

注: --root-password还有file:SOMEFILE的格式。

5. 启动

qemu-system-aarch64 \
    -nographic \
    -machine virt \
    -m 1024M \
    -cpu cortex-a57 \
    -bios ./qemu-7.0.0/QEMU_EFI_2.fd \
    -device virtio-blk-device,drive=image \
    -drive if=none,file=ubuntu-22.04-server-cloudimg-arm64.img,id=image \
    -device virtio-blk-device,drive=cloud \
    -drive if=none,file=user-data-qcow2.img,id=cloud \
    -device virtio-blk-device,drive=hd1 \
    -drive if=none,file=ubuntu-image.img,id=hd1,format=raw \
    -net user,id=user0,hostfwd=tcp::30022-:22 \
    -net nic \
    -L ~/installed/qemu-7.0.0/pc-bios/ \
    -vnc :3

如果报错找不到efi,添加-L ~/installed/qemu-7.0.0/pc-bios/, 注意目录指向你的安装目录.

如果界面卡在EFI stub这里,请确保QEMU_EFI_2.fd不是默认的qemu自带的文件,而是前面下载的内容。
在这里插入图片描述

5.使用root登录,密码:1111

在这里插入图片描述

在这里插入图片描述

使用ssh登录

证书

在虚拟机上,更改/etc/ssh/sshd_config, 使其包含PubkeyAuthentication yes
然后重启服务:

vim /etc/ssh/sshd_config
...

sudo systemctl restart sshd.service

生成key/pub密钥对,然后将pub复制到~/.ssh/authroized_keys中:

ssh-keygen -t rsa -f ~/.ssh/id_rsa_qemu
cat ~/.ssh/id_rsa_qemu.pub >> ~/.ssh/authorized_keys

将key复制到host上,改变其权限,然后登录:

...copy the content to ~/.ssh/id_rsa_qemu...
chmod g-rw ~/.ssh/id_rsa_qemu
chmod o-rw ~/.ssh/id_rsa_qemu

ssh  -p 30022 -i ~/.ssh/id_rsa_qemu root@127.0.0.1

复制文件:

scp -p 30022 -i ~/.ssh/id_rsa_qemu root@127.0.0.1

密码(不行)

在虚拟机上,更改/etc/ssh/sshd_config, 使其包含PasswordAuthentication yes
然后重启服务:

sudo systemctl restart sshd.service

登录:

ssh -vvv -p 30022 root@127.0.0.1

挂载目录

qemu-system-aarch64  \
   ... 
   -virtfs local,path=/Users/xhd2015,mount_tag=host0,security_model=mapped,id=host0 \
   ...

Historical Reference

Setup

https://futurewei-cloud.github.io/ARM-Datacenter/qemu/how-to-launch-aarch64-vm/

# download QEMU_EFI.fd
# borrow from https://gist.github.com/oznu/ac9efae7c24fd1f37f1d933254587aa4
wget https://releases.linaro.org/components/kernel/uefi-linaro/latest/release/qemu64/QEMU_EFI.fd

Setup 2(latest)

https://gist.github.com/oznu/ac9efae7c24fd1f37f1d933254587aa4

Ubuntu Boot

https://askubuntu.com/questions/281763/is-there-any-prebuilt-qemu-ubuntu-image32bit-online/1081171#1081171

ssh-able

https://gist.github.com/oznu/ac9efae7c24fd1f37f1d933254587aa4

qemu-system-aarch64 -m 1024 -smp 2 -cpu cortex-a57 -M virt -nographic \
  -pflash flash0.img \
  -pflash flash1.img \
  -drive if=none,file=ubuntu-16.04-server-cloudimg-arm64-uefi1.img,id=hd0 \
  -device virtio-blk-device,drive=hd0 \
  -drive if=none,id=cloud,file=cloud.img \
  -device virtio-blk-device,drive=cloud \
  -netdev user,id=user0 -device virtio-net-device,netdev=user0 \
  -redir tcp:2222::22

-redir已经不再支持, 使用hostfwd=hostip:hostport-guestip:guestport:

qemu-system-aarch64 -m 1024 -smp 2 -cpu cortex-a57 -M virt -nographic \
  -pflash flash0.img \
  -pflash flash1.img \
  -drive if=none,file=ubuntu-16.04-server-cloudimg-arm64-uefi1.img,id=hd0 \
  -device virtio-blk-device,drive=hd0 \
  -drive if=none,id=cloud,file=cloud.img \
  -device virtio-blk-device,drive=cloud \
  -netdev user,id=user0,hostfwd=tcp::30022-:22 \
  -device virtio-net-device,netdev=user0 \
  -net nic

root password

https://serverascode.com/2018/06/26/using-cloud-images.html

sudo virt-customize -a bionic-server-cloudimg-amd64.img --root-password password:coolpass

注: --root-password还有file:SOMEFILE的格式。

ubuntu 2022 img

https://cloud-images.ubuntu.com/releases/22.04/release/

Error:failed to find romfile “efi-virtio.rom”

添加-L ~/installed/qemu-7.0.0/pc-bios/

qemu-system-aarch64 -nographic -machine virt,gic-version=max -m 512M -cpu max -smp 4 \
-L ~/installed/qemu-7.0.0/pc-bios/ \
-netdev user,id=vnet,hostfwd=:127.0.0.1:0-:22 -device virtio-net-pci,netdev=vnet \
-drive file=ubuntu-image.img,if=none,id=drive0,cache=writeback -device virtio-blk,drive=drive0,bootindex=0 \
-drive file=mini.iso,if=none,id=drive1,cache=writeback -device virtio-blk,drive=drive1,bootindex=1 \
-drive file=flash0.img,format=raw,if=pflash -drive file=flash1.img,format=raw,if=pflash

退出qemu

C-a然后x

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值