~toabctl/vmbuilder/automated-ec2-builds-move-to-git

« back to all changes in this revision

Viewing changes to ovf/armel2qemu

  • Committer: Thomas Bechtold
  • Date: 2021-11-08 17:41:33 UTC
  • Revision ID: thomas.bechtold@canonical.com-20211108174133-rrmf5tdru036aafv
Drop all code & update README with the new url

The code moved to git, so drop all code and
mention in the README the new URL and that
the code from this repo is still there,
just in the 2nd oldest revision.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
cmd_args=$*
3
 
base_d=$(dirname $(readlink -f "${0}"))
4
 
 
5
 
 
6
 
# General functions
7
 
Usage() {
8
 
    cat <<EOF
9
 
${0##*/} - Create QEMU or hardware capable ARMEL images
10
 
 
11
 
Useage:
12
 
    --type [raw|qcow2]) Image format type
13
 
    --mlo)              MLO firmware for ARM hardware
14
 
    --uboot)            u-boot binary
15
 
    --rootfs)           Image containing root file system to put into new image
16
 
    --disk)             Name of final image
17
 
    --label)            Rootfs label
18
 
 
19
 
    Example: ${0##*/} --type qcow2 --mlo MLO -uboot u-boot.bin --rootfs mfs.img --disk new.img
20
 
 
21
 
EOF
22
 
}
23
 
bad_Usage() { Usage 1>&2; fail "$@"; }
24
 
 
25
 
debug() { error "$(date -R):" "$@"; }
26
 
 
27
 
error() { echo -e "$@" 1>&2; }
28
 
 
29
 
do_done() {
30
 
    # done on exit or fail
31
 
 
32
 
    # Unmount file-systems
33
 
    [ -z "${rootfs_dm}" ]        || { umount "${rootfs_dm}" 2> /dev/null; }
34
 
    [ -z "${bootfs_dm}" ]        || { umount "${bootfs_dm}" 2> /dev/null; }
35
 
    [ -z "${copy_rootfs_dm}" ]   || { umount "${copy_rootfs_dm}" 2> /dev/null; }
36
 
 
37
 
    # Clean up the device mappings.
38
 
    # Due to bugginess in kpartx on lucid, dmsetup is being used first to remove devices
39
 
    #     and then fallback to kpartx just in case.
40
 
    [ -z "${copy_rootfs_loop}" ] || {
41
 
        local loop_dev=$(echo $copy_rootfs_loop | awk -F\/ '{print$NF}')
42
 
        find /dev/mapper -iname "${loop_dev}*" | xargs -n1  -I DEVICE dmsetup remove DEVICE ||
43
 
            kpartx -d "${copy_rootfs_loop}" ||
44
 
                echo "Failed to remove device mapper devices for ${copy_rootfs_loop}"
45
 
        losetup -d "${copy_rootfs_loop}";
46
 
    }
47
 
 
48
 
    [ -z "${loop}" ] || {
49
 
        local loop_dev=$(echo ${loop} | awk -F\/ '{print$NF}')
50
 
        find /dev/mapper -iname "${loop_dev}*" | xargs -n1  -I DEVICE dmsetup remove DEVICE ||
51
 
            kpartx -d "${loop}" ||
52
 
                echo "Failed to remove device mapper devices for ${loop}"
53
 
        losetup -d "${loop}" ||
54
 
            echo "Failed to remove loop device ${loop}"
55
 
    }
56
 
 
57
 
    # Clean up temporary directories
58
 
    [ -z "${TEMPDIR}"   ] || rm -rf "${TEMPDIR}"
59
 
    [ -z "${mp_rootfs}" ] || rm -rf "${mp_rootfs}"
60
 
    [ -z "${mp_uboot}"  ] ||  rm -rf "${mp_uboot}"
61
 
}
62
 
 
63
 
fail() {
64
 
    [ $# -eq 0 ] || {
65
 
        error "$(date -R): $@";
66
 
        do_done
67
 
        exit 1;
68
 
    }
69
 
}
70
 
 
71
 
check_prog() {
72
 
    which $1 > /dev/null >&1 ||
73
 
        fail "Sorry, you need to have $1 installed"
74
 
}
75
 
 
76
 
need_file() {
77
 
   fail "Unable to find required file $@"
78
 
}
79
 
 
80
 
short_opts="h"
81
 
long_opts="disk:,rootfs:,mlo:,uboot:,type:,label:"
82
 
getopt_out=$(getopt --name "${0##*/}" \
83
 
    --options "${short_opts}" --long "${long_opts}" -- "$@") &&
84
 
    eval set -- "${getopt_out}" ||
85
 
    bad_Usage
86
 
 
87
 
# Basic control variables that are required
88
 
img=""              # Base working image
89
 
mlo_firmware=""     # MLO firmware file
90
 
uboot_firmware=""   # u-boot firmware
91
 
stuff_img=""        # Rootfs to stuff into image
92
 
img_type=""         # Image format type
93
 
 
94
 
while [ $# -ne 0 ]; do
95
 
    case "${1}" in
96
 
        --disk)   img="${2}.raw";         shift;;
97
 
        --rootfs) stuff_img="${2}";       shift;;
98
 
        --mlo)    mlo_firmware="${2}";    shift;;
99
 
        --uboot)  uboot_firmware="${2}";  shift;;
100
 
        --type)   img_type="${2}";        shift;;
101
 
        --label)  img_label="${2}";       shift;;
102
 
        -h)     Usage; exit 0;;
103
 
        --)     shift; break;;
104
 
    esac
105
 
    shift
106
 
done
107
 
 
108
 
[ -n "${mlo_firmware}" ]   || need_file "${mlo_firmware} (identified as MLO firmware)"
109
 
[ -n "${uboot_firmware}" ] || need_file "${uboot_firmware} (identified as u-boot firmware)"
110
 
[ -n "${stuff_img}" ]      || need_file "${stuff_img} (identified as root file system)"
111
 
[ -n "${img}" ]            || fail "--disk <NAME> is required"
112
 
[ -n "${img_type}" ]       || fail "--type <[raw|qcow2]> is required"
113
 
 
114
 
[ "${img_type}" = "qcow2" -o "${img_type}" = "raw" ] ||
115
 
    fail "Valid types are raw or qcow2."
116
 
 
117
 
# Clean up the image name
118
 
final_img="${img//.raw/}"
119
 
working_stuff_img=""
120
 
mp_uboot=$(mktemp -d)
121
 
mp_rootfs=""
122
 
copy_rootfs_dm=""
123
 
copy_rootfs_loop=""
124
 
bootfs_dm=""
125
 
rootfs_dm=""
126
 
loop=""
127
 
rootfs_dm=""
128
 
rootfs_label=""
129
 
TEMPDIR=${TEMPDIR:-$(mktemp -d )}
130
 
uimage="${TEMPDIR}/uImage"
131
 
bootscr="${TEMPDIR}/boot.scr"
132
 
uinitrd="${TEMPDIR}/uInitrd"
133
 
kernel="${TEMPDIR}/vmlinuz"
134
 
initrd="${TEMPDIR}/initrd"
135
 
 
136
 
debug "IMAGE PARAMETERS: "
137
 
debug "   Image with rootfs: ${stuff_img}"
138
 
debug "        MLO Firmware: ${mlo_firmware}"
139
 
debug "     u-boot Firmware: ${uboot_firmware}"
140
 
debug "          Image Type: ${img_type}"
141
 
debug "         Final Image: ${final_img}"
142
 
 
143
 
 
144
 
# Support multiple formats
145
 
convert_image() {
146
 
  file_name="${TEMPDIR}/$(echo $stuff_img | awk -F\/ '{print$NF}')"
147
 
  if [ "$( file ${stuff_img}  | awk '/Qemu Image/ {print$NF}' )" = "2" ]; then
148
 
      debug "Converting ${stuff_img} to ${file_name}.raw "
149
 
      qemu-img convert -O raw ${stuff_img} "${file_name}.raw" ||
150
 
        fail "Unable to convert disk image"
151
 
      working_stuff_img="${file_name}.raw"
152
 
  elif [ "$( file ${stuff_img}  | awk '/VMware/ {print$2}' )" = "VMware4" ]; then
153
 
      debug "Converting ${stuff_img} to ${file_name}.raw "
154
 
      vboxmanage clonehd --format RAW ${stuff_img} "${file_name}.raw" ||
155
 
        fail "Unable to convert disk image"
156
 
      working_stuff_img="${file_name}.raw"
157
 
  else
158
 
      working_stuff_img="${stuff_img}"
159
 
  fi
160
 
}
161
 
 
162
 
# Make the boot files
163
 
