Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: c314c93475
Fetching contributors…

Cannot retrieve contributors at this time

executable file 250 lines (203 sloc) 6.193 kB
#!/bin/bash
# full automated cloudpipe image creation
# environment variables
LOG_FILE=${LOG_FILE:-ubuntukickstart.log}
DIR=`pwd`
HTTP_ROOT=${HTTP_ROOT:-/var/www}
CLOUDPIPECONF_PATH=${CLOUDPIPECONF_PATH:-cloudpipeconf.sh}
UBUNTU_MIRROR_URL=${UBUNTU_MIRROR_URL:-http://archive.ubuntu.com/ubuntu}
NETBOOT_IMG=${NETBOOT_IMG:-http://archive.ubuntu.com/ubuntu/dists/precise/main/installer-i386/current/images/netboot/netboot.tar.gz}
NETWORK_NAME=${NETWORK_NAME:-ubuntu-pxe}
REAL_IFACE=${REAL_IFACE:-eth0}
BRIDGE_IFACE=${BRIDGE_IFACE:-vbr27}
BRIDGE_IP=${BRIDGE_IP:-192.168.119.1}
BRIDGE_NETMASK=${BRIDGE_NETMASK:-255.255.255.0}
DHCP_START=${DHCP_START:-192.168.119.2}
DHCP_END=${DHCP_END:-192.168.119.254}
TFTPBOOT_DIR=${TFTPBOOT_DIR:-/tftpboot}
BOOTP_FILE=${BOOTP_FILE:-pxelinux.0}
ROOT_PASSWD_FILE=${ROOT_PASSWD_FILE:-rootpasswd.txt}
VM_MAC=${VM_MAC:-00:16:3e:77:e2:ed}
VM_IP=${VM_IP:-192.168.119.10}
VM_NAME=${VM_NAME:-NewMach}
VM_RAM=${VM_RAM:-384}
VM_DISK_SIZE_GB=${VM_DISK_SIZE_GB:-1}
VM_DISK_PATH=${VM_DISK_PATH:-new_cloudpipe_image.img}
KICKSTART_HTTP_PATH=${KICKSTART_HTTP_PATH:-http://$BRIDGE_IP/ks.cfg}
PACKAGES="virtinst libvirt-bin kvm-pxe virt-viewer apache2 expect makepasswd qemu-kvm"
#functions
function wait_and_ping_it {
while :
do
echo -n "."
status=`LANG=C virsh domstate $VM_NAME`
if [ "$status" = "shut off" ];
then
MESSAGE="shutdowing the VM .."
log "$MESSAGE"
echo "$MESSAGE"
sleep 1
break
fi
ping -c 1 -W 3 $VM_IP 2>/dev/null 1>/dev/null
if [ $? = "0" ];
then
expect ssh.sh $VM_IP root $ROOT_PASSWORD 2>/dev/null 1>/dev/null
if [ $? = "0" ];
then
MESSAGE="shutdowing the VM .."
log "$MESSAGE"
echo "$MESSAGE"
sleep 15
break
fi
fi
sleep $1
done
}
function log {
echo "log: `date +%X` : $1" >> $LOG_FILE
}
function run {
#run command
log "$MESSAGE"
echo "$MESSAGE"
$1
if [ $? != "0" ];
then
log "$ERROR_MESSAGE"
echo "$ERROR_MESSAGE"
exit 1
fi
}
# run
cat > $LOG_FILE <<FILE_EOF
FILE_EOF
MESSAGE="running apt-get update ..."
ERROR_MESSAGE="apt-get update failed"
run "apt-get update"
MESSAGE="installing packages ..."
ERROR_MESSAGE="installation failed"
run "apt-get -y install $PACKAGES"
#configure new virtual network
FILE=$NETWORK_NAME.xml
cat > $FILE << FILE_EOF
<network>
<name>$NETWORK_NAME</name>
<forward mode='nat' dev='$REAL_IFACE'/>
<bridge name='$BRIDGE_IFACE' stp='on' delay='0' />
<ip address='$BRIDGE_IP' netmask='$BRIDGE_NETMASK'>
<tftp root='$TFTPBOOT_DIR' />
<dhcp>
<range start='$DHCP_START' end='$DHCP_END' />
<host mac='$VM_MAC' ip='$VM_IP' />
<bootp file='$BOOTP_FILE' />
</dhcp>
</ip>
</network>
FILE_EOF
#create this network
MESSAGE="creating virtual network ..."
ERROR_MESSAGE="virtual network creation failed"
run "virsh net-create $NETWORK_NAME.xml"
#configure tftp
MESSAGE="making directory for tftp ..."
ERROR_MESSAGE="tftp directory making failed"
run "mkdir -p $TFTPBOOT_DIR"
rm -f `basename $NETBOOT_IMG`
MESSAGE="downloading netboot image ..."
ERROR_MESSAGE="netboot image downloading failed"
run "wget $NETBOOT_IMG"
MESSAGE="extracting files from netboot image ..."
ERROR_MESSAGE="netboot image files extracting failed"
run "tar xf `basename $NETBOOT_IMG` --directory $TFTPBOOT_DIR"
FILE=$TFTPBOOT_DIR/pxelinux.cfg/default
cat > $FILE << FILE_EOF
include ubuntu-installer/i386/boot-screens/txt.cfg
default ubuntu-installer/i386/boot-screens/vesamenu.c32
FILE_EOF
FILE=$TFTPBOOT_DIR/ubuntu-installer/i386/boot-screens/txt.cfg
cat > $FILE << FILE_EOF
timeout 1
default install
label install
menu label ^Install
menu default
kernel ubuntu-installer/i386/linux
append ks=$KICKSTART_HTTP_PATH vga=788 initrd=ubuntu-installer/i386/initrd.gz -- quiet
FILE_EOF
ROOT_PASSWORD=${ROOT_PASSWORD:-`makepasswd --chars=12`}
echo $ROOT_PASSWORD > $ROOT_PASSWD_FILE
FILE=$HTTP_ROOT/ks.cfg
cat > $FILE << FILE_EOF
#Generated by Kickstart Configurator
#platform=x86
#System language
lang en_US
#Language modules to install
langsupport en_US
#System keyboard
keyboard us
#System mouse
mouse
#System timezone
timezone --utc America/New_York
#Root password
rootpw --iscrypted $( echo $ROOT_PASSWORD | makepasswd --clearfrom=- --crypt-md5 | awk '{print $2}' )
#Initial user
user --disabled
#Reboot after installation
reboot
#Use text mode install
text
#Install OS instead of upgrade
install
#Use Web installation
url --url $UBUNTU_MIRROR_URL
#System bootloader configuration
bootloader --location=mbr
#Clear the Master Boot Record
zerombr yes
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
preseed --owner partman-basicfilesystems partman-basicfilesystems/no_swap boolean false
part / --fstype ext4 --size 700 --grow --maxsize 10000 --asprimary
#System authorization infomation
auth --useshadow --enablemd5
#Network information
network --bootproto=dhcp --device=eth0
#Firewall configuration
firewall --disabled
#Do not configure the X Window System
skipx
%packages
openssh-server
%post --interpreter=/bin/bash
mkdir /tmp/cloud
cd /tmp/cloud
wget $BRIDGE_IP/cloudpipeconf.sh
chmod +x cloudpipeconf.sh
./cloudpipeconf.sh
cd /
rm -fr /tmp/cloud/
#end
FILE_EOF
cd $DIR
MESSAGE="copying cloudpipeconf.sh to $HTTP_ROOT"
ERROR_MESSAGE="cloudpipeconf.sh copying failed"
run "cp $CLOUDPIPECONF_PATH $HTTP_ROOT"
MESSAGE="starting new VM"
ERROR_MESSAGE="new VM starting failed"
run "virt-install --pxe --name=$VM_NAME --ram=$VM_RAM --disk path=$VM_DISK_PATH,size=$VM_DISK_SIZE_GB --network bridge=$BRIDGE_IFACE,mac=$VM_MAC --noautoconsole"
#wait for about 1-2 hour(s) while installation is not finished
echo -n "waiting for an end of installation: "
wait_and_ping_it 15
#undefine machine
MESSAGE="undefining VM"
ERROR_MESSAGE="undefining VM failed"
run "virsh undefine $VM_NAME"
MESSAGE="destroying virtual net"
ERROR_MESSAGE="destroying virtual net failed"
run "virsh net-destroy $NETWORK_NAME"
#end
Jump to Line
Something went wrong with that request. Please try again.