Skip to content

Commit 0b08759

Browse files
committed
Add VM scripts
1 parent f15da53 commit 0b08759

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

util/vm/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.log
2+
.vagrant

util/vm/Vagrantfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
REQUIRED_PLUGINS = %w( vagrant-vbguest vagrant-reload vagrant-disksize )
2+
3+
Vagrant.configure(2) do |config|
4+
5+
# Install plugins if missing...
6+
_retry = false
7+
REQUIRED_PLUGINS.each do |plugin|
8+
unless Vagrant.has_plugin? plugin
9+
system "vagrant plugin install #{plugin}"
10+
_retry = true
11+
end
12+
end
13+
14+
if (_retry)
15+
exec "vagrant " + ARGV.join(' ')
16+
end
17+
18+
# Common config.
19+
config.vm.box = "lasp/ubuntu16.04-desktop"
20+
config.vbguest.auto_update = true
21+
config.disksize.size = '50GB'
22+
config.vm.synced_folder ".", "/vagrant", disabled: false, type: "virtualbox"
23+
config.vm.network "private_network", :type => 'dhcp', :adapter => 2
24+
25+
config.vm.define "default" do |d|
26+
d.vm.hostname = "tutorial-vm"
27+
d.vm.provider "virtualbox" do |vb|
28+
vb.name = "ONF NG-SDN Tutorial " + Time.now.strftime("(%Y-%m-%d)")
29+
vb.gui = true
30+
vb.cpus = 8
31+
vb.memory = 8192
32+
vb.customize ['modifyvm', :id, '--clipboard', 'bidirectional']
33+
vb.customize ["modifyvm", :id, "--accelerate3d", "on"]
34+
vb.customize ["modifyvm", :id, "--graphicscontroller", "vboxvga"]
35+
vb.customize ["modifyvm", :id, "--vram", "128"]
36+
end
37+
d.vm.provision "shell", path: "root-bootstrap.sh"
38+
d.vm.provision "shell", inline: "su sdn '/vagrant/user-bootstrap.sh'"
39+
end
40+
end

util/vm/cleanup.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
sudo apt-get clean
5+
sudo apt-get -y autoremove
6+
7+
sudo rm -rf /tmp/*
8+
9+
history -c
10+
rm -f ~/.bash_history
11+
12+
# Zerofill virtual hd to save space when exporting
13+
time sudo dd if=/dev/zero of=/tmp/zero bs=1M || true
14+
sync ; sleep 1 ; sync ; sudo rm -f /tmp/zero
15+
16+
# Delete vagrant user
17+
sudo userdel -r -f vagrant

util/vm/export.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
set -xe
4+
5+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
7+
function wait_vm_shutdown {
8+
set +x
9+
while vboxmanage showvminfo $1 | grep -c "running (since"; do
10+
echo "Waiting for VM to shutdown..."
11+
sleep 1
12+
done
13+
sleep 2
14+
set -x
15+
}
16+
17+
# Provision
18+
vagrant up
19+
20+
VB_UUID=$(cat .vagrant/machines/default/virtualbox/id)
21+
VBoxManage snapshot ${VB_UUID} take "pre-cleanup"
22+
23+
# Cleanup
24+
vagrant ssh -c 'bash /vagrant/cleanup.sh'
25+
sleep 5
26+
vboxmanage controlvm "${VB_UUID}" acpipowerbutton
27+
wait_vm_shutdown "${VB_UUID}"
28+
# Remove vagrant shared folder
29+
vboxmanage sharedfolder remove ${VB_UUID} -name "vagrant"
30+
31+
# Export
32+
rm -f ngsdn-tutorial.ova
33+
vboxmanage export "${VB_UUID}" -o ngsdn-tutorial.ova

util/vm/root-bootstrap.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
3+
set -xe
4+
5+
# Create user sdn
6+
useradd -m -d /home/sdn -s /bin/bash sdn
7+
usermod -aG sudo sdn
8+
usermod -aG vboxsf sdn
9+
echo "sdn:rocks" | chpasswd
10+
echo "sdn ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/99_sdn
11+
chmod 440 /etc/sudoers.d/99_sdn
12+
update-locale LC_ALL="en_US.UTF-8"
13+
14+
apt-get update
15+
16+
apt-get install -y --no-install-recommends apt-transport-https ca-certificates
17+
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
18+
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
19+
apt-get update
20+
21+
# Required packages
22+
DEBIAN_FRONTEND=noninteractive apt-get -y --no-install-recommends install \
23+
avahi-daemon \
24+
git \
25+
bash-completion \
26+
htop \
27+
python \
28+
zip unzip \
29+
make \
30+
wget \
31+
curl \
32+
vim nano emacs \
33+
docker-ce
34+
35+
# Enable Docker at startup
36+
systemctl start docker
37+
systemctl enable docker
38+
# Add sdn user to docker group
39+
usermod -a -G docker sdn
40+
41+
# Install pip
42+
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
43+
python get-pip.py --force-reinstall
44+
rm -f get-pip.py
45+
46+
# Bash autocompletion
47+
echo "source /etc/profile.d/bash_completion.sh" >> ~/.bashrc
48+
49+
# Fix SSH server config
50+
tee -a /etc/ssh/sshd_config <<EOF
51+
52+
UseDNS no
53+
EOF
54+
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
55+
56+
# IntelliJ
57+
snap install intellij-idea-community --classic

util/vm/user-bootstrap.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -xe
3+
4+
cd /home/sdn
5+
6+
cp /etc/skel/.bashrc ~/
7+
cp /etc/skel/.profile ~/
8+
cp /etc/skel/.bash_logout ~/
9+
10+
# With Ubuntu 18.04 sometimes .cache is owned by root...
11+
mkdir -p ~/.cache
12+
sudo chown -hR sdn:sdn ~/.cache
13+
14+
git clone https://github.com/opennetworkinglab/ngsdn-tutorial.git
15+
cd ngsdn-tutorial
16+
make pull-deps

0 commit comments

Comments
 (0)