-
Notifications
You must be signed in to change notification settings - Fork 18
/
create
executable file
·178 lines (153 loc) · 5.31 KB
/
create
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
# Copyright (C) 2010, 2011, 2012 Oregon State University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
set -e
. common.sh
debug set -x
case "$IMAGE_TYPE" in
tarball)
IMG_EXT=".tar.gz"
;;
qemu)
IMG_EXT=".img"
;;
raw)
IMG_EXT=".raw"
;;
qcow2)
IMG_EXT=".qcow2"
;;
dump)
IMG_EXT="-root.dump"
;;
esac
if [ ! -z "$IMAGE_URL" ] ; then
cd ${CACHE_DIR}
if [ ! -e ${CACHE_DIR}/${IMAGE_NAME}-${ARCH}${IMG_EXT} ]; then
$CURL -O "${IMAGE_URL}/${IMAGE_NAME}-${ARCH}${IMG_EXT}" 2>/dev/null\
|| (log_error "Failed to find the image on ${IMAGE_URL}"; exit 1)
fi
if [ "$IMAGE_VERIFY" = "yes" ]; then
$CURL -O "${IMAGE_URL}/${IMAGE_NAME}-${ARCH}${IMG_EXT}.sig" 2>/dev/null\
|| (log_error "Failed to find the checksum file"; exit 1)
$SHA1SUM -c "${IMAGE_NAME}-${ARCH}${IMG_EXT}.sig" >/dev/null \
|| (log_error "Image doesn't match checksum!"; exit 1)
CLEANUP+=("rm ${CACHE_DIR}/${IMAGE_NAME}-${ARCH}${IMG_EXT}.sig")
fi
if [ "$IMAGE_CLEANUP" = "yes" ]; then
CLEANUP+=("rm ${CACHE_DIR}/${IMAGE_NAME}-${ARCH}${IMG_EXT}")
fi
IMAGE_FILE="${CACHE_DIR}/${IMAGE_NAME}-${ARCH}${IMG_EXT}"
cd $OLDPWD
else
IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}${IMG_EXT}"
fi
if [ "$IMAGE_TYPE" = "dump" ] ; then
# Workaround for restore's non-unique /tmp/rstdir* and /tmp/rstmode*
export TMPDIR=$(mktemp -d /tmp/${INSTANCE_NAME}_XXXXXXXX)
CLEANUP+=("rmdir --ignore-fail-on-non-empty $TMPDIR")
fi
if [ "$CDINSTALL" = "no" ] ; then
# If the target device is not a real block device we'll first losetup it.
# This is needed for file disks.
if [ ! -b $blockdev ]; then
ORIGINAL_BLOCKDEV=$blockdev
blockdev=$($LOSETUP -sf $blockdev)
CLEANUP+=("$LOSETUP -d $blockdev")
fi
DISK_SIZE="$(expr `blockdev --getsize64 $blockdev` / 1048576)"
if [ "${SWAP}" = "yes" ] ; then
if [ -z "$SWAP_SIZE" ] ; then
log_error "SWAP_SIZE not set however SWAP is enabled"
exit 1
elif [ "${SWAP_SIZE}" -gt "${DISK_SIZE}" ] ; then
log_error "SWAP_SIZE larger than system disk size: "
log_error " ${SWAP_SIZE}MB swap > ${DISK_SIZE}MB system disk"
exit 1
fi
fi
if [ ! -f "$IMAGE_FILE" ] ; then
log_error "Can't find image file: $IMAGE_FILE"
exit 1
fi
case "$IMAGE_TYPE" in
tarball|dump)
# If the image is tarball based, then we need to manually create the
# volumes, filesystems, etc
# Create 3 partitions, /boot, swap, & /
format_disk0 $blockdev
;;
qemu|raw|qcow2)
# need a recent version of qemu for this
${QEMU_IMG} convert ${IMAGE_FILE} -O host_device ${blockdev} > /dev/null
;;
esac
# deploying something like a windows image, skip the rest
if [ "${NOMOUNT}" = "yes" ] ; then
cleanup
exit 0
fi
filesystem_dev=$(map_disk0 $blockdev)
CLEANUP+=("unmap_disk0 $blockdev")
root_dev=$(map_partition $filesystem_dev root)
boot_dev=$(map_partition $filesystem_dev boot)
swap_dev=$(map_partition $filesystem_dev swap)
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
mkfs_disk0
root_uuid="$($VOL_ID $root_dev)"
[ -n "$boot_dev" ] && sleep 1 && boot_uuid="$($VOL_ID $boot_dev)"
[ -n "$swap_dev" ] && sleep 1 && swap_uuid="$($VOL_ID $swap_dev)"
fi
TARGET=`mktemp -d` || exit 1
CLEANUP+=("rmdir $TARGET")
# mount filesystems
mount_disk0 $TARGET
if [ "${IMAGE_TYPE}" = "tarball" ] ; then
# unpack image
tar pzxf $IMAGE_FILE -C $TARGET
elif [ "${IMAGE_TYPE}" = "dump" ] ; then
root_dump="${IMAGE_FILE}"
( cd ${TARGET}; restore -r -y -f ${root_dump} > /dev/null )
if [ -n "${boot_dev}" ] ; then
boot_dump="${IMAGE_FILE/root.dump/boot.dump}"
if [ ! -f "$boot_dump" ] ; then
log_error "Can't find image file: $boot_dump"
exit 1
fi
( cd ${TARGET}/boot; restore -r -y -f ${boot_dump} > /dev/null )
fi
fi
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
setup_fstab $TARGET
fi
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
setup_console $TARGET
fi
RUN_PARTS=`which run-parts`
if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
TARGET=$TARGET
BLOCKDEV=$blockdev
ROOT_DEV=$root_dev
BOOT_DEV=$boot_dev
export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE
$RUN_PARTS $CUSTOMIZE_DIR
fi
fi
# execute cleanups
cleanup
trap - EXIT
exit 0