Statistics
| Branch: | Revision:

root / create @ f73b7ba5

History | View | Annotate | Download (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
if [ "$IMAGE_TYPE" = "tarball" ] ; then
27
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.tar.gz"
28
elif [ "$IMAGE_TYPE" = "qemu" ] ; then
29
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}.img"
30
elif [ "$IMAGE_TYPE" = "dump" ] ; then
31
    IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}-root.dump"
32
    # Workaround for restore's non-unique /tmp/rstdir* and /tmp/rstmode*
33
    export TMPDIR=$(mktemp -d /tmp/${INSTANCE_NAME}_XXXXXXXX)
34
    CLEANUP+=("rmdir --ignore-fail-on-non-empty $TMPDIR")
35
fi
36

    
37
if [ "$CDINSTALL" = "no" ] ; then
38
    # If the target device is not a real block device we'll first losetup it.
39
    # This is needed for file disks.
40
    if [ ! -b $blockdev ]; then
41
      ORIGINAL_BLOCKDEV=$blockdev
42
      blockdev=$($LOSETUP -sf $blockdev)
43
      CLEANUP+=("$LOSETUP -d $blockdev")
44
    fi
45

    
46
    if [ ! -f "$IMAGE_FILE" ] ; then
47
      log_error "Can't find image file: $IMAGE_FILE"
48
      exit 1
49
    fi
50

    
51
    # If the image is tarball based, then we need to manually create the
52
    # volumes, filesystems, etc
53
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
54
        # Create 3 partitions, /boot, swap, & /
55
        format_disk0 $blockdev
56
    elif [ "${IMAGE_TYPE}" = "qemu" ] ; then
57
        # need a recent version of qemu for this
58
        ${QEMU_IMG} convert ${IMAGE_FILE} -O host_device ${blockdev} > /dev/null
59
    fi
60

    
61
    # deploying something like a windows image, skip the rest
62
    if [ "${NOMOUNT}" = "yes" ] ; then
63
      cleanup
64
      exit 0
65
    fi
66

    
67
    filesystem_dev=$(map_disk0 $blockdev)
68
    CLEANUP+=("unmap_disk0 $blockdev")
69
    root_dev=$(map_partition $filesystem_dev root)
70
    boot_dev=$(map_partition $filesystem_dev boot)
71
    swap_dev=$(map_partition $filesystem_dev swap)
72

    
73
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
74
        mkfs_disk0
75
        root_uuid="$($VOL_ID $root_dev)"
76
        [ -n "$boot_dev" ] && sleep 1 && boot_uuid="$($VOL_ID $boot_dev)"
77
        [ -n "$swap_dev" ] && sleep 1 && swap_uuid="$($VOL_ID $swap_dev)"
78
    fi
79

    
80
    TARGET=`mktemp -d` || exit 1
81
    CLEANUP+=("rmdir $TARGET")
82

    
83
    # mount filesystems
84
    mount_disk0 $TARGET
85

    
86
    if [ "${IMAGE_TYPE}" = "tarball" ] ; then
87
        # unpack image
88
        tar pzxf $IMAGE_FILE -C $TARGET
89
    elif [ "${IMAGE_TYPE}" = "dump" ] ; then
90
        root_dump="${IMAGE_FILE}"
91
        ( cd ${TARGET}; restore -r -y -f ${root_dump} > /dev/null )
92

    
93
        if [ -n "${boot_dev}" ] ; then
94
            boot_dump="${IMAGE_FILE/root.dump/boot.dump}"
95
            if [ ! -f "$boot_dump" ] ; then
96
              log_error "Can't find image file: $boot_dump"
97
              exit 1
98
            fi
99
            ( cd ${TARGET}/boot; restore -r -y -f ${boot_dump} > /dev/null )
100
        fi
101
    fi
102

    
103
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
104
        setup_fstab $TARGET
105
    fi
106

    
107
    if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
108
        setup_console $TARGET
109
    fi
110

    
111
    RUN_PARTS=`which run-parts`
112

    
113
    if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
114
      TARGET=$TARGET
115
      BLOCKDEV=$blockdev
116
      ROOT_DEV=$root_dev
117
      BOOT_DEV=$boot_dev
118
      export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE
119
      $RUN_PARTS $CUSTOMIZE_DIR
120
    fi
121
fi
122

    
123
# execute cleanups
124
cleanup
125
trap - EXIT
126

    
127
exit 0