#!/bin/bash set -e set -v set -x R="saucy" # release MR="./roottemp" # mounted root RI="./raw.img" # raw image VGN="vmvg0" # volume group name DSIZE="25G" # disk size DATE="$(date +%Y%m%d)" LOOPDEV="/dev/loop5" LDBASE="$(basename $LOOPDEV)" ROOTPW="7c493cc530734f4c11e00bcecadb7b73" function detect_local_mirror () { AL="$(avahi-browse -p -t -r _ubuntumirror._tcp | grep '^=' | head -1)" UM="" if [ -n "$AL" ]; then NAME="$(echo \"$AL\" | cut -d\; -f 8)" PORT="$(echo \"$AL\" | cut -d\; -f 9)" UM="http://${NAME}:${PORT}/ubuntu/" fi if [ -z "$UM" ]; then # maybe try hetzner mirror? UM="http://mirror.hetzner.de/ubuntu/packages/" TF="${UM}/dists/${R}/Release" MOK="$(curl --head ${TF} 2>&1 | grep '200 OK' | wc -l)" if [ $MOK -gt 0 ]; then echo "$UM" else echo "http://archive.ubuntu.com/ubuntu/" fi else echo "$UM" fi } UM="$(detect_local_mirror)" 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 mkfs.ext4 -L BOOT /dev/mapper/${LDBASE}p1 tune2fs -c -1 /dev/mapper/${LDBASE}p1 pvcreate /dev/mapper/${LDBASE}p2 vgcreate $VGN /dev/mapper/${LDBASE}p2 lvcreate -l 100%FREE -n root $VGN mkfs.ext4 -L ROOT /dev/$VGN/root mkdir -p $MR MR="$(readlink -f $MR)" mount /dev/$VGN/root $MR mkdir $MR/boot mount /dev/mapper/${LDBASE}p1 $MR/boot echo "*** installing base $R system from $UM..." # install base: 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 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 /dev/mapper/${LDBASE}p1)" 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/hostname cat > $MR/etc/hosts < $MR/usr/sbin/policy-rc.d <> $MR/etc/default/grub echo "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=9600 --stop=1\"" \ >> $MR/etc/default/grub echo "GRUB_TERMINAL=\"serial\"" >> $MR/etc/default/grub echo "GRUB_GFXPAYLOAD=\"text\"" >> $MR/etc/default/grub chroot $MR /bin/bash -c "echo \"root:$ROOTPW\" | chpasswd" chroot $MR update-grub 2> /dev/null chroot $MR grub-mkconfig -o /boot/grub/grub.cfg 2> /dev/null cat > $MR/boot/grub/device.map < /dev/null # get rid of device.map after grub is installed... rm $MR/boot/grub/device.map chroot $MR update-initramfs -d -k all # for some stupid reason, -k all doesn't work after removing: KERN="$(cd $MR/boot && ls vmlinuz*)" VER="${KERN#vmlinuz-}" chroot $MR update-initramfs -c -k $VER cat > $MR/etc/init/ttyS0.conf < $MR/etc/dhcp/dhclient-exit-hooks.d/hostname <> $MR/etc/ssh/sshd_config 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="mirror://mirrors.ubuntu.com/mirrors.txt" 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 # clear issue echo "clear > /etc/issue" | chroot $MR # remove instance ssh host keys rm $MR/etc/ssh/*key* # regenerate them on first boot cat > $MR/etc/rc.local <