mk_boot_files() {
164
 
 
165
 
## DON'T CHANGE THESE VALUES! IT WON'T WORK!
166
 
        cat << END >> ${TEMPDIR}/boot.cmd
167
 
setenv dvimode 1280x720MR-16@60
168
 
setenv vram 12MB
169
 
setenv bootcmd 'fatload mmc 0:1 0x80300000 uImage; fatload mmc 0:1 0x81600000 uInitrd; bootm 0x80300000 0x81600000'
170
 
setenv bootargs console=ttyO2 omapfb.mode=dvi root=LABEL=${rootfs_label:-cloudimg-rootfs} rw fixrtc
171
 
boot
172
 
END
173
 
 
174
 
    debug "Starting to build boot images"
175
 
    debug "Making kernel image"
176
 
        mkimage -A arm -O linux -T kernel -C none -a 0x80300000 -e 0x80300000 -n "Linux" -d "${kernel}" "${uimage}" ||
177
 
        fail "Failed to create uImage"
178
 
 
179
 
    debug "Making initrd image"
180
 
        mkimage -A arm -O linux -T ramdisk -C none -a 0x81600000 -e 0x81600000 -n "Initrd" -d "${initrd}" "${uinitrd}" ||
181
 
        fail "Failed to create uInitrd"
182
 
 
183
 
    debug "Making boot.cmd image"
184
 
        mkimage -A arm -O linux -T script -C none -a 0 -e 0 -n "Ubuntu" -d "${TEMPDIR}/boot.cmd" "${bootscr}" ||
185
 
        fail "Failed to create boot.scr"
186
 
 
187
 
    [ -e "${uimage}" ]  || fail "Failed to find ${uimage}"
188
 
    [ -e "${uinitrd}" ] || fail "Failed to find ${uinitrd}"
189
 
    [ -e "${bootscr}" ] || fail "Failed to find ${bootscr}"
190
 
 
191
 
    debug "Finished making boot images"
192
 
}
193
 
 
194
 
# build the disk
195
 
mk_disk() {
196
 
    debug "Creating disk image...may take a while"
197
 
    [ -e "${img}" ] || {
198
 
            dd if=/dev/zero of=${img} bs=1M count=2120
199
 
            debug "Created ${img}"
200
 
         } || fail "Destination disk image already exists, bailing"
201
 
 
202
 
sfdisk --no-reread --force -uM -H255 -S63 ${img} <<END_DISK
203
 
0,64,c,*
204
 
,
205
 
END_DISK
206
 
 
207
 
        loop_raw="$(kpartx -v -a ${img} )" || fail "Failed to add image via kpartx"
208
 
    debug "Setup raw destination disk"
209
 
    echo -e "${loop_raw}"
210
 
 
211
 
 
212
 
    sfdisk -l ${img}
213
 
 
214
 
    #Extract information about loop
215
 
        loop="$(echo -e ${loop_raw} |  head -n1 | awk '{print$8}')" ||
216
 
                fail "Unable to determine loop device"
217
 
        bootfs_dm="/dev/mapper${loop///dev/}p1"
218
 
        rootfs_dm="/dev/mapper${loop///dev/}p2"
219
 
 
220
 
    debug "Boot partition setup on ${bootfs_dm}"
221
 
    debug "Root partition setup on ${rootfs_dm}"
222
 
}
223
 
 
224
 
copy_or_fail() {
225
 
    file_base="$(echo $1 | awk -F\/ '{print$NF}')"
226
 
    debug "Copying ${file_base}"
227
 
    cp $1 $2 || fail "failed to copy ${1} to ${2}"
228
 
    [ -e "${2}" ] || fail "${file_base} is not found on destination file system"
229
 
}
230
 
 
231
 
create_boot_fs() {
232
 
    # boot fs must be 32-bit vFAT
233
 
        mkfs.msdos -F32 -v "${bootfs_dm}" 2> /dev/null ||
234
 
        fail "Failed to create boot file system"
235
 
 
236
 
    [ -e "${mp_uboot}" -o -z  "${bootfs_dm}" ] && mount "${bootfs_dm}" "${mp_uboot}" ||
237
 
        fail "Unable to mount uboot partition"
238
 
 
239
 
    copy_or_fail "${mlo_firmware}" "${mp_uboot}/MLO"
240
 
    copy_or_fail "${uboot_firmware}" "${mp_uboot}/u-boot.bin"
241
 
    copy_or_fail "${uimage}" "${mp_uboot}/uImage"
242
 
    copy_or_fail "${uinitrd}" "${mp_uboot}/uInitrd"
243
 
    copy_or_fail "${bootscr}" "${mp_uboot}/boot.scr"
244
 
    sync
245
 
 
246
 
    debug "Contents of u-boot device"
247
 
    ls -lh1 "${mp_uboot}"
248
 
}
249
 
 
250
 
