~rcj/vmbuilder/jenkins_kvm-test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
source "./functions/locker"

usage() {
cat << EOF
This program is used to convert raw images to VHD files.

    --suite:        the Ubuntu Code name to build against
    --source_file:  the name of the raw image file to convert
    --size:         the size of the converted image in G (defaults to 30G)
EOF
exit 1
}

# Defaults
vhd_size=30

# Command line parsing
short_opts="h"
long_opts="suite:,source_file:,size:"
getopt_out=$(getopt --name "${0##*/}" --options "${short_opts}"\
             --long "${long_opts}" -- "$@")
if [ $? -eq 0 ]; then
    eval set -- "${getopt_out}"
else
    usage
    exit 1
fi

while [ $# -ne 0 ]; do
    cur=${1}; next=${2};

    case "${cur}" in
        --size)         vhd_size="${2}"; shift;;
        --source_file)  source_file="${2}"; shift;;
        --suite)        suite="${2}"; shift;;
        -h|--help)      usage; exit 0;;
        ?)              usage; exit 1;;
        --) shift; break;;
    esac
    shift;
done

if [ -z "$source_file" -o -z "$suite" ]; then
    echo "--source_file and --suite required."
    exit 1
fi

raw_name=$(readlink -f "$source_file")
case ${suite} in
    precise|trusty|wily|xenial)
        vhd_name="${PWD}/${suite}-server-cloudimg-amd64-disk1.vhd"
        ;;
    *)
        vhd_name="${PWD}/${suite}-server-cloudimg-amd64.vhd"
        ;;
esac

# Copy the raw image to make it ready for VHD production
cp  --sparse=always "${raw_name}" "${raw_name}.pre-vhd" &&
    debug "Copied raw image VHD production" ||
    fail "Failed to copy raw image to ${raw_name}.pre-vhd"

# Resize the copied RAW image
debug "Truncating image to ${vhd_size}G"
truncate -s "${vhd_size}G" "${raw_name}.pre-vhd" &&
    debug "Truncated image at ${vhd_size}G" ||
    fail  "Failed to truncate disk image"

# Convert to VHD first, step 1 of cheap hack
# This is a cheap hack...half the time the next command
# will fail with "VERR_INVALID_PARAMETER", so this is the,
# er, workaround
debug "Converting to VHD"
_vbox_cmd convertfromraw --format VHD \
    "${raw_name}.pre-vhd" \
    "${vhd_name}.pre" &&
    debug "Converted raw disk to VHD" ||
    fail "Failed to convert raw image to VHD"

# Clone the disk to fixed, VHD for Azure
debug "Converting to VHD format from raw..."
debug ".....this might take a while...."
_vbox_cmd clonehd --format VHD --variant Fixed \
    "${vhd_name}.pre" \
    "${vhd_name}" &&
    debug "Converted raw disk to VHD format using VirtualBox" ||
    fail  "Failed to convert raw image to VHD disk!"

# Remove the unneeded files
rm "${vhd_name}.pre" "${raw_name}.pre-vhd"

debug "Image Characteristics:"
_vbox_cmd showhdinfo "${vhd_name}"


debug "Raw image converted to VHD"