From 3c9e5fe87c548082f81a495f107214f773419199 Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Tue, 28 Jan 2014 00:58:04 +0100 Subject: [PATCH] scripts to build a ubuntu qcow2 root image for kvm --- build.ubuntu.vmimg/Makefile | 8 ++ build.ubuntu.vmimg/Vagrantfile | 65 +++++++++ build.ubuntu.vmimg/buildimage.sh | 224 +++++++++++++++++++++++++++++++ build.ubuntu.vmimg/cleanup.sh | 13 ++ 4 files changed, 310 insertions(+) create mode 100644 build.ubuntu.vmimg/Makefile create mode 100644 build.ubuntu.vmimg/Vagrantfile create mode 100755 build.ubuntu.vmimg/buildimage.sh create mode 100755 build.ubuntu.vmimg/cleanup.sh diff --git a/build.ubuntu.vmimg/Makefile b/build.ubuntu.vmimg/Makefile new file mode 100644 index 0000000..c8b803e --- /dev/null +++ b/build.ubuntu.vmimg/Makefile @@ -0,0 +1,8 @@ +default: gen + +gen: + vagrant destroy -f + vagrant up + +sync: + rsync -avP ./make.sh root@nue1d0.datavibe.net:/storage/buildimage/make.sh diff --git a/build.ubuntu.vmimg/Vagrantfile b/build.ubuntu.vmimg/Vagrantfile new file mode 100644 index 0000000..815108f --- /dev/null +++ b/build.ubuntu.vmimg/Vagrantfile @@ -0,0 +1,65 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! +VAGRANTFILE_API_VERSION = "2" + +SETUP_BASE = <<-EOF + +echo "UseDNS no" >> /etc/ssh/sshd_config +service ssh restart + +function detect_local_mirror () { + AL="$(avahi-browse -p -t -r _ubuntumirror._tcp | grep '^=' | head -1)" + if [ -n "$AL" ]; then + NAME="$(echo \"$AL\" | cut -d';' -f 8)" + PORT="$(echo \"$AL\" | cut -d';' -f 9)" + UM="http://${NAME}:${PORT}/ubuntu/" + MOK="$(curl --head \"${UM}ls-lR.gz\" 2>&1 | grep '200 OK' | wc -l)" + if [ $MOK -gt 0 ]; then + echo "$UM" + fi + fi +} + +function set_apt_mirror () { + CN="$(lsb_release -c -s)" + RPS="main restricted multiverse universe" + echo "deb $1 $CN $RPS" > /etc/apt/sources.list.new + for P in updates backports security ; do + echo "deb $1 $CN-$P $RPS" >> /etc/apt/sources.list.new + done + mv /etc/apt/sources.list.new /etc/apt/sources.list +} + + +set_apt_mirror "mirror://mirrors.ubuntu.com/mirrors.txt" +apt-get update +apt-get -y install avahi-utils jq curl + +UM="$(detect_local_mirror)" +if [ -n "$UM" ]; then + echo "Detected LAN ubuntu mirror at $UM - configuring!" + set_apt_mirror "$UM" +fi +echo "**********************************************************" +echo "**********************************************************" +cat /etc/apt/sources.list +echo "**********************************************************" +echo "**********************************************************" +apt-get update +apt-get -y install kpartx debootstrap lvm2 qemu-utils +EOF + + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + config.vm.box = "ubuntu-12.04" + config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/precise/" + \ + "current/precise-server-cloudimg-amd64-vagrant-disk1.box" + config.vm.network "public_network", :bridge => "en4: Display Ethernet" + config.vm.provision "shell", inline: SETUP_BASE + config.vm.provision "shell", path: "buildimage.sh" + config.vm.provider :virtualbox do |vb| + vb.customize ["modifyvm", :id, "--memory", "2048"] + end +end diff --git a/build.ubuntu.vmimg/buildimage.sh b/build.ubuntu.vmimg/buildimage.sh new file mode 100755 index 0000000..4f67149 --- /dev/null +++ b/build.ubuntu.vmimg/buildimage.sh @@ -0,0 +1,224 @@ +#!/bin/bash + +R="saucy" # release +MR="./roottemp" # mounted root +RI="./raw.img" # raw image +VGN="vmvg0" # volume group name +DSIZE="8G" # 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 + echo "http://archive.ubuntu.com/ubuntu/" + else + echo "$UM" + fi +} + +UM="$(detect_local_mirror)" + +set -e +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 +mount /dev/$VGN/root $MR +mkdir $MR/boot +mount /dev/mapper/${LDBASE}p1 $MR/boot + +# 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 + +cp /etc/resolv.conf $MR/etc/resolv.conf + +cat > $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/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/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 + +# run firstboot on boot if exists +echo "if test -x /firstboot.sh ; then /firstboot.sh ; fi" \ + >> $MR/etc/rc.local + +# write firstboot file +cat > $MR/firstboot.sh <