Statistics
| Branch: | Revision:

root / create @ e9568fe9

History | View | Annotate | Download (4.9 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
        ntfsclone --restore-image --overwrite $root_dev $IMAGE_FILE
110
    fi
111

    
112
    # mount filesystems
113
    mount_disk0 $TARGET
114

    
115
    if [ "${IMAGE_TYPE}" = "tarball" ] ; then
116
        # unpack image
117
        tar pzxf $IMAGE_FILE -C $TARGET
118
    elif [ "${IMAGE_TYPE}" = "dump" ] ; then
119
        root_dump="${IMAGE_FILE}"
120
        ( cd ${TARGET}; restore -r -y -f ${root_dump} > /dev/null )
121

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

    
132
    if [ "${IMAGE_TYPE}" = "tarball" -o "${IMAGE_TYPE}" = "dump" ] ; then
133
        ${OS_FAMILY}_setup_fstab $TARGET
134
    fi
135

    
136
    if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
137
        ${OS_FAMILY}_setup_console $TARGET
138
    fi
139

    
140
    ${OS_FAMILY}_filesystem_check $TARGET
141

    
142
    RUN_PARTS=`which run-parts`
143

    
144
    if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "${CUSTOMIZE_DIR}/$OS_FAMILY" ]; then
145
      TARGET=$TARGET
146
      BLOCKDEV=$blockdev
147
      ROOT_DEV=$root_dev
148
      BOOT_DEV=$boot_dev
149
      IMG_PASSWD=$IMG_PASSWD
150
      export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE IMG_PASSWD
151
      $RUN_PARTS $CUSTOMIZE_DIR/$OS_FAMILY
152
    fi
153
fi
154

    
155
# execute cleanups
156
cleanup
157
trap - EXIT
158

    
159
exit 0