Statistics
| Branch: | Revision:

root / create @ 59aff5e7

History | View | Annotate | Download (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
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
    # Check partition existence
85
    if [ ! -e "$root_dev" ] ; then
86
        log_error "Root partition: $root_dev does not exist!"
87
        exit 1;
88
    fi
89
    if [ -n "$boot_dev" -a ! -e "$boot_dev" ] ; then
90
        log_error "Boot partition: $boot_dev does not exist!"
91
        exit 1;
92
    fi
93
    if [ -n "$swap_dev" -a ! -e "$swap_dev" ] ; then
94
        log_error "Swap partiion: $swap_dev does not exist!"
95
        exit 1;
96
    fi
97

    
98
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
99
        ${OS_FAMILY}_mkfs_disk0
100
        root_uuid="$($VOL_ID $root_dev)"
101
        [ -n "$boot_dev" ] && sleep 1 && boot_uuid="$($VOL_ID $boot_dev)"
102
        [ -n "$swap_dev" ] && sleep 1 && swap_uuid="$($VOL_ID $swap_dev)"
103
    fi
104

    
105
    TARGET=`mktemp -d` || exit 1
106
    CLEANUP+=("rmdir $TARGET")
107

    
108
    if [ "${IMAGE_TYPE}" = "ntfsclone" ] ; then
109
        # Restore the ntfs image
110
        $NTFSCLONE --restore-image --overwrite $root_dev $IMAGE_FILE > /dev/null
111
        # Resize it to take over the whole patition
112
        $NTFSRESIZE -P $root_dev
113

    
114
    fi
115

    
116
    # mount filesystems
117
    mount_disk0 $TARGET
118

    
119
    if [ "${IMAGE_TYPE}" = "tarball" ] ; then
120
        # unpack image
121
        tar pzxf $IMAGE_FILE -C $TARGET
122
    elif [ "${IMAGE_TYPE}" = "dump" ] ; then
123
        root_dump="${IMAGE_FILE}"
124
        ( cd ${TARGET}; $RESTORE -r -y -f ${root_dump} > /dev/null )
125

    
126
        if [ -n "${boot_dev}" ] ; then
127
            boot_dump="${IMAGE_FILE/root.dump/boot.dump}"
128
            if [ ! -f "$boot_dump" ] ; then
129
              log_error "Can't find image file: $boot_dump"
130
              exit 1
131
            fi
132
            ( cd ${TARGET}/boot; $RESTORE -r -y -f ${boot_dump} > /dev/null )
133
        fi
134
    fi
135

    
136
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
137
        ${OS_FAMILY}_setup_fstab $TARGET
138
    fi
139

    
140
    if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
141
        ${OS_FAMILY}_setup_console $TARGET
142
    fi
143

    
144
    ${OS_FAMILY}_epilogue $TARGET
145

    
146
    RUN_PARTS=`which run-parts`
147

    
148
    if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "${CUSTOMIZE_DIR}/$OS_FAMILY" ]; then
149
      TARGET=$TARGET
150
      BLOCKDEV=$blockdev
151
      ROOT_DEV=$root_dev
152
      BOOT_DEV=$boot_dev
153
      IMG_PASSWD=$IMG_PASSWD
154
      HOSTNAME=$instance
155
      export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE IMG_PASSWD HOSTNAME
156
      $RUN_PARTS $CUSTOMIZE_DIR/$OS_FAMILY
157
    fi
158
fi
159

    
160
# execute cleanups
161
cleanup
162
trap - EXIT
163

    
164
exit 0