<?xml version="1.0" encoding="utf-8" ?>

<rss version="2.0" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    
    <title>Raymond P. Burkholder - Things I Do - VirtualBox</title>
    <link>https://blog.raymond.burkholder.net/</link>
    <description>In And Around Technology and The Arts</description>
    <dc:language>en</dc:language>
    <generator>Serendipity 1.7.2 - http://www.s9y.org/</generator>
    <pubDate>Thu, 17 Nov 2016 17:52:03 GMT</pubDate>

    <image>
        <url>https://blog.raymond.burkholder.net/templates/bulletproof/img/s9y_banner_small.png</url>
        <title>RSS: Raymond P. Burkholder - Things I Do - VirtualBox - In And Around Technology and The Arts</title>
        <link>https://blog.raymond.burkholder.net/</link>
        <width>100</width>
        <height>21</height>
    </image>

<item>
    <title>Automating the Vagrant Guest Additions Build</title>
    <link>https://blog.raymond.burkholder.net/index.php?/archives/695-Automating-the-Vagrant-Guest-Additions-Build.html</link>
            <category>VirtualBox</category>
    
    <comments>https://blog.raymond.burkholder.net/index.php?/archives/695-Automating-the-Vagrant-Guest-Additions-Build.html#comments</comments>
    <wfw:comment>https://blog.raymond.burkholder.net/wfwcomment.php?cid=695</wfw:comment>

    <slash:comments>0</slash:comments>
    <wfw:commentRss>https://blog.raymond.burkholder.net/rss.php?version=2.0&amp;type=comments&amp;cid=695</wfw:commentRss>
    

    <author>nospam@example.com (Raymond P. Burkholder)</author>
    <content:encoded>
    &lt;p&gt;In an earlier post, I wrote about installing Vagrant Guest Additions in a Debian Stretch distribution.

&lt;p&gt;Here is a script, which needs to be run as root inside the guest to be used as the Vagrant package base, which will perform the necessary changes.  For running with Linux kernel 4.8 or later, VirtualBox 5.1.8 or later will be required.  

&lt;p&gt;The command line takes one parameter... the target VirtualBox version, which at the time of this writing, is 5.1.8.  This example would be run as root with (if the code is in a file called additions.sh):

&lt;blockquote&gt;&lt;pre&gt;
bash additions.sh 5.1.8
&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;The script takes care of obtaining the GuestAdditions iso, installing related packages, building the additions, removing the packages, and zeroing out dead disk space.  This makes it ready for the second script which will generate the package.box.

&lt;blockquote&gt;&lt;pre&gt;
#! /usr/bin/bash

function cleanup {

  # package clean up
  apt-get autoremove
  apt-get purge
  apt-get clean
  apt-get autoclean

  # remove indexes
  rm /var/lib/apt/lists/*

  # todo: look at /lib/modules when kernel upgraded

  # zero out unused space for more efficient packing
  echo &quot;Zeroing out dead space ...&quot;
  dd if=/dev/zero of=/EMPTY bs=1M
  rm -f /EMPTY
  }

function vagrantuser {
  # sudo settings
cat &lt;&lt;EOT &gt;&gt; /etc/sudoers.d/10-sudo-overrides
Defaults      env_keep += &quot;GIT_*&quot;
Defaults&gt;root env_keep += SSH_AUTH_SOCK
Defaults&gt;root !requiretty
%sudo         ALL=NOPASSWD: ALL
EOT

  # default vagrant key for bootstrapping
  echo &quot;Installing default key ....&quot;
  mkdir /home/vagrant/.ssh
  chmod 700 /home/vagrant/.ssh
  pushd /home/vagrant/.ssh
  wget --no-check-certificate \
    &#039;https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub&#039; \
    -O authorized_keys
  chmod 600 /home/vagrant/.ssh/authorized_keys
  chown -R vagrant /home/vagrant/.ssh
  popd
  }

function guestadditions {

  VBOXVER=&quot;$1&quot;

  # obtain and mount the VBoxGuestAdditions iso
  echo &quot;Obtaining Guest Additions ...&quot;
  wget \
    http://download.virtualbox.org/virtualbox/${VBOXVER}/VBoxGuestAdditions_${VBOXVER}.iso
  mkdir /media/VBoxGuestAdditions
  mount -o loop,ro VBoxGuestAdditions_${VBOXVER}.iso /media/VBoxGuestAdditions

  # build the additions
  echo &quot;Building additions ...&quot;
  KERN_DIR=/usr/src/linux-headers-$(uname -r)/ \
    sh /media/VBoxGuestAdditions/VBoxLinuxAdditions.run

  # unmount and remove the iso
  echo &quot;Building done, unmount iso ...&quot;
  umount /media/VBoxGuestAdditions
  rmdir /media/VBoxGuestAdditions
  rm VBoxGuestAdditions_${VBOXVER}.iso

  }

function build {

  VBOXVER=&quot;$1&quot;

  echo &quot;sources:  &quot;
  cat /etc/apt/sources.list | awk &#039;/^deb/ {print $0}&#039;

  # sudo and ssh key are one time settings
  if [[ &quot;&quot; == &quot;$(ls -d /usr/src/vbox*)&quot; ]]; then
    vagrantuser
    fi

  # install build tools for the VirtualBox Guest Additions, required for Vagrant
  echo &quot;Installing packages ...&quot;
  apt-get update
  PACKAGES=$(apt-get -y install linux-headers-$(uname -r) build-essential | awk \
&#039;BEGIN { flag=0; list=&quot;&quot; } \
/^[^ ]/ { flag=0 } \
/NEW packages will be installed/ { flag=1 } \
/  / { if (1 == flag) list=list$0 } \
END { print list }&#039; \
)
  echo &quot;Installed packages:  ${PACKAGES}&quot;

  guestadditions ${VBOXVER}

  # remove packages
  echo &quot;Removing packages and cleaning up ...&quot;
  apt-get -y remove ${PACKAGES}
  apt-get -y remove linux-headers-$(uname -r)

  # add in some commonly used packages
  apt-get -y install less tcpdump vim

  cleanup

  # show guest stuff installed
  ls -alt /usr/src
  df -h

  IMAGECNT=$(dpkg -l | grep linux-image | wc -l)
   if  [ &quot;1&quot; != &quot;${IMAGECNT}&quot; ]; then
     echo &quot;More than one image installed, remove excess to reclaim space&quot;
     dpkg -l | grep linux-image
     fi

  }

if [ &quot;root&quot; != $(whoami) ]; then
  echo &quot;need to be user root&quot;
else
  if [ &quot;&quot; == &quot;$1&quot; ]; then
    echo &quot;[virtualbox version code (like 5.1.&lt;img src=&quot;https://blog.raymond.burkholder.net/templates/default/img/emoticons/cool.png&quot; alt=&quot;8-)&quot; style=&quot;display: inline; vertical-align: bottom;&quot; class=&quot;emoticon&quot; /&gt; | clean]&quot;
  else
    case &quot;$1&quot; in
      clean)
        cleanup
        ;;
      *)
        build $1
        ;;
      esac
    fi
  fi
&lt;/pre&gt;&lt;/blockquote&gt;

&lt;p&gt;Ensure the VirtualBox guest network interface #1 is NAT with para-virtualized IO.  And verify the CD is removed.
&lt;p&gt;Here is a script to run to pack a VirtualBox VM/Template into a box file for use by Vagrant (after the above script has been applied inside of the guest).  On Windows, I run it in a cygwin environment.  On Linux, it can be run natively.

&lt;blockquote&gt;&lt;pre&gt;
#! /usr/bin/bash

# $1 - source base name
# $2 - packaged box name

if [ 2 != $# ]; then
  echo &quot;two arguments:  VirtualBoxVMName PackagedBoxName&quot;
else 

  VIRTUALBOXNAME=$1
  PACKAGEDNAME=$2

  # create a basic description file
  cat &amp;lt;&amp;lt;EOT &amp;gt;&amp;gt; ${PACKAGEDNAME}.vf
Vagrant::Config.run do |config|
  config.ssh.username = &quot;vagrant&quot;
end
EOT

  # package up the guest
  vagrant package --base ${VIRTUALBOXNAME} --vagrantfile ${PACKAGEDNAME}.vf
  if [[ ! -d boxes ]]; then
    mkdir boxes
    fi
  if [[ -e boxes/${PACKAGEDNAME}.box ]]; then
    rm boxes/${PACKAGEDNAME}.box
    fi
  mv package.box boxes/${PACKAGEDNAME}.box
  rm ${PACKAGEDNAME}.vf

  fi
&lt;/pre&gt;&lt;/blockquote&gt; 
    </content:encoded>

    <pubDate>Fri, 28 Oct 2016 16:08:41 +0000</pubDate>
    <guid isPermaLink="false">https://blog.raymond.burkholder.net/index.php?/archives/695-guid.html</guid>
    
</item>

</channel>
</rss>
