summaryrefslogtreecommitdiffstats
path: root/bsp/meta-arm/meta-arm-autonomy/recipes-extended/xenguest/files/xenguest-mkimage
blob: 4c1b4a8700959c2efc30df92d4ade4d7d20263e1 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/bin/bash
# This script must be used to manipulate xenguest images
#
# xenguest image topology:
#  params.cfg: guest global configuration file. Only edited using this script.
#  guest.cfg: xen main configuration file. Only edited using this script.
#  guest.d: directory contains files with custom xen configuration entries
#    which are appended to guest.cfg before starting the guest
#  files: directory where files used by xen configuration are stored
#  disk.cfg: guest disk configuration file. Only edited using this script.
#    (dtb, kernel image, etc)
#  disk: directory where files for disk creation are stored
#  init.[pre,d,post]: directories containing init pre, base and post scripts
set -u
set -e

this="$0"

IMAGE_TMPDIR=""

usage() {
    cat <<EOF
Usage $this ACTION XENGUEST [ARGS]

Where XENGUEST is a xenguest image file or a xenguest directory.

The following actions are supported:
help               Display this help
create             Create a xenguest image
update             Update/modify a xenguest image
partial            Create partial xenguest image in a directory
pack               Pack a xenguest directory into an image
check              Check a xenguest image
dump-paramsconfig  Display the guest configuration of a xenguest image
dump-xenconfig     Display the xen configuration of a xenguest image
dump-diskconfig    Display the disk configuration of a xenguest image
dump-init          Display init scripts of a xenguest image
extract            Extract a xenguest image content
extract-config     Extract the guest configuration from a xenguest image
extract-disk-file  Extract a disk file from a xenguest image
Use $this ACTION --help to have help on a specific action and its arguments.
EOF
}

usage-check() {
    cat <<EOF
Usage $this check XENGUEST

Check a xenguest image or a xenguest directory.
EOF
}

usage-update-create() {
    cat <<EOF
All arguments are handled in order.

Global configuration for the guest
--guest-config-reset    reset guest global configuration
--set-param=PARAM       disable parameter PARAM in guest global configuration
--set-param=PARAM=VAL   set parameter PARAM to value VAL in guest global
                        configuration.
                        Example of parameters supported are:
                        GUEST_AUTOBOOT: if set to 1 (default), guest will be
                         automatically created and started during host init.

Xen configuration for the guest
--xen-reset-config      reset xen guest configuration to default
--xen-name=             disable name parameter in xen configuration
--xen-name=NAME         set guest name in xen configuration
--xen-kernel=           disable guest kernel parameter in xen configuration
--xen-kernel=FILE       set guest kernel to FILE (file is added and xen
                         configuration is set to use it)
--xen-memory            set guest memory size (in MB)
--xen-vcpus             set guest number of virtual cpus
--xen-clean-extra       set guest command line (extra) to an empty string
--xen-extra=ARG         append ARG to the guest command line (with space)
                         use this several time to set command line.
                         To set the command line to "console=ttyS0 rw" do
                         --xen-extra=console=ttyS0 --xen-extra=rw
--xen-root=             disable root parameter in xen configuration
--xen-root=ROOT         set guest root in xen configuration
--xen-device-tree=      disable device tree parameter in xen configuration
--xen-device-tree=FILE  set guest device tree in xen configuration and add
                         file to xen files
--xen-disk=             disable disk parameter in xen configuration
--xen-disk=DEV          set guest disk to device DEV (phy:DEV,xvda,w is set)
--xen-append=FILE       append FILE content to xen configuration

Xen files
--xen-add-file=SRC:DST  add file SRC as file DST in the xenguest image.
                         If DST already exist in the image, it is overwritten.
                         DST must be the same as arguments passed to other
                         options (like --xen-kernel)
--xen-rm-file=DST       remove file DST from the xenguest image.

Init configuration
  This can be used to add init scripts for the guest. There are 3 possible init
  scripts which are called at different time. The pre scripts are called first,
  then the xen guest is created in pause and standard init scripts are called.
  Finally the xen guest is started then the post init scripts are called.
  Each script is called with the name of the guest as first argument and
  multiple scripts can be added (they must have different names).

--init-script=FILE      add FILE as init script
--init-pre=FILE         add FILE as pre init script
--init-post=FILE        add FILE as post init script

Disk configuration
--disk-reset-config     reset disk guest configuration to default (no disk)
--disk-size=SZ          set guest disk size (in GB)
--disk-device=DEV       set device to be used to create the guest disk
                        if unset or set to an empty string, the volume will be
                        create in the default manager volume group.
--disk-add-part=DEF     add a partition to the guest disk with definition DEF
                         a partition definition must have the following format:
                         ID:SIZE:FORMAT:CONTENT where:
                         - ID is the partition numeric ID (1 to 4)
                         - FORMAT is the filesystem format (supported formats
                           are none, vfat, swap, ext2, ext3 and ext4) or can be
                           left empty to not format
                         - CONTENT can be used to point to a file added using
                          --disk-add-file to be used as partition initial
                          content (tar file or img file)
--disk-rm-part=ID       remove partition ID from the guest disk
--disk-add-file=SRC:DST add file SRC as disk file DST in the xenguest image.
                         DST can then be used as a partition CONTENT.
--disk--rm-file=DST     remove disk file DST from the xenguest image.
EOF
}

