Make update-helper update the images /etc/rc.local
[snf-image] / snf-image-host / snf-image-update-helper.in
1 #!/bin/bash
2
3 set -e
4
5 . @osdir@/common.sh
6
7 CACHE_FILE="$HELPER_DIR/cache.tar"
8 ARCH=amd64
9 EXTRA_PKGS="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python"
10
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"
14     exit 1
15 fi
16 cat >&1 <<EOF
17 This program will overwrite the following files:
18   \`$HELPER_DIR/initrd'
19   \`$HELPER_DIR/kernel'
20   \`$HELPER_DIR/image'
21 EOF
22
23 while [[ 1 ]]; do
24     echo -n "Do you want to continue [y/N]?"
25     read answer
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
28         log_error "Abord."
29         exit 1
30     fi
31 done
32
33 rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
34
35 echo -n "Allocating space for helper disk image..."
36 helper_img=$(mktemp $HELPER_DIR/image.XXXXXXXX)
37
38 dd if=/dev/zero of=$helper_img bs=1k count=400000 &> /dev/null
39 echo "done"
40
41 blockdev=$(losetup -sf $helper_img)
42 CLEANUP+=("losetup -d $blockdev")
43
44 sleep 1 # sometimes losetup returns and the device is still busy..
45
46 echo -n "Creating partitions..."
47 format_disk0 $blockdev "extdump"
48 echo "done"
49
50 root_dev=$(map_disk0 $blockdev)-1
51 CLEANUP+=("unmap_disk0 $blockdev")
52
53 mkfs.ext3 $root_dev
54
55 TMPDIR=$(mktemp -d)
56 CLEANUP+=("rmdir $TMPDIR")
57
58 mount $root_dev $TMPDIR
59 CLEANUP+=("umount $root_dev")
60
61 echo "Checking for cached root filesystem file \`$CACHE_FILE'..." 
62 if [  -f "$CACHE_FILE" ]; then
63     echo "found"
64     tar xf "$CACHE_FILE" -C $TMPDIR
65 else
66     echo "not found"
67     echo "Debootstraping to create a new root filesystem:"
68     debootstrap --arch "$ARCH" --include $EXTRA_PKGS --variant=minbase \
69             squeeze $TMPDIR
70
71     # remove the downloaded debs, as they are no longer needed
72     find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
73         xargs -r0 rm -f
74
75     TMP_CACHE=$(mktemp "${CACHE_FILE}.XXXXXX")
76     tar cf "$TMP_CACHE" -C $TMPDIR .
77     mv -f "$TMP_CACHE" "$CACHE_FILE"
78 fi
79
80 echo -n "Configureing filesystem..."
81 echo helper > $TMPDIR/etc/hostname
82
83 cat > $TMPDIR/etc/fstab <<EOF
84 # /etc/fstab: static file system information.
85 #
86 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
87 /dev/sda1         /               ext3    defaults        0       1
88 proc              /proc           proc    defaults        0       0
89 EOF
90 echo "done"
91
92 echo -n "Extracting kernel..."
93 if [ ! -L "$TMPDIR/vmlinuz" -o ! -L "$TMPDIR/vmlinuz" ]; then
94
95         log_error "vmlinuz or initrd.img link in root is missing."
96         log_error "I don't know how to find a usable kernel/initrd pair."
97         exit 1
98 fi
99 echo "done"
100
101 kernel=$(readlink -en $TMPDIR/vmlinuz)
102 initrd=$(readlink -en $TMPDIR/initrd.img)
103
104 echo "Moving $(basename $kernel) and $(basename $initrd) to $HELPER_DIR"
105 mv $kernel $initrd $HELPER_DIR
106
107 kernel=$(basename $kernel)
108 initrd=$(basename $initrd)
109
110 (cd $HELPER_DIR; ln -fs $kernel kernel; ln -fs $initrd initrd)
111
112 rm $TMPDIR/vmlinuz $TMPDIR/initrd.img
113
114 echo "Installing snf-image-helper pkg in the new image..."
115 cp $HELPER_PKG $TMPDIR/tmp/
116 pkg_name=$(basename "$HELPER_PKG")  
117 CLEANUP+=("rm $TMPDIR/tmp/$pkg_name")
118 chroot ${TMPDIR} dpkg -i /tmp/$pkg_name
119
120 cat > ${TMPDIR}/etc/rc.local <<EOF
121 #!/bin/sh -e
122 #
123 # rc.local
124 #
125 # This script is executed at the end of each multiuser runlevel.
126 # Make sure that the script will "exit 0" on success or any other
127 # value on error.
128 #
129 # In order to enable or disable this script just change the execution
130 # bits.
131 #
132 # By default this script does nothing.
133
134 /usr/bin/snf-image-helper
135
136 exit 0
137 EOF
138
139 chmod +x ${TMPDIR}/etc/rc.local
140
141 echo "done"
142
143 cleanup
144
145 mv $helper_img $HELPER_DIR/image
146
147 trap - EXIT
148
149 exit 0
150
151 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :