i BUILD A DEBIAN MIPS IMAGE ON QEMU – All things in moderation

BUILD A DEBIAN MIPS IMAGE ON QEMU

Today, I will present how to build your MIPS image running under the QEMU emulator. This guide can also be applied to other architectures.

Prepare:

The first, we only require a specific package namely: qemu-system-mips.

$ sudo apt-get install qemu-system-mips.

There are two versions of the MIPS-32 (Big Endian); Malta and Octeon. This guide will be using the Malta version. Download both the installer and boot files from the stable release:

Create a QEMU image file:

Create a qcow2 format image with 10gb of storage:

$ qemu-img create -f qcow2 hda.img 10G

The table shows the minimal hardware requirements here

Install Debian MIPS:

Before starting to make sure all three files (hda.img, vmlinux-4.9.0-8-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 512 -hda hda.img \
  -kernel vmlinux-4.9.0-8-4kc-malta \
  -initrd initrd.gz \
  -append "console=ttyS0 nokaslr" \
  -nographic

By default, QEMU enables a NATed network interface for Internet connectivity through the host’s network. This allows the virtual machine to install and update packages.

Set password for root user and create a new user.

Install SSH server:

I highly recommend installing an SSH server in Software selection window so you can communicate with the host machine for uploading and downloading files whilst in a NATed network.

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 the entire folder to the current directory:

$ cp -r /mnt/boot/ .

Unmount the image:

$ sudo umount /mnt
$ sudo qemu-nbd -d /dev/nbd0

Installation Completed:

Once you see this screen your installation has completed and it’s time to shut down. 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 shut down the virtual machine.

Fix SSH server:

if you have any problem about permission denied when login ssh, you can do the following:

  • Change your root password.
  • Edit file “/etc/ssh/sshd_config”: replace “#PermitRootLogin no” with “PermitRootLogin yes”.
  • Restart service:
$ sudo systemctl reload sshd

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 512 -hda hda.img \
  -kernel vmlinux-4.9.0-8-4kc-malta \
  -initrd ./boot/initrd.img-4.9.0-8-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
Tags: , ,

Leave a Reply