[ Level 3 ] The script for creating virtualbox envrionment (Ubuntu 12.04) with lauch new OS image.
In my company, I need to test it when I build a new image.
Therefore I have to setup my testing environment for integration test.
I wrote a script to automatically setup the environment from new install Ubuntu.
The steps are:
1. Download and setup tftp server.
2. Download and setup dhcp server with netboot option.
3. Install virtualbox.
After that you could create a VM then choose netboot. (the first interface must connect to tap0 to get boot server information)
Wish this helps.
regards,
Stanley Huang
Therefore I have to setup my testing environment for integration test.
I wrote a script to automatically setup the environment from new install Ubuntu.
The steps are:
1. Download and setup tftp server.
2. Download and setup dhcp server with netboot option.
3. Install virtualbox.
After that you could create a VM then choose netboot. (the first interface must connect to tap0 to get boot server information)
#!/bin/bash ######## utilities ######## _getTasks() { indent="$1" cat $0 | grep '()' | grep -v "^_" | grep "^[^ ]" | cut -d'(' -f1 | sed -e "s/^/$indent/" } _showUsage() { indent=" " cat <<EOF Usage: $0 task Ex. $0 `_getTasks | head -1` tasks: ${indent}[ all ] `_getTasks "$indent"` EOF } _die() { echo "$1" exit ${2:-1} } _checkID() { uid=`id | cut -d'(' -f1 | cut -d'=' -f2` [ $uid -ne 0 ] && _die "Need root to execute this command, exit!" } ######## utilities ######## ######## create tftpd configuration ######## TFTP_DIRECTORY=/tftpboot installTftpd() { apt-get -y remove tftpd ## conflict with tftpd-hpa apt-get -y install tftpd-hpa tftp } #installTftpd createTftpdConf() { echo "Run createTftpdConf()" cat > /etc/default/tftpd-hpa <<EOF TFTP_USERNAME="tftp" TFTP_DIRECTORY="$TFTP_DIRECTORY" TFTP_ADDRESS="0.0.0.0:69" TFTP_OPTIONS="-c --secure" EOF } #createTftpdConf ######## create tftpd configuration ######## ######## create /tftpboot content ######## installSyslinux() { apt-get -y install syslinux syslinux-common } testTftp() { test_filename="$1" cd /tmp rm -f ./$test_filename tftp localhost <<EOF get /$test_filename EOF [ ! ./$test_filename ] && echo "Get file ($test_filename) from tftp server fail..." && exit 1 rm -f ./$test_filename } createTftpbootContent() { echo "Run createTftpbootContent()" mkdir -p $TFTP_DIRECTORY cp /usr/lib/syslinux/pxelinux.0 $TFTP_DIRECTORY
cp /usr/lib/syslinux/*menu.c32 $TFTP_DIRECTORY mkdir -p $TFTP_DIRECTORY/pxelinux.cfg && chmod 777 $TFTP_DIRECTORY/pxelinux.cfg mkdir -p $TFTP_DIRECTORY/boot && chmod 777 $TFTP_DIRECTORY/boot cat > $TFTP_DIRECTORY/pxelinux.cfg/default <<EOF prompt 0 default default_install timeout 3 label default_install kernel boot/vmlinuz-2.6.32.24 append initrd=boot/default-installer.img EOF chown -R tftp:tftp $TFTP_DIRECTORY testTftp "pxelinux.0" } #createTftpbootContent ######## create /tftpboot content ######## ######## install user-mod linux package ######## installUserModPackages() { echo "Run installUserModPackages()" apt-get -y install uml-utilities } #installUserModPackages _getRealUserName() { tty_filter="`tty | sed -e 's|/dev/||'`" who | grep "$tty_filter" | awk '{print($1)}' } createTap() { echo "Run createTap()" user_name="`_getRealUserName`" subnet=10.10 netmask=255.255.255.0 TIMEOUT=5 interface_file="/etc/network/interfaces" interface_orig="$interface_file.orig" [ ! -f $interface_orig ] && cp $interface_file $interface_orig if [ -f $interface_orig ] then read -t $TIMEOUT -p "Do you want to use orig interfaces file ($interface_orig) to be the template? (Timeout for $TIMEOUT seconds)) [Y/n]: " Yn [ "$Yn" != "n" ] && [ "$Yn" != "N" ] && cp $interface_orig $interface_file fi for index in `seq 0 1` do cat >> $interface_file <<EOF auto tap$index iface tap$index inet static pre-up tunctl -t tap$index -u $user_name up ifconfig tap$index up down ifconfig tap$index down address $subnet.$index.254 netmask $netmask EOF done echo "New interfaces file:" echo "================================================" cat $interface_file echo "================================================" } #createTap ######## install user-mod linux package ######## ######## create dhcpd.conf ######## installDhcpd() { apt-get -y install isc-dhcp-server ## isc-dhcp-server replaced dhcp3-server } createDhcpdConf() { echo "Run createDhcpdConf()" subnet=10.10 ## for tap0/tap1 #subnet=192.168 ## for virtualbox vboxnet0 netmask=255.255.255.0 TIMEOUT=5 dhcpd_conf_file="/etc/dhcp/dhcpd.conf" dhcpd_conf_orig="$dhcpd_conf_file.orig" [ ! -f $dhcpd_conf_orig ] && cp $dhcpd_conf_file $dhcpd_conf_orig if [ -f $dhcpd_conf_orig ] then read -t $TIMEOUT -p "Do you want to use orig dhcpd file ($dhcpd_conf_file) to be the template? (Timeout for $TIMEOUT seconds)) [Y/n]: " Yn [ "$Yn" != "n" ] && [ "$Yn" != "N" ] && cp $dhcpd_conf_orig $dhcpd_conf_file fi for index in `seq 0 1` ## for tap0/tap1 #for index in `seq 56 56` ## for virtualbox vboxnet0 do cat >> $dhcpd_conf_file <<EOF subnet $subnet.$index.0 netmask $netmask { option subnet-mask $netmask; option broadcast-address $subnet.$index.255; option routers $subnet.$index.254; range $subnet.$index.210 $subnet.$index.220; filename "/pxelinux.0"; } EOF done echo "New dhcpd config file:" echo "================================================" cat $dhcpd_conf_file echo "================================================" } #createDhcpdConf checkDhcpService() { netstat -tlunp | grep dhcp || _die "Cannot find binding address of dhcp!" } #checkDhcpService ######## install virtualbox ######## installVBox() { echo "Run installVBox()" ## Url for virtualbox 4.2.12 to download vbox_binary_url_4_2_12_for_12_10="http://download.virtualbox.org/virtualbox/4.2.12/virtualbox-4.2_4.2.12-84980~Ubuntu~quantal_amd64.deb" ## Url for virtualbox extension pack 4.2.12 to download vbox_extpack_url_4_2_12="http://dlc.sun.com.edgesuite.net/virtualbox/4.2.12/Oracle_VM_VirtualBox_Extension_Pack-4.2.12.vbox-extpack " ## download virtualbox and install it vbox_binary_url=$vbox_binary_url_4_2_12_for_12_10 vbox_binary_filename=${vbox_binary_url##*/} wget $vbox_binary_url dpkg -i ./$vbox_binary_filename ## setup vbox driver apt-get -y install linux-headers-3.5.0-27 linux-headers-3.5.0-27-generic /etc/init.d/vboxdrv setup ## download virtualbox extension and install it vbox_extpack_url=$vbox_extpack_url_4_2_12 vbox_extpack_filename=${vbox_extpack_url##*/} wget $vbox_extpack_url vboxmanage extpack install ./$vbox_extpack_filename } #installVBox ######## install virtualbox ######## ######## main ######## _checkID ## Start install vm echo "Start install vm..." if [ -z "$1" ] then _showUsage elif [ "$1" == "all" ] then task_list="$@" else task_list="$1" fi for task in $task_list do eval $task done ## Install done echo "Install vm done! Please restart the machine~"
Wish this helps.
regards,
Stanley Huang
Comments
Post a Comment