root / create @ a33d2ab2
History | View | Annotate | Download (4.4 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 |
if [ "$OS_FAMILY" = "linux" ]; then |
40 |
. common_linux.sh |
41 |
elif [ "$OS_FAMILY" = "windows" ]; then |
42 |
. common_windows.sh |
43 |
SWAP="no" |
44 |
fi |
45 |
|
46 |
if [ "$CDINSTALL" = "no" ] ; then |
47 |
# If the target device is not a real block device we'll first losetup it. |
48 |
# This is needed for file disks. |
49 |
if [ ! -b $blockdev ]; then |
50 |
ORIGINAL_BLOCKDEV=$blockdev |
51 |
blockdev=$($LOSETUP -sf $blockdev) |
52 |
CLEANUP+=("$LOSETUP -d $blockdev") |
53 |
fi |
54 |
|
55 |
if [ ! -f "$IMAGE_FILE" ] ; then |
56 |
log_error "Can't find image file: $IMAGE_FILE" |
57 |
exit 1 |
58 |
fi |
59 |
|
60 |
# If the image is tarball based, then we need to manually create the |
61 |
# volumes, filesystems, etc |
62 |
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then |
63 |
# Create 3 partitions, /boot, swap, & / |
64 |
${OS_FAMILY}_format_disk0 $blockdev |
65 |
elif [ "${IMAGE_TYPE}" = "qemu" ] ; then |
66 |
# need a recent version of qemu for this |
67 |
${QEMU_IMG} convert ${IMAGE_FILE} -O host_device ${blockdev} > /dev/null |
68 |
elif [ "${IMAGE_TYPE}" = "ntfsclone" ] ; then |
69 |
${OS_FAMILY}_format_disk0 $blockdev |
70 |
fi |
71 |
|
72 |
# deploying something like a windows image, skip the rest |
73 |
if [ "${NOMOUNT}" = "yes" ] ; then |
74 |
cleanup |
75 |
exit 0 |
76 |
fi |
77 |
|
78 |
filesystem_dev=$(map_disk0 $blockdev) |
79 |
CLEANUP+=("unmap_disk0 $blockdev") |
80 |
root_dev=$(map_partition $filesystem_dev root) |
81 |
boot_dev=$(map_partition $filesystem_dev boot) |
82 |
swap_dev=$(map_partition $filesystem_dev swap) |
83 |
|
84 |
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then |
85 |
${OS_FAMILY}_mkfs_disk0 |
86 |
root_uuid="$($VOL_ID $root_dev)" |
87 |
[ -n "$boot_dev" ] && sleep 1 && boot_uuid="$($VOL_ID $boot_dev)" |
88 |
[ -n "$swap_dev" ] && sleep 1 && swap_uuid="$($VOL_ID $swap_dev)" |
89 |
fi |
90 |
|
91 |
TARGET=`mktemp -d` || exit 1 |
92 |
CLEANUP+=("rmdir $TARGET") |
93 |
|
94 |
if [ "${IMAGE_TYPE}" = "ntfsclone" ] ; then |
95 |
ntfsclone --restore-image --overwrite $root_dev $IMAGE_FILE |
96 |
fi |
97 |
|
98 |
# mount filesystems |
99 |
mount_disk0 $TARGET |
100 |
|
101 |
if [ "${IMAGE_TYPE}" = "tarball" ] ; then |
102 |
# unpack image |
103 |
tar pzxf $IMAGE_FILE -C $TARGET |
104 |
elif [ "${IMAGE_TYPE}" = "dump" ] ; then |
105 |
root_dump="${IMAGE_FILE}" |
106 |
( cd ${TARGET}; restore -r -y -f ${root_dump} > /dev/null ) |
107 |
|
108 |
if [ -n "${boot_dev}" ] ; then |
109 |
boot_dump="${IMAGE_FILE/root.dump/boot.dump}" |
110 |
if [ ! -f "$boot_dump" ] ; then |
111 |
log_error "Can't find image file: $boot_dump" |
112 |
exit 1 |
113 |
fi |
114 |
( cd ${TARGET}/boot; restore -r -y -f ${boot_dump} > /dev/null ) |
115 |
fi |
116 |
fi |
117 |
|
118 |
if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then |
119 |
${OS_FAMILY}_setup_fstab $TARGET |
120 |
fi |
121 |
|
122 |
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then |
123 |
${OS_FAMILY}_setup_console $TARGET |
124 |
fi |
125 |
|
126 |
${OS_FAMILY}_filesystem_check $TARGET |
127 |
|
128 |
RUN_PARTS=`which run-parts` |
129 |
|
130 |
if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "${CUSTOMIZE_DIR}/$OS_FAMILY" ]; then |
131 |
TARGET=$TARGET |
132 |
BLOCKDEV=$blockdev |
133 |
ROOT_DEV=$root_dev |
134 |
BOOT_DEV=$boot_dev |
135 |
IMG_PASSWD=$IMG_PASSWD |
136 |
export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE IMG_PASSWD |
137 |
$RUN_PARTS $CUSTOMIZE_DIR/$OS_FAMILY |
138 |
fi |
139 |
fi |
140 |
|
141 |
# execute cleanups |
142 |
cleanup |
143 |
trap - EXIT |
144 |
|
145 |
exit 0 |