Skip to content

Instantly share code, notes, and snippets.

@groundwater
Last active December 19, 2015 06:59
Show Gist options
  • Save groundwater/5915312 to your computer and use it in GitHub Desktop.
Save groundwater/5915312 to your computer and use it in GitHub Desktop.

Notes

  • virtualbox additions need to be installed in the global zone, then mount privileges need to be given to the guest zone using fs_allowd. The guest zone then mounts the virtualbox shared directory.
  • do not give the VM direct access to the virtualbox network, create an internal network to SmartOS and use ipnat to forward traffic internally.
  • once ipnat is forwarding traffic, forwarded ports from virtualbox will be redirected to the guest zone always
  • unpack the virtualbox guest additions for Solaris tools with pkgtrans VBoxSolarisAdditions.pkg ~/tmp
## Configurable
# VM id
# see: imgadm avail
IMAGE_ID=1fc068b0-13b0-11e2-9f4e-2f3f6a96d9bc
# These must be in the same /24 subnet
GLOBAL_IP=10.0.0.1
GUESTZ_IP=10.0.0.2
# This assumes you chose `dhcp` in the original smartos install
# otherwise e1000g0/ might be names something else
# This is the address object of your main NIC in the global zone
# see: ipadm show-addr
ADDR_OBJ=e1000g0/
## Setup Internal Network
# create an internal network and provide NAT and port forwarding to it
dladm create-etherstub stub0
# Attach global zone to internal network
dladm create-vnic -l stub0 vnic0
ipadm create-if vnic0
ipadm create-addr -T static -a $GLOBAL_IP/24 vnic0/static
# Download Image
imgadm import $IMAGE_ID
# Create Zone from Image
vmadm create <<EOF
{
"brand": "joyent",
"alias": "vagrant",
"image_uuid": "$IMAGE_ID",
"autoboot": true,
"resolvers": ["8.8.8.8","8.8.4.4"],
"fs_allowed": "vboxfs",
"nics": [
{
"nic_tag": "stub0",
"ip": "$GUESTZ_IP",
"netmask": "255.255.255.0",
"gateway": "$GLOBAL_IP"
}
]
}
EOF
IP_ADDRESS=$(ipadm show-addr $ADDR_OBJ -p -o ADDR | sed 's/\/.*//')
# Setup NAT and Port Forwarding
cat <<EOH > /etc/ipf/ipnat.conf
map e1000g0 0/0 -> $IP_ADDRESS/32
rdr e1000g0 0/0 -> $GUESTZ_IP
EOH
# Reload ipnat
ipnat -FCf /etc/ipf/ipnat.conf
ipnat -l
# Turn on services
svcadm enable ipfilter
routeadm -u -e ipv4-forwarding
#!/bin/bash
TEMP_DIR=/tmp
MOUNT_DIR=/mnt
PKG_URL="http://download.virtualbox.org/virtualbox/4.2.8/VBoxGuestAdditions_4.2.8.iso"
echo "Installing Virtualbox Guest Additions"
# Setup
ISO=$TEMP_DIR/vbox-additions.iso
if [[ ! -f $ISO ]]; then
echo "Downloading ISO"
curl -Lso $ISO "$PKG_URL"
else
echo "Using Existing ISO $ISO"
fi
# Test Download
if [[ ! -f $ISO ]]; then
echo "Failed to Download ISO"
exit -1
fi
VMNT=$MOUNT_DIR/vbox
mkdir -p $VMNT
NUM=$(lofiadm -a $ISO || lofiadm -f $ISO)
mount -o ro -F hsfs $NUM $VMNT
# Test Mount
mounted=$(cat /etc/mnttab | grep $MOUNT_DIR/vbox | wc -l)
if [[ $mounted -ne 1 ]]; then
echo "Failed to Mount ISO"
exit -2
fi
GA=$TEMP_DIR/GuestAdditions
PKG=$VMNT/VBoxSolarisAdditions.pkg
# Extract package
mkdir -p $GA
pkgtrans -o $PKG $GA all
# Test package
if [[ ! -f $PKG ]]; then
echo "Failed to Find Solaris Guest Additions Package"
exit -3
fi
# Copy in kernel modules
REL=$GA/SUNWvboxguest/reloc
cp $REL/opt/VirtualBoxAdditions/amd64/vboxfs /kernel/fs/amd64/
cp $REL/usr/kernel/drv/amd64/vboxguest /kernel/drv/amd64/
cp $REL/usr/kernel/drv/vboxguest.conf /kernel/drv/
# Enable kernel modules
add_drv -m '* 0666 root sys' -i 'pci80ee,cafe' vboxguest
devfsadm -i vboxguest
ln -fns /devices/pci@0,0/pci80ee,cafe@4:vboxguest /dev/vboxguest
modload /kernel/fs/amd64/vboxfs
# Add Mount Type vboxfs
VBOXFS=/lib/fs/vboxfs
mkdir -p $VBOXFS
cp $GA/SUNWvboxguest/reloc/opt/VirtualBoxAdditions/amd64/vboxfsmount $VBOXFS/mount
echo "Installation Done"
echo 'Usage: /lib/fs/vboxfs/mount vagrant /vagrant'
# Create vagrant user in zone
useradd -b /home -c "Vagrant User" -g admin -m -s /bin/bash vagrant
# Unlock account for login
passwd -u vagrant
# Setup SSH Key Login
mkdir -p /home/vagrant/.ssh
cat <<EOH > /home/vagrant/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
EOH
chown -R vagrant /home/vagrant
# Add to sudoers
cat <<EOH >> /opt/local/etc/sudoers
%admin ALL=(ALL) NOPASSWD: ALL
EOH
# Mount Shared Folder
mkdir -p /vagrant
/lib/fs/vboxfs/mount vagrant /vagrant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment