Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / snf-image-update-helper.in @ 5a31f7ef

History | View | Annotate | Download (4.6 kB)

1
#!/bin/bash
2

    
3
set -e
4
set -o pipefail
5

    
6
. @osdir@/common.sh
7

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

    
14
cat >&1 <<EOF
15
This program will overwrite the following files:
16
  \`$HELPER_DIR/initrd'
17
  \`$HELPER_DIR/kernel'
18
  \`$HELPER_DIR/image'
19
EOF
20

    
21
while [[ 1 ]]; do
22
    echo -n "Do you want to continue [y/N]? "
23
    read answer
24
    [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
25
    if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
26
        log_error "Abort."
27
        exit 1
28
    fi
29
done
30

    
31
rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
32

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

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

    
39
echo "Creating partitions..."
40
blockdev=$("$LOSETUP" -sf $helper_img)
41
add_cleanup "$LOSETUP" -d "$blockdev"
42

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

    
45
format_disk0 "$blockdev" "extdump"  2>&1 | sed -e 's/^/CFDISK: /g'
46

    
47
root_dev=$(map_disk0 "$blockdev")-1
48
add_cleanup unmap_disk0 "$blockdev"
49

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

    
55
target=$(mktemp -d)
56
add_cleanup rmdir "$target"
57

    
58
mount "$root_dev" "$target"
59
add_cleanup umount "$root_dev"
60

    
61
echo -n "Checking for cached root filesystem file \`$HELPER_CACHE_FILE'..." 
62
if [  -f "$HELPER_CACHE_FILE" ]; then
63
    echo "found"
64
    tar xf "$HELPER_CACHE_FILE" -C "$target"
65
else
66
    echo "not found"
67
    echo "Debootstraping to create a new root filesystem:"
68

    
69
    # Create a policy-rc.d file to deny init script execution
70
    mkdir -p "$target/usr/sbin"
71
    cat > "$target/usr/sbin/policy-rc.d" <<EOF
72
#!/bin/sh
73
exit 101
74
EOF
75
    chmod +x "$target/usr/sbin/policy-rc.d"
76

    
77
    debootstrap --arch amd64 --include "$HELPER_EXTRA_PKGS" \
78
        --variant=minbase stable "$target" "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
79

    
80
    rm "$target/usr/sbin/policy-rc.d"
81
    
82
    # remove the downloaded debs, as they are no longer needed
83
    find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
84
        xargs -r0 rm -f
85

    
86
    tmp_cache=$(mktemp "$HELPER_CACHE_FILE.XXXXXX")
87
    tar cf "$tmp_cache" --one-file-system -C "$target" . || { rm "$tmp_cache"; false; }
88
    mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
89
fi
90

    
91
echo -n "Configuring the helper image..."
92
echo snf-image-helper > "$target/etc/hostname"
93

    
94
cat > "$target/etc/fstab" <<EOF
95
# /etc/fstab: static file system information.
96
#
97
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
98
/dev/sda1         /               ext3    defaults        0       1
99
proc              /proc           proc    defaults        0       0
100
EOF
101
echo "done"
102

    
103
echo -n "Extracting kernel..."
104
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
105
    echo -e "\033[1;31mfailed\033[0m"
106
    log_error "vmlinuz or initrd.img link in root is missing."
107
    log_error "I don't know how to find a usable kernel/initrd pair."
108
    exit 1
109
fi
110
echo "done"
111

    
112
kernel=$(readlink -en "$target/vmlinuz")
113
initrd=$(readlink -en "$target/initrd.img")
114

    
115
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
116
mv "$kernel" "$initrd" "$HELPER_DIR"
117

    
118
kernel=$(basename "$kernel")
119
initrd=$(basename "$initrd")
120

    
121
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
122

    
123
rm "$target/vmlinuz" "$target/initrd.img"
124

    
125
echo "Installing snf-image-helper pkg in the new image..."
126
cp "$HELPER_PKG" "$target/tmp/"
127
pkg_name=$(basename "$HELPER_PKG")
128
add_cleanup rm "$target/tmp/$pkg_name"
129
chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
130

    
131
cat > "$target/etc/rc.local" <<EOF
132
#!/bin/sh -e
133
#
134
# rc.local
135
#
136
# This script is executed at the end of each multiuser runlevel.
137
# Make sure that the script will "exit 0" on success or any other
138
# value on error.
139
#
140
# In order to enable or disable this script just change the execution
141
# bits.
142
#
143
# By default this script does nothing.
144

    
145
if ! grep -q snf_image_activate_helper /proc/cmdline; then
146
    echo "WARNING: NOT calling snf-image-helper, add snf_image_activate_helper"
147
    echo "to the kernel command line if you want to do so."
148
else
149
    /usr/bin/snf-image-helper --force
150
fi
151

    
152
exit 0
153
EOF
154

    
155
chmod +x "$target/etc/rc.local"
156

    
157
echo "done"
158

    
159
cleanup
160

    
161
mv "$helper_img" "$HELPER_DIR/image"
162

    
163
trap - EXIT
164

    
165
echo "Files in \`$HELPER_DIR' were updated successfully"
166
exit 0
167

    
168
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :