How to set up and build your own MIPS big endian or little endian image running under the QEMU emulator. This guide can also be applied to other architectures. For example I’m currently running this in a virtual machine inside another virtual machine on my MacBook Pro. “We Need To Go Deeper” - Dom Cobb, Inception.
Since we are only emulating a MIPS system on QEMU, we only require a specific package namely; qemu-system-mips. On most Linux distos you can simply install through apt-get. This will also install further packages required by QEMU:
$ sudo apt-get install qemu-system-mips
The exact version used: QEMU emulator version 2.8.1(Debian 1:2.8+dfsg-6+deb9u3).
There are two versions of the MIPS-32 (Big Endian); Malta and Octeon. This guide will be using the Malta version. Although it’s almost the exact same process for Octeon with a few minor option differences.
The Kernel filename may differ to one listed below. As the Debian team provides newer releases and updates, the filename will change over time. Link to Latest Stable.
Download both the installer and boot files from stable release:
- Installer (initrd.gz) ~21MB:
$ wget http://ftp.debian.org/debian/dists/stable/main/installer-mips/current/images/malta/netboot/initrd.gz
- Kernel boot (vmlinux-4.9.0-6-4kc-malta) ~11MB:
$ wget http://ftp.debian.org/debian/dists/stable/main/installer-mips/current/images/malta/netboot/vmlinux-4.9.0-6-4kc-malta
- Optional: Verify downloaded files with SHA256SUMS by manually comparing the hash values:
$ shasum -a 256 initrd.gz vmlinux-4.9.0-6-4kc-malta ea6acc7cc48c7bdb81fd0af2b3546a299e00172539d735001b550b0582b97c0a initrd.gz b15610b7aa81d56855627cae7518a396081929ccb28dc1add873322d69f8f08e vmlinux-4.9.0-6-4kc-malta
Create an QEMU image file
Create an QEMU image file specifying its storage size and filetype to be used as installation media. The table below shows the minimal hardware requirements as per Debian official documentation: Link
|Install Type||Minimum (RAM)||Recommended (RAM)||Storage|
Create an qcow2 format image with 2G of storage:
$ qemu-img create -f qcow2 hda.img 2G
Install Debian MIPS
Before starting make sure all three files (hda.img, vmlinux-4.9.0-6-4kc-malta and initrd.gz) are actually in the current working directory. The installation process is almost identical to the standard x86_64 or i386 architectures.
To start the installation type:
$ qemu-system-mips -M malta \ -m 256 -hda hda.img \ -kernel vmlinux-4.9.0-6-4kc-malta \ -initrd initrd.gz \ -append "console=ttyS0 nokaslr" \ -nographic
By default QEMU enables a NATed network interface for Internet connectivity through the hosts network. This allows the virtual machine to install and update packages.
Install SSH server
I highly recommend installing a SSH server so you can communicate with the host machine for uploading and downloading files whilst in a NATed network. The writer has yet to explore network bridging and other network connectivity. This will probably be the next post.
Once you see this screen your installation has completed and it’s time to shutdown. Unfortunately, if you hit continue qemu will reboot right back into the installer. Therefore you’d either want to kill process or enter cli shell by selecting Go Back > Go Down > Execute Shell and type command
poweroff that will shutdown the virtual machine.
Copy over Kernel initrd.img file
During the installation stage you’ll see this screen warning us that no bootloader has been installed.
Before you can use the freshly installed MIPS image you first need to extract the Kernel initrd.img-[version] file found in the /boot partition of the image. We must manually copy it by mounting the image and executing a few commands.
- Mount the boot partition of the image file:
sudo modprobe nbd max_part=63 sudo qemu-nbd -c /dev/nbd0 hda.img sudo mount /dev/nbd0p1 /mnt
- Copy a single file or the entire folder to the current directory:
cp -r /mnt/boot/initrd.img-4.9.0-6-4kc-malta . # copy only initrd.img file cp -r /mnt/boot . # copy the entire boot folder
- Unmount the image:
sudo umount /mnt sudo qemu-ndb -d /dev/nbd0
Running the QEMU image
Now that all the files have been configured and set up. It’s time to officially start the virtual machine. The following set of options can be changed to your liking. You could also make the following into a Bash script.
To start the image type:
$ qemu-system-mips -M malta \ -m 256 -hda hda.img \ -kernel vmlinux-4.9.0-6-4kc-malta \ -initrd initrd.img-4.9.0-6-4kc-malta \ -append "root=/dev/sda1 console=ttyS0 nokaslr" \ -nographic \ -device e1000-82545em,netdev=user.0 \ -netdev user,id=user.0,hostfwd=tcp::5555-:22
The last option enables port forwarding on host machine port 5555 to the guest machine on port 22 for ssh communication.
To access the guest machine from Host machine to upload a file:
$ scp -P 5555 file.txt [email protected]:/tmp
Or to connect via ssh:
$ ssh [email protected] -p 5555
A few other resources that were very helpful:
- QEMU - Debian Wiki
- Building a Debian Stretch QEMU image for MIPSel - Blah Cats
- Pre-configured Debian Squeeze and Wheezy images
- OpenWrt in QEMU - OpenWrt Wiki
- Debian on an emulated MIPS(EL) machine