<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.7.4">Jekyll</generator><link href="https://mgerdts.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://mgerdts.github.io/" rel="alternate" type="text/html" /><updated>2019-03-15T14:57:48+00:00</updated><id>https://mgerdts.github.io/feed.xml</id><title type="html">Virtually Anything</title><subtitle>Musings on virtualization and other stuff</subtitle><entry><title type="html">Using kvm images with bhyve</title><link href="https://mgerdts.github.io/2018/04/17/kvm-image-with-bhyve.html" rel="alternate" type="text/html" title="Using kvm images with bhyve" /><published>2018-04-17T22:00:00+00:00</published><updated>2018-04-17T22:00:00+00:00</updated><id>https://mgerdts.github.io/2018/04/17/kvm-image-with-bhyve</id><content type="html" xml:base="https://mgerdts.github.io/2018/04/17/kvm-image-with-bhyve.html">&lt;p&gt;So far, most of my use of bhyve has been with kvm images that have been
customize to work better with bhyve.  For Ubuntu images, those customizations
have generally been:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Configure grub to use the serial console&lt;/li&gt;
  &lt;li&gt;Install a version of cloud-init that has some fixes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So long as nothing is putting garbage on the second serial line, the version of
cloud-init that comes with Ubuntu works well enough that it can be used to
configure the serial console.  Early on, I was having troubles with garbage on
the serial line, but that seems to have been an ephemeral issue.&lt;/p&gt;

&lt;p&gt;Cloud-init supports &lt;code class=&quot;highlighter-rouge&quot;&gt;user-data&lt;/code&gt; with a variety of content.  I’ll use
&lt;code class=&quot;highlighter-rouge&quot;&gt;cloud-config&lt;/code&gt; to accomplish what I’m after.  This is not as straight-forward as
we would like - ideally we would just splat some more json in
&lt;code class=&quot;highlighter-rouge&quot;&gt;customer_metadata&lt;/code&gt;.  But &lt;code class=&quot;highlighter-rouge&quot;&gt;cloud-init&lt;/code&gt; really likes yaml.  And even if it
didn’t, &lt;code class=&quot;highlighter-rouge&quot;&gt;customer_metadata&lt;/code&gt; does not support nested structures.  So, we need
some yaml wrapped in json.&lt;/p&gt;

&lt;p&gt;The cloud-config that’s needed looks like this:&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;#cloud-config&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;write_files&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_DEFAULT=0&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_TIMEOUT=5&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_DISTRIBUTOR=`lsb_release -i -s 2&amp;gt; /dev/null || echo Debian`&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_CMDLINE_LINUX_DEFAULT=&quot;tsc=reliable earlyprintk&quot;&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_CMDLINE_LINUX=&quot;&quot;&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_TERMINAL=&quot;serial console&quot;&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_CMDLINE_LINUX=&quot;console=tty0 console=ttyS0,115200n8&quot;&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;GRUB_SERIAL_COMMAND=&quot;serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;/etc/default/grub&lt;/span&gt;
&lt;span class=&quot;na&quot;&gt;runcmd&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;update-grub2 &amp;gt;/var/tmp/update-grub2.log 2&amp;gt;&amp;amp;1&lt;/span&gt;
  &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;shutdown -r +1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To get that into json, I use a modified version of a &lt;a href=&quot;http://cloudinit.readthedocs.io/en/latest/topics/format.html#helper-script-to-generate-mime-messages&quot;&gt;helper script found in the
cloud-init docs&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#!/usr/bin/python&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sys&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;email.mime.multipart&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MIMEMultipart&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;email.mime.text&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MIMEText&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s input-file:type ...&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;combined_message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MIMEMultipart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]:&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;format_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;contents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fh&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sub_message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;MIMEText&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;format_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getdefaultencoding&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sub_message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;add_header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Content-Disposition'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'attachment; filename=&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;s&quot;'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filename&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;combined_message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attach&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'cloud-init:user-data'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;combined_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;customer_metadata&lt;/code&gt; fodder is generated with:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ userdatagen /tmp/kvm-to-bhyve.yaml:cloud-config
{&quot;cloud-init:user-data&quot;: &quot;From nobody Tue Apr 17 16:40:55 2018\nContent-Type: multipart/mixed; boundary=\&quot;===============8760689715383894136==\&quot;\nMIME-Version: 1.0\n\n--===============8760689715383894136==\nMIME-Version: 1.0\nContent-Type: text/cloud-config; charset=\&quot;us-ascii\&quot;\nContent-Transfer-Encoding: 7bit\nContent-Disposition: attachment; filename=\&quot;/tmp/kvm-to-bhyve.yaml\&quot;\n\n#cloud-config\nwrite_files:\n  - content: |\n      GRUB_DEFAULT=0\n      GRUB_TIMEOUT=5\n      GRUB_DISTRIBUTOR=`lsb_release -i -s 2&amp;gt; /dev/null || echo Debian`\n      GRUB_CMDLINE_LINUX_DEFAULT=\&quot;tsc=reliable earlyprintk\&quot;\n      GRUB_CMDLINE_LINUX=\&quot;\&quot;\n      GRUB_TERMINAL=\&quot;serial console\&quot;\n      GRUB_CMDLINE_LINUX=\&quot;console=tty0 console=ttyS0,115200n8\&quot;\n      GRUB_SERIAL_COMMAND=\&quot;serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1\&quot;\n    path: /etc/default/grub\nruncmd:\n  - update-grub2 &amp;gt;/var/tmp/update-grub2.log 2&amp;gt;&amp;amp;1\n  - shutdown -r +1\n\n--===============8760689715383894136==--\n&quot;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That gets copied into the &lt;code class=&quot;highlighter-rouge&quot;&gt;customer_metadata&lt;/code&gt; section of my &lt;code class=&quot;highlighter-rouge&quot;&gt;vmadm&lt;/code&gt; payload.&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;alias&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;kvm2bhyve&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;brand&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;bhyve&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;resolvers&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;8.8.8.8&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;8.8.4.4&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ram&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;1024&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;vcpus&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;nics&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;nic_tag&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;admin&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;ip&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;10.88.88.207&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;netmask&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;255.255.255.0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;gateway&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;10.88.88.2&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;model&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;virtio&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;primary&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;disks&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;image_uuid&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;429bf9f2-bb55-4c6f-97eb-046fa905dd03&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;boot&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;model&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;virtio&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;zlog_max_size&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1048576&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;zlog_keep_rotated&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;customer_metadata&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;cloud-init:user-data&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;From nobody Tue Apr 17 16:40:55 2018&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Content-Type: multipart/mixed; boundary=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;===============8760689715383894136==&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;MIME-Version: 1.0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;--===============8760689715383894136==&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;MIME-Version: 1.0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Content-Type: text/cloud-config; charset=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;us-ascii&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Content-Transfer-Encoding: 7bit&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Content-Disposition: attachment; filename=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/tmp/kvm-to-bhyve.yaml&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\n\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;#cloud-config&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;write_files:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  - content: |&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_DEFAULT=0&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_TIMEOUT=5&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_DISTRIBUTOR=`lsb_release -i -s 2&amp;gt; /dev/null || echo Debian`&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_CMDLINE_LINUX_DEFAULT=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;tsc=reliable earlyprintk&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_CMDLINE_LINUX=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\&quot;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_TERMINAL=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;serial console&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_CMDLINE_LINUX=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;console=tty0 console=ttyS0,115200n8&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;      GRUB_SERIAL_COMMAND=&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;    path: /etc/default/grub&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;runcmd:&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  - update-grub2 &amp;gt;/var/tmp/update-grub2.log 2&amp;gt;&amp;amp;1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;  - shutdown -r +1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;--===============8760689715383894136==--&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that the &lt;code class=&quot;highlighter-rouge&quot;&gt;image_uuid&lt;/code&gt; is for an &lt;code class=&quot;highlighter-rouge&quot;&gt;ubuntu-certified-16.04&lt;/code&gt; image.&lt;/p&gt;

&lt;p&gt;If all goes well, a couple minutes after running &lt;code class=&quot;highlighter-rouge&quot;&gt;vmadm create&lt;/code&gt;, you will notice
the grub menu on the console.&lt;/p&gt;</content><author><name></name></author><category term="bhyve" /><category term="zones" /><summary type="html">So far, most of my use of bhyve has been with kvm images that have been customize to work better with bhyve. For Ubuntu images, those customizations have generally been:</summary></entry><entry><title type="html">Mutual exclusion of bhyve and kvm on SmartOS</title><link href="https://mgerdts.github.io/2018/03/20/bhyve-kvm-mutual-exclusion.html" rel="alternate" type="text/html" title="Mutual exclusion of bhyve and kvm on SmartOS" /><published>2018-03-20T14:00:00+00:00</published><updated>2018-03-20T14:00:00+00:00</updated><id>https://mgerdts.github.io/2018/03/20/bhyve-kvm-mutual-exclusion</id><content type="html" xml:base="https://mgerdts.github.io/2018/03/20/bhyve-kvm-mutual-exclusion.html">&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This post is of historical interest only.  With the &lt;a href=&quot;https://github.com/joyent/illumos-joyent/commit/befffd577ca6c3a090d7d3c72d267a383c3a3c45&quot;&gt;fix&lt;/a&gt; for &lt;a href=&quot;https://smartos.org/bugview/OS-7080&quot;&gt;OS-7080&lt;/a&gt; in July of 2018 bhyve and kvm can coexist in peace.&lt;/p&gt;

&lt;p&gt;If two hypervisors are not able to coordinate with each other, they must not
both use hardware viritualization.  Within SmartOS, there is a pair of
functions, &lt;code class=&quot;highlighter-rouge&quot;&gt;hvm_excl_hold()&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;hvm_excl_rele()&lt;/code&gt;, described by &lt;a href=&quot;https://github.com/joyent/illumos-joyent/blob/master/usr/src/uts/i86pc/os/pc_hvm.c#L24&quot;&gt;this
comment&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/*
 * HVM Exclusion Interface
 *
 * To avoid VMX/SVM conflicts from arising when multiple hypervisor providers
 * (eg. KVM, bhyve) are shipped with the system, this simple advisory locking
 * system is presented for their use.  Until a proper hypervisor API, like the
 * one in OSX, is shipped in illumos, this will serve as opt-in regulation to
 * dictate that only a single hypervisor be allowed to configure the system and
 * run at any given time.
 */
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This advisory lock is taken as the first VM is starting and is released when the
last one is stopped.  This allows the use of either hypervisor, but prevents the
concurrent use of both.  Notably, the lock is not taken at module load time.&lt;/p&gt;

&lt;h3 id=&quot;who-has-the-lock&quot;&gt;Who has the lock?&lt;/h3&gt;

&lt;p&gt;You can use &lt;code class=&quot;highlighter-rouge&quot;&gt;mdb&lt;/code&gt; to see which hypervisor currently has the lock:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# mdb -k
Loading modules: [ ... ]
&amp;gt; hvm_excl_holder::print
0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After starting a kvm instance, it says:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; hvm_excl_holder::print
0xfffffffff8224185 &quot;SmartOS KVM&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;how-is-the-lock-taken-and-released&quot;&gt;How is the lock taken and released?&lt;/h3&gt;

&lt;p&gt;Using a little dtrace, we can see the call stack involved in taking the lock.
For some reason, &lt;code class=&quot;highlighter-rouge&quot;&gt;dtrace&lt;/code&gt; couldn’t resolve the symbols so I used &lt;code class=&quot;highlighter-rouge&quot;&gt;::whatis&lt;/code&gt; in
&lt;code class=&quot;highlighter-rouge&quot;&gt;mdb&lt;/code&gt; to fill in the missing symbols.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# dtrace -n 'hvm_excl_*:entry { stack() }'
dtrace: description 'hvm_excl_*:entry ' matched 2 probes
CPU     ID                    FUNCTION:NAME
 48   8666              hvm_excl_hold:entry 
              0xfffffffff82aec97 (vmmdev_mod_incr+0x37)
              0xfffffffff82aee23 (vmmdev_do_vm_create+0x73)
              0xfffffffff82afbd3 (vmm_ioctl+0x193)
              genunix`cdev_ioctl+0x39
              specfs`spec_ioctl+0x60
              genunix`fop_ioctl+0x55
              genunix`ioctl+0x9b
              unix`sys_syscall+0x19f

 72   9030              hvm_excl_rele:entry 
              0xfffffffff82aed11 (vmmdev_mod_decr+0x31)
              0xfffffffff82af6b6 (vmm_do_vm_destroy_locked+0xe6)
              0xfffffffff82af70d (vmm_do_vm_destroy+0x3d)
              0xfffffffff82c9f9a (vmm_zsd_destroy+0x3a)
              genunix`zsd_apply_destroy+0x1d1
              genunix`zsd_apply_all_keys+0x5f
              genunix`zone_zsd_callbacks+0xe6
              genunix`zone_destroy+0x124
              genunix`zone+0x1e7
              unix`sys_syscall+0x19f
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This release output shows that the bhyve instance I was running is in a zone and
the destroy of the VM state was taken care of by the &lt;em&gt;zone specific data&lt;/em&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;destroy&lt;/code&gt; callback.&lt;/p&gt;

&lt;h3 id=&quot;and-now-for-something-unexpected&quot;&gt;And now for something unexpected&lt;/h3&gt;

&lt;p&gt;Despite what I said above, it is possible to run bhyve and kvm instances
simultaneously.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[root@emy-17 /root]# vmadm start $(vm test)
Successfully started VM 79062669-e229-e55d-960d-9b18d0fed8d0
[root@emy-17 /root]# vmadm start $(vm kvm-debian8)
Successfully started VM 57409636-927a-cc7a-d744-e0f92b73f4e4
[root@emy-17 /root]# vmadm list state=running
UUID                                  TYPE  RAM      STATE             ALIAS
57409636-927a-cc7a-d744-e0f92b73f4e4  KVM   1024     running           kvm-debian8
79062669-e229-e55d-960d-9b18d0fed8d0  BHYV  2048     running           test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It turns out that bhyve has the lock.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# echo hvm_excl_holder::print | mdb -k
0xfffffffff82caded &quot;bhyve&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, what’s happening here?  In this case, kvm was not able to get the lock to
use hardware virtualization.  It is falling back to QEMU’s software
virtualization, which is much slower.&lt;/p&gt;

&lt;p&gt;Had the kvm instance been started first, kvm would have gotten the lock and the
bhyve instance would have failed to boot.  Now that both of these VMs are set to
being enabled, on the next reboot of the host, they will race to get the lock.&lt;/p&gt;

&lt;p&gt;To state the obvious, trying to run bhyve and kvm VMs simultaneously is not
recommended.&lt;/p&gt;</content><author><name></name></author><category term="omnios" /><category term="bhyve" /><summary type="html">Note: This post is of historical interest only. With the fix for OS-7080 in July of 2018 bhyve and kvm can coexist in peace.</summary></entry><entry><title type="html">OmniOS bhyve guest</title><link href="https://mgerdts.github.io/2018/03/14/omnios-bhyve-guest.html" rel="alternate" type="text/html" title="OmniOS bhyve guest" /><published>2018-03-14T23:00:00+00:00</published><updated>2018-03-14T23:00:00+00:00</updated><id>https://mgerdts.github.io/2018/03/14/omnios-bhyve-guest</id><content type="html" xml:base="https://mgerdts.github.io/2018/03/14/omnios-bhyve-guest.html">&lt;p&gt;I had a very pleasant surprise today.  I have some work that I need to
contribute to illumos and figured that a little dogfooding would be good for me.
It seems as though two excellent distros for building illumos are OpenIndiana
and OmniOS.  Since I don’t really need a desktop and I’ve not used OmniOS
before, OmniOS seemed like a great choice for the day.&lt;/p&gt;

&lt;p&gt;After a bit of looking around at
&lt;a href=&quot;https://omniosce.org/download.html&quot;&gt;omniosce.org&lt;/a&gt; I came across something that
was almost too good to be true:
&lt;a href=&quot;https://downloads.omniosce.org/media/bloody/omnios-bhyve.zfs.xz&quot;&gt;omnios-bhyve.zfs.xz&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Let’s take that for a spin:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# curl https://downloads.omniosce.org/media/bloody/omnios-bhyve.zfs.xz | xzcat | zfs recv zones/omnios
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  343M  100  343M    0     0  3374k      0  0:01:44  0:01:44 --:--:-- 4368k
# zfs clone zones/omnios@good zones/o1
# bhyve -m 16g -c 16 -l com1,stdio -P -H -s 1,lpc -s 3,virtio-blk,/dev/zvol/rdsk/zones/o1 -s 4,virtio-net-viona,o1 -l bootrom,/usr/share/bhyve/uefi-csm-rom.bin o1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;OMG!  A few seconds later and I saw loader, and a few seconds after that I had a
login prompt.  Woot!&lt;/p&gt;

&lt;p&gt;Well, let’s make this work with the SmartOS bits that are almost ready for you
to take for a spin.  First, I’ll turn this into an image that will work well
with &lt;code class=&quot;highlighter-rouge&quot;&gt;vmadm&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# zfs rename zones/omnios@good zones/omnios@final
# zfs send zones/omnios@final | pigz &amp;gt; /zones/images/6c4da93e-cffd-e809-a352-e4e11de3b067/disk0.zfs.gz
# ls -l disk0.zfs.gz
total 1011562
-rw-r--r--   1 root     root     517860460 Mar 15 00:20 disk0.zfs.gz
-rw-r--r--   1 root     root         918 Mar 15 00:19 manifest.json
# digest -a sha1 disk0.zfs.gz
9d14866b9ec35443adcffc34fed54bd7f1e62700
# vi manifest.json
# imgadm install -m manifest.json -f disk0.zfs.gz
Installing image 6c4da93e-cffd-e809-a352-e4e11de3b067 (omnios-master-39b8863d01@20180301)
...-e809-a352-e4e11de3b067 [=========================================&amp;gt;] 100% 493.87MB  29.32MB/s    16s
Installed image 6c4da93e-cffd-e809-a352-e4e11de3b067 (omnios-master-39b8863d01@20180301)
[root@emy-17 ~]# imgadm list os=illumos
UUID                                  NAME                      VERSION   OS       TYPE  PUB
6c4da93e-cffd-e809-a352-e4e11de3b067  omnios-master-39b8863d01  20180301  illumos  zvol  2018-03-15
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Next, shamelessly copy an early &lt;code class=&quot;highlighter-rouge&quot;&gt;vmadm create&lt;/code&gt; payload to create a new vm.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# cp test.json omni1.json
# vi omni1.json
# vmadm create -f omni1.json
[some output that I lost in my haste.  Just the normal stuff that says things went well.]
# vmadm console $(vm omni1)
...
Copyright (c) 1983, 2010, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2017-2018 OmniOS Community Edition (OmniOSce) Association.
Loading smf(5) service descriptions: 131/131
Configuring devices.
NOTICE: vioif0: Got MAC address from host: 72:93:42:ef:32:a0
NOTICE: Csum enabled.
Applying initial boot settings...
Hostname: omniosce

omniosce console login:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Log in as &lt;code class=&quot;highlighter-rouge&quot;&gt;root&lt;/code&gt; with no password, and I’m ready to configure networking, users,
etc.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@omniosce:~# dladm show-link
LINK        CLASS     MTU    STATE    BRIDGE     OVER
vioif0      phys      1500   up       --         --
root@omniosce:~# ipadm create-if vioif0
root@omniosce:~# ipadm create-addr -T static -a 172.26.17.173/24 vioif0/v4
root@omniosce:~# ping 172.26.17.1
172.26.17.1 is alive
root@omniosce:~# route -p add default 172.26.17.1
add net default: gateway 172.26.17.1: entry exists
add persistent net default: gateway 172.26.17.1
root@omniosce:~# vi /etc/resolv.conf
root@omniosce:~# vi /etc/nsswitch.conf
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Thanks much to (andyf?) for creating the image.&lt;/p&gt;</content><author><name></name></author><category term="omnios" /><category term="bhyve" /><summary type="html">I had a very pleasant surprise today. I have some work that I need to contribute to illumos and figured that a little dogfooding would be good for me. It seems as though two excellent distros for building illumos are OpenIndiana and OmniOS. Since I don’t really need a desktop and I’ve not used OmniOS before, OmniOS seemed like a great choice for the day.</summary></entry><entry><title type="html">Why must we manually configure a serial console?</title><link href="https://mgerdts.github.io/2018/03/11/why-configure-serial-console.html" rel="alternate" type="text/html" title="Why must we manually configure a serial console?" /><published>2018-03-11T18:00:00+00:00</published><updated>2018-03-11T18:00:00+00:00</updated><id>https://mgerdts.github.io/2018/03/11/why-configure-serial-console</id><content type="html" xml:base="https://mgerdts.github.io/2018/03/11/why-configure-serial-console.html">&lt;p&gt;I was looking at ACPI tables in a bhyve guest and stumbled across this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;root@test:/tmp# acpidump -b spcr
root@test:/tmp# iasl -d spcr.dat

Intel ACPI Component Architecture
ASL+ Optimizing Compiler version 20160831-64
Copyright (c) 2000 - 2016 Intel Corporation

Input file spcr.dat, Length 0x50 (80) bytes
ACPI: SPCR 0x0000000000000000 000050 (v01 BHYVE  BVSPCR   00000001 BHYV 00000001)
Acpi Data Table [SPCR] decoded
Formatted output:  spcr.dsl - 2599 bytes
root@test:/tmp# cat spcr.dsl
/*
 * Intel ACPI Component Architecture
 * AML/ASL+ Disassembler version 20160831-64
 * Copyright (c) 2000 - 2016 Intel Corporation
 *
 * Disassembly of spcr.dat, Sun Mar 11 17:44:37 2018
 *
 * ACPI Data Table [SPCR]
 *
 * Format: [HexOffset DecimalOffset ByteLength]  FieldName : FieldValue
 */

[000h 0000   4]                    Signature : &quot;SPCR&quot;    [Serial Port Console Redirection table]
[004h 0004   4]                 Table Length : 00000050
[008h 0008   1]                     Revision : 01
[009h 0009   1]                     Checksum : 78
[00Ah 0010   6]                       Oem ID : &quot;BHYVE &quot;
[010h 0016   8]                 Oem Table ID : &quot;BVSPCR  &quot;
[018h 0024   4]                 Oem Revision : 00000001
[01Ch 0028   4]              Asl Compiler ID : &quot;BHYV&quot;
[020h 0032   4]        Asl Compiler Revision : 00000001

[024h 0036   1]               Interface Type : 00
[025h 0037   3]                     Reserved : 000000

[028h 0040  12]         Serial Port Register : [Generic Address Structure]
[028h 0040   1]                     Space ID : 01 [SystemIO]
[029h 0041   1]                    Bit Width : 08
[02Ah 0042   1]                   Bit Offset : 00
[02Bh 0043   1]         Encoded Access Width : 00 [Undefined/Legacy]
[02Ch 0044   8]                      Address : 00000000000003F8

[034h 0052   1]               Interrupt Type : 01
[035h 0053   1]          PCAT-compatible IRQ : 04
[036h 0054   4]                    Interrupt : 00000000
[03Ah 0058   1]                    Baud Rate : 07
[03Bh 0059   1]                       Parity : 00
[03Ch 0060   1]                    Stop Bits : 01
[03Dh 0061   1]                 Flow Control : 03
[03Eh 0062   1]                Terminal Type : 02
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that it seems to have the address for &lt;code class=&quot;highlighter-rouge&quot;&gt;COM1&lt;/code&gt; (0x3F8), IRQ 4, etc., which
all seems legit for my setup.  If all of this available when a machine redirects
a serial console, why have I been manually configuring serial consoles all these
years?&lt;/p&gt;

&lt;p&gt;Let me predict the most common reasons:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;It is somethign “new” that Microsoft came up with.  By “new” I mean sometime
in the past decade.&lt;/li&gt;
  &lt;li&gt;The BIOS implementations that support it are so buggy that the information is
not reliable&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;FWIW, &lt;a href=&quot;https://docs.microsoft.com/en-us/previous-versions/windows/hardware/design/dn639132(v=vs.85)&quot;&gt;the specification&lt;/a&gt; is available in &lt;code class=&quot;highlighter-rouge&quot;&gt;.docx&lt;/code&gt; format from Microsoft.&lt;/p&gt;</content><author><name></name></author><category term="acpi" /><category term="console" /><category term="bhyve" /><summary type="html">I was looking at ACPI tables in a bhyve guest and stumbled across this:</summary></entry><entry><title type="html">Socializing my blog</title><link href="https://mgerdts.github.io/2018/03/04/socializing-my-blog.html" rel="alternate" type="text/html" title="Socializing my blog" /><published>2018-03-04T16:00:00+00:00</published><updated>2018-03-04T16:00:00+00:00</updated><id>https://mgerdts.github.io/2018/03/04/socializing-my-blog</id><content type="html" xml:base="https://mgerdts.github.io/2018/03/04/socializing-my-blog.html">&lt;p&gt;There have been a few changes in this blog:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Now has &lt;a href=&quot;/feed.xml&quot;&gt;an atom feed&lt;/a&gt;.  Thanks to John Levon for the nudge and the
&lt;a href=&quot;https://github.com/jekyll/jekyll-feed&quot;&gt;Jekyll Feed Plugin&lt;/a&gt; for the
simplicity.&lt;/li&gt;
  &lt;li&gt;Comments as github issues.  Thanks to
&lt;a href=&quot;http://ivanzuzak.info/2011/02/18/github-hosted-comments-for-github-hosted-blogs.html&quot;&gt;Ivan Zuzak&lt;/a&gt;
and &lt;a href=&quot;http://hydroecology.net/using-github-to-host-blog-comments/&quot;&gt;Michael Koohafkan&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;Prefer to tweet?  We got that too!  Check out the recent mentions on the
right-hand side and add your own. Sadly, the &lt;em&gt;View on Twitter&lt;/em&gt; button seems to
be useless.&lt;/li&gt;
&lt;/ul&gt;</content><author><name></name></author><summary type="html">There have been a few changes in this blog:</summary></entry><entry><title type="html">uefi debug in bhyve</title><link href="https://mgerdts.github.io/2018/02/26/uefi-debug-in-bhyve.html" rel="alternate" type="text/html" title="uefi debug in bhyve" /><published>2018-02-26T02:00:00+00:00</published><updated>2018-02-26T02:00:00+00:00</updated><id>https://mgerdts.github.io/2018/02/26/uefi-debug-in-bhyve</id><content type="html" xml:base="https://mgerdts.github.io/2018/02/26/uefi-debug-in-bhyve.html">&lt;p&gt;While debugging a bug in &lt;a href=&quot;https://smartos.org/bugview/OS-6604&quot;&gt;SmartOS bhyve&lt;/a&gt; seen on Ubuntu guests, I needed to debug two areas that most people avoid: &lt;code class=&quot;highlighter-rouge&quot;&gt;grub&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;uefi&lt;/code&gt;.  Debugging &lt;code class=&quot;highlighter-rouge&quot;&gt;grub&lt;/code&gt; is not so bad, as you can at least add print statements and they appear on the console.  &lt;code class=&quot;highlighter-rouge&quot;&gt;uefi&lt;/code&gt; was not quite as cooperative at first.&lt;/p&gt;

&lt;p&gt;There are several things involved at build time:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;While running the uefi-edk2 build script, you need the command line argument &lt;code class=&quot;highlighter-rouge&quot;&gt;-DDEBUG_ON_SERIAL_PORT=TRUE&lt;/code&gt;.  If building &lt;a href=&quot;https://github.com/joyent/illumos-extra/tree/master/uefi-edk2&quot;&gt;uefi-edk2 in illumos-extra&lt;/a&gt;, this is already taken care of.&lt;/li&gt;
  &lt;li&gt;You may need to tweak the &lt;code class=&quot;highlighter-rouge&quot;&gt;PcdDebugPrintErrorLevel&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;PcdDebugPropertyMask&lt;/code&gt; in &lt;code class=&quot;highlighter-rouge&quot;&gt;BhyvePkg/BhyvePkgX64.dsc&lt;/code&gt; to filter the particular types of messages you see.  I’ve not experimented with these.&lt;/li&gt;
  &lt;li&gt;In &lt;code class=&quot;highlighter-rouge&quot;&gt;BhyvePkg/Csm/BhyveCsm16/GNUmakefile&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;DEBUG_PORT&lt;/code&gt; needs to point to the proper &lt;a href=&quot;https://en.wikipedia.org/wiki/COM_(hardware_interface)&quot;&gt;COM port&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;In &lt;code class=&quot;highlighter-rouge&quot;&gt;BhyvePkg/Csm/BhyveCsm16/Printf.c&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;DebugLevel&lt;/code&gt; needs to be set to a level that gets the desired verbosity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These last two bullets are taken care of with &lt;a href=&quot;https://github.com/mgerdts/uefi-edk2/commit/eeeeb9c01ecab3d0830d2e00e43a1f9d8a408c2a&quot;&gt;this changeset&lt;/a&gt;.  Notice that it also changes &lt;code class=&quot;highlighter-rouge&quot;&gt;PcdDebugIoPort&lt;/code&gt; in &lt;code class=&quot;highlighter-rouge&quot;&gt;BhyvePkg/BhyvePkg.dec&lt;/code&gt;.  I had expected that that would be sufficient to change the debug port to &lt;code class=&quot;highlighter-rouge&quot;&gt;COM1&lt;/code&gt;, but it seemed to have no effect.&lt;/p&gt;

&lt;p&gt;Once I did this, I wasn’t completely out of the woods, as I found that this &lt;a href=&quot;https://smartos.org/bugview/OS-6694&quot;&gt;caused bhyve to crash&lt;/a&gt; because its uart emulation only handled 1 or 2 byte operations.  uefi works 4 bytes at a time.  &lt;a href=&quot;https://github.com/mgerdts/illumos-joyent/commit/ec04362e4d1964b7135cd7b70545ab97ce33882b&quot;&gt;Here’s the fix&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;By the time you are reading this, the &lt;code class=&quot;highlighter-rouge&quot;&gt;bhyve&lt;/code&gt; bug will probably be long fixed.  To save you the hassle of building a debug uefi image, you can snag &lt;a href=&quot;https://us-east.manta.joyent.com/mgerdts/public/blog/downloads/uefi-csm-rom-debug-com1-info.bin&quot;&gt;the one I built&lt;/a&gt;.  You can activate that for a single bhyve zone with:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cp .../uefi-csm-rom-debug-com1-info.bin /zones/$uuid/root
vmadm update $uuid bootrom=/uefi-csm-rom-debug-com1-info.bin
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;</content><author><name></name></author><category term="uefi" /><category term="bhyve" /><summary type="html">While debugging a bug in SmartOS bhyve seen on Ubuntu guests, I needed to debug two areas that most people avoid: grub and uefi. Debugging grub is not so bad, as you can at least add print statements and they appear on the console. uefi was not quite as cooperative at first.</summary></entry><entry><title type="html">dtrace or mdb new processes</title><link href="https://mgerdts.github.io/2018/02/19/dtrace-or-mdb-new-processes.html" rel="alternate" type="text/html" title="dtrace or mdb new processes" /><published>2018-02-19T01:57:23+00:00</published><updated>2018-02-19T01:57:23+00:00</updated><id>https://mgerdts.github.io/2018/02/19/dtrace-or-mdb-new-processes</id><content type="html" xml:base="https://mgerdts.github.io/2018/02/19/dtrace-or-mdb-new-processes.html">&lt;p&gt;I have a process that will be started later in a special context that I need to
trace with &lt;code class=&quot;highlighter-rouge&quot;&gt;truss(1M)&lt;/code&gt;.  An easy way to do this is with a little help from &lt;code class=&quot;highlighter-rouge&quot;&gt;dtrace&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;In real life I did this as a one-liner on the command line.  I expand it here for the sake of comments&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-dtrace&quot;&gt;#! /usr/sbin/dtrace -ws

/*
 * Watch for the first system call performed by the zhyve program and
 * then trace that program with truss.
 */
syscall:::entry
/execname == &quot;zhyve&quot;/
{
	/* Stop the program */
	stop();
        /* Use truss to start tracing the program.  This causes the program to continue. */
	system(&quot;truss -t all -v all -w all -p %d&quot;, pid);
        /* Tell dtrace to exit once truss starts */
        exit(0);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This same approach works for attaching a debugger.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# dtrace -wn 'syscall:::entry / execname == &quot;zhyve&quot; / {stop(); system(&quot;mdb -p %d&quot;, pid); exit(0);}'
dtrace: description 'syscall:::entry ' matched 235 probes
dtrace: allowing destructive actions
CPU     ID                    FUNCTION:NAME
  0    795                 systeminfo:entry Loading modules: [ ld.so.1 ]
&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;From the stack trace we can see that we caught it very early.  This is even before &lt;code class=&quot;highlighter-rouge&quot;&gt;main()&lt;/code&gt; starts.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt; $C
ffffbf7fffdff970 ld.so.1`sysinfo+0xa()
ffffbf7fffdffcb0 ld.so.1`setup+0xebd(ffffbf7fffdffe78, ffffbf7fffdffe80, 0, ffffbf7fffdfffd0, 1000, ffffbf7fef3a99c8)
ffffbf7fffdffdd0 ld.so.1`_setup+0x282(ffffbf7fffdffde0, 190)
ffffbf7fffdffe60 ld.so.1`_rt_boot+0x6c()
0000000000000001 0xffffbf7fffdfffc0()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This example was taken from some work I am doing to bring bhyve to SmartOS.  In
this case, &lt;code class=&quot;highlighter-rouge&quot;&gt;zhyve&lt;/code&gt; is the replacement for the &lt;code class=&quot;highlighter-rouge&quot;&gt;init&lt;/code&gt; process in a &lt;code class=&quot;highlighter-rouge&quot;&gt;bhyve&lt;/code&gt;
branded zone.&lt;/p&gt;</content><author><name></name></author><category term="dtrace" /><category term="mdb" /><category term="bhyve" /><summary type="html">I have a process that will be started later in a special context that I need to trace with truss(1M). An easy way to do this is with a little help from dtrace:</summary></entry><entry><title type="html">ksh stack trace</title><link href="https://mgerdts.github.io/2011/01/03/ksh93-stack-traces.html" rel="alternate" type="text/html" title="ksh stack trace" /><published>2011-01-03T01:57:23+00:00</published><updated>2011-01-03T01:57:23+00:00</updated><id>https://mgerdts.github.io/2011/01/03/ksh93-stack-traces</id><content type="html" xml:base="https://mgerdts.github.io/2011/01/03/ksh93-stack-traces.html">&lt;p&gt;It is often times handy to get a
&lt;a href=&quot;http://en.wikipedia.org/wiki/Stack_trace&quot;&gt;stack trace&lt;/a&gt; to help you understand
how a program got to an error condition.  Unfortunately, shell scripting
languages tend to not provide an easy mechanism to get a stacktrace.  The
following examines how it can be accomplished with ksh93.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#! /bin/ksh&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;backtrace &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;typeset&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; stack 
        &lt;span class=&quot;c&quot;&gt;# Use &quot;set -u&quot; and an undefined variable access in a subshell&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# to figure out how we got here.  Each token of the result is&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# stored as an element in an indexed array named &quot;stack&quot;.&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-A&lt;/span&gt; stack &lt;span class=&quot;k&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;exec &lt;/span&gt;2&amp;gt;&amp;amp;1&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;unset &lt;/span&gt;__unset__&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$__unset__&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;)&lt;/span&gt;

        &lt;span class=&quot;c&quot;&gt;# Trim the last entries in stack array until we find the one that&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# matches the name of this function.&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;typeset &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;0
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt; i &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;${#&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[@]&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt; - 1&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; 0&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; i-- &lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;[[&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[i]&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.sh.fun&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;:&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;break
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;

        &lt;span class=&quot;c&quot;&gt;# Print the name of the function that called this one, stripping off&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# the [lineno] and appending any arguments provided to this function.&lt;/span&gt;
        print &lt;span class=&quot;nt&quot;&gt;-u2&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[i-1]/\[[0-9]*\]&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$*&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# Print the backtrace.&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt; i--&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; i &lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt; 0&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; i-- &lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do
                &lt;/span&gt;print &lt;span class=&quot;nt&quot;&gt;-u2&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\t&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;${&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[i]%&lt;/span&gt;:&lt;span class=&quot;k&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# A couple functions to illustrate the output&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;a &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        b &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;b &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        c &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function &lt;/span&gt;c &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# Trigger a backtrace and exit if no arguments were passed&lt;/span&gt;
        &lt;span class=&quot;o&quot;&gt;((&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$# &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; 0 &lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; backtrace &lt;span class=&quot;s2&quot;&gt;&quot;missing arg&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;exit &lt;/span&gt;1
        print &lt;span class=&quot;nt&quot;&gt;--&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

a &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$@&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A couple example runs:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./backtrace.ksh hello world!
hello world!
$ ./backtrace.ksh
c: missing arg
        c[37]
        b[32]
        a[28]
        ./backtrace.ksh[41]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This post first appeared on a &lt;a href=&quot;http://mgerdts.blogspot.com/2011/01/ksh93-backtraces.html&quot;&gt;legacy
platform&lt;/a&gt;&lt;/p&gt;</content><author><name></name></author><category term="shell" /><summary type="html">It is often times handy to get a stack trace to help you understand how a program got to an error condition. Unfortunately, shell scripting languages tend to not provide an easy mechanism to get a stacktrace. The following examines how it can be accomplished with ksh93.</summary></entry></feed>