Buildroot
The qemu_x86_64_defconfig defconfig almost worked, except that I had to add ::sysinit:/sbin/mdev -s to the inittab. I think this is because Buildroot relies on CONFIG_DEVTMPFS_MOUNT to create /dev.
Rootfs:
git clone git://git.buildroot.net/buildroot
cd buildroot
git checkout 2017.02
make qemu_x86_64_defconfig
# Custom inittab.
echo 'BR2_ROOTFS_OVERLAY="rootfs_overlay"' >>.config
make olddefconfig
mkdir -p rootfs_overlay/etc
printf '
::sysinit:/bin/mount -t proc proc /proc
::sysinit:/bin/mount -o remount,rw /
::sysinit:/bin/mkdir -p /dev/pts
::sysinit:/bin/mkdir -p /dev/shm
::sysinit:/bin/mount -a
::sysinit:/sbin/mdev -s
::sysinit:/bin/hostname -F /etc/hostname
::sysinit:/etc/init.d/rcS
console::respawn:/sbin/getty -n -L console 0 vt100
::ctrlaltdel:/sbin/reboot
::shutdown:/etc/init.d/rcK
::shutdown:/sbin/swapoff -a
::shutdown:/bin/umount -a -r
' > rootfs_overlay/etc/inittab
# Build image.
make BR2_JLEVEL=$(($(nproc)-2))
cp output/images/rootfs.ext2 /path/to/linux
Then on kernel source:
cd /path/to/linux
git checkout v4.9
make mrproper
make defconfig ARCH=um
make ARCH=um
./linux eth0=tuntap,,,192.168.0.254
Now you are inside the VM, and you can leave with:
poweroff
The filesystem is persistent, try it out with:
date >f
and reboot.
TODO get network working. The current eth0= is just a dummy to prevent Buildroot's init from stalling waiting for the network device.
You can also step debug the kernel as shown at: https://stackoverflow.com/questions/4943857/linux-kernel-live-debugging-how-its-done-and-what-tools-are-used/44669413#44669413
TODO I don't know how to deal with kernel modules, since they must be compiled against the UML, not x86. The first problem is that insmod will fail because UML does not have SMP which affects vermagic, and if you force vermagic, weird things happen like printk does not print anything. Related: https://stackoverflow.com/questions/2488625/user-mode-linux-installing-a-module-error
You can also inspect that image with QEMU if you prefer:
qemu-system-x86_64 -M pc -kernel output/images/bzImage -drive file=output/images/rootfs.ext2,if=virtio,format=raw -append root=/dev/vda -net nic,model=virtio -net user
Tested in Ubuntu 14.04, kernel 3.13.0 host.