Build the Linux Kernel for VirtualBox
This post is a just a quick walkthrough of how to build the Linux kernel and run it on VirtualBox, instead of your own machine. This is useful for any development in the kernel you might do that is purely software related, and doesn’t require direct access to any hardware (though you can give VirtualBox direct access to a USB device).
The benefits of installing a custom kernel on a VM are manifold. The most compelling, in my opinion, is that you don’t have to worry about messing up your system, so you have freedom to really experiment.
- Go ahead and download and install VirtualBox.
- Next, we need to clone the Linux repository. Open a terminal tab and run:
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
This will take a while (it is gigabytes large), and we have other stuff to do.
sudo modprobe nbd max_part=8
sudo qemu-nbd -c /dev/nbd1 /home/$YOUR_USER/VirtualBox\ VMs/$YOUR_DISTRO/$YOUR_DISTRO.vdi
sudo mount /dev/nbd1p1 /mnt # assuming this is root sudo mount /dev/nbd1p2 /mnt/boot # assuming this is boot
cd /home/$YOUR_USER/Development/linux-stable cp /boot/config-`uname -r`* .config make # this will take a while
make -jX # X is the number of cores you want to dedicate to the build process; this will slow your system
sudo make INSTALL_MOD_PATH=/mnt/lib/modules/`uname -r` modules_install sudo make INSTALL_PATH=/mnt/boot install
sudo umount /mnt/boot sudo umount /mnt sudo qemu-nbd -d /deb/nbd1
sudo rm /boot/initramfs-`uname -r`.img sudo update-initramfs -c -k $VERSION_OF_LINUX_YOU_BUILT # make sure this looks like what uname -r would output sudo update-grub
Running Linux kernel in Virtualbox
I just built a Linux kernel on Ubuntu and now I want to make it run on VirtualBox. I wanted it to be in an img format. I created a hard drive image by doing the commands:
$ qemu-img create disk.img 512M $ mkfs.ext2 -F disk.img
Your question looks vague now, were you trying to boot from the disk(that has a boot loader installed already), or use qemu to boot that disk with an external kernel file?
2 Answers 2
Why are you creating the disk manually?
You should use VirtualBox to create the disk while creating the VM, unless you plan to use it together with qemu. (why?)
Here, when adding disks, use the existing disk that you created.
All I have done is create the kernel. I want to take the bzImage and put that with a bootloader, and a hard drive image then run that in Virtualbox (or possibly qemu if there is a way to do both).
To run a kernel directly in qemu or kvm , use the -kernel argument:
qemu-system-x86_64 -kernel /boot/vmlinuz-3.2.0-2-amd64
Virtualbox may have something similar.
If you really want it installed on a hard drive image, it’s a bit harder. You need a bootloader installed, which may also require a partition table. Something like SYSLINUX or EXTLINUX could be straightforward, or you could just go straight ahead to what most real systems use and install GRUB. For GRUB, the instructions in Linux From Scratch will be helpful, as will the GRUB documentation itself.