7 CACHE_FILE="$HELPER_DIR/cache.tar"
9 EXTRA_PKGS="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python"
11 if [ ! -e "$HELPER_PKG" ]; then
12 log_error "Helper package \`$HELPER_PKG' not found."
13 log_error "You need to provide this for the script to work"
17 This program will overwrite the following files:
24 echo -n "Do you want to continue [y/N]? "
26 [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
27 if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
33 rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
35 echo -n "Allocating space for helper disk image..."
36 helper_img=$(mktemp $HELPER_DIR/image.XXXXXXXX)
38 dd if=/dev/zero of=$helper_img bs=1k count=400000 &> /dev/null
41 blockdev=$(losetup -sf $helper_img)
42 CLEANUP+=("losetup -d $blockdev")
44 sleep 1 # sometimes losetup returns and the device is still busy..
46 echo "Creating partitions..."
47 format_disk0 $blockdev "extdump" 2>&1 | sed -e 's/^/CFDISK: /g'
49 root_dev=$(map_disk0 $blockdev)-1
50 CLEANUP+=("unmap_disk0 $blockdev")
52 echo Creating and configuring filesystem...
53 mkfs.ext3 $root_dev 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
54 # The helper vm should never do filesystem checks...
55 tune2fs -i 0 -c 0 $root_dev 2>&1 | sed -e 's/^/TUNE2FS: /g'
58 CLEANUP+=("rmdir $TMPDIR")
60 mount $root_dev $TMPDIR
61 CLEANUP+=("umount $root_dev")
63 echo -n "Checking for cached root filesystem file \`$CACHE_FILE'..."
64 if [ -f "$CACHE_FILE" ]; then
66 tar xf "$CACHE_FILE" -C $TMPDIR
69 echo "Debootstraping to create a new root filesystem:"
70 debootstrap --arch "$ARCH" --include $EXTRA_PKGS --variant=minbase \
71 squeeze $TMPDIR 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
73 # remove the downloaded debs, as they are no longer needed
74 find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
77 TMP_CACHE=$(mktemp "${CACHE_FILE}.XXXXXX")
78 tar cf "$TMP_CACHE" -C $TMPDIR .
79 mv -f "$TMP_CACHE" "$CACHE_FILE"
82 echo -n "Configuring the helper image..."
83 echo snf-image-helper > $TMPDIR/etc/hostname
85 cat > $TMPDIR/etc/fstab <<EOF
86 # /etc/fstab: static file system information.
88 # <file system> <mount point> <type> <options> <dump> <pass>
89 /dev/sda1 / ext3 defaults 0 1
90 proc /proc proc defaults 0 0
94 echo -n "Extracting kernel..."
95 if [ ! -L "$TMPDIR/vmlinuz" -o ! -L "$TMPDIR/vmlinuz" ]; then
97 log_error "vmlinuz or initrd.img link in root is missing."
98 log_error "I don't know how to find a usable kernel/initrd pair."
103 kernel=$(readlink -en $TMPDIR/vmlinuz)
104 initrd=$(readlink -en $TMPDIR/initrd.img)
106 echo "Moving $(basename $kernel) and $(basename $initrd) to $HELPER_DIR"
107 mv $kernel $initrd $HELPER_DIR
109 kernel=$(basename $kernel)
110 initrd=$(basename $initrd)
112 (cd $HELPER_DIR; ln -fs $kernel kernel; ln -fs $initrd initrd)
114 rm $TMPDIR/vmlinuz $TMPDIR/initrd.img
116 echo "Installing snf-image-helper pkg in the new image..."
117 cp $HELPER_PKG $TMPDIR/tmp/
118 pkg_name=$(basename "$HELPER_PKG")
119 CLEANUP+=("rm $TMPDIR/tmp/$pkg_name")
120 chroot ${TMPDIR} dpkg -i /tmp/$pkg_name 2>&1 | sed -e 's/^/DPKG: /g'
122 cat > ${TMPDIR}/etc/rc.local <<EOF
127 # This script is executed at the end of each multiuser runlevel.
128 # Make sure that the script will "exit 0" on success or any other
131 # In order to enable or disable this script just change the execution
134 # By default this script does nothing.
136 /usr/bin/snf-image-helper
141 chmod +x ${TMPDIR}/etc/rc.local
147 mv $helper_img $HELPER_DIR/image
151 echo "Files in \`$HELPER_DIR' were updated successfully"
154 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :