Revision bad5ca1f

b/snf-image-helper/common.sh
38 38

  
39 39
CLEANUP=( )
40 40

  
41
add_cleanup() {
42
    local cmd=""
43
    for arg; do cmd+=$(printf "%q " "$arg"); done
44
    CLEANUP+=("$cmd")
45
}
46

  
41 47
log_error() {
42 48
    echo "ERROR: $@" | tee $RESULT >&2
43 49
    exit 1
b/snf-image-helper/snf-image-helper.in
32 32
. @commondir@/common.sh
33 33

  
34 34
set -e
35
set -o pipefail
35 36

  
36 37
# terminate helper vm when the script exits
37
CLEANUP+=("telinit 0")
38
add_cleanup telinit 0
38 39

  
39
if [ ! -b $FLOPPY_DEV ]; then
40
if [ ! -b "$FLOPPY_DEV" ]; then
40 41
    log_error "Floppy device is not present!"
41 42
fi
42 43

  
43
floppy=$(mktemp -d --tmpdir floppy.XXXXXXXX)
44
CLEANUP+=("rmdir $floppy")
44
floppy=$(mktemp -d --tmpdir floppy.XXXXXX)
45
add_cleanup rmdir "$floppy"
45 46

  
46 47
mount $FLOPPY_DEV $floppy
47
CLEANUP+=("umount $floppy")
48
add_cleanup umount "$floppy"
48 49

  
49
if [ -f $floppy/rules ]; then
50
    source $floppy/rules
50
if [ -f "$floppy/rules" ]; then
51
    source "$floppy/rules"
51 52
else
52 53
    log_error "Floppy does not contain \`rules\' file"
53 54
fi
54 55

  
55 56
# Image mount point...
56
target=$(mktemp -d --tmpdir target.XXXXXXXX)
57
CLEANUP+=("rmdir $target")
57
target=$(mktemp -d --tmpdir target.XXXXXX)
58
add_cleanup rmdir "$target"
58 59

  
59
export SNF_IMAGE_TARGET=$target
60
export SNF_IMAGE_TARGET="$target"
60 61

  
61 62
if [ ! -d "@tasksdir@" ]; then
62 63
    log_error "snf-image/tasks directory is missing"
......
69 70

  
70 71
# If something goes wrong with the tasks, try to umount the target filesystem
71 72
# in case it is left mounted...
72
trap '{ umount $target; }' ERR
73
trap '{ umount "$target"; }' ERR
73 74

  
74
echo "Execute all snf-image tasks...." 
75
echo "Execute all snf-image tasks...."
75 76
$RUN_PARTS -v --exit-on-error "@tasksdir@"
76 77

  
77 78
# Disable the trap. If code reaches here, the filesystem is unmounted.
78 79
trap - ERR
79 80

  
80
echo "SUCCESS" > $RESULT
81
echo "SUCCESS" > "$RESULT"
81 82

  
82 83
cleanup
83 84
trap - EXIT
b/snf-image-helper/tasks/10ResizeUnmounted.in
7 7
### END TAST INFO
8 8

  
9 9
set -e
10
. @commondir@/common.sh
10
. "@commondir@/common.sh"
11 11

  
12 12
if [ ! -b "$SNF_IMAGE_DEV" ]; then
13 13
    log_error "Device file:\`${SNF_IMAGE_DEV}' is not a block device"
......
18 18
fi
19 19

  
20 20
if [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
21
    $RESIZE2FS $SNF_IMAGE_DEV
21
    "$RESIZE2FS" "$SNF_IMAGE_DEV"
22 22
fi	
23 23

  
24 24
exit 0
b/snf-image-helper/tasks/30MountImage.in
7 7
### END TAST INFO
8 8

  
9 9
set -e
10
. @commondir@/common.sh
10
. "@commondir@/common.sh"
11 11

  
12 12
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
13 13
    log_error "Target dir:\`$SNF_IMAGE_TARGET' is missing"
......
17 17
    log_error "Device file:\`$SNF_IMAGE_DEV' is not a block device"
18 18
fi
19 19

  
20
mount $SNF_IMAGE_DEV $SNF_IMAGE_TARGET
20
mount "$SNF_IMAGE_DEV" "$SNF_IMAGE_TARGET"
21 21

  
22 22
exit 0
23 23

  
b/snf-image-helper/tasks/40AddDeleteUnattendScript.in
8 8
### END TAST INFO
9 9

  
10 10
set -e
11
. @commondir@/common.sh
11
. "@commondir@/common.sh"
12 12

  
13 13
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
14 14
	log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"	
b/snf-image-helper/tasks/40DeleteSSHKeys.in
7 7
### END TAST INFO
8 8

  
9 9
set -e
10
. @commondir@/common.sh
10
. "@commondir@/common.sh"
11 11

  
12 12
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
13 13
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing."
......
19 19
    DSA_KEY="/etc/ssh/ssh_host_dsa_key"
20 20

  
21 21
    for key in $HOST_KEY $RSA_KEY $DSA_KEY ; do
22
        if [ -f "${SNF_IMAGE_TARGET}/${key}" ] ; then
23
            rm -f ${SNF_IMAGE_TARGET}/${key}*
22
        if [ -f "$SNF_IMAGE_TARGET/$key" ] ; then
23
            rm -f "$SNF_IMAGE_TARGET"/$key*
24 24
        fi
25 25
    done
26 26
fi
b/snf-image-helper/tasks/40InstallUnattend.in
8 8
### END TAST INFO
9 9

  
10 10
set -e
11
. @commondir@/common.sh
11
. "@commondir@/common.sh"
12 12

  
13 13
if [ -z "$SNF_IMAGE_TARGET" ]; then
14 14
    log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"	
......
18 18
    exit 0
19 19
fi
20 20

  
21
if [ -f @commondir@/unattend.xml ]; then
22
    cat @commondir@/unattend.xml > $SNF_IMAGE_TARGET/Unattend.xml
21
if [ -f "@commondir@/unattend.xml" ]; then
22
    cat "@commondir@/unattend.xml" > "$SNF_IMAGE_TARGET/Unattend.xml"
23 23
else
24 24
    log_error "File \`@commondir@/unattend.xml' is missing."
25 25
fi
b/snf-image-helper/tasks/40ResizeMounted.in
8 8
### END TAST INFO
9 9

  
10 10
set -e
11
. @commondir@/common.sh
11
. "@commondir@/common.sh"
12 12

  
13 13
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
14 14
    log_error "Target directory \`$SNF_IMAGE_TARGET' is missing"
b/snf-image-helper/tasks/40SELinuxAutorelabel.in
8 8
### END TAST INFO
9 9

  
10 10
set -e
11
. @commondir@/common.sh
11
. "@commondir@/common.sh"
12 12

  
13 13
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
14 14
	log_error "Target dir: \`$SNF_IMAGE_TARGET' is missing"	
......
21 21
        # we have to force a filesystem relabeling for SELinux after messing
22 22
        # around with the filesystem in redhat derived OSs
23 23
        echo "Enforce an automatic relabeling in the initial boot process..."
24
        touch $SNF_IMAGE_TARGET/.autorelabel
24
        touch "$SNF_IMAGE_TARGET/.autorelabel"
25 25
    fi
26 26
fi
27 27

  
b/snf-image-helper/tasks/50AssignHostname.in
8 8
### END TAST INFO
9 9

  
10 10
set -e
11
. @commondir@/common.sh
11
. "@commondir@/common.sh"
12 12

  
13 13
windows_hostname() {
14
    local target=$1
15
    local password=$2
14
    local target="$1"
15
    local password="$2"
16 16

  
17 17
    local tmp_unattend=`mktemp` || exit 1
18
    CLEANUP+=("rm $tmp_unattend")
18
    add_cleanup rm "$tmp_unattend"
19 19

  
20 20
    echo -n "Assigning new computer name..."
21 21

  
22 22
    local namespace="urn:schemas-microsoft-com:unattend"
23 23
    
24
    $XMLSTARLET ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v $password "$target/Unattend.xml" > $tmp_unattend
24
    "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:ComputerName" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
25 25

  
26
    cat $tmp_unattend > "$target/Unattend.xml"
26
    cat "$tmp_unattend" > "$target/Unattend.xml"
27 27
    echo done
28 28
}
29 29

  
30 30
linux_hostname() {
31
    local target=$1
32
    local hostname=$2
31
    local target="$1"
32
    local hostname="$2"
33 33

  
34
    local distro=$(get_base_distro $target)
34
    local distro=$(get_base_distro "$target")
35 35

  
36 36
    case "$distro" in
37 37
        debian)
38
            echo "$hostname" > $target/etc/hostname;;
38
            echo "$hostname" > "$target/etc/hostname";;
39 39
        redhat)
40
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" $target/etc/sysconfig/network;;
40
            sed -ie "s/HOSTNAME=.*$/HOSTNAME=$hostname/g" "$target/etc/sysconfig/network";;
41 41
        slackware|suse)
42 42
        #local domain=$(sed -e 's/^[^\.]*//g' < /etc/HOSTNAME)
43 43
        
44 44
        # In slackware hostname and domain name are joined together. For now I
45 45
        # will not retain the domain name.
46 46
        
47
        echo $hostname > ${target}/etc/HOSTNAME;;
47
        echo "$hostname" > "${target}/etc/HOSTNAME";;
48 48
    gentoo)
49
        sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" $target/etc/conf.d/hostname;;
49
        sed -ie "s/\(\(HOSTNAME\)\|\(hostname\)\)=.*$/\1=\"$hostname\"/" "$target/etc/conf.d/hostname";;
50 50
    esac
51 51

  
52 52
    # Some Linux distributions assign the hostname to 127.0.1.1 in order to be
53 53
    # resolvable to an IP address. Lets replace this if found in /etc/hosts
54
    sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" $target/etc/hosts
54
    sed -ie "s/^[[:blank:]]*127\.0\.1\.1[[:blank:]].\+$/127.0.1.1\t$hostname/" "$target/etc/hosts"
55 55
}
56 56

  
57 57
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
......
63 63
fi
64 64

  
65 65
if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
66
    windows_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_HOSTNAME
66
    windows_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
67 67
elif [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
68
    linux_hostname $SNF_IMAGE_TARGET $SNF_IMAGE_HOSTNAME
68
    linux_hostname "$SNF_IMAGE_TARGET" "$SNF_IMAGE_HOSTNAME"
69 69
fi
70 70

  
71 71
cleanup
b/snf-image-helper/tasks/50ChangePassword.in
8 8
### END TAST INFO
9 9

  
10 10
set -e
11
. @commondir@/common.sh
11
. "@commondir@/common.sh"
12 12

  
13 13
windows_password() {
14
    local target=$1
15
    local password=$2
14
    local target="$1"
15
    local password="$2"
16 16

  
17
    local tmp_unattend=`mktemp` || exit 1
18
    CLEANUP+=("rm $tmp_unattend")
17
    local tmp_unattend="$(mktemp)"
18
    add_cleanup rm "$tmp_unattend"
19 19

  
20 20
    echo -n "Installing new admin password..."
21 21

  
22 22
    local namespace="urn:schemas-microsoft-com:unattend"
23 23
    
24
    $XMLSTARLET ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:UserAccounts/x:AdministratorPassword/x:Value" -v $password "$target/Unattend.xml" > $tmp_unattend
24
    "$XMLSTARLET" ed -N x=$namespace -u "/x:unattend/x:settings/x:component/x:UserAccounts/x:AdministratorPassword/x:Value" -v "$password" "$target/Unattend.xml" > "$tmp_unattend"
25 25

  
26
    cat $tmp_unattend > "$target/Unattend.xml"
26
    cat "$tmp_unattend" > "$target/Unattend.xml"
27 27
    echo done
28 28
}
29 29

  
30 30
linux_password() {
31
    local target=$1
32
    local password=$2
31
    local target="$1"
32
    local password="$2"
33 33

  
34
    local hash=$(@scriptsdir@/snf-passtohash.py $password)
35
    if [ ! -e ${target}/etc/shadow ]; then
34
    local hash=$("@scriptsdir@/snf-passtohash.py" "$password")
35
    if [ ! -e "$target/etc/shadow" ]; then
36 36
       log_error "No /etc/shadow found!" 
37 37
    fi
38 38
    
......
46 46
    fi
47 47

  
48 48
    for i in $(seq 0 1 $((${#users[@]}-1))); do
49
        local tmp_shadow=$(mktemp)
50
        CLEANUP+=("rm $tmp_shadow")
49
        local tmp_shadow="$(mktemp)"
50
        add_cleanup rm "$tmp_shadow"
51 51

  
52 52
        echo -n "Setting ${users[$i]} password..."
53 53
    
54
        echo "${users[$i]}:$hash:15103:0:99999:7:::" > $tmp_shadow
55
        grep -v "${users[$i]}" ${TARGET}/etc/shadow >> $tmp_shadow
56
        cat $tmp_shadow > ${target}/etc/shadow
54
        echo "${users[$i]}:$hash:15103:0:99999:7:::" > "$tmp_shadow"
55
        grep -v "${users[$i]}" "$target/etc/shadow" >> "$tmp_shadow"
56
        cat "$tmp_shadow" > "$target/etc/shadow"
57 57
        echo "done"
58 58
    done
59 59
}
......
67 67
fi
68 68

  
69 69
if [ "$SNF_IMAGE_TYPE" = "ntfsdump" ]; then
70
    windows_password $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
70
    windows_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
71 71
elif [ "$SNF_IMAGE_TYPE" = "extdump" ]; then
72
    linux_password $SNF_IMAGE_TARGET $SNF_IMAGE_PASSWORD
72
    linux_password "$SNF_IMAGE_TARGET" "$SNF_IMAGE_PASSWORD"
73 73
fi
74 74

  
75 75
echo "done"
b/snf-image-helper/tasks/80UmountImage.in
8 8
### END TAST INFO
9 9

  
10 10
set -e
11
. @commondir@/common.sh
11
. "@commondir@/common.sh"
12 12

  
13 13
if [ ! -d "$SNF_IMAGE_TARGET" ]; then
14 14
	log_error "Target dir:\`$SNF_IMAGE_TARGET' is missing"
15 15
fi
16 16

  
17
umount $SNF_IMAGE_TARGET
17
umount "$SNF_IMAGE_TARGET"
18 18

  
19 19
cleanup
20 20
trap - EXIT
b/snf-image-host/common.sh.in
40 40

  
41 41
CLEANUP=( )
42 42

  
43
add_cleanup() {
44
    local cmd=""
45
    for arg; do cmd+=$(printf "%q " "$arg"); done
46
    CLEANUP+=("$cmd")
47
}
48

  
43 49
log_error() {
44 50
    echo "$@" >&2
45 51
}
......
139 145
        log_error "Cannot interpret kpartx output and get partition mapping"
140 146
        exit 1
141 147
    fi
142
    $KPARTX -a -p- $blockdev > /dev/null
148
    $KPARTX -a -p- "$blockdev" > /dev/null
143 149
    filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
144 150
    if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
145 151
        log_error "Can't find kpartx mapped partition:" \
......
150 156
}
151 157

  
152 158
unmap_disk0() {
153
    $KPARTX -d -p- $1
159
    $KPARTX -d -p- "$1"
154 160
}
155 161

  
156 162
format_disk0() {
......
171 177
create_floppy() {
172 178
    local img=$1
173 179

  
174
    local target=$(mktemp -d) || exit 1
175
    CLEANUP+=("rmdir $target")
180
    local target=$(mktemp -d)
181
    add_cleanup rmdir "$target"
176 182

  
177
    dd bs=512 count=2880 if=/dev/zero of=$img
178
    mkfs.ext2 -F $img > /dev/null
179
    mount $img $target -o loop
183
    dd bs=512 count=2880 if=/dev/zero of="$img"
184
    mkfs.ext2 -F "$img" > /dev/null
185
    mount "$img" "$target" -o loop
180 186
    set | egrep ^snf_export_\\w+=|sed -e 's/^snf_export_/SNF_IMAGE_/' | \
181 187
    while read line; do
182
        echo "export $line" >> $target/rules
188
        echo "export $line" >> "$target/rules"
183 189
    done
184
    umount $target
190
    umount "$target"
185 191
}
186 192

  
187 193
# this one is only to be called by create
......
275 281
: ${HELPER_KERNEL:="@HELPER_KERNEL@"}
276 282
: ${HELPER_INITRD:="@HELPER_INITRD@"}
277 283
: ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
278
: ${HELPER_SOFT_TIMEOUT=15}
279
: ${HELPER_HARD_TIMEOUT=5}
280
: ${HELPER_USER="nobody"}
284
: ${HELPER_SOFT_TIMEOUT:=15}
285
: ${HELPER_HARD_TIMEOUT:=5}
286
: ${HELPER_USER:="nobody"}
287
: ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
288
: ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python"}
289

  
281 290

  
282 291
SCRIPT_NAME=$(basename $0)
283 292

  
b/snf-image-host/create
30 30
# policies, either expressed or implied, of GRNET S.A.
31 31

  
32 32
set -e
33
set -o pipefail
33 34

  
34 35
. common.sh
35 36

  
......
37 38

  
38 39
case "$IMAGE_TYPE" in
39 40
    extdump)
40
	IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.extdump";;
41
	image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.extdump";;
41 42
    ntfsdump)
42
        IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.ntfsdump";;
43
        image_file="$IMAGE_DIR/$IMAGE_NAME-$ARCH.ntfsdump";;
43 44
    *)
44 45
        log_error "Unknown image type: \`$IMAGE_TYPE'.";
45 46
        exit 1
46 47
esac
47 48

  
48
if [ ! -e "$IMAGE_FILE" ]; then
49
    log_error "Image file \`$IMAGE_FILE' does not exit."
49
if [ ! -e "$image_file" ]; then
50
    log_error "Image file \`$image_file' does not exit."
50 51
    exit 1
51 52
fi
52 53

  
53
MONITOR="" #Empty if progress monitor support is disabled
54
monitor="" #Empty if progress monitor support is disabled
54 55
if [ "$progress_monitor_support" = "yes" ]; then
55
    IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})"
56
    MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}"
56
    image_size="$(stat -L -c %s "$IMAGE_FILE")"
57
    monitor="$(printf "%q" "$PROGRESS_MONITOR") \
58
        -i $(printf "%q" "$INSTANCE_NAME") -r $image_size"
57 59
fi
58 60

  
59 61
# If the target device is not a real block device we'll first losetup it.
60 62
# This is needed for file disks.
61
if [ ! -b $blockdev ]; then
62
    ORIGINAL_BLOCKDEV=$blockdev
63
    blockdev=$($LOSETUP -sf $blockdev)
64
    CLEANUP+=("$LOSETUP -d $blockdev")
63
if [ ! -b "$blockdev" ]; then
64
    original_blockdev="$blockdev"
65
    blockdev=$("$LOSETUP" -sf "$blockdev")
66
    add_cleanup "$LOSETUP" -d "$blockdev"
65 67
fi
66 68

  
67
format_disk0 $blockdev ${IMAGE_TYPE}
69
format_disk0 "$blockdev" "$IMAGE_TYPE"
68 70

  
69
filesystem_dev=$(map_disk0 $blockdev)
70
CLEANUP+=("unmap_disk0 $blockdev")
71
filesystem_dev=$(map_disk0 "$blockdev")
72
add_cleanup unmap_disk0 "$blockdev"
71 73

  
72 74
root_dev="${filesystem_dev}-1"
73 75

  
......
75 77
# Deploying an image file on a target block device is a streaming
76 78
# copy operation. Enable the direct I/O flag on the output fd to 
77 79
# avoid polluting the host cache with useless data.
78
$MONITOR dd bs=4M if=$IMAGE_FILE of=$root_dev oflag=direct
80
$monitor dd bs=4M if="$IMAGE_FILE" of="$root_dev" oflag=direct
79 81

  
80 82
# Create a floppy image
81
floppy=$(mktemp --tmpdir floppy.XXXXXXXX)
82
CLEANUP+=("rm -f $floppy")
83
floppy=$(mktemp --tmpdir floppy.XXXXXX)
84
add_cleanup rm "$floppy"
83 85

  
84 86
snf_export_DEV=/dev/vda
85
snf_export_TYPE=${IMG_FORMAT}
86
snf_export_PASSWORD=${IMG_PASSWD}
87
snf_export_HOSTNAME=${instance}
87
snf_export_TYPE="$IMG_FORMAT"
88
snf_export_PASSWORD="$IMG_PASSWD"
89
snf_export_HOSTNAME="$instance"
88 90
if [ -n "$IMG_PERSONALITY" ]; then
89
    snf_export_PERSONALITY=${IMG_PERSONALITY}
91
    snf_export_PERSONALITY="$IMG_PERSONALITY"
90 92
fi
91 93

  
92
create_floppy $floppy
94
create_floppy "$floppy"
93 95

  
94 96
# Invoke the helper vm to do the dirty job...
95
result_file=$(mktemp --tmpdir result.XXXXXXXX)
96
CLEANUP+=("rm -f $result_file")
97
result_file=$(mktemp --tmpdir result.XXXXXX)
98
add_cleanup rm "$result_file"
97 99

  
98
image_file=$(mktemp --tmpdir helperXXXXXXXX.img)
99
CLEANUP+=("rm -f $image_file")
100
snapshot=$(mktemp --tmpdir helperXXXXXX.img)
101
add_cleanup rm "$snapshot"
100 102

  
101
$QEMU_IMG create -f qcow2 -b ${HELPER_IMG} $image_file
103
"$QEMU_IMG" create -f qcow2 -b "$HELPER_IMG" "$snapshot"
102 104

  
103
$TIMELIMIT -t $HELPER_SOFT_TIMEOUT -T $HELPER_HARD_TIMEOUT \
104
    kvm -runas $HELPER_USER -drive file=${image_file} \
105
    -drive file=$root_dev,format=raw,if=virtio,cache=none \
106
    -boot c -serial stdio -serial file:$result_file -fda $floppy \
