Statistics
| Branch: | Revision:

root / create @ 27e784fb

History | View | Annotate | Download (4.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
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
fi
33

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

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

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

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

    
64
    filesystem_dev=$(map_disk0 $blockdev)
65
    CLEANUP+=("unmap_disk0 $blockdev")
66
    root_dev=$(map_partition $filesystem_dev root)
67
    boot_dev=$(map_partition $filesystem_dev boot)
68
    swap_dev=$(map_partition $filesystem_dev swap)
69

    
70
    # Check partition existence
71
    if [ ! -e "$root_dev" ] ; then
72
        log_error "Root partition: $root_dev does not exist!"
73
        exit 1;
74
    fi
75
    if [ -n "$boot_dev" -a ! -e "$boot_dev" ] ; then
76
        log_error "Boot partition: $boot_dev does not exist!"
77
        exit 1;
78
    fi
79
    if [ -n "$swap_dev" -a ! -e "$swap_dev" ] ; then
80
        log_error "Swap partiion: $swap_dev does not exist!"
81
        exit 1;
82
    fi
83

    
84
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
85
        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
    # mount filesystems
95
    mount_disk0 $TARGET
96

    
97
    if [ "${IMAGE_TYPE}" = "tarball" ] ; then
98
        # unpack image
99
        tar pzxf $IMAGE_FILE -C $TARGET
100
    elif [ "${IMAGE_TYPE}" = "dump" ] ; then
101
        root_dump="${IMAGE_FILE}"
102
        ( cd ${TARGET}; restore -r -y -f ${root_dump} > /dev/null )
103

    
104
        if [ -n "${boot_dev}" ] ; then
105
            boot_dump="${IMAGE_FILE/root.dump/boot.dump}"
106
            if [ ! -f "$boot_dump" ] ; then
107
              log_error "Can't find image file: $boot_dump"
108
              exit 1
109
            fi
110
            ( cd ${TARGET}/boot; restore -r -y -f ${boot_dump} > /dev/null )
111
        fi
112
    fi
113

    
114
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
115
        setup_fstab $TARGET
116
    fi
117

    
118
    if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
119
        setup_console $TARGET
120
    fi
121

    
122
    epilogue $TARGET
123

    
124
    RUN_PARTS=`which run-parts`
125

    
126
    if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
127
      TARGET=$TARGET
128
      BLOCKDEV=$blockdev
129
      ROOT_DEV=$root_dev
130
      BOOT_DEV=$boot_dev
131
      IMG_PASSWD=$IMG_PASSWD
132
      HOSTNAME=$instance
133
      export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE IMG_PASSWD HOSTNAME
134
      $RUN_PARTS $CUSTOMIZE_DIR
135
    fi
136
fi
137

    
138
# execute cleanups
139
cleanup
140
trap - EXIT
141

    
142
exit 0