#!/bin/bash set -e . @osdir@/common.sh CACHE_FILE="$HELPER_DIR/cache.tar" ARCH=amd64 EXTRA_PKGS="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python" if [ ! -e "$HELPER_PKG" ]; then log_error "Helper package \`$HELPER_PKG' not found." log_error "You need to provide this for the script to work" exit 1 fi cat >&1 < /dev/null echo "done" blockdev=$(losetup -sf $helper_img) CLEANUP+=("losetup -d $blockdev") sleep 1 # sometimes losetup returns and the device is still busy.. echo "Creating partitions..." format_disk0 $blockdev "extdump" 2>&1 | sed -e 's/^/CFDISK: /g' root_dev=$(map_disk0 $blockdev)-1 CLEANUP+=("unmap_disk0 $blockdev") echo Creating and configuring filesystem... mkfs.ext3 $root_dev 2>&1 | sed -e 's/^/MKFS.EXT3: /g' # The helper vm should never do filesystem checks... tune2fs -i 0 -c 0 $root_dev 2>&1 | sed -e 's/^/TUNE2FS: /g' TMPDIR=$(mktemp -d) CLEANUP+=("rmdir $TMPDIR") mount $root_dev $TMPDIR CLEANUP+=("umount $root_dev") echo -n "Checking for cached root filesystem file \`$CACHE_FILE'..." if [ -f "$CACHE_FILE" ]; then echo "found" tar xf "$CACHE_FILE" -C $TMPDIR else echo "not found" echo "Debootstraping to create a new root filesystem:" debootstrap --arch "$ARCH" --include $EXTRA_PKGS --variant=minbase \ squeeze $TMPDIR 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g' # remove the downloaded debs, as they are no longer needed find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \ xargs -r0 rm -f TMP_CACHE=$(mktemp "${CACHE_FILE}.XXXXXX") tar cf "$TMP_CACHE" -C $TMPDIR . mv -f "$TMP_CACHE" "$CACHE_FILE" fi echo -n "Configuring the helper image..." echo snf-image-helper > $TMPDIR/etc/hostname cat > $TMPDIR/etc/fstab < /dev/sda1 / ext3 defaults 0 1 proc /proc proc defaults 0 0 EOF echo "done" echo -n "Extracting kernel..." if [ ! -L "$TMPDIR/vmlinuz" -o ! -L "$TMPDIR/vmlinuz" ]; then log_error "vmlinuz or initrd.img link in root is missing." log_error "I don't know how to find a usable kernel/initrd pair." exit 1 fi echo "done" kernel=$(readlink -en $TMPDIR/vmlinuz) initrd=$(readlink -en $TMPDIR/initrd.img) echo "Moving $(basename $kernel) and $(basename $initrd) to $HELPER_DIR" mv $kernel $initrd $HELPER_DIR kernel=$(basename $kernel) initrd=$(basename $initrd) (cd $HELPER_DIR; ln -fs $kernel kernel; ln -fs $initrd initrd) rm $TMPDIR/vmlinuz $TMPDIR/initrd.img echo "Installing snf-image-helper pkg in the new image..." cp $HELPER_PKG $TMPDIR/tmp/ pkg_name=$(basename "$HELPER_PKG") CLEANUP+=("rm $TMPDIR/tmp/$pkg_name") chroot ${TMPDIR} dpkg -i /tmp/$pkg_name 2>&1 | sed -e 's/^/DPKG: /g' cat > ${TMPDIR}/etc/rc.local <