Compiling and executing linux/kernel with qemu under mac

Posted by signer on Mon, 03 Jan 2022 14:51:48 +0100

Main reference

Using qemu to build kernel development environment - lixiaomu - blog Park

It mainly involves the following points:

  • virtualbox6.1, run ubuntu server 18.04, and then open ssh server for mac host access. Otherwise, the virtual box screen can't zoom automatically. It's really hot eyes. It is estimated that it is not so easy to compile directly on a linux kernel.
sudo apt-get install openssh-server

Configure port forwarding for the host.

 

  • Download the linux kernel on the virtual machine for compilation.

I'm through wget The Linux Kernel Archives Download the source file of 5.10.88 above. After downloading, compile bzImage and * ko files through the following commands.

make x86_64_defconfig
make bzImage
make modules
  • In addition to the kernel, the root file system is also required to make qemu boot an accessible linux. Make the root file system with the following command. The first command is equivalent to creating a disk, mkfs formats it and establishes the ext4 file system. The third command mounts the disk to / home/tianyxu/img. The IMG directory cannot be a shared folder of MAC virtual box, otherwise the following error will be reported. The fourth command is to install the module on disk.
    mount: /media/sf_SharedFolder/testout: can't read superblock on /dev/loop0"
qemu-img create -f raw disk.raw 512M
mkfs -t ext4 ./disk.raw
sudo mount -o loop ./disk.raw /home/tianyxu/img
sudo make modules_install \ # Installing kernel modules
INSTALL_MOD_PATH=/home/tianyxu/img  # Specify the installation path
  • Share files under mac and virtualbox.

Sharing files between mac and virtualbox can be set through device - > Share folders in the virtualbox/linux interface. After setting, you need to mount the corresponding folder on linux. I also encountered a problem when I was in mount. https://medium.com/macoclock/share-folder-between-macos-and-ubuntu-4ce84fb5c1ad

Solve sudo apt get install VirtualBox guest utils by installing VirtualBox guest utils.

mount: wrong fs type, bad option, bad superblock .... or helper program, or other error

sudo mount -t vboxsf linux2001 /mnt/mac
  • Add the init program to the root file system. Compile, install and configure busybox. There are three main init programs, sysv init, systemd and busybox. We choose busybox, which is commonly used in embedded system. I used the following commands to configure and compile busybox under linux.
make defconfig # use default config

make manuconfig # make a little change
Busybox Settings --->
       --- Build Options
       [*] Build BusyBox as a static binary (no shared libs)

make # compile busybox

make CONFIG_PREFIX=<path_to_disk_img_mount_point> install # install to the root fs
  • After busybox init starts, the / etc/inittab configuration file will be scanned. This configuration file defines the corresponding behavior. Without this file, init will work as defined below.
::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
::restart:/sbin/init
tty2::askfirst:/bin/sh
tty3::askfirst:/bin/sh
tty4::askfirst:/bin/sh

In order to work normally under qemu, we delete the last three lines. And configure / etc / init D / RCS is "#! / bin/sh".

  • After that, you can start normally, but the file system is a read-only system. It needs to be explored in the next step.
tianyxu@TIANYXU-M-V83Z linux2001 % qemu-system-x86_64 \
-m 512M \
-smp 4 \
-kernel ./bzImage \
-drive format=raw,file=./disk.raw \
-append "init=/linuxrc root=/dev/sda"


 

Topics: Linux Operation & Maintenance macOS