Statistics
| Branch: | Revision:

root / create @ e34560c8

History | View | Annotate | Download (5.5 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
    # Workaround for restore's non-unique /tmp/rstdir* and /tmp/rstmode* 
35
    export TMPDIR=$(mktemp -d /tmp/${INSTANCE_NAME}_XXXXXXXX)
36
    CLEANUP+=("rmdir --ignore-fail-on-non-empty $TMPDIR")
37
elif [ "$IMAGE_TYPE" = "ntfsclone" ]; then
38
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.img"
39
    OS_FAMILY="windows"
40
fi
41

    
42
MONITOR="" #Empty if progress monitor support is disabled
43
if [ "$progress_monitor_support" = "yes" ]; then
44
    IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})"
45
    MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}"
46
fi
47

    
48
if [ "$OS_FAMILY" = "linux" ]; then
49
    . common_linux.sh
50
elif [ "$OS_FAMILY" = "windows" ]; then
51
    . common_windows.sh
52
    SWAP="no"
53
fi
54

    
55
if [ "$CDINSTALL" = "no" ] ; then
56
    # If the target device is not a real block device we'll first losetup it.
57
    # This is needed for file disks.
58
    if [ ! -b $blockdev ]; then
59
      ORIGINAL_BLOCKDEV=$blockdev
60
      blockdev=$($LOSETUP -sf $blockdev)
61
      CLEANUP+=("$LOSETUP -d $blockdev")
62
    fi
63

    
64
    if [ ! -f "$IMAGE_FILE" ] ; then
65
      log_error "Can't find image file: $IMAGE_FILE"
66
      exit 1
67
    fi
68

    
69
    # If the image is tarball based, then we need to manually create the
70
    # volumes, filesystems, etc
71
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
72
        # Create 3 partitions, /boot, swap, & /
73
        ${OS_FAMILY}_format_disk0 $blockdev
74
    elif [ "${IMAGE_TYPE}" = "qemu" ] ; then
75
        # need a recent version of qemu for this
76
        ${QEMU_IMG} convert ${IMAGE_FILE} -O host_device ${blockdev} > /dev/null
77
    elif [ "${IMAGE_TYPE}" = "ntfsclone" ] ; then
78
        ${OS_FAMILY}_format_disk0 $blockdev
79
    fi
80

    
81
    # deploying something like a windows image, skip the rest
82
    if [ "${NOMOUNT}" = "yes" ] ; then
83
      cleanup
84
      exit 0
85
    fi
86

    
87
    filesystem_dev=$(map_disk0 $blockdev)
88
    CLEANUP+=("unmap_disk0 $blockdev")
89
    root_dev=$(map_partition $filesystem_dev root)
90
    boot_dev=$(map_partition $filesystem_dev boot)
91
    swap_dev=$(map_partition $filesystem_dev swap)
92

    
93
    # Check partition existence
94
    if [ ! -e "$root_dev" ] ; then
95
        log_error "Root partition: $root_dev does not exist!"
96
        exit 1;
97
    fi
98
    if [ -n "$boot_dev" -a ! -e "$boot_dev" ] ; then
99
        log_error "Boot partition: $boot_dev does not exist!"
100
        exit 1;
101
    fi
102
    if [ -n "$swap_dev" -a ! -e "$swap_dev" ] ; then
103
        log_error "Swap partiion: $swap_dev does not exist!"
104
        exit 1;
105
    fi
106

    
107
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
108
        ${OS_FAMILY}_mkfs_disk0
109
        root_uuid="$($VOL_ID $root_dev)"
110
        [ -n "$boot_dev" ] && sleep 1 && boot_uuid="$($VOL_ID $boot_dev)"
111
        [ -n "$swap_dev" ] && sleep 1 && swap_uuid="$($VOL_ID $swap_dev)"
112
    fi
113

    
114
    TARGET=`mktemp -d` || exit 1
115
    CLEANUP+=("rmdir $TARGET")
116

    
117
    if [ "${IMAGE_TYPE}" = "ntfsclone" ] ; then
118
        # Restore the ntfs image
119
        $MONITOR $NTFSCLONE --restore-image --overwrite $root_dev $IMAGE_FILE \
120
            > /dev/null
121
        # Resize it to take over the whole patition
122
        $NTFSRESIZE -P $root_dev
123

    
124
    fi
125

    
126
    # mount filesystems
127
    mount_disk0 $TARGET
128

    
129
    if [ "${IMAGE_TYPE}" = "tarball" ] ; then
130
        # unpack image
131
        tar pzxf $IMAGE_FILE -C $TARGET
132
    elif [ "${IMAGE_TYPE}" = "dump" ] ; then
133
        root_dump="${IMAGE_FILE}"
134
        ( cd ${TARGET}; $MONITOR $RESTORE -r -y -f ${root_dump} > /dev/null )
135

    
136
        if [ -n "${boot_dev}" ] ; then
137
            boot_dump="${IMAGE_FILE/root.dump/boot.dump}"
138
            if [ ! -f "$boot_dump" ] ; then
139
              log_error "Can't find image file: $boot_dump"
140
              exit 1
141
            fi
142
            ( cd ${TARGET}/boot; $RESTORE -r -y -f ${boot_dump} > /dev/null )
143
        fi
144
    fi
145

    
146
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
147
        ${OS_FAMILY}_setup_fstab $TARGET
148
    fi
149

    
150
    if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
151
        ${OS_FAMILY}_setup_console $TARGET
152
    fi
153

    
154
    ${OS_FAMILY}_epilogue $TARGET
155

    
156
    RUN_PARTS=`which run-parts`
157

    
158
    if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "${CUSTOMIZE_DIR}/$OS_FAMILY" ]; then
159
      TARGET=$TARGET
160
      BLOCKDEV=$blockdev
161
      ROOT_DEV=$root_dev
162
      BOOT_DEV=$boot_dev
163
      IMG_PASSWD=$IMG_PASSWD
164
      HOSTNAME=$instance
165
      export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE IMG_PASSWD HOSTNAME
166
      $RUN_PARTS $CUSTOMIZE_DIR/$OS_FAMILY
167
    fi
168
fi
169

    
170
# execute cleanups
171
cleanup
172
trap - EXIT
173

    
174
exit 0