Revision bad5ca1f snf-image-host/snf-image-update-helper.in

b/snf-image-host/snf-image-update-helper.in
1 1
#!/bin/bash
2 2

  
3 3
set -e
4
set -o pipefail
4 5

  
5 6
. @osdir@/common.sh
6 7

  
7
CACHE_FILE="$HELPER_DIR/cache.tar"
8
ARCH=amd64
9
EXTRA_PKGS="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python"
10

  
11 8
if [ ! -e "$HELPER_PKG" ]; then
12 9
    log_error "Helper package \`$HELPER_PKG' not found."
13 10
    log_error "You need to provide this for the script to work"
14 11
    exit 1
15 12
fi
13

  
16 14
cat >&1 <<EOF
17 15
This program will overwrite the following files:
18 16
  \`$HELPER_DIR/initrd'
......
33 31
rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
34 32

  
35 33
echo -n "Allocating space for helper disk image..."
36
helper_img=$(mktemp $HELPER_DIR/image.XXXXXXXX)
34
helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
37 35

  
38
dd if=/dev/zero of=$helper_img bs=1k count=400000 &> /dev/null
36
dd if=/dev/zero of="$helper_img" bs=1k count=400000 &> /dev/null
39 37
echo "done"
40 38

  
39
echo "Creating partitions..."
41 40
blockdev=$(losetup -sf $helper_img)
42
CLEANUP+=("losetup -d $blockdev")
41
add_cleanup losetup -d "$blockdev"
43 42

  
44 43
sleep 1 # sometimes losetup returns and the device is still busy..
45 44

  
46
echo "Creating partitions..."
47
format_disk0 $blockdev "extdump"  2>&1 | sed -e 's/^/CFDISK: /g'
45
format_disk0 "$blockdev" "extdump"  2>&1 | sed -e 's/^/CFDISK: /g'
48 46

  
49
root_dev=$(map_disk0 $blockdev)-1
50
CLEANUP+=("unmap_disk0 $blockdev")
47
root_dev=$(map_disk0 "$blockdev")-1
48
add_cleanup unmap_disk0 "$blockdev"
51 49

  
52 50
echo Creating and configuring filesystem...
53
mkfs.ext3 $root_dev 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
51
mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
54 52
# The helper vm should never do filesystem checks...
55
tune2fs -i 0 -c 0 $root_dev 2>&1 | sed -e 's/^/TUNE2FS: /g'
53
tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g'
56 54

  
57
TMPDIR=$(mktemp -d)
58
CLEANUP+=("rmdir $TMPDIR")
55
target=$(mktemp -d)
56
add_cleanup rmdir "$target"
59 57

  
60
mount $root_dev $TMPDIR
61
CLEANUP+=("umount $root_dev")
58
mount "$root_dev" "$target"
59
add_cleanup umount "$root_dev"
62 60

  
63
echo -n "Checking for cached root filesystem file \`$CACHE_FILE'..." 
64
if [  -f "$CACHE_FILE" ]; then
61
echo -n "Checking for cached root filesystem file \`$HELPER_CACHE_FILE'..." 
62
if [  -f "$HELPER_CACHE_FILE" ]; then
65 63
    echo "found"
66
    tar xf "$CACHE_FILE" -C $TMPDIR
64
    tar xf "$HELPER_CACHE_FILE" -C "$target"
67 65
else
68 66
    echo "not found"
69 67
    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'
68
    debootstrap --arch amd64 --include "$HELPER_EXTRA_PKGS" \
69
        --variant=minbase squeeze "$target" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
72 70

  
73 71
    # remove the downloaded debs, as they are no longer needed
74
    find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
72
    find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
75 73
        xargs -r0 rm -f
76 74

  
77
    TMP_CACHE=$(mktemp "${CACHE_FILE}.XXXXXX")
78
    tar cf "$TMP_CACHE" -C $TMPDIR .
79
    mv -f "$TMP_CACHE" "$CACHE_FILE"
75
    tmp_cache=$(mktemp "$HELPER_CACHE_FILE.XXXXXX")
76
    tar cf "$tmp_cache" -C "$target" . || { rm "$tmp_cache"; false; }
77
    mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
80 78
fi
81 79

  
82 80
echo -n "Configuring the helper image..."
83
echo snf-image-helper > $TMPDIR/etc/hostname
81
echo snf-image-helper > "$target/etc/hostname"
84 82

  
85
cat > $TMPDIR/etc/fstab <<EOF
83
cat > "$target/etc/fstab" <<EOF
86 84
# /etc/fstab: static file system information.
87 85
#
88 86
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
......
92 90
echo "done"
93 91

  
94 92
echo -n "Extracting kernel..."
95
if [ ! -L "$TMPDIR/vmlinuz" -o ! -L "$TMPDIR/vmlinuz" ]; then
96

  
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."
99
	exit 1
93
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
94
    echo -e "\033[1;31mfailed\033[0m"
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
100 98
fi
101 99
echo "done"
102 100

  
103
kernel=$(readlink -en $TMPDIR/vmlinuz)
104
initrd=$(readlink -en $TMPDIR/initrd.img)
101
kernel=$(readlink -en "$target/vmlinuz")
102
initrd=$(readlink -en "$target/initrd.img")
105 103

  
106
echo "Moving $(basename $kernel) and $(basename $initrd) to $HELPER_DIR"
107
mv $kernel $initrd $HELPER_DIR
104
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
105
mv "$kernel" "$initrd" "$HELPER_DIR"
108 106

  
109
kernel=$(basename $kernel)
110
initrd=$(basename $initrd)
107
kernel=$(basename "$kernel")
108
initrd=$(basename "$initrd")
111 109

  
112
(cd $HELPER_DIR; ln -fs $kernel kernel; ln -fs $initrd initrd)
110
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
113 111

  
114
rm $TMPDIR/vmlinuz $TMPDIR/initrd.img
112
rm "$target/vmlinuz" "$target/initrd.img"
115 113

  
116 114
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'
115
cp "$HELPER_PKG" "$target/tmp/"
116
pkg_name=$(basename "$HELPER_PKG")
117
add_cleanup rm "$target/tmp/$pkg_name"
118
chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
121 119

  
122
cat > ${TMPDIR}/etc/rc.local <<EOF
120
cat > "$target/etc/rc.local" <<EOF
123 121
#!/bin/sh -e
124 122
#
125 123
# rc.local
......
138 136
exit 0
139 137
EOF
140 138

  
141
chmod +x ${TMPDIR}/etc/rc.local
139
chmod +x "$target/etc/rc.local"
142 140

  
143 141
echo "done"
144 142

  
145 143
cleanup
146 144

  
147
mv $helper_img $HELPER_DIR/image
145
mv "$helper_img" "$HELPER_DIR/image"
148 146

  
149 147
trap - EXIT
150 148

  

Also available in: Unified diff