root / create @ d55a2cc1
History | View | Annotate | Download (5.3 kB)
1 |
#!/bin/bash |
---|---|
2 |
|
3 |
# Copyright (C) 2010 Oregon State University |
4 |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 2 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
# General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
18 |
# 02110-1301, USA. |
19 |
|
20 |
set -e |
21 |
|
22 |
. common.sh |
23 |
|
24 |
debug set -x |
25 |
|
26 |
OS_FAMILY="linux" |
27 |
|
28 |
if [ "$IMAGE_TYPE" = "tarball" ] ; then |
29 |
IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.tar.gz" |
30 |
elif [ "$IMAGE_TYPE" = "qemu" ] ; then |
31 |
IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.img" |
32 |
elif [ "$IMAGE_TYPE" = "dump" ] ; then |
33 |
IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}-root.dump" |
34 |
elif [ "$IMAGE_TYPE" = "ntfsclone" ]; then |
35 |
IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.img" |
36 |
OS_FAMILY="windows" |
37 |
fi |
38 |
|
39 |
MONITOR="" #Empty if progress monitor support is disabled |
40 |
if [ "$progress_monitor_support" = "yes" ]; then |
41 |
IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})" |
42 |
MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}" |
43 |
fi |
44 |
|
45 |
if [ "$OS_FAMILY" = "linux" ]; then |
46 |
. common_linux.sh |
47 |
elif [ "$OS_FAMILY" = "windows" ]; then |
48 |
. common_windows.sh |
49 |
SWAP="no" |
50 |
fi |
51 |
|
52 |
if [ "$CDINSTALL" = "no" ] ; then |
53 |
# If the target device is not a real block device we'll first losetup it. |
54 |
# This is needed for file disks. |
55 |
if [ ! -b $blockdev ]; then |
56 |
ORIGINAL_BLOCKDEV=$blockdev |
57 |
blockdev=$($LOSETUP -sf $blockdev) |
58 |
CLEANUP+=("$LOSETUP -d $blockdev") |
59 |
fi |
60 |
|
61 |
if [ ! -f "$IMAGE_FILE" ] ; then |
62 |
log_error "Can't find image file: $IMAGE_FILE" |
63 |
exit 1 |
64 |
fi |
65 |
|
66 |
# If the image is tarball based, then we need to manually create the |
67 |
# volumes, filesystems, etc |
68 |
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then |
69 |
# Create 3 partitions, /boot, swap, & / |
70 |
${OS_FAMILY}_format_disk0 $blockdev |
71 |
elif [ "${IMAGE_TYPE}" = "qemu" ] ; then |
72 |
# need a recent version of qemu for this |
73 |
${QEMU_IMG} convert ${IMAGE_FILE} -O host_device ${blockdev} > /dev/null |
74 |
elif [ "${IMAGE_TYPE}" = "ntfsclone" ] ; then |
75 |
${OS_FAMILY}_format_disk0 $blockdev |
76 |
fi |
77 |
|
78 |
# deploying something like a windows image, skip the rest |
79 |
if [ "${NOMOUNT}" = "yes" ] ; then |
80 |
cleanup |
81 |
exit 0 |
82 |
fi |
83 |
|
84 |
filesystem_dev=$(map_disk0 $blockdev) |
85 |
CLEANUP+=("unmap_disk0 $blockdev") |
86 |
root_dev=$(map_partition $filesystem_dev root) |
87 |
boot_dev=$(map_partition $filesystem_dev boot) |
88 |
swap_dev=$(map_partition $filesystem_dev swap) |
89 |
|
90 |
# Check partition existence |
91 |
if [ ! -e "$root_dev" ] ; then |
92 |
log_error "Root partition: $root_dev does not exist!" |
93 |
exit 1; |
94 |
fi |
95 |
if [ -n "$boot_dev" -a ! -e "$boot_dev" ] ; then |
96 |
log_error "Boot partition: $boot_dev does not exist!" |
97 |
exit 1; |
98 |
fi |
99 |
if [ -n "$swap_dev" -a ! -e "$swap_dev" ] ; then |
100 |
log_error "Swap partiion: $swap_dev does not exist!" |
101 |
exit 1; |
102 |
fi |
103 |
|
104 |
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then |
105 |
${OS_FAMILY}_mkfs_disk0 |
106 |
root_uuid="$($VOL_ID $root_dev)" |
107 |
[ -n "$boot_dev" ] && sleep 1 && boot_uuid="$($VOL_ID $boot_dev)" |
108 |
[ -n "$swap_dev" ] && sleep 1 && swap_uuid="$($VOL_ID $swap_dev)" |
109 |
fi |
110 |
|
111 |
TARGET=`mktemp -d` || exit 1 |
112 |
CLEANUP+=("rmdir $TARGET") |
113 |
|
114 |
if [ "${IMAGE_TYPE}" = "ntfsclone" ] ; then |
115 |
# Restore the ntfs image |
116 |
$MONITOR $NTFSCLONE --restore-image --overwrite $root_dev $IMAGE_FILE \ |
117 |
> /dev/null |
118 |
# Resize it to take over the whole patition |
119 |
$NTFSRESIZE -P $root_dev |
120 |
|
121 |
fi |
122 |
|
123 |
# mount filesystems |
124 |
mount_disk0 $TARGET |
125 |
|
126 |
if [ "${IMAGE_TYPE}" = "tarball" ] ; then |
127 |
# unpack image |
128 |
tar pzxf $IMAGE_FILE -C $TARGET |
129 |
elif [ "${IMAGE_TYPE}" = "dump" ] ; then |
130 |
root_dump="${IMAGE_FILE}" |
131 |
( cd ${TARGET}; $MONITOR $RESTORE -r -y -f ${root_dump} > /dev/null ) |
132 |
|
133 |
if [ -n "${boot_dev}" ] ; then |
134 |
boot_dump="${IMAGE_FILE/root.dump/boot.dump}" |
135 |
if [ ! -f "$boot_dump" ] ; then |
136 |
log_error "Can't find image file: $boot_dump" |
137 |
exit 1 |
138 |
fi |
139 |
( cd ${TARGET}/boot; $RESTORE -r -y -f ${boot_dump} > /dev/null ) |
140 |
fi |
141 |
fi |
142 |
|
143 |
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then |
144 |
${OS_FAMILY}_setup_fstab $TARGET |
145 |
fi |
146 |
|
147 |
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then |
148 |
${OS_FAMILY}_setup_console $TARGET |
149 |
fi |
150 |
|
151 |
${OS_FAMILY}_epilogue $TARGET |
152 |
|
153 |
RUN_PARTS=`which run-parts` |
154 |
|
155 |
if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "${CUSTOMIZE_DIR}/$OS_FAMILY" ]; then |
156 |
TARGET=$TARGET |
157 |
BLOCKDEV=$blockdev |
158 |
ROOT_DEV=$root_dev |
159 |
BOOT_DEV=$boot_dev |
160 |
IMG_PASSWD=$IMG_PASSWD |
161 |
HOSTNAME=$instance |
162 |
export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE IMG_PASSWD HOSTNAME |
163 |
$RUN_PARTS $CUSTOMIZE_DIR/$OS_FAMILY |
164 |
fi |
165 |
fi |
166 |
|
167 |
# execute cleanups |
168 |
cleanup |
169 |
trap - EXIT |
170 |
|
171 |
exit 0 |