usage-create() {
    cat <<EOF
Usage $this create XENGUEST [ARGS]

Create a xenguest image as XENGUEST file.

EOF
    usage-update-create
}

usage-update() {
    cat <<EOF
Usage $this update XENGUEST [ARGS]

Update or modify a xenguest image or a xenguest directory.

EOF
    usage-update-create
}

usage-pack() {
    cat <<EOF
Usage $this pack XENGUEST DESTFILE

Pack a xenguest directory in XENGUEST to create a xenguest image DESTFILE.

EOF
}

usage-partial() {
    cat <<EOF
Usage $this partial XENGUEST [ARGS]

Update or modify a partial xenguest image.

EOF
    usage-update-create
}

usage-dump-paramsconfig() {
    cat <<EOF
Usage $this dump-paramsconfig XENGUEST

Dump the guest parameters of a xenguest image or directory
EOF
}

usage-dump-xenconfig() {
    cat <<EOF
Usage $this dump-xenconfig XENGUEST

Dump the xen configuration of a xenguest image or directory
EOF
}

usage-dump-diskconfig() {
    cat <<EOF
Usage $this dump-diskconfig XENGUEST

Dump the disk configuration of a xenguest image or directory
EOF
}

usage-extract() {
    cat <<EOF
Usage $this extract XENGUEST DESTDIR

Extract guest to DESTDIR
EOF
}

usage-extract-config() {
    cat <<EOF
Usage $this extract-config XENGUEST DESTDIR

Extract guest configuration to DESTDIR
EOF
}

usage-extract-disk-file() {
    cat <<EOF
Usage $this extract-disk-file XENGUEST DISKFILE

Extract disk file DISKFILE to stdout.
EOF
}

check_image() {
    local tstfile=${1}

    if [ ! -e ${tstfile} -o ! -w ${tstfile} ]; then
        echo "Error: File ${tstfile} does not exist or is not writeable"
        exit 1
    fi

    if [ -f ${tstfile} ]; then
        # This is a xenguest file
        local res=$(tar -tvf ${tstfile} ./guest.cfg ./disk.cfg \
            ./params.cfg > /dev/null 2>&1 || echo "error")
        if [ -n "${res}" ]; then
            echo "Error: File ${tstfile} is not a valid xenguest"
            exit 1
        fi
    elif [ -d ${tstfile} ]; then
        if [ ! -f ${tstfile}/guest.cfg -o ! -f  ${tstfile}/disk.cfg -o \
            ! ${tstfile}/params.cfg ]; then
            echo "Error: Directory ${tstfile} is not a valid xenguest"
            exit 1
        fi
    fi
}

