Virtualbox automation scripts

As I’m trying to speed up things while I’m creating my ORACLE environments I started to build up some scripts to help me on doing it faster.
1. Adapt the network and the hosts file
Note: I’m running this script from my local server and not from VM, like this:

# root>ssh root@192.168.1.130 < $script_location/adapt_network.sh
================================== START =========================================
#!/bin/bash

################################################
# Adapting the /etc/sysconfig/network-scripts 

################################################

# Regenerate the IP rules
#mv /etc/udev/rules.d/70-persistent-net.rules ~/70-persistent-net.rules.bck
#/sbin/udevadm trigger --type=devices --action=add

### adapt the IPADDR 
cd /etc/sysconfig/network-scripts
grep -q '^IPADDR' ifcfg-eth1 && sed -i 's/^IPADDR.*/'IPADDR=192.168.100.10'/' ifcfg-eth1 || echo 'IPADDR=192.168.100.10' >> ifcfg-eth1
grep -q '^IPADDR' ifcfg-eth2 && sed -i 's/^IPADDR.*/'IPADDR=192.168.101.11'/' ifcfg-eth2 || echo 'IPADDR=192.168.101.10' >> ifcfg-eth2

### adapt the Name
grep -q '^NAME' ifcfg-eth1 && sed -i 's/^NAME.*/NAME="eth1"/' ifcfg-eth1 || echo 'NAME="eth1"' >> ifcfg-eth1
grep -q '^NAME' ifcfg-eth2 && sed -i 's/^NAME.*/NAME="eth2"/' ifcfg-eth2 || echo 'NAME="eth2"' >> ifcfg-eth2

### adapt the BOOTPROTO and remove the UUID
for f in ifcfg-eth{1..2}; do
grep -q '^BOOTPROTO' $f && sed -i 's/^BOOTPROTO.*/BOOTPROTO=none/' $f || echo 'BOOTPROTO=none' >> $f
sed -i '/UUID/d' $f
done

==================================== END =========================================

Note: I’m still searching for a way to use variables instead the “fixed” IP addresses; didn’t find a way yet.
Looking forwards for your comments.

How to change the network device name from eth5 to eth0

As I’m using a lot Virtualbox, I have often encountered the situation where I had to adapt the network adapters to follow some naming standards (to match my scripts 🙂 of course).

I’ve created some time ago a BASE VM where I deployed all the packages that I would possibly need for any ORACLE installation. On this BASE VM I have created 3 network adapters (1 bridged one and 2 host-only ones). Each time I need to create a VM, I’m just cloning the BASE VM – it saves A LOT of time. However, I still need to apply some changes, such as the hostname and network adapters.

So, here is what I did:

After the new VM is up&running, grab the MAC addresses first:

[root@base rules.d]# ifconfig | grep HWadd
eth3      Link encap:Ethernet  HWaddr 08:00:27:50:9C:9D
eth4      Link encap:Ethernet  HWaddr 08:00:27:B6:B4:99
eth5      Link encap:Ethernet  HWaddr 08:00:27:B1:8F:0A
[root@base rules.d]#

Go to /etc/udev/rules.d/ and edit 70-persistent-net.rules

This is what I have:

[root@base rules.d]# cat 70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:22:74:f9", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:b1:b2:5c", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:89:47:fd", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:50:9c:9d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth3"
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:b6:b4:99", ATTR{type}=="1", KERNEL=="eth*", NAME="eth4"
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:b1:8f:0a", ATTR{type}=="1", KERNEL=="eth*", NAME="eth5"
[root@base rules.d]#

As you can see, the new names are eth3 – eth4 – eth5. I’m going to delete the previous ones and rename the new ones as eth0 – eth1 – eth2. My file is going to look like this:

[root@base rules.d]# cat 70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:50:9c:9d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:b6:b4:99", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:b1:8f:0a", ATTR{type}=="1", KERNEL=="eth*", NAME="eth2"
[root@base rules.d]#

The second step is to go here “/etc/sysconfig/network-scripts” and adapt the ifcfg-eth* files accordingly.

[root@base network-scripts]# ll ifcfg-eth*
-rw-r--r--. 1 root root 231 Jun 18  2014 ifcfg-eth0
-rw-r--r--. 1 root root 231 Jun 18  2014 ifcfg-eth1
-rw-r--r--. 1 root root 231 Jun 18  2014 ifcfg-eth2
[root@base network-scripts]#

Delete in each file the UUID line, replace the MAC address with the one u get with “ifconfig | grep HW” and adapt the Name with a proper one.

The last step is to REBOOT the server and check the network adapters.