107
    -vga none -nographic -parallel none -monitor null -nographic \
108
    -kernel ${HELPER_KERNEL} -initrd ${HELPER_INITRD} \
105
"$TIMELIMIT" -t "$HELPER_SOFT_TIMEOUT" -T "$HELPER_HARD_TIMEOUT" \
106
    kvm -runas "$HELPER_USER" -drive file="$snapshot" \
107
    -drive file="$root_dev",format=raw,if=virtio,cache=none \
108
    -boot c -serial stdio -serial file:"$result_file" \
109
    -fda "$floppy" -vga none -nographic -parallel none -monitor null \
110
    -kernel "$HELPER_KERNEL" -initrd "$HELPER_INITRD" \
109 111
    -append "quiet ro root=/dev/sda1 console=ttyS0,9600n8" \
110 112
    2>&1 | sed 's|^|HELPER: |g'
111 113

  
......
117 119
fi
118 120

  
119 121
# Read the first line. This will remove \r and \n chars
120
result=$(sed 's|\r||g' $result_file | xargs echo)
122
result=$(sed 's|\r||g' "$result_file" | xargs echo)
121 123

  
122 124
if [ "x$result" != "xSUCCESS" ]; then
123 125
    log_error "Helper VM returned error"
......
125 127
fi
126 128

  
127 129
# Install a new MBR
128
$INSTALL_MBR -p 1 -i n ${blockdev}
130
"$INSTALL_MBR" -p 1 -i n "$blockdev"
129 131

  
130 132
# Execute cleanups
131 133
cleanup
b/snf-image-host/defaults
43 43
# user the helper vm should run as is specifies by HELPER_USR variable.
44 44
# HELPER_USER="nobody"
45 45

  
46
# HELPER_CACHE_FILE: Cache file update-helper script uses to avoid running
47
# `debootstrap' all the time.
48
# HELPER_CACHE_FILE=$(HELPER_DIR)/cache.tar
49

  
50
# HELPER_EXTRA_PKGS: Extra packages that will need to be supplied to de supplied
51
# to debootstrap to make the resulting helper image workable
52
# HELPER_EXTRA_PKGS="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python"
53

  
46 54
# Paths for needed programs. Uncommend and change the variables below, if you
47 55
# don't want to use the default one.
48 56
# LOSETUP="losetup"
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