params_config_reset() {
    cat <<EOF > ${IMAGE_TMPDIR}/params.cfg
# Xenguest-image guest global configuration
#
# !! This file must not be modified manually !!
#
# You can use xenguest-image to modify parameters.
#

# Guest auto boot during Dom0 init
GUEST_AUTOBOOT="1"
EOF
}

params_config_setparam() {
    param="${1}"
    shift
    value="$@"

    if [ -z "$value" ]; then
        sed -i "/.*${param}=.*/d" ${IMAGE_TMPDIR}/params.cfg
    elif grep -e "^${param}=" ${IMAGE_TMPDIR}/params.cfg > /dev/null; then
        sed -i "s/${param}=\".*\"/${param}=\"${value}\"/" \
            ${IMAGE_TMPDIR}/params.cfg
    else
        echo "${param}=\"${value}\"" >> ${IMAGE_TMPDIR}/params.cfg
    fi
}

xen_config_reset() {
    cat <<EOF > ${IMAGE_TMPDIR}/guest.cfg
# Xenguest-image main configuraiton
#
# !! This file must not be modified manually !!
#
# You can use xenguest-image to modify parameters.
#
# You can add custom entries to configuration in the guest.d directory.

# Guest name (set by manager when guest is created)
# name = ""

# Guest memory size in MB
memory = 1024

# Number of VCPUS
vcpus = 1

# Guest command line
extra = "earlyprintk=xenboot console=hvc0 rw"

# Guest root filesystem device (from guest point of view)
# root = "/dev/xvda2"

# Disk that will be used by the guest (set by manager when guest is created)
# disk = ['phy:/dev/vg-xen/guestname,xvda,w']

EOF
}

get_param_file() {
    param="${1}"

    if grep ${param} ${IMAGE_TMPDIR}/guest.cfg > /dev/null 2>&1; then
        echo "${IMAGE_TMPDIR}/guest.cfg"
    else
        if [ ! -f ${IMAGE_TMPDIR}/guest.d/${param}.cfg ]; then
            mkdir -p ${IMAGE_TMPDIR}/guest.d
            echo "# ${param} = \"\"" > ${IMAGE_TMPDIR}/guest.d/${param}.cfg
        fi
        echo "${IMAGE_TMPDIR}/guest.d/${param}.cfg"
    fi
}

xen_config_disable_param() {
    param="${1}"
    dst=$(get_param_file ${param})

    sed -i "s@.*\(${param} = .*\)\$@# \1@" ${dst}
}

xen_config_set_number() {
    param="${1}"
    shift
    value="$@"
    dst=$(get_param_file ${param})

    sed -i "s@.*${param} = .*@${param} = ${value}@" ${dst}
}

xen_config_set_string() {
    param="${1}"
    shift
    value="$@"
    dst=$(get_param_file ${param})

    sed -i "s@.*${param} = .*@${param} = \"${value}\"@" ${dst}
}

xen_config_append_string() {
    param="${1}"
    shift
    value="$@"
    dst=$(get_param_file ${param})

    sed -i "s@.*${param} = \"\([^\"]*\)\"@${param} = \"\1 ${value}\"@" ${dst}
}

xen_config_set_list() {
    param="${1}"
    shift
    value=$(echo $@ | tr " " ",")
    dst=$(get_param_file ${param})

    sed -i "s@.*${param} = .*@${param} = ['${value}']@" ${dst}
}

disk_config_reset() {
    echo "DISK_SIZE=\"0\"" > ${IMAGE_TMPDIR}/disk.cfg
    echo "DISK_DEVICE=\"\"" >> ${IMAGE_TMPDIR}/disk.cfg
}

disk_config_rm_part() {
    partid=$1
    sed -i "/DISK_PART${partid}=.*/d" ${IMAGE_TMPDIR}/disk.cfg
}

disk_config_add_part() {
    partconf="${1}"
    partid=$(echo ${partconf} | sed -e "s/:.*//")
    partinfo=$(echo ${partconf} | sed -e "s/[^:]*://")

    # Make sure we don't add the same partition twice
    disk_config_rm_part ${partid}
    echo "DISK_PART${partid}=\"${partinfo}\"" >> \
        ${IMAGE_TMPDIR}/disk.cfg
}

# We need an action as first argument
action="${1:-}"

if [ -z "${action}" ]; then
    echo "Error: No ACTION provided"
    usage
    exit 1
fi

# Only help does not require a xenguest argument so treat this first
# while there we also check that user is asking for a supported action
case $action in
    help|--help|-h|-?)
        usage
        exit 0
        ;;
    check|create|update|pack|partial)
        ;;
    dump-xenconfig|dump-diskconfig|dump-init|dump-paramsconfig)
        ;;
    extract|extract-config|extract-disk-file)
        ;;
    *)
        echo "Error: Invalid action $action"
        exit 1
        ;;
esac

# Second argument should be the file name or directory
guestfile="${2:-}"

# Handle user asking for help on a specific action
case $guestfile in
    help|--help|-h|-?)
        usage-${action}
        exit 0
        ;;
esac

if [ -z "${guestfile}" ]; then
    echo "Error: no GUESTFILE provided"
    usage
    exit 1
fi

shift 2

