#!/bin/bash set -e set -v set -x SAVESPACE=1 WITHCHEF=1 ORGNAME="eeqj" DSIZE="25G" # disk size # releases we support right now SUPPORTED="precise saucy" if [[ $# -ne 1 ]]; then echo "usage: $0 " > /dev/stderr echo "supported ubuntu releases: $SUPPORTED" > /dev/stderr exit 127 fi R="$1" # release if ! [[ "$SUPPORTED" =~ "$R" ]] ; then echo "$0: unsupported ubuntu release $R, sorry." > /dev/stderr exit 127 fi MR="/tmp/kvmbuild-${R}" RI="/tmp/kvmbuild-${R}.img" # raw image VGN="vmvg0" # volume group name DATE="$(date -u +%Y%m%d)" LONGDATE="$(date -u +%Y-%m-%dT%H:%M:%S%z)" LOOPDEV="$(losetup -f)" LDBASE="$(basename $LOOPDEV)" ROOTPW="root" if [[ -e /dev/$VGN ]]; then echo "$0: error, vg $VGN already exists" > /dev/stderr exit 127 fi if [[ -e "$MR" ]]; then echo "$0: error, chroot dir $MR already exists" > /dev/stderr exit 127 fi if [[ -e "$RI" ]]; then echo "$0: error, intermediate image file $RI already exists" > /dev/stderr exit 127 fi function detect_local_mirror () { TF="${UBUNTU_MIRROR_URL}/dists/${R}/Release" MOK="$(curl -m 1 --head ${TF} 2>&1 | grep '200 OK' | wc -l)" if [ $MOK -gt 0 ]; then echo "$UBUNTU_MIRROR_URL" else echo "http://archive.ubuntu.com/ubuntu/" fi } UM="$(detect_local_mirror)" # create sparse file and partition it dd if=/dev/zero of=$RI bs=1 count=0 seek=$DSIZE parted -s $RI mklabel msdos parted -a optimal $RI mkpart primary 0% 200MiB parted -a optimal $RI mkpart primary 200MiB 100% parted $RI set 1 boot on losetup $LOOPDEV $RI kpartx -av $LOOPDEV BOOTPARTLOOP="$(losetup -f)" losetup $BOOTPARTLOOP /dev/mapper/${LDBASE}p1 # make boot filesystem: if [[ "$R" == "saucy" ]]; then FSTYPE="ext4" else FSTYPE="ext3" fi mkfs.${FSTYPE} -L BOOT $BOOTPARTLOOP tune2fs -c -1 $BOOTPARTLOOP # create root vg and filesystem: pvcreate /dev/mapper/${LDBASE}p2 vgcreate $VGN /dev/mapper/${LDBASE}p2 lvcreate -l 100%FREE -n root $VGN mkfs.${FSTYPE} -L ROOT /dev/$VGN/root # mount stuff mkdir -p $MR MR="$(readlink -f $MR)" mount /dev/$VGN/root $MR mkdir $MR/boot mount $BOOTPARTLOOP $MR/boot # install base: echo "*** installing base $R system from $UM..." debootstrap --arch amd64 $R $MR $UM # temporary config for install: RPS="main restricted multiverse universe" echo "deb $UM $R $RPS" > $MR/etc/apt/sources.list for P in updates backports security ; do echo "deb $UM $R-$P $RPS" >> $MR/etc/apt/sources.list done # disable apt installation exuberance cat > $MR/etc/apt/apt.conf.d/99-vm-no-extras-please < $MR/etc/environment </dev/null' | chroot $MR BUUID="$(blkid -s UUID -o value $BOOTPARTLOOP)" RUUID="$(blkid -s UUID -o value /dev/${VGN}/root)" # this has to come before packages: cat > $MR/etc/fstab < $MR/etc/network/interfaces < $MR/etc/hosts < $MR/usr/sbin/policy-rc.d < /etc/ntp.conf.new mv /etc/ntp.conf.new /etc/ntp.conf EOF # tell ntp not to try to sync to anything # if an ntp server comes from the dhcp server then it will use that cat >> $MR/etc/ntp.conf <> $MR/etc/default/grub # FIXME i think this is bogus, test changing it echo "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=9600 --stop=1\"" \ >> $MR/etc/default/grub if [[ "$R" == "saucy" ]]; then echo "GRUB_TERMINAL=\"serial\"" >> $MR/etc/default/grub fi echo "GRUB_GFXPAYLOAD=\"text\"" >> $MR/etc/default/grub # set root password (only useful at console, ssh password login is disabled) chroot $MR /bin/bash -c "echo \"root:$ROOTPW\" | chpasswd" chroot $MR grub-mkconfig -o /boot/grub/grub.cfg 2> /dev/null cat > $MR/boot/grub/device.map < /dev/null # get rid of temporary device.map after grub is installed rm $MR/boot/grub/device.map # remove initramfs entirely: chroot $MR update-initramfs -d -k all # for some stupid reason, -k all doesn't work on gen after removing: KERN="$(cd $MR/boot && ls vmlinuz*)" VER="${KERN#vmlinuz-}" chroot $MR update-initramfs -c -k $VER # start a getty on the serial port for kvm console login if [[ "$R" == "saucy" ]]; then cat > $MR/etc/init/ttyS0.conf < $MR/etc/dhcp/dhclient-exit-hooks.d/hostname <> $MR/etc/ssh/sshd_config # in case dns is broken, don't lag logins echo "UseDNS no" >> $MR/etc/ssh/sshd_config # clean apt cache rm $MR/var/cache/apt/archives/*.deb # set dist apt source: RPS="main restricted multiverse universe" MURL="http://archive.ubuntu.com/ubuntu" echo "deb $MURL $R $RPS" > $MR/etc/apt/sources.list for P in updates backports security ; do echo "deb $MURL $R-$P $RPS" >> $MR/etc/apt/sources.list done # remove instance ssh host keys rm $MR/etc/ssh/*key* rm $MR/var/lib/dhcp/*.leases # remove temporary resolver, dhcp will fix it: rm $MR/etc/resolv.conf # if there is an /etc/hostname then it won't # pick up the right hostname from dhcp test -e $MR/etc/hostname || rm $MR/etc/hostname mkdir $MR/lib/eeqjvmtools cat > $MR/lib/eeqjvmtools/expandroot.sh < $MR/etc/rc.local <