stuff_image() {
251
 
   [ -e "${working_stuff_img}" ] ||
252
 
        fail "Unable to find the working copy of root file system image"
253
 
 
254
 
   # Set up image for extacting kernel and initrd
255
 
   mp_rootfs=$(mktemp -d) ||
256
 
        fail "Unable to create temp mont point for rootfs"
257
 
   copy_root_raw=$(kpartx -a -v "${working_stuff_img}" ) ||
258
 
        fail "Unable to map rootfs"
259
 
 
260
 
   debug "Setup Source FS:"
261
 
   echo -e "${copy_root_raw}"
262
 
 
263
 
   # Determine devices of rootfs image and mount
264
 
   copy_rootfs_loop=$(echo ${copy_root_raw} | head -n 1 | awk '{print$8}' ) ||
265
 
        fail "Unable to determine copy_rootfs_loop"
266
 
   # Assume that the last partition has the root file system
267
 
   copy_rootfs_dm=$(echo ${copy_root_raw} | tail -n1 | awk '{print"/dev/mapper/"$3}' ) ||
268
 
        fail "Unable to determine copy_rootfs_dm"
269
 
 
270
 
   debug "Source rootfs loop devices is ${copy_rootfs_loop}"
271
 
   debug "Source rootfs device mapper is ${copy_rootfs_dm}"
272
 
 
273
 
   [ -n "${img_label}" ] &&
274
 
       rootfs_label=${img_label} ||
275
 
       {
276
 
            rootfs_label=$(tune2fs -l ${copy_rootfs_dm} | awk '/Filesystem volume name/ {print$NF}') ||
277
 
            fail "Unable to determine copy rootfs id"
278
 
       }
279
 
 
280
 
   [ -n "${rootfs_label}" ] ||
281
 
        fail "root file system lacks a label, this is fatal"
282
 
 
283
 
   mount "${copy_rootfs_dm}" "${mp_rootfs}" ||
284
 
        fail "Failed to mount copy_rootfs"
285
 
 
286
 
   # Extract the kernel and intrd after mounting
287
 
   [ -d "${mp_rootfs}/boot" ] || fail "Unable to find boot directory within image"
288
 
   cp -a "${mp_rootfs}/boot/" "${TEMPDIR}" ||
289
 
        fail "Unable to copy boot directory to ${TEMPDIR}"
290
 
 
291
 
   # Get the most recent kernel and initrd name
292
 
   kernel=$(find ${TEMPDIR}/ -iname "vmlinuz*" | sort -rn | head -n1 )
293
 
   initrd=$(find ${TEMPDIR}/ -iname "initrd*"  | sort -rn | head -n1 )
294
 
 
295
 
   [ -e "${kernel}" ] || fail "Did not find any kernel images"
296
 
   [ -e "${initrd}" ] || fail "Did not find any initrd images"
297
 
 
298
 
   debug "Using ${kernel} as kernel image"
299
 
   debug "Using ${initrd} as initrd image"
300
 
 
301
 
   # Copy the image
302
 
   debug "Copying source root file system to image"
303
 
   dd if="${copy_rootfs_dm}" of="${rootfs_dm}" && debug "Finished copying rootfile system" ||
304
 
        error "Potential problem copying rootfs image into parition"
305
 
 
306
 
}
307
 
 
308
 
finalize_img() {
309
 
    if [ "${img_type}" = "qcow2" ]; then
310
 
            debug "Converting ${img} to QCOW2 format"
311
 
            qemu-img convert -c -O qcow2 ${img} ${final_img} && rm "${img}" ||
312
 
                fail "Failed to convert image to qcow format"
313
 
    else
314
 
        mv "${img}" "${final_img}" ||
315
 
            fail "Unable to write ${final} from ${img}"
316
 
    fi
317
 
 
318
 
   [ -e "${final_img}" ] && debug "Final image emitted to ${final_img}" ||
319
 
        fail "Final image ${final} is missing; FATAL"
320
 
}
321
 
 
322
 
#do the work
323
 
check_prog kpartx
324
 
check_prog mkimage
325
 
convert_image
326
 
mk_disk
327
 
stuff_image
328
 
mk_boot_files
329
 
create_boot_fs
330
 
finalize_img
331
 
do_done
332