case ${action} in
    check)
        check_image ${guestfile}
        echo "Image is OK"
        exit 0
        ;;
    dump-paramsconfig)
        check_image ${guestfile}
        echo "Guest configuration:"
        if [ -f ${guestfile} ]; then
            tar -xOf ${guestfile} ./params.cfg
        else
            cat ${guestfile}/params.cfg
        fi
        exit 0
        ;;
    dump-xenconfig)
        check_image ${guestfile}
        echo "Xen configuration:"
        if [ -f ${guestfile} ]; then
            tar -xOf ${guestfile} ./guest.cfg
            tar -xOf ${guestfile} ./guest.d 2> /dev/null || true
        else
            cat ${guestfile}/guest.cfg
            cat ${guestfile}/guest.d/* 2> /dev/null || true
        fi
        echo
        exit 0
        ;;
    dump-diskconfig)
        check_image ${guestfile}
        echo "Disk configuration:"
        if [ -f ${guestfile} ]; then
            tar -xOf ${guestfile} ./disk.cfg
        else
            cat ${guestfile}/disk.cfg
        fi
        echo
        exit 0
        ;;
    dump-init)
        check_image ${guestfile}
        for init in init.d init-pre init-post; do
            echo "=== ${init} ==="
            if [ -f ${guestfile} ]; then
                tar -xOf ${guestfile} ./${init} 2> /dev/null || \
                    echo "No ${init} scripts."
            else
                cat ${guestfile}/${init}/* 2> /dev/null || \
                    echo "No ${init} scripts."
            fi
            echo "==============="
            echo
        done
        exit 0
        ;;
    pack)
        check_image ${guestfile}
        if [ ! -d ${guestfile} ]; then
            echo "Error: Pack can only be done on a xenguest directory"
            exit 1
        fi

        if [ -z "${1:-}" ] || [ -f ${1} ]; then
            echo "Error: No destination file or already existing file"
            exit 1
        fi

        tar -C ${guestfile} -cf ${1} .
        exit 0
        ;;
    extract)
        check_image ${guestfile}
        if [ -d ${guestfile} ]; then
            echo "Error: Cannot extract config from xenguest directory"
            exit 1
        fi

        if [ -z "${1:-}" ] || [ ! -d ${1} ]; then
            echo "Error: No destination directory for image extract"
            exit 1
        fi

        tar -C ${1} -xf ${guestfile}
        exit 0
        ;;
    extract-config)
        check_image ${guestfile}
        if [ -d ${guestfile} ]; then
            echo "Error: Cannot extract config from xenguest directory"
            exit 1
        fi

        if [ -z "${1:-}" ] || [ ! -d ${1} ]; then
            echo "Error: No destination directory for config extract"
            exit 1
        fi

        #extract all but disk files
        tar -C ${1} --exclude='./disk' -xf ${guestfile}
        exit 0
        ;;
    extract-disk-file)
        check_image ${guestfile}

        if [ -d ${guestfile} ]; then
            echo "Error: Cannot extract disk file from xenguest directory" >&2
            exit 1
        fi

        if [ -z "${1:-}" ]; then
            echo "Error: No file to extract" >&2
            exit 1
        fi

        tar -xOf ${guestfile} ./disk/${1}
        exit 0
        ;;
    create)
        if [ -f ${guestfile} ]; then
            echo "Error: File ${guestfile} already exist"
            exit 1
        elif [ -d ${guestfile} ]; then
            if [ -n "$(ls -A ${guestfile})" ]; then
                echo "Error: Directory ${guestfile} is not empty"
                exit 1
            fi
            IMAGE_TMPDIR=$(realpath -m ${guestfile})
        else
            IMAGE_TMPDIR=$(mktemp -d)
        fi

        # Create initial content
        params_config_reset
        xen_config_reset
        disk_config_reset
        ;;
    update)
        check_image ${guestfile}

        if [ -f ${guestfile} ]; then
            # Extract the image to update it
            IMAGE_TMPDIR=$(mktemp -d)
            tar -C ${IMAGE_TMPDIR} -xf ${guestfile}
        else
            IMAGE_TMPDIR=$(realpath -m ${guestfile})
        fi
        ;;
    partial)
        if [ -e ${guestfile} -a ! -d ${guestfile} ]; then
            echo "Error: Invalid partial output directory"
            exit 1
        fi
        mkdir -p ${guestfile}
        IMAGE_TMPDIR=$(realpath -m ${guestfile})
        ;;
    *)
        echo "Invalid action ${action}"
        usage
        exit 1
        ;;
esac

# Process command line arguments
for arg in "${@}"; do
    case ${arg} in
        --*=*)
            optarg=$(echo ${arg} | sed -e "s/[^=]*=//")
            ;;
        *)
            optarg=""
            ;;
    esac

    case ${arg} in
        --guest-reset-config)
            params_config_reset
            ;;
        --set-param=*=*)
            param_name=$(echo $optarg | sed -e "s/=.*//")
            param_value=$(echo $optarg | sed -e "s/[^=]*=//")
            params_config_setparam "$param_name" "$param_value"
            ;;
        --set-param=*)
            params_config_setparam "$optarg"
            ;;
        --xen-reset-config)
            xen_config_create
            ;;
        --xen-name=*)
            if [ -z "${optarg}" ]; then
                xen_config_disable_param "name"
            else
                xen_config_set_string "name" "${optarg}"
            fi
            ;;
        --xen-kernel=*)
            if [ -z "${optarg}" ]; then
                xen_config_disable_param "kernel"
                rm -f ${IMAGE_TMPDIR}/files/kernel
            else
                if [ ! -f ${optarg} ]; then
                    echo "Error: invalid kernel file ${optarg}"
                    exit 1
                fi
                xen_config_set_string "kernel" "files/kernel"
                mkdir -p ${IMAGE_TMPDIR}/files
                install -m 644 ${optarg} ${IMAGE_TMPDIR}/files/kernel
            fi
            ;;
        --xen-memory=*)
            xen_config_set_number "memory" ${optarg}
            ;;
        --xen-vcpus=*)
            xen_config_set_number "vcpus" ${optarg}
            ;;
        --xen-clean-extra)
            xen_config_set_string "extra" ""
            ;;
        --xen-extra=*)
            xen_config_append_string "extra" ${optarg}
            ;;
        --xen-root=*)
            if [ -z "${optarg}" ]; then
                xen_config_disable_param "root"
            else
                xen_config_set_string "root" "${optarg}"
            fi
            ;;
        --xen-device-tree=*)
            if [ -z "${optarg}" ]; then
                xen_config_disable_param "device_tree"
                rm -f ${IMAGE_TMPDIR}/files/guest.dtb
            else
                if [ ! -f ${optarg} ]; then
                    echo "Error: invalid dtb file ${optarg}"
                    exit 1
                fi
                xen_config_set_string "device_tree" "files/guest.dtb"
                mkdir -p ${IMAGE_TMPDIR}/files
                install -m 644 ${optarg} ${IMAGE_TMPDIR}/files/guest.dtb
            fi
            ;;
        --xen-disk=*)
            if [ -z "${optarg}" ]; then
                xen_config_disable_param "disk"
            else
                xen_config_set_list "disk" "phy:${optarg}" "xvda" "w"
            fi
            ;;
        --xen-append=*)
            if [ ! -f ${optarg} ]; then
                echo "Error: invalid xen append file ${optarg}"
                exit 1
            fi
            mkdir -p ${IMAGE_TMPDIR}/guest.d
            install -m 755 ${optarg} ${IMAGE_TMPDIR}/guest.d/.
            ;;
        --xen-add-file=*)
            src=$(echo "${optarg}" | sed -e "s/:.*//")
            dst=$(echo "${optarg}" | sed -e "s/.*://")
            if [ ! -f ${src} ]; then
                echo "Error: Invalid file: ${src}"
                rm -rf ${IMAGE_TMPDIR}
                exit 1
            fi
            if [ -z "${dst}" ]; then
                dst=$(basename ${src})
            fi
            mkdir -p ${IMAGE_TMPDIR}/files/$(dirname ${dst})
            cp -f ${src} ${IMAGE_TMPDIR}/files/${dst}
            ;;
        --xen-rm-file=*)
            rm -f ${IMAGE_TMPDIR}/files/${optarg}
            ;;
        --init-script=*|--init-pre=*|--init-post=*)
            dst=""
            case $arg in
                --init-script=*)
                    dst="init.d"
                    ;;
                --init-pre=*)
                    dst="init.pre"
                    ;;
                --init-post=*)
                    dst="init.post"
                    ;;
            esac
            if [ ! -f ${optarg} ]; then
                echo "${optarg} does not point to a valid file"
                exit 1
            else
                mkdir -p ${IMAGE_TMPDIR}/${dst}
                install -m 755 ${optarg} ${IMAGE_TMPDIR}/${dst}/.
            fi
            ;;
        --disk-reset-config)
            disk_config_reset
            ;;
        --disk-size=*)
            sed -i "s/DISK_SIZE=.*/DISK_SIZE=\"${optarg}\"/" \
                ${IMAGE_TMPDIR}/disk.cfg
            ;;
        --disk-device=*)
            sed -i "s/DISK_DEVICE=.*/DISK_SIZE=\"${optarg}\"/" \
                ${IMAGE_TMPDIR}/disk.cfg
            ;;
        --disk-add-part=*)
            disk_config_add_part ${optarg}
            ;;
        --disk-rm-part=*)
            disk_config_rm_part ${optarg}
            ;;
        --disk-add-file=*)
            src=$(echo "${optarg}" | sed -e "s/:.*//")
            dst=$(echo "${optarg}" | sed -e "s/.*://")
            if [ ! -f ${src} ]; then
                echo "Error: Invalid disk file: ${src}"
                rm -rf ${IMAGE_TMPDIR}
                exit 1
            fi
            if [ -z "${dst}" ]; then
                dst=$(basename ${src})
            fi
            mkdir -p ${IMAGE_TMPDIR}/disk/$(dirname ${dst})
            cp -f ${src} ${IMAGE_TMPDIR}/disk/${dst}
            ;;
        --disk-rm-file=*)
            rm -f ${IMAGE_TMPDIR}/disk/${optarg}
            ;;
        *)
            echo "Unsupported command: ${arg}"
            exit 1
            ;;
    esac
done

if [ ! -d ${guestfile} ]; then
    # If the original guest was in a file we need to repack the file
    # with the changes we did on it in the IMAGE_TMPDIR
    rm -f ${guestfile}
    tar -C ${IMAGE_TMPDIR} -cf ${guestfile} .
    rm -rf ${IMAGE_TMPDIR}
fi