Statistics
| Branch: | Tag: | Revision:

root / host / create @ cc4a63ae

History | View | Annotate | Download (3.5 kB)

1
#!/bin/bash
2

    
3
# Copyright 2011 GRNET S.A. All rights reserved.
4
#
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
7
# are met:
8
#
9
#   1. Redistributions of source code must retain the above copyright
10
#      notice, this list of conditions and the following disclaimer.
11
#
12
#  2. Redistributions in binary form must reproduce the above copyright
13
#     notice, this list of conditions and the following disclaimer in the
14
#     documentation and/or other materials provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
# SUCH DAMAGE.
27
#
28
# The views and conclusions contained in the software and documentation are
29
# those of the authors and should not be interpreted as representing official
30
# policies, either expressed or implied, of GRNET S.A.
31

    
32
set -e
33

    
34
. common.sh
35

    
36
case "$IMAGE_TYPE" in
37
    extdump)
38
	IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}-root.extdump";;
39
    ntfsdump)
40
        IMAGE_FILE="${IMAGE_DIR}/${IMAGE_NAME}-${ARCH}-root.ntfsdump";;
41
    *)
42
        log_error "Unknown image type: \`$IMAGE_TYPE'.";
43
        exit 1
44
esac
45

    
46
if [ ! -e "$IMAGE_FILE" ]; then
47
    log_error "Image file \`$IMAGE_FILE' does not exit."
48
    exit 1
49
fi
50

    
51
MONITOR="" #Empty if progress monitor support is disabled
52
if [ "$progress_monitor_support" = "yes" ]; then
53
    IMAGE_SIZE="$(stat -L -c %s ${IMAGE_FILE})"
54
    MONITOR="$PROGRESS_MONITOR -i ${INSTANCE_NAME} -r ${IMAGE_SIZE}"
55
fi
56

    
57
# If the target device is not a real block device we'll first losetup it.
58
# This is needed for file disks.
59
if [ ! -b $blockdev ]; then
60
    ORIGINAL_BLOCKDEV=$blockdev
61
    blockdev=$($LOSETUP -sf $blockdev)
62
    CLEANUP+=("$LOSETUP -d $blockdev")
63
fi
64

    
65
format_disk0 $blockdev ${IMAGE_TYPE}
66

    
67
filesystem_dev=$(map_disk0 $blockdev)
68
CLEANUP+=("unmap_disk0 $blockdev")
69

    
70
root_dev="${filesystem_dev}-1"
71

    
72
# dd the dump to its new home :-)
73
# Deploying an image file on a target block device is a streaming
74
# copy operation. Enable the direct I/O flag on the output fd to 
75
# avoid polluting the host cache with useless data.
76
$MONITOR dd bs=4M if=$IMAGE_FILE of=$root_dev oflag=direct
77

    
78
# Create a floppy image
79
floppy=$(mktemp --tmpdir floppy.XXXXXXXX) || exit 1
80
CLEANUP+=("rm -f $floppy")
81

    
82
snf_export_DEV=/dev/vda
83
snf_export_TYPE=${IMG_FORMAT}
84
snf_export_PASSWORD=${IMG_PASSWD}
85
snf_export_HOSTNAME=${instance}
86

    
87
create_floppy $floppy
88

    
89
# Invoke the helper vm to do the dirty job...
90
syslog=$(mktemp --tmpdir syslog.XXXXXXXX) || exit 1
91
CLEANUP+=("rm -f $syslog")
92
result=$(mktemp --tmpdir result.XXXXXXXX) || exit 1
93
CLEANUP+=("rm -f $result")
94

    
95
#kvm -drive file=${HELPER},snapshot=on,format=raw,if=virtio,boot=on \
96
#    -boot c -serial stdout -serial $syslog -serial $result -fda $floppy
97

    
98
# Install a new MBR
99
$INSTALL_MBR -p 1 -i n ${blockdev}
100

    
101
# Execute cleanups
102
cleanup
103
trap - EXIT
104

    
105
exit 0
106

    
107
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :