<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Posts tagged with “Packer” on Mark van Lent’s weblog</title>
  <updated>2017-09-29T13:50:00+00:00</updated>
  <link rel="self" type="application/atom+xml" href="https://markvanlent.dev/tags/packer/index.xml" hreflang="en"/>
  <id>tag:markvanlent.dev,2010-04-02:/tags/packer/index.xml</id>
  <link rel="alternate" type="text/html" href="https://markvanlent.dev/tags/packer/" hreflang="en"/>
  <author>
      <name>Mark van Lent</name>
      <uri>https://markvanlent.dev/about/</uri>
    </author>
  <rights>Copyright (c) Mark van Lent, Creative Commons Attribution 4.0 International License.</rights>
  <icon>https://markvanlent.dev/favicon.ico</icon>
  <entry>
    <title type="html"><![CDATA[How to create a Debian VirtualBox machine with Packer with an additional host-only adapter]]></title>
    <link rel="alternate" href="https://markvanlent.dev/2017/09/29/how-to-create-a-debian-virtualbox-machine-with-packer-with-an-additional-host-only-adapter/" type="text/html" />
    <id>https://markvanlent.dev/2017/09/29/how-to-create-a-debian-virtualbox-machine-with-packer-with-an-additional-host-only-adapter/</id>
    <author>
      <name>map[name:Mark van Lent uri:https://markvanlent.dev/about/]</name>
    </author>
    <category term="devops" />
    <category term="packer" />
    <category term="tools" />
    <category term="virtualbox" />
    
    <updated>2021-10-07T18:13:55Z</updated>
    <published>2017-09-29T13:50:00Z</published>
    <content type="html"><![CDATA[<p>For a project I am working on, there is this virtual machine we can
use to do our development work in. This machine has grown organically
and I want to replace it with something I can reproduce. I wanted to
experiment with Packer but had problems with generating a machine with
two network adapters where the second one is connected to a host-only
network.</p>
<h2 id="context">Context</h2>
<p>Before getting into the details, first a bit of context. The virtual
machine (VM) we are using is really convenient. It was created by a
co-worker and was exported some time in the past and is now available
as an <code>.ova</code> for other developers in the team.  But the machine itself
has been built over a longer period of time, using Debian Jessie
(which is now the
<a href="https://wiki.debian.org/DebianOldStable">oldstable</a>) and some
project requirements have changed.</p>
<p>I could upgrade the VM to Stretch, update the packages and export the
VM again. But I&rsquo;d rather get something reproducible. Something I can
build again and again and know exactly <em>how</em> it was created, <em>what</em>
was installed, et cetera. (And truth be told, perhaps there&rsquo;s
also a bit of
<a href="https://en.wikipedia.org/wiki/Not_invented_here">not invented here</a>
involved.)</p>
<h2 id="problem">Problem</h2>
<p>To get to such a reproducible VM I decided to try out
<a href="https://www.packer.io/">Packer</a>. I also found a great Git repository
which you can use to generate a Debian 9 Vagrant Box:
<a href="https://github.com/geerlingguy/packer-debian-9">https://github.com/geerlingguy/packer-debian-9</a>. This
got me started fairly quickly.</p>
<p><em>(As a side note: if you are interested in Ansible, you should
definitely check out
<a href="https://github.com/geerlingguy?tab=repositories">Jeff Geerling&rsquo;s other GitHub repositories</a>.)</em></p>
<p>The problem was that I needed not one, but <strong>two</strong> network adapters
for my VM, where the second interface had to be connected to a
<a href="https://www.virtualbox.org/manual/ch06.html#network_hostonly">host-only</a>
network.</p>
<h2 id="the-solution">The solution</h2>
<p>First of all, I needed to add the second adapter to the
configuration. In the <code>debian9.json</code> file, I replaced the original
<code>vboxmanage</code> section with the following:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="s2">&#34;vboxmanage&#34;</span><span class="err">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;modifyvm&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;{{.Name}}&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;--memory&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;1024&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">],</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;modifyvm&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;{{.Name}}&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;--cpus&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;1&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">],</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;modifyvm&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;{{.Name}}&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;--nic2&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;hostonly&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">],</span>
</span></span><span class="line"><span class="cl">  <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;modifyvm&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;{{.Name}}&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;--hostonlyadapter2&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="s2">&#34;vboxnet0&#34;</span>
</span></span><span class="line"><span class="cl">  <span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="p">]</span>
</span></span></code></pre></div><p>In other words I added the <code>--nic2</code> and <code>--hostonlyadapter2</code> options.</p>
<p>But when I tried to build the VM, Packer kept waiting for SSH to
become available. After starting the build again with the &ldquo;<code>headless</code>&rdquo;
option set to &ldquo;<code>false</code>&rdquo; I could see the problem: the Debian installer
was waiting for me to select which interface to use (since there were
two now).</p>
<p>I solved this by inserting the following line to the <code>boot_command</code>
section in the <code>debian9.json</code> file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="s2">&#34;interface=enp0s3 &lt;wait&gt;&#34;</span><span class="err">,</span>
</span></span></code></pre></div><p>Now the first interface was automatically chosen and the build
succeeded again. The result: a VM with two network adapters, where the
first one is attached to the NAT network, and the second one to the
host-only network <code>vboxnet0</code>.</p>]]></content>
  </entry>
</feed>
