{"id":831,"date":"2015-06-09T22:09:04","date_gmt":"2015-06-10T05:09:04","guid":{"rendered":"http:\/\/www.pagetable.com\/?p=831"},"modified":"2015-06-09T22:09:04","modified_gmt":"2015-06-10T05:09:04","slug":"xhyve-lightweight-virtualization-on-os-x-based-on-bhyve","status":"publish","type":"post","link":"https:\/\/www.pagetable.com\/?p=831","title":{"rendered":"xhyve &#8211; Lightweight Virtualization on OS X Based on bhyve"},"content":{"rendered":"<p><div style=\"float: right\"><img loading=\"lazy\" decoding=\"async\" src=\"docs\/xhyve_logo.png\" width=\"253\" height=\"99\"><\/div>\n<p>The <a href=\"https:\/\/developer.apple.com\/library\/mac\/releasenotes\/MacOSX\/WhatsNewInOSX\/Articles\/MacOSX10_10.html\">Hypervisor.framework<\/a> user mode <a href=\"http:\/\/www.pagetable.com\/?p=348\">virtualization<\/a> API introduced in Mac OS X 10.10 (Yosemite) cannot only be used for toy projects like the <a href=\"http:\/\/www.pagetable.com\/?p=764\">hvdos<\/a> DOS Emulator, but is full-featured enough to support a full virtualization solution that can for example run Linux.<\/p>\n<p><a href=\"https:\/\/github.com\/mist64\/xhyve\">xhyve<\/a> is a lightweight virtualization solution for OS X that is capable of running Linux. It is a port of FreeBSD&#8217;s <a href=\"http:\/\/bhyve.org\">bhyve<\/a>, a KVM+QEMU alternative written by Peter Grehan and Neel Natu.<\/p>\n<ul>\n<li>super lightweight, only 230 KB in size\n<li>completely standalone, no dependencies\n<li>the only BSD-licensed virtualizer on OS X\n<li>does not require a kernel extension (bhyve&#8217;s kernel code was ported to user mode code calling into Hypervisor.framework)\n<li>multi-CPU support\n<li>networking support\n<li>can run off-the-shelf Linux distributions (and could be extended to run other operating systems)\n<\/ul>\n<p>xhyve may make a good solution for running Docker on your Mac, for instance.<\/p>\n<h2>Running Tiny Core Linux on xhyve<\/h2>\n<p>The xhyve repository already contains a small Linux system for testing, so you can try out xhyve by just typing these few lines:<\/p>\n<pre>\n$ git clone https:\/\/github.com\/mist64\/xhyve\n$ cd xhyve\n$ make\n$ .\/xhyverun.sh\n<\/pre>\n<p>And you will see <a href=\"http:\/\/tinycorelinux.net\">Tiny Core Linux<\/a> booting in your terminal window:<\/p>\n<pre>\nInitializing cgroup subsys cpuset\nInitializing cgroup subsys cpu\nInitializing cgroup subsys cpuacct\nLinux version 3.16.6-tinycore64 (tc@box) (gcc version 4.9.1 (GCC) ) #777 SMP Thu Oct 16 10:21:00 UTC 2014\nCommand line: earlyprintk=serial console=ttyS0 acpi=off\ne820: BIOS-provided physical RAM map:\nBIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable\nBIOS-e820: [mem 0x0000000000100000-0x000000003fffffff] usable\nNX (Execute Disable) protection: active\nSMBIOS 2.6 present.\nAGP: No AGP bridge found\ne820: last_pfn = 0x40000 max_arch_pfn = 0x400000000\nx86 PAT enabled: cpu 0, old 0x7040600070406, new 0x7010600070106\nCPU MTRRs all blank - virtualized system.\n\n[...]\n\n (?-\n \/\/\\   Core is distributed with ABSOLUTELY NO WARRANTY.\n v_\/_           www.tinycorelinux.com\n\ntc@box:~$\n<\/pre>\n<p>To shut down the VM and exit to the Mac&#8217;s command line, enter:<\/p>\n<pre>\n$ sudo halt\n<\/pre>\n<h2>Running Ubuntu on xhyve<\/h2>\n<p>You can also install a more complete Linux distribution on xhyve. The tricky bit is that xhyve doesn&#8217;t come with a BIOS or EFI booter, so it is necessary to extract the kernel and initrd from the Linux image and pass them to xhyve manually.<\/p>\n<p>First download <a href=\"http:\/\/www.ubuntu.com\/download\/server\">Ubuntu Server<\/a> (the desktop version doesn&#8217;t support the text mode installer) into the directory &#8220;ubuntu&#8221; inside the &#8220;xhyve&#8221; directory:<\/p>\n<pre>\n$ ls -l\ntotal 1218560\n-rw-r--r--@ 1 mist  staff  623902720  6 Jun 22:14 ubuntu-14.04.2-server-amd64.iso\n<\/pre>\n<p>We need to extract the kernel and initrd, which is a little tricky, because OS X doesn&#8217;t recognize the hybrid file system on the image without a little hack:<\/p>\n<pre>\n$ dd if=\/dev\/zero bs=2k count=1 of=\/tmp\/tmp.iso\n$ dd if=ubuntu-14.04.2-server-amd64.iso bs=2k skip=1 >> \/tmp\/tmp.iso\n$ hdiutil attach \/tmp\/tmp.iso\n$ cp \/Volumes\/Ubuntu-Server\\ 14\/install\/vmlinuz .\n$ cp \/Volumes\/Ubuntu-Server\\ 14\/install\/initrd.gz .\n<\/pre>\n<p>Create a virtual hard disk image (8 GB in the example):<\/p>\n<pre>\n$ dd if=\/dev\/zero of=hdd.img bs=1g count=8\n<\/pre>\n<p>Then create a script to run xhyve with the correct arguments for the installer:<\/p>\n<pre>\n#!\/bin\/sh\n\nKERNEL=\"ubuntu\/vmlinuz\"\nINITRD=\"ubuntu\/initrd.gz\"\nCMDLINE=\"earlyprintk=serial console=ttyS0 acpi=off\"\n\nMEM=\"-m 1G\"\n#SMP=\"-c 2\"\nNET=\"-s 2:0,virtio-net\"\nIMG_CD=\"-s 3,ahci-cd,ubuntu\/ubuntu-14.04.2-server-amd64.iso\"\nIMG_HDD=\"-s 4,virtio-blk,ubuntu\/hdd.img\"\nPCI_DEV=\"-s 0:0,hostbridge -s 31,lpc\"\nLPC_DEV=\"-l com1,stdio\"\n\nbuild\/xhyve $MEM $SMP $PCI_DEV $LPC_DEV $NET $IMG_CD $IMG_HDD -f kexec,$KERNEL,$INITRD,\"$CMDLINE\"\n<\/pre>\n<p>You will want networking enabled, so it&#8217;s easiest to run the script as root (this requirement is lifted if you codesign the binary):<\/p>\n<pre>\n$ sudo .\/xhyverun_ubuntu_install.sh\n<\/pre>\n<p>You will see the Ubuntu text mode installer:<\/p>\n<pre>\n  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524 [!!] Select a language \u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n  \u2502                                                                         \u2502\n  \u2502 Choose the language to be used for the installation process. The        \u2502\n  \u2502 selected language will also be the default language for the installed   \u2502\n  \u2502 system.                                                                 \u2502\n  \u2502                                                                         \u2502\n  \u2502 Language:                                                               \u2502\n  \u2502                                                                         \u2502\n  \u2502                               C                                         \u2502\n  \u2502                               English                                   \u2502\n  \u2502                                                                         \u2502\n  \u2502     &lt;Go Back&gt;                                                           \u2502\n  \u2502                                                                         \u2502\n  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n\n&lt;Tab&gt; moves; &lt;Space&gt; selects; &lt;Enter&gt; activates buttons\n<\/pre>\n<p>All answers should be straightforward, and the defaults are usually fine. Make sure to select &#8220;Yes&#8221; when asked &#8220;Install the GRUB boot loader to the master boot record&#8221;.<\/p>\n<p>At the very end, on the &#8220;Installation complete&#8221; screen, select &#8220;Go back&#8221; and &#8220;Execute a shell&#8221;, so you can copy the installed kernel and initrd to the Mac side. In the VM, type this:<\/p>\n<pre>\n# cd \/target\n# sbin\/ifconfig\n# tar c boot | nc -l -p 1234\n<\/pre>\n<p>On the Mac, type this, replacing the IP with the output from ifconfig before:<\/p>\n<pre>\n$ cd ubuntu\n$ nc <font color=\"red\">192.168.64.7<\/font> 1234 | tar x\n<\/pre>\n<p>In the VM, exit the shell:<\/p>\n<pre>\n# exit\n<\/pre>\n<p>Then select &#8220;Finish the installation&#8221;.<\/p>\n<p>To run the Ubuntu installation from the virtual hard disk, create the following script, fixing up the kernel and initrd version numbers:<\/p>\n<pre>\n#!\/bin\/sh\n\nKERNEL=\"ubuntu\/boot\/vmlinuz-<font color=\"red\">3.16.0-30-generic<\/font>\"\nINITRD=\"ubuntu\/boot\/initrd.img-<font color=\"red\">3.16.0-30-generic<\/font>\"\nCMDLINE=\"earlyprintk=serial console=ttyS0 acpi=off root=\/dev\/vda1 ro\"\n\nMEM=\"-m 1G\"\n#SMP=\"-c 2\"\nNET=\"-s 2:0,virtio-net\"\nIMG_HDD=\"-s 4,virtio-blk,ubuntu\/hdd.img\"\nPCI_DEV=\"-s 0:0,hostbridge -s 31,lpc\"\nLPC_DEV=\"-l com1,stdio\"\n\nbuild\/xhyve $MEM $SMP $PCI_DEV $LPC_DEV $NET $IMG_CD $IMG_HDD -f kexec,$KERNEL,$INITRD,\"$CMDLINE\"\n<\/pre>\n<p>Then run the script:<\/p>\n<pre>\n$ sudo .\/xhyverun_ubuntu.sh\n<\/pre>\n<p>To make your Linux installation useful, you may want to install an SSH server:<\/p>\n<pre>\n$ sudo apt-get install openssh-server\n<\/pre>\n<p>Or install a full UI that you can access using VNC:<\/p>\n<pre>\n$ sudo apt-get install xubuntu-desktop vnc4server\n<\/pre>\n<p>Then run the VNC server:<\/p>\n<pre>\n$ vnc4server :0 -geometry 1024x768\n<\/pre>\n<p>And conntect to it by pasting this into Finder&#8217;s Cmd+K &#8220;Connect to Server&#8221; dialog:<\/p>\n<pre>\nvnc:\/\/ubuntu.local\n<\/pre>\n<p>If you also follow <a href=\"http:\/\/skerit.com\/en\/computer\/english-vnc-x11-session-on-ubuntu-12-04-server-without-monitor-or-graphics-card\/\">skerit&#8217;s Ubuntu VNC tutorial<\/a>, you&#8217;ll get to a desktop like this.<\/p>\n<p><a href=\"docs\/xhyve_vnc.png\"><img decoding=\"async\" src=\"docs\/xhyve_vnc.png\" width=\"100%\"><\/a><\/p>\n<h2>Next Steps<\/h2>\n<p>xhyve is very basic and lightweight, but it has a lot of potential. If you are a developer, you are welcome to contribute to it. A list of current TODOs and ideas is part of the README file in the repository.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Hypervisor.framework user mode virtualization API introduced in Mac OS X 10.10 (Yosemite) cannot only be used for toy projects like the hvdos DOS Emulator, but is full-featured enough to support a full virtualization solution that can for example run Linux. xhyve is a lightweight virtualization solution for OS X that is capable of running &#8230; <a title=\"xhyve &#8211; Lightweight Virtualization on OS X Based on bhyve\" class=\"read-more\" href=\"https:\/\/www.pagetable.com\/?p=831\" aria-label=\"Read more about xhyve &#8211; Lightweight Virtualization on OS X Based on bhyve\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-831","post","type-post","status-publish","format-standard","hentry","category-virtualization"],"_links":{"self":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=831"}],"version-history":[{"count":0,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=\/wp\/v2\/posts\/831\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pagetable.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}