在QEMU中安装mipsel虚拟机(Linux)

计算机系统课上需要用到 MIPS 虚拟机。在网上找到些资料,记录在这里。

注:已将配置好的镜像上传,可以直接在Linux/Windows下使用。用户名 root 密码 rootroot,装好了gdb。推荐在Linux子系统中运行。

initrd.img-4.19.0-6-4kc-malta

vmlinux-4.19.0-6-4kc-malta

disk.qcow2

Windows下使用方法:

 “C:\Program Files\qemu\qemu-system-mipsel.exe” -hda  D:\disk.qcow2  -M malta -kernel D:\vmlinux-4.19.0-6-4kc-malta  -initrd D:\initrd.img-4.19.0-6-4kc-malta  -m 512  --serial stdio -append "root=/dev/sda1 nokaslr console=ttyAMA0,115200 console=tty  highres=off console=ttyS0 "

Ubuntu系系统/Windows下的Linux子系统使用方法:

  • 安装 qemu-system-mipsel : $ sudo apt install qemu-system-mipsel -y
  • 下载上面三个文件到工作目录
wget https://ftp.mrning.com/uploads/blog/initrd.img-4.19.0-6-4kc-malta
wget https://ftp.mrning.com/uploads/blog/vmlinux-4.19.0-6-4kc-malta
wget https://ftp.mrning.com/uploads/blog/disk.qcow2
  • 执行下面的命令启动
qemu-system-mipsel -M malta -m 1G \
   -hda ./disk.qcow2 \
   -initrd ./initrd.img-4.19.0-6-4kc-malta \
   -kernel ./vmlinux-4.19.0-6-4kc-malta -append "nokaslr root=/dev/sda1" \
   -nographic

将外部文件传到虚拟机中

如何将外部文件传到虚拟机中?可以在Linux系统中挂载虚拟盘,将文件复制到虚拟盘中,参考命令如下:

注:Linux子系统缺少内核模块,没法直接弄。

# 安装需要的软件包
sudo apt install nbd-client

# 挂载虚拟磁盘 disk.qcow2 到  mnt 目录
 sudo modprobe nbd max_part=8
 sudo qemu-nbd --connect=/dev/nbd0 disk.qcow2
 mkdir mnt
 sudo mount /dev/nbd0p1 mnt  ##挂载虚拟盘到mnt目录下

## 然后你可以复制文件到虚拟磁盘里了

# 卸载虚拟盘 
 sudo umount /dev/nbd0p1
 sudo nbd-client -d /dev/nbd0

其他方法可以考虑在外部机子上搭建简易 web 服务器,在虚拟机中下载文件。也可以参考后面的启动脚本,安装并启动 ssh 服务。

如果自己安装虚拟机,可以参考以下步骤:

准备阶段

安装QEMU 

对于 Ubuntu 系列系统,运行 sudo apt install qemu-system-mipsel -y 即可

在镜像站获取最新的 initrd  和 kernel

可用的地址:
http://ftp.debian.org/debian/dists/stable/main/installer-mipsel/current/images/malta/netboot/ 
或清华镜像站
https://mirrors.tuna.tsinghua.edu.cn/debian/dists/stable/main/installer-mipsel/current/images/malta/netboot/
如:
 $ wget http://ftp.debian.org/debian/dists/stable/main/installer-mipsel/current/images/malta/netboot/initrd.gz 
 $ wget http://ftp.debian.org/debian/dists/stable/main/installer-mipsel/current/images/malta/netboot/vmlinux-4.19.0-6-4kc-malta  

新建虚拟磁盘

$ qemu-img create -f qcow2 disk.qcow2 20G

运行 Debian 网络安装程序

$ qemu-system-mipsel -M malta -m 1G \
  -hda ./disk.qcow2 \
  -initrd ./initrd.gz \
  -kernel ./vmlinux-4.19.0-6-4kc-malta -append "nokaslr" \
  -nographic

为减少安装时间,正确选择网络镜像,根据机器性能,安装需要一个多小时。

Debian 网络安装程序

从虚拟磁盘中提取 initrd

 sudo apt install nbd-client
 sudo modprobe nbd max_part=8
 sudo qemu-nbd --connect=/dev/nbd0 disk.qcow2
 mkdir mnt
 sudo mount /dev/nbd0p1 mnt
 cp mnt/boot/initrd.img-4.19.0-6-4kc-malta . 
 sudo umount /dev/nbd0p1
 sudo nbd-client -d /dev/nbd0

