Statistics
| Branch: | Revision:

root / create @ bb78ba49

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
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
fi
35

    
36
. common_linux.sh
37

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

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

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

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

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

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

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

    
84
    # mount filesystems
85
    mount_disk0 $TARGET
86

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

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

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

    
108
    if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
109
        ${OS_FAMILY}_setup_console $TARGET
110
    fi
111

    
112
    ${OS_FAMILY}_filesystem_check $TARGET
113

    
114
    RUN_PARTS=`which run-parts`
115

    
116
    if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "${CUSTOMIZE_DIR}/$OS_FAMILY" ]; then
117
      TARGET=$TARGET
118
      BLOCKDEV=$blockdev
119
      ROOT_DEV=$root_dev
120
      BOOT_DEV=$boot_dev
121
      IMG_PASSWD=$IMG_PASSWD
122
      export TARGET SUITE BLOCKDEV ROOT_DEV BOOT_DEV IMAGE_TYPE IMG_PASSWD
123
      $RUN_PARTS $CUSTOMIZE_DIR/$OS_FAMILY
124
    fi
125
fi
126

    
127
# execute cleanups
128
cleanup
129
trap - EXIT
130

    
131
exit 0