Bridging Ethernet Virtualbox di Ubuntu

Virtualbox dapat berkomunikasi dengan host-nya dalam beberapa cara. Salah satunya dalah bridging (pemakaian bersama) satu ethernet untuk host dan guest (sistem operasi dalam Virtualbox).
Caranya sedikit rumit, tapi saya mencoba membuat skrip, yang walau jauh dari sempurna, untuk mengotomatisasi perintah-perintahnya.

1. Menginstalasi paket yang diperlukan
sudo apt-get install uml-utilities bridge-utils


2. Membuat file /etc/udev/rules.d/10-local.rules
sudo touch /etc/udev/rules.d/10-local.rules


3. Mengisi file /etc/udev/rules.d/10-local.rules dengan beberapa perintah

sudo gedit /etc/udev/rules.d/10-local.rules

Baris yang ditambahkan adalah:
KERNEL=="tun", NAME="net/%k", GROUP="vboxusers", MODE="0660"

4. Menambahkan user yang diinginkan ke dalam group vboxusers
sudo /usr/sbin/usermod -G vboxusers -a {nama_user}


5. Mengkopikan skrip berikut sebagai bridgeeth-up.sh
#!/bin/bash

# bridge ethernet up for VirtualBox (Sun) under Ubuntu 8.10
# Anton Rahmhadi @24 Jan 2009
# version 0.1a GPL
# note:
# 1. please do sudo apt-get install uml-utilities bridge-utils
# prior to start this configuration

USERNAME="{nama_user_disini}"
IPADDR="192.168.100.50"
MASK="255.255.255.0"

#--- lines below are not configurable
function checkrunninguser {
IAM=`whoami`
if [ "$IAM" != root ]; then
clear
echo "This program should be run as root"
exit 1
else
echo "1. Run as root: [OK]"
fi
}
function checkusergroups {
RETVAL=`groups $USERNAME | grep -c vboxusers`
if [ $RETVAL != 1 ]; then
echo "No group vboxusers on user $USERNAME"
echo "run: /usr/sbin/usermod -G vboxusers -a $USERNAME"
exit 1
else
echo "2. vboxusers is a group for $USERNAME: [OK]"
fi
}
function checkudevrulesd {
RULESCMD="KERNEL==\"tun\", NAME=\"net/%k\", GROUP=\"vboxusers\", MODE=\"0660\""
RULESFILE="/etc/udev/rules.d/10-local.rules"
RETVAL=`ls $RULESFILE`
if [ $RETVAL != $RULESFILE ]; then
echo "No file: $RULESFILE"
exit 1
else
echo "3. a udev rule-file is exist: [OK]"
fi
RETVAL=`cat $RULESFILE | grep KERNEL | grep NAME | grep GROUP | grep -c MODE`
if [ $RETVAL != 1 ]; then
echo "Please insert this line:"
echo "---------------"
echo "$RULESCMD"
echo "---------------"
echo "in file: $RULESFILE"
exit 1
else
echo "4. a udev rule-line is exist: [OK]"
fi
}
function runallcmd {
brctl addbr br0
ifconfig eth0 0.0.0.0
brctl addif br0 eth0
ifconfig br0 $IPADDR netmask $MASK up
VBoxTunctl -b -u $USERNAME
ifconfig tap0 up
brctl addif br0 tap0
iptables -I INPUT -i br0 -j ACCEPT
}

# running
clear
checkrunninguser
checkusergroups
checkudevrulesd
runallcmd
6. Gantilah {nama_user} pada skrip bridgeeth-up.sh sesuai dengan user yang diinginkan

7. Mengkopikan skrip berikut sebagai bridgeeth-down.sh
#!/bin/bash

# bridge ethernet down for VirtualBox (Sun) under Ubuntu 8.10
# Anton Rahmhadi @24 Jan 2009
# version 0.1a GPL
# note:
# 1. please do sudo apt-get install uml-utilities bridge-utils
# prior to start this configuration

#--- lines below are not configurable
function checkrunninguser {
IAM=`whoami`
if [ "$IAM" != root ]; then
clear
echo "This program should be run as root"
exit 1
else
echo "1. Run as root: [OK]"
fi
}
function runnallcmd {
iptables -D INPUT -i br0 -j ACCEPT
ifconfig tap0 down
VBoxTunctl -d tap0
ifconfig br0 down
brctl delbr br0
dhclient eth0
}

# running
clear
checkrunninguser
runnallcmd
8. Rubah permisi file
chmod 755 bridgeeth-up.sh bridgeeth-down.sh

9. Apabila ingin menjalankan virtualbox, maka jalankan skrip bridgeeth-up.sh
root@sydrake:/home/{nama_user}# ./bridgeeth-up.sh

1. Run as root: [OK]
2. vboxusers is a group for {nama_user}: [OK]
3. a udev rule-file is exist: [OK]
4. a udev rule-line is exist: [OK]
tap0
root@sydrake:/home/{nama_user}#


10. Sesuaikan setting network dari Virtualbox



11. Setelah mematikan virtualbox, maka jangan lupa jalankan skrip bridgeeth-down.sh
root@sydrake:/home/{nama_user}# ./bridgeeth-down.sh
1. Run as root: [OK]
Set 'tap0' nonpersistent
Internet Systems Consortium DHCP Client V3.1.1
Copyright 2004-2008 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

wmaster0: unknown hardware address type 801
wmaster0: unknown hardware address type 801
Listening on LPF/eth0/00:a0:d1:4e:de:49
Sending on LPF/eth0/00:a0:d1:4e:de:49
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 4
root@sydrake:/home/{nama_user}#


Bacaan:
a. Bridge Networking with Virtualbox on Linux hosts

Comments

Popular posts from this blog

Merakit Destilator Bioetanol Sederhana (1)

DHT11 incubator with I2C LCD (part 1)

LM35 Incubator with LCD 16x2 on Arduino