others/vm-create.bash
author Paul Tonelli <paul.tonelli@logilab.fr>
Tue, 12 Mar 2013 11:51:26 +0100
changeset 66 23a31e4cbb65
parent 1 8b17affcf529
permissions -rwxr-xr-x
the scripts now cleans the ssh keys if present as well as the apt cache

#!/bin/bash

set -e

#install necessary software to work
sudo aptitude install glance mercurial

# Basic arg check
archive=$1
[ ! -f "$archive" ] && echo -e "[USAGE]\n$0 EXISTING_IMAGE.tar.gz" && exit 1

#create directory to work in
[ ! -d mnt ] && mkdir mnt

#extract image only to mnt
tar xzf $archive --wildcards -C mnt "*.img"
#check
cd mnt
image=`ls *.img`

# increase the image size of 4G (append 0s)
echo "adding space to image to reach 4G"
dd if=/dev/zero bs=4096k count=1024 >> $image
# check
e2fsck -n -f $image
[ $? != 0 ] && echo "Incorrect filesystem on image $image" && exit 1
# resize the image filesystem to the size of the file
resize2fs -f $image
[ $? != 0 ] && echo "Could not resize filesystem in $image" && exit 1
ls -l $image

# mount as loopback
imagedir=`echo $image | sed 's:\.img$::'`
mkdir $imagedir
sudo mount -o loop $image $imagedir
CHROOT="sudo chroot $imagedir"
$CHROOT df -h .

# Prepare actual install work

$CHROOT mount -t proc none /proc
$CHROOT mount -t devpts none /dev/pts
echo "Acquire::http::Proxy \"http://proxy.logilab.priv:3142/\";" > 01proxy

sudo cp 01proxy $imagedir/etc/apt/apt.conf.d/ && \rm 01proxy
$CHROOT mv /etc/resolv.conf /etc/resolv.conf.orig
sudo cp /etc/resolv.conf $imagedir/etc/resolv.conf

#no questions 
DEBIAN_FRONTEND=noninteractive

#forbid apt to add scripts to boot (will need to add salt minion to boot)
#doc in /usr/share/doc/sysv-rc/README.policy-rc.d.gz
echo "exit 101" > policy-rc.d
chmod +x policy-rc.d
sudo cp policy-rc.d $imagedir/usr/sbin && rm policy-rc.d


#special treatment for whoopsie which fails to update
#it is a crash report util from ubuntu
exist_whoopsie=`$CHROOT dpkg -l |grep " whoopsie "|wc -l`
[ $exist_whoopsie -ge 1 ] && echo "I need to delete whoopsie"
[ $exist_whoopsie -ge 1 ] && $CHROOT apt-get remove -y whoopsie

#upgrade machine
$CHROOT apt-get update &&
sudo -E chroot $imagedir apt-get dist-upgrade -y

# =========================
# Install work is done HERE
# =========================

#install salt-minion
$CHROOT apt-get install -y salt-minion

#patch salt minion config
echo "making salt minion work locally"
patch $imagedir/etc/salt/minion `find /mnt -name minion.patch`
[ $? != 0 ] && echo "error while patching minion config file, is the minion.patch file available ?" && exit 1

#install using salt call, --local require salt-minion >=12.0 and is not necessary if previous patch applied
$CHROOT salt-call --local pkg.install -l warning pkgs='["mercurial"]'

#you should modify the files in imagedir to change the configuration of the client
cp -r ../srv $imagedir/etc/salt/
[ $? != 0 ] && echo "error while moving the minion config folder  ?" && exit 1
$CHROOT salt-call state.highstate


# ==========================================
# getting files for uploading / ami creation
# ==========================================

kernel_filepath=`find $imagedir/boot -name "*vmlinuz*"`
kernel_filename=`basename $kernel_filename`
initrd_filepath=`find $imagedir/boot -name "*initrd*"`
initrd_filename=`basename $initrd_filename`

cp $kernel_filepath $kernel_filename
cp $kernel_filepath $initrd_filename

# =========================
# End of install work
# =========================

# Cleanup
$CHROOT rm /usr/sbin/policy-rc.d
$CHROOT service rsyslog stop
$CHROOT umount /proc
$CHROOT umount /dev/pts
sudo \rm $imagedir/etc/apt/apt.conf.d/01proxy
$CHROOT mv -f /etc/resolv.conf.orig /etc/resolv.conf

sudo umount $imagedir

echo "WARNING: image $image was left in the tmpfs mounted directory mnt"

# =================================
# unmounting image and creating ami
# =================================

echo "unmounting chroot"
sudo umount $imagedir/proc
sudo umount $imagedir/dev/pts
sudo umount $imagedir

# check filesystem and shrink to minimal size
sudo e2fsck -f $image
sudo resize2fs -M -f $image 

#glance upload initrd, kernel and get ids
echo "sending kernel"
output=`glance add is_public=false disk_format=aki container_format=aki\
    name="$imagedir-kernel" < $kernel_filename`
kernel_id=`echo $output|awk -F':' '{print $2}'|sed 's/ //g'`

echo "sending initrd"
output=`glance add is_public=false disk_format=ari container_format=ari\
    name="$imagedir-initrd" < $initrd_filename`
initrd_id=`echo $output|awk -F':' '{print $2}'|sed 's/ //g'`

echo "sending image"
#glance upload corresponding image
glance add disk_format=ami container_format=ami\
    name="debian-squeeze-uec-x86_64"\
    kernel_id=$kernel_id\
    ramdisk_id=$initrd_id\
    < $image