最后,可以启动虚拟机了

$ qemu-system-mipsel -M malta -m 1G \
  -hda ./disk.qcow2 \
  -initrd ./initrd.img-4.19.0-6-4kc-malta \
  -kernel ./vmlinux-4.19.0-6-4kc-malta -append "nokaslr root=/dev/sda1" \
  -nographic

更多资料:

从外部连接到虚拟机 链接–> Debian on an emulated MIPS(EL) machine

Connect your emulated machine to a real network
When no option is specified QEMU uses a non priviledged user mode network stack that gives the emulated machine access to the world. But you probably want to make your emulated machine accessible from the outside. It is possible by using the tap mode and bridging the tap interface with the network interface of the host machine.
The first thing to do is to active a bridge on your host machine. For that you have to modify the /etc/network/interfaces file as follow:
Before:
auto eth0
iface eth0 inet dhcp

After:
auto br0
iface br0 inet dhcp
  bridge_ports eth0
  bridge_maxwait 0

Then you need to install the bridge-utils package and restart your network interface:
# apt-get install bridge-utils
# ifdown eth0
# ifup br0

Create a script call /etc/qemu-ifup that will be executed upon the start of QEMU:
#!/bin/sh
echo “Executing /etc/qemu-ifup”
echo “Bringing up $1 for bridged mode…”
sudo /sbin/ifconfig $1 0.0.0.0 promisc up
echo “Adding $1 to br0…”
sudo /usr/sbin/brctl addif br0 $1
sleep 2
As you probably don’t want to execute QEMU as root, you need to create a qemu user group and authorize the brctl and ifconfig commands for users of the qemu via sudo. You need to add the following lines to /etc/sudoers (edit the file using visudo):

Cmnd_Alias QEMU = /usr/sbin/brctl, /sbin/ifconfig
%qemu ALL=NOPASSWD: QEMU

Finally you can start your emulated machine using the following command
$ qemu-system-mips -M mips -kernel vmlinux-2.6.18-3-qemu -hda hda.img -append “root=/dev/hda1 console=ttyS0” -net nic,macaddr=00:16:3e:00:00:01 -net tap
You don’t need to give a MAC address if you are emulating only one machine, as QEMU will use a default one. However if you have more than one emulated machine (don’t forget QEMU can also emulate other architectures than MIPS), you will have to specify a unique MAC address for each machine. I advise you to select an address from the range 00:16:3e:xx:xx:xx, which has been assigned to Xen.

便捷的启动脚本 详见–> Building a Debian Stretch QEMU image for MIPSel

Debian doesn’t properly DHCP the Ethernet interface (get a wrong name for the interface), so it must be done manually at the first boot (use ip -a to show the interface name):

# cat > /etc/network/interfaces << EOF
auto lo
iface lo inet loopback
iface enp0s18 inet dhcp
EOF
# shutdown -h now

You can now use the start.sh script to init the VM, and ssh.sh to SSH to it as user.

The start.sh usually looks like

!/bin/bash
KERNEL=./vmlinux-4.9.0-4-5kc-malta
INITRD=./initrd.img-4.9.0-4-5kc-malta
HDD=./disk.qcow2
SSH_PORT=22055
EXTRA_PORT=33055
qemu-system-mips64el -M malta -m 512 -cpu MIPS64R2-generic \
-kernel ${KERNEL} \
-initrd ${INITRD} \
-hda ${HDD} \
-net nic,model=e1000 \
-net user,hostfwd=tcp:127.0.0.1:${SSH_PORT}-:22,hostfwd=tcp:127.0.0.1:${EXTRA_PORT}-:4444 \
-display none -vga none -nographic \
-append 'nokaslr root=/dev/sda1 console=ttyS0'
exit 0


And the ssh.sh:

!/bin/sh
echo "Existing users : 'root/root' & 'user/user'"
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 22055 user@127.0.0.1 -- $*
exit 0


For Windows, simply convert script.sh to Batch.

参考资料

QEMU - Debian Wiki https://wiki.debian.org/QEMU
Building a Debian Stretch QEMU image for MIPSel - Blah Cats https://blahcat.github.io/2017/07/14/building-a-debian-stretch-qemu-image-for-mipsel/
Debian on an emulated MIPS(EL) machine https://www.aurel32.net/info/debian_mips_qemu.php
How to build a Debian MIPS image on QEMU https://markuta.com/how-to-build-a-mips-qemu-image-on-debian/

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Back to Top