<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">

  <channel>
    <title>Nathan Sweet&apos;s Blog</title>
    <description>Nathan Sweet&apos;s thoughts on computer programming, protips, and, hopefully, not too many rants...</description>
    <link>https://www.nathansweet.me/</link>
    <atom:link href="https://www.nathansweet.me/feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Build the Linux Kernel for VirtualBox</title>
        <description>&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;Go ahead and &lt;a href=&quot;https://www.virtualbox.org/wiki/Downloads&quot;&gt;download and install VirtualBox&lt;/a&gt;.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Next, we need to clone the Linux repository. Open a terminal tab and run:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
 &lt;code class=&quot;hljs bash&quot;&gt;
 git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
 &lt;/code&gt;
 &lt;/pre&gt;

    &lt;p&gt;This will take a while (it is gigabytes large), and we have other stuff to do.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Go ahead and download the boot ISO for your favorite distro (the instructions will assume
the distro you are currently running, but that doesn’t have to be the case). Make sure that
the distro you want to use can work with the version of Linux that you want to build and install.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Next open up VirtualBox and create a new virtual machine. When you come to the option about
disks make sure to choose “Create a virtual hard disk now”. Make sure to choose the “VDI”
format rather than “VHD” or “VMDK”. Depending on what your’re doing and what your distro
expects you might have to allocate more or less space for the virtual disk, and if disk
performance isn’t a concern you can choose “Dynamically Allocated” rather than “Fixed Size”
for how to store it on your machine. Make note of where the vdi file is saved. Also, make
sure to size the volume decently large (say at least 15GB), the unmodified install commands
we will be running require a lot of space.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Click on the “Settings” button for your new virtual machine and then the “Storage” setting.
Click on the CD-looking icon and choose “Live CD/DVD” then choose your distro ISO from the
other CD-looking icon next to the “Optical Drive” dropdown.&lt;/p&gt;

    &lt;p&gt;&lt;img src=&quot;/images/linux-build-1.jpg&quot; alt=&quot;load live cd&quot; /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Start the virtual machine and install the distro.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Once the distro is installed shutdown the VM, remove the Live CD/DVD and start the VM up
again to make sure the distro is installed.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Make sure you qemu-nbd installed:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
 &lt;code class=&quot;hljs bash&quot;&gt;
 lsmod | grep nbd
 &lt;/code&gt;
 &lt;/pre&gt;

    &lt;p&gt;If not run:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
 &lt;code class=&quot;hljs bash&quot;&gt;
 sudo modprobe nbd max_part=8
 &lt;/code&gt;
 &lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Next create a device from the vdi file:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
 &lt;code class=&quot;hljs bash&quot;&gt;
 sudo qemu-nbd -c /dev/nbd1 /home/$YOUR_USER/VirtualBox\ VMs/$YOUR_DISTRO/$YOUR_DISTRO.vdi
 &lt;/code&gt;
 &lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Sometimes the partitions won’t show up. If that happens run:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
&lt;code class=&quot;hljs bash&quot;&gt;
sudo partprobe /dev/nbd1
&lt;/code&gt;
&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Mount the boot and rootpartition:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
&lt;code class=&quot;hljs bash&quot;&gt;
sudo mount /dev/nbd1p1 /mnt &lt;span class=&quot;hljs-comment&quot;&gt;# assuming this is root&lt;/span&gt;
sudo mount /dev/nbd1p2 /mnt/boot &lt;span class=&quot;hljs-comment&quot;&gt;# assuming this is boot&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cd&lt;/code&gt; into the linux-stable git folder when it’s fully downloaded. Copy your current distro’s config (or generate one in another way). Build the kernel.&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
&lt;code class=&quot;hljs bash&quot;&gt;
cd /home/$YOUR_USER/Development/linux-stable
cp /boot/config-`uname -r`* .config
make &lt;span class=&quot;hljs-comment&quot;&gt;# this will take a while&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;

    &lt;p&gt;Note: you can speed the build process by running:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
&lt;code class=&quot;hljs bash&quot;&gt;
make -jX &lt;span class=&quot;hljs-comment&quot;&gt;# X is the number of cores you want to dedicate to the build process; this will slow your system&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Install the modules and kernel into the nbd mount.&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
&lt;code class=&quot;hljs bash&quot;&gt;
sudo make INSTALL_MOD_PATH=/mnt/lib/modules/`uname -r` modules_install
sudo make INSTALL_PATH=/mnt/boot install
&lt;/code&gt;
&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Unmount the nbd paritions and remove the nbd device:&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
&lt;code class=&quot;hljs bash&quot;&gt;
sudo umount /mnt/boot
sudo umount /mnt
sudo qemu-nbd -d /deb/nbd1
&lt;/code&gt;
&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Start up you virtual machine, remove the old ramdisk, create a new one, and update grub.&lt;/p&gt;

    &lt;pre class=&quot;highlight&quot;&gt;
&lt;code class=&quot;hljs bash&quot;&gt;
sudo rm /boot/initramfs-`uname -r`.img
sudo update-initramfs -c -k $VERSION_OF_LINUX_YOU_BUILT &lt;span class=&quot;hljs-comment&quot;&gt;# make sure this looks like what uname -r would output&lt;/span&gt;
sudo update-grub
&lt;/code&gt;
&lt;/pre&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Restart your virtual machine, and you should now see your custom linux build.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Fri, 03 Feb 2017 00:00:00 +0000</pubDate>
        <link>https://www.nathansweet.me/2017/02/03/build-linux-for-virtualbox</link>
        <guid isPermaLink="true">https://www.nathansweet.me/2017/02/03/build-linux-for-virtualbox</guid>
      </item>
    
  </channel>
</rss>
