Statistics
| Branch: | Revision:

root / create @ 9ecbf7ad

History | View | Annotate | Download (5.8 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
case "$IMAGE_TYPE" in
29
tarball)
30
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.tar.gz"
31
    ;;
32
qemu)
33
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.img"
34
    ;;
35
dump)
36
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}-root.dump"
37
    # Workaround for restore's non-unique /tmp/rstdir* and /tmp/rstmode* 
38
    export TMPDIR=$(mktemp -d /tmp/${INSTANCE_NAME}_XXXXXXXX)
39
    CLEANUP+=("rmdir --ignore-fail-on-non-empty $TMPDIR")
40
    ;;
41
extdump)
42
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}-root.extdump"
43
    ;;
44
ntfsclone)
45
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.img"
46
    OS_FAMILY="windows"
47
    ;;
48
ntfsdump)
49
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.ntfsdump"
50
    OS_FAMILY="windows"
51
    ;;
52
*)
53
    log_error "Unknown image type.";
54
    exit 1
55
esac
56

    
57
    
58
MONITOR="" #Empty if progress monitor support is disabled
59
if [ "$progress_monitor_support" = "yes" ]; then
60
    IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})"
61
    MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}"
62
fi
63

    
64
if [ "$OS_FAMILY" = "linux" ]; then
65
    . common_linux.sh
66
elif [ "$OS_FAMILY" = "windows" ]; then
67
    . common_windows.sh
68
    SWAP="no"
69
fi
70

    
71
if [ "$CDINSTALL" = "no" ] ; then
72
    # If the target device is not a real block device we'll first losetup it.
73
    # This is needed for file disks.
74
    if [ ! -b $blockdev ]; then
75
      ORIGINAL_BLOCKDEV=$blockdev
76
      blockdev=$($LOSETUP -sf $blockdev)
77
      CLEANUP+=("$LOSETUP -d $blockdev")
78
    fi
79

    
80
    if [ ! -f "$IMAGE_FILE" ] ; then
81
      log_error "Can't find image file: $IMAGE_FILE"
82
      exit 1
83
    fi
84

    
85
    case "$IMAGE_TYPE" in
86
    tarball|dump|ntfsclone|ntfsdump|extdump)
87
        #manually create volumes, filesystems, etc
88
        ${OS_FAMILY}_format_disk0 $blockdev
89
        ;;
90
    qemu)
91
        # need a recent version of qemu for this
92
        ${QEMU_IMG} convert ${IMAGE_FILE} -O host_device ${blockdev} > /dev/null
93
        ;;
94
    esac
95

    
96
    # deploying something like a windows image, skip the rest
97
    if [ "${NOMOUNT}" = "yes" ] ; then
98
      cleanup
99
      exit 0
100
    fi
101

    
102
    filesystem_dev=$(map_disk0 $blockdev)
103
    CLEANUP+=("unmap_disk0 $blockdev")
104
    root_dev=$(map_partition $filesystem_dev root)
105
    boot_dev=$(map_partition $filesystem_dev boot)
106
    swap_dev=$(map_partition $filesystem_dev swap)
107

    
108
    # Check partition existence
109
    if [ ! -e "$root_dev" ] ; then
110
        log_error "Root partition: $root_dev does not exist!"
111
        exit 1;
112
    fi
113
    if [ -n "$boot_dev" -a ! -e "$boot_dev" ] ; then
114
        log_error "Boot partition: $boot_dev does not exist!"
115
        exit 1;
116
    fi
117
    if [ -n "$swap_dev" -a ! -e "$swap_dev" ] ; then
118
        log_error "Swap partiion: $swap_dev does not exist!"
119
        exit 1;
120
    fi
121

    
122
    # Before mounting
123
    case "${IMAGE_TYPE}" in
124
    tarball|dump)
125
        ${OS_FAMILY}_mkfs_disk0
126
        root_uuid="$($VOL_ID $root_dev)"
127
        [ -n "$boot_dev" ] && sleep 1 && boot_uuid="$($VOL_ID $boot_dev)"
128
        [ -n "$swap_dev" ] && sleep 1 && swap_uuid="$($VOL_ID $swap_dev)"
129
        ;;
130
    ntfsclone)
131
        # Restore the ntfs image
132
        $MONITOR $NTFSCLONE --restore-image --overwrite $root_dev $IMAGE_FILE \
133
            > /dev/null
134
        ;;
135
    ntfsdump|extdump)
136
        # dd the dump to its new home :-)
137
        # Deploying an image file on a target block device is a streaming
138
        # copy operation. Enable the direct I/O flag on the output fd to 
139
        # avoid polluting the host cache with useless data.
140
        $MONITOR dd bs=4M if=$IMAGE_FILE of=$root_dev oflag=direct
141
        # resize the partition
142
        [ "${IMAGE_TYPE}" = "extdump" ] && $RESIZE2FS $root_dev
143
        ;;
144
    esac
145

    
146
    TARGET=`mktemp -d` || exit 1
147
    CLEANUP+=("rmdir $TARGET")
148

    
149
    # mount filesystems
150
    mount_disk0 $TARGET
151

    
152
    if [ "${IMAGE_TYPE}" = "tarball" ] ; then
153
        # unpack image
154
        tar pzxf $IMAGE_FILE -C $TARGET
155
    elif [ "${IMAGE_TYPE}" = "dump" ] ; then
156
        root_dump="${IMAGE_FILE}"
157
        ( cd ${TARGET}; $MONITOR $RESTORE -r -y -f ${root_dump} > /dev/null )
158

    
159
        if [ -n "${boot_dev}" ] ; then
160
            boot_dump="${IMAGE_FILE/root.dump/boot.dump}"
161
            if [ ! -f "$boot_dump" ] ; then
162
              log_error "Can't find image file: $boot_dump"
163
              exit 1
164
            fi
165
            ( cd ${TARGET}/boot; $RESTORE -r -y -f ${boot_dump} > /dev/null )
166
        fi
167
    fi 
168

    
169
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
170
        ${OS_FAMILY}_setup_fstab $TARGET
171
    fi
172

    
173
    if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
174
        ${OS_FAMILY}_setup_console $TARGET
175
    fi
176

    
177
    ${OS_FAMILY}_epilogue $TARGET
178

    
179
    RUN_PARTS=`which run-parts`
180

    
181
    if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "${CUSTOMIZE_DIR}/$OS_FAMILY" ]; then
182
      TARGET=$TARGET
183
      BLOCKDEV=$blockdev
184
      ROOT_DEV=$root_dev
185
      BOOT_DEV=$boot_dev
186
      SWAP_DEV=$swap_dev
187
      IMG_PASSWD=$IMG_PASSWD
188
      HOSTNAME=$instance
189
      export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV SWAP_DEV IMAGE_TYPE IMG_PASSWD HOSTNAME
190
      $RUN_PARTS $CUSTOMIZE_DIR/$OS_FAMILY
191
    fi
192
fi
193

    
194
# execute cleanups
195
cleanup
196
trap - EXIT
197

    
198
exit 0
199