This file is indexed.

/usr/lib/curtin/helpers/common is in curtin-common 18.1-5-g572ae5d6-0ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/bin/bash
# This file is part of curtin. See LICENSE file for copyright and license info.

TEMP_D=""
CR="
"
VERBOSITY=${VERBOSITY:-${CURTIN_VERBOSITY:-0}}

error() { echo "$@" 1>&2; }
debug() {
    [ ${VERBOSITY:-0} -ge "$1" ] || return
    shift
    error "$@"
}

partition_main_usage() {
    cat <<EOF
Usage: ${0##*/} [ options ] target-dev

   partition target-dev with a single partition
   destroy any partition table that might be there already.

   options:
     -f | --format F   use partition table format F. [mbr, gpt, uefi, prep]
                       default mbr
     -E | --end E      end the partition at E (unit 1k bytes)
     -b | --boot       create a boot partition (512 MiB - default)
EOF
    [ $# -eq 0 ] || echo "$@"
}

grub_install_usage() {
    cat <<EOF
Usage: ${0##*/} [ options ] mount-point target-dev

   perform grub-install with mount-point onto target-dev.

   options:
          --uefi           install grub-efi instead of grub-pc
          --update-nvram   request grub to update nvram
EOF
    [ $# -eq 0 ] || echo "$@"
}

cleanup() {
    if [ -d "$TEMP_D" ]; then
        rm -Rf "$TEMP_D"
    fi
}


wipe_partitions() {
    # wipe_partition(blockdev, ptno)
    local dev="" target="" ret="" part="" full="" out=""
    if [ "$1" = "--full" ]; then
        full="--full"
        shift
    fi
    dev="$1"
    shift
    for part in "$@"; do
        find_partno "$dev" $part ||
            { ret=$?; error "did not find $part on $dev"; return $ret; }
        target="$_RET"
        wipedev $full "$target" false false ||
            { ret=$?; error "failed to wipe $part on $dev"; return $ret; }
    done
    return 0
}

wipedev() {
    # wipedev([--full,] target, wipe_end=true, reread=true)
    # wipe the front and optionally end of $target
    # if reread=true call rereadpt and settle
    local full="0"
    if [ "$1" = "--full" ]; then
        full="1"
        shift
    fi
    local target="$1" wipe_end=${2:-true} rereadpt=${3:-true}
    local size="" out="" bs="" count="" seek="" mb=$((1024*1024))
    local info=""
    getsize "$target" ||
        { error "failed to get size of $target"; return 1; }
    size="$_RET"

    # select a block size that evenly divides size. bigger is generally faster.
    for bs in $mb 4096 1024 512 1; do
        [ "$((size % bs))" = "0" ] && break
    done
    if [ "$bs" = "1" ]; then
        error "WARN: odd sized '$target' ($size). not divisible by 512."
    fi

    if [ "$full" != "0" ]; then
        count=$((size / bs))
        info="size=$size conv=notrunc count=$count bs=$bs"
        debug 1 "wiping full '$target' with ${info}."
        out=$(LANG=C dd if=/dev/zero conv=notrunc "of=$target" \
            bs=$bs count=$count 2>&1) || {
            error "wiping entire '$target' with ${info} failed."
            error "$out"
            return 1
        }
    else
        local fbs=$bs
        count=$((size / bs))
        if [ "$size" -ge "$mb" ]; then
            count=1
            fbs=$mb
        fi
        info="size=$size count=$count bs=$fbs"
        debug 1 "wiping start of '$target' with ${info}."
        # wipe the first MB (up to 'size')
        out=$(dd if=/dev/zero conv=notrunc "of=$target" \
                "bs=$fbs" "count=$count" 2>&1) || {
            error "wiping start of '$target' with ${info} failed."
            error "$out"
            return 1
        }

        if $wipe_end && [ "$size" -gt "$mb" ]; then
            # do the last 1MB
            count=$((mb / bs))
            seek=$(((size / bs) - $count))
            info="size=$size count=$count bs=$bs seek=$seek"
            debug 1 "wiping end of '$target' with ${info}."
            out=$(dd if=/dev/zero conv=notrunc "of=$target" "seek=$seek" \
                "bs=$bs" "count=$count" 2>&1)
            if [ $? -ne 0 ]; then
                error "wiping end of '$target' with ${info} failed."
                error "$out";
                return 1;
            fi
        fi
    fi

    if $rereadpt && [ -b "$target" ]; then
        blockdev --rereadpt "$target"
        udevadm settle
    fi
}

find_partno() {
    local devname="$1" partno="$2"
    local devbname cand msg="" slash="/"
    devbname="${devname#/dev/}"
    # /dev/cciss/c0d0 -> ccis!c0d0
    devbname="${devbname//$slash/!}"
    if [ -d "/sys/class/block/${devbname}" ]; then
        local cand candptno name partdev
        debug 1 "using sys/class/block/$devbname"
        for cand in /sys/class/block/$devbname/*/partition; do
            [ -f "$cand" ] || continue
            read candptno < "$cand"
            [ "$candptno" = "$partno" ] || continue
            name=${cand#/sys/class/block/${devbname}/}
            name=${name%/partition}
            # ccis!c0d0p1 -> ccis/c0d0p1
            name=${name//!/$slash}
            partdev="/dev/$name"
            [ -b "$partdev" ] && _RET="$partdev" && return 0
            msg="expected $partdev to exist as partition $partno on $devname"
            error "WARN: $msg. it did not exist."
        done
    else
        for cand in "${devname}$partno" "${devname}p${partno}"; do
            [ -b "$cand" ] && _RET="$cand" && return 0
        done
    fi
    return 1
}

part2bd() {
    # part2bd given a partition, return the block device it is on
    # and the number the partition is.  ie, 'sda2' -> '/dev/sda 2'
    local dev="$1" fp="" sp="" bd="" ptnum=""
    dev="/dev/${dev#/dev/}"
    fp=$(readlink -f "$dev") || return 1
    sp="/sys/class/block/${fp##*/}"
    [ -f "$sp/partition" ] || { _RET="$fp 0"; return 0; }
    read ptnum < "$sp/partition"
    sp=$(readlink -f "$sp") || return 1
    # sp now has some /sys/devices/pci..../0:2:0:0/block/sda/sda1
    bd=${sp##*/block/}
    bd="${bd%/*}"
    _RET="/dev/$bd $ptnum"
    return 0
}

pt_gpt() {
    local target="$1" end=${2:-""} boot="$3" size="" s512=""
    local start="2048" rootsize="" bootsize="1048576" maxend=""
    local isblk=false
    getsize "$target" ||
        { error "failed to get size of $target"; return 1; }
    size="$_RET"
    if [ -z "$end" ]; then
        end=$(($size/512))
    else
        end=$(($end/512))
    fi

    if [ "$boot" = true ]; then
        maxend=$((($size/512)-$start-$bootsize))
        if [ $maxend -lt 0 ]; then
            error "Disk is not big enough for /boot partition on $target";
            return 1;
        fi
    else
        maxend=$((($size/512)-$start))
    fi
    [ "$end" -gt "$maxend" ] && end="$maxend"
    debug 1 "maxend=$maxend end=$end size=$size"

    [ -b "$target" ] && isblk=true

    if [ "$boot" = true ]; then
        # Creating 'efi', '/boot' and '/' partitions
        sgdisk --new "15:$start:+1M" --typecode=15:ef02 \
            --new "1::+512M" --typecode=1:8300 \
            --new "2::$end" --typecode=2:8300 "$target" ||
            { error "failed to gpt partition $target"; return 1; }
    else
        # Creating 'efi' and '/' partitions
        sgdisk --new "15:$start:+1M" --typecode=15:ef02 \
            --new "1::$end" --typecode=1:8300 "$target" ||
            { error "failed to gpt partition $target"; return 1; }
    fi

    if $isblk; then
        local expected="1 15"
        [ "$boot" = "true" ] && expected="$expected 2"
        blockdev --rereadpt "$target"
        udevadm settle
        assert_partitions "$target" $expected ||
            { error "$target missing partitions: $_RET"; return 1; }
        wipe_partitions "$target" $expected ||
            { error "$target: failed to wipe partitions"; return 1; }
    fi
}

assert_partitions() {
    local dev="$1" missing="" part=""
    shift
    for part in "$@"; do
        find_partno "$dev" $part || missing="${missing} ${part}"
    done
    _RET="${missing# }"
    [ -z "$missing" ]
}

pt_uefi() {
    local target="$1" end=${2:-""} size="" s512=""
    local start="2048" rootsize="" maxend=""
    local isblk=false
    getsize "$target" ||
        { error "failed to get size of $target"; return 1; }
    size="$_RET"
    if [ -z "$end" ]; then
        end=$(($size/512))
    else
        end=$(($end/512))
    fi

    maxend=$((($size/512)-$start))
    [ "$end" -gt "$maxend" ] && end="$maxend"
    debug 1 "maxend=$maxend end=$end size=$size"

    [ -b "$target" ] && isblk=true

    # Creating 'UEFI' and '/' partitions
    sgdisk --new "15:2048:+512M" --typecode=15:ef00 \
           --new "1::$end" --typecode=1:8300 "$target" ||
        { error "failed to sgdisk for uefi to $target"; return 1; }

    if $isblk; then
        blockdev --rereadpt "$target"
        udevadm settle
        assert_partitions "$target" 1 15 ||
            { error "$target missing partitions: $_RET"; return 1; }
        wipe_partitions "$target" 1 15 ||
            { error "$target: failed to wipe partitions"; return 1; }
    fi

    local pt15
    find_partno "$target" 15 && pt15="$_RET" ||
        { error "failed to find partition 15 for $target"; return 1; }
    mkfs -t vfat -F 32 -n uefi-boot "$pt15" ||
        { error "failed to partition :$pt15' for UEFI vfat"; return 1; }
}


pt_mbr() {
    local target="$1" end=${2:-""} boot="$3" size="" s512="" ptype="L"
    local start="2048" rootsize="" maxsize="4294967296"
    local maxend="" isblk=false def_bootsize="1048576" bootsize=0
    local isblk=false
    getsize "$target" ||
        { error "failed to get size of $target"; return 1; }
    size="$_RET"

    if $boot; then
        bootsize=$def_bootsize
    fi

    s512=$(($size/512))
    if [ $s512 -ge $maxsize ]; then
        debug 1 "disk is larger than max for mbr (2TB)"
        s512=$maxsize
    fi

    # allow 33 sectors for the secondary gpt header in the case that
    # the user wants to later 'sgdisk --mbrtogpt'
    local gpt2hsize="33"
    if [ -n "$end" ]; then
        rootsize=$(((end/512)-start-bootsize))
    else
        rootsize=$((s512-start-bootsize-$gpt2hsize))
    fi

    [ -b "$target" ] && isblk=true

    # interact with sfdisk in units of 512 bytes (--unit S)
    # we start all partitions at 2048 of those (1M)
    local sfdisk_out="" sfdisk_in="" sfdisk_cmd="" t="" expected=""
    if "$boot"; then
        t="$start,$bootsize,$ptype,-${CR}"
        t="$t$(($start+$bootsize)),$rootsize,$ptype,*"
        sfdisk_in="$t"
        expected="1 2"
    else
        sfdisk_in="$start,$rootsize,$ptype,*"
        expected=1
    fi
    sfdisk_cmd=( sfdisk --no-reread --force --Linux --unit S "$target" )
    debug 1 "sfdisking with: echo '$sfdisk_in' | ${sfdisk_cmd[*]}"
    sfdisk_out=$(echo "$sfdisk_in" | "${sfdisk_cmd[@]}" 2>&1)
    ret=$?
    [ $ret -eq 0 ] || {
        error "failed to partition $target [${sfdisk_out}]";
        return 1;
    }
    if $isblk; then
        blockdev --rereadpt "$target"
        udevadm settle
        assert_partitions "$target" ${expected} ||
            { error "$target missing partitions: $_RET"; return 1; }

        wipe_partitions "$target" ${expected} ||
            { error "failed to wipe partition 1 on $target"; return 1; }
    fi

}

pt_prep() {
    local target="$1" end=${2:-""}
    local cmd="" isblk=false
    [ -b "$target" ] && isblk=true

    local pprep="1" proot="2"
    wipedev "$target" ||
        { error "failed to clear $target"; return 1; }

    cmd=(
        sgdisk
           --new "${pprep}::+8M"  "--typecode=${pprep}:4100"
           --new "${proot}::$end" "--typecode=${proot}:8300"
           "$target"
    )
    debug 1 "partitioning '$target' with ${cmd[*]}"
    "${cmd[@]}" ||
        fail "Failed to create GPT partitions (${cmd[*]})"

    udevadm trigger
    udevadm settle

    if $isblk; then
        blockdev --rereadpt "$target"
        udevadm settle
        assert_partitions "$target" "${proot}" "${pprep}"  ||
            { error "$target missing partitions: $_RET"; return 1; }
        # wipe the full prep partition
        wipe_partitions --full "$target" "${pprep}" ||
            { error "$target: failed to wipe full PReP partition"; return 1;}
        wipe_partitions "$target" "${proot}" ||
            { error "$target: failed to wipe partition ${proot}"; return 1;}
    fi

    return 0
}

partition_main() {
    local short_opts="hE:f:bv"
    local long_opts="help,end:,format:,boot,verbose"
    local getopt_out=$(getopt --name "${0##*/}" \
        --options "${short_opts}" --long "${long_opts}" -- "$@") &&
        eval set -- "${getopt_out}" ||
        { partition_main_usage 1>&2; return 1; }

    local cur="" next=""
    local format="mbr" boot=false target="" end="" ret=0

    while [ $# -ne 0 ]; do
        cur="$1"; next="$2";
        case "$cur" in
            -h|--help) partition_main_usage ; exit 0;;
            -E|--end) end=$next; shift;;
            -f|--format) format=$next; shift;;
            -b|--boot) boot=true;;
            -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
            --) shift; break;;
        esac
        shift;
    done

    [ $# -gt 1 ] && { partition_main_usage "got $# args, expected 1" 1>&2; return 1; }
    [ $# -eq 0 ] && { partition_main_usage "must provide target-dev" 1>&2; return 1; }
    target="$1"
    if [ -n "$end" ]; then
        human2bytes "$end" ||
            { error "failed to convert '$end' to bytes"; return 1; }
        end="$_RET"
    fi

    [ "$format" = "gpt" -o "$format" = "mbr" ] ||
        [ "$format" = "uefi" -o "$format" = "prep" ] ||
        { partition_main_usage "invalid format: $format" 1>&2; return 1; }

    TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
        fail "failed to make tempdir"
    trap cleanup EXIT

    [ -e "$target" ] || { error "$target does not exist"; return 1; }
    [ -f "$target" -o -b "$target" ] ||
        { error "$target not a block device"; return 1; }

    wipedev "$target" ||
        { error "wiping $target failed"; return 1; }

    if [ "$format" = "mbr" ]; then
        pt_mbr "$target" "$end" "$boot"
    elif [ "$format" = "gpt" ]; then
        pt_gpt "$target" "$end" "$boot"
    elif [ "$format" = "uefi" ]; then
        pt_uefi "$target" "$end"
    elif [ "$format" = "prep" ]; then
        pt_prep "$target" "$end"
    fi
    ret=$?

    return $ret
}

human2bytes() {
    # converts size suitable for input to resize2fs to bytes
    # s:512 byte sectors, K:kilobytes, M:megabytes, G:gigabytes
    # none: block size of the image
    local input=${1} defunit=${2:-1024}
    local unit count;
    case "$input" in
        *s) count=${input%s}; unit=512;;
        *K) count=${input%K}; unit=1024;;
        *M) count=${input%M}; unit=$((1024*1024));;
        *G) count=${input%G}; unit=$((1024*1024*1024));;
        *)  count=${input}  ; unit=${defunit};;
    esac
   _RET=$((${count}*${unit}))
}

getsize() {
    # return size of target in bytes
    local target="$1"
    if [ -b "$target" ]; then
        _RET=$(blockdev --getsize64 "$target")
    elif [ -f "$target" ]; then
        _RET=$(stat "--format=%s" "$target")
    else
        return 1;
    fi
}

is_md() {
    case "${1##*/}" in
        md[0-9]) return 0;;
    esac
    return 1
}

get_carryover_params() {
    local cmdline=" $1 " extra="" lead="" carry_extra="" carry_lead=""
    # return a string to append to installed systems boot parameters
    # it may include a '--' after a '---'
    # see LP: 1402042 for some history here.
    # this is similar to 'user-params' from d-i
    local preferred_sep="---"  # KERNEL_CMDLINE_COPY_TO_INSTALL_SEP
    local legacy_sep="--"
    case "$cmdline" in
        *\ ${preferred_sep}\ *)
            extra=${cmdline#* ${preferred_sep} }
            lead=${cmdline%% ${preferred_sep} *}
            ;;
        *\ ${legacy_sep}\ *)
            extra="${cmdline#* ${legacy_sep} }"
            lead=${cmdline%% ${legacy_sep} *}
            ;;
        *)
            extra=""
            lead="$cmdline"
            ;;
    esac

    if [ -n "$extra" ]; then
        carry_extra=$(set -f;
            c="";
            for p in $extra; do
                case "$p" in
                    (BOOTIF=*|initrd=*|BOOT_IMAGE=*) continue;;
                esac
                c="$c $p";
            done
            echo "${c# }"
        )
    fi

    # these get copied even if they werent after the separator
    local padded=" $carry_extra "
    carry_lead=$(set -f;
        padded=" ${carry_extra} "
        c=""
        for p in $lead; do
            # skip any that are already in carry_extra
            [ "${padded#* $p }" != "$padded" ] && continue
            case "$p" in
                (console=*) c="$c $p";;
            esac
        done
        echo "${c# }"
    )
    _RET="${carry_lead:+${carry_lead} }${carry_extra}"
}

install_grub() {
    local long_opts="uefi,update-nvram"
    local getopt_out="" mp_efi=""
    getopt_out=$(getopt --name "${0##*/}" \
        --options "" --long "${long_opts}" -- "$@") &&
        eval set -- "${getopt_out}"

    local uefi=0
    local update_nvram=0

    while [ $# -ne 0 ]; do
        cur="$1"; next="$2";
        case "$cur" in
            --uefi) uefi=$((${uefi}+1));;
            --update-nvram) update_nvram=$((${update_nvram}+1));;
            --) shift; break;;
        esac
        shift;
    done

    [ $# -lt 2 ] && { grub_install_usage "must provide mount-point and target-dev" 1>&2; return 1; }

    local mp="$1"
    local cmdline tmp r=""
    shift
    local grubdevs
    grubdevs=( "$@" )
    if [ "${#grubdevs[@]}" = "1" -a "${grubdevs[0]}" = "none" ]; then
        grubdevs=( )
    fi

    # find the mp device
    local mp_dev="" fstype=""
    mp_dev=$(awk -v "MP=$mp" '$2 == MP { print $1 }' /proc/mounts) || {
        error "unable to determine device for mount $mp";
        return 1;
    }

    fstype=$(awk -v MP=$mp '$2 == MP { print $3 }' /proc/mounts) || {
        error "unable to fstype for mount $mp";
        return 1;
    }

    [ -z "$mp_dev" ] && {
        error "did not find '$mp' in /proc/mounts"
        cat /proc/mounts 1>&2
        return 1
    }
    # check if parsed mount point is a block device
    # error unless fstype is zfs, where entry will not point to block device.
    if ! [ -b "$mp_dev" ] && [ "$fstype" != "zfs" ]; then
        # error unless mp is zfs, entry doesn't point to block devs
        error "$mp_dev ($fstype) is not a block device!"; return 1;
    fi

    # get dpkg arch
    local dpkg_arch=""
    dpkg_arch=$(chroot "$mp" dpkg --print-architecture)
    r=$?
    [ $r -eq 0 ] || {
        error "failed to get dpkg architecture [$r]"
        return 1;
    }

    # grub is not the bootloader you are looking for
    if [ "${dpkg_arch}" = "s390x" ]; then
	return 0;
    fi

    # set correct grub package
    local grub_name="grub-pc"
    local grub_target="i386-pc"
    if [ "${dpkg_arch#ppc64}" != "${dpkg_arch}" ]; then
        grub_name="grub-ieee1275"
        grub_target="powerpc-ieee1275"
    elif [ "$uefi" -ge 1 ]; then
        grub_name="grub-efi-$dpkg_arch"
        case "$dpkg_arch" in
            amd64)
                grub_target="x86_64-efi";;
            arm64)
                grub_target="arm64-efi";;
        esac
    fi

    # check that the grub package is installed
    tmp=$(chroot "$mp" dpkg-query --show \
        --showformat='${Status}\n' $grub_name)
    r=$?
    if [ $r -ne 0 -a $r -ne 1 ]; then
        error "failed to check if $grub_name installed";
        return 1;
    fi
    case "$tmp" in
        install\ ok\ installed) :;;
        *) debug 1 "$grub_name not installed, not doing anything";
            return 0;;
    esac

    local grub_d="etc/default/grub.d"
    local mygrub_cfg="$grub_d/50-curtin-settings.cfg"
    [ -d "$mp/$grub_d" ] || mkdir -p "$mp/$grub_d" ||
        { error "Failed to create $grub_d"; return 1; }

    # LP: #1179940 . The 50-cloudig-settings.cfg file is written by the cloud
    # images build and defines/override some settings. Disable it.
    local cicfg="$grub_d/50-cloudimg-settings.cfg"
    if [ -f "$mp/$cicfg" ]; then
       debug 1 "moved $cicfg out of the way"
       mv "$mp/$cicfg" "$mp/$cicfg.disabled"
    fi

    # get the user provided / carry-over kernel arguments
    local newargs=""
    read cmdline < /proc/cmdline &&
        get_carryover_params "$cmdline" && newargs="$_RET" || {
        error "Failed to get carryover parrameters from cmdline"; 
        return 1;
    }
    debug 1 "carryover command line params: $newargs"

    : > "$mp/$mygrub_cfg" ||
        { error "Failed to write '$mygrub_cfg'"; return 1; }
    {
        [ "${REPLACE_GRUB_LINUX_DEFAULT:-1}" = "0" ] ||
            echo "GRUB_CMDLINE_LINUX_DEFAULT=\"$newargs\""
        echo "# disable grub os prober that might find other OS installs."
        echo "GRUB_DISABLE_OS_PROBER=true"
        echo "GRUB_TERMINAL=console"
    } >> "$mp/$mygrub_cfg"

    local short="" bd="" grubdev grubdevs_new=""
    grubdevs_new=()
    for grubdev in "${grubdevs[@]}"; do
        if is_md "$grubdev"; then
            short=${grubdev##*/}
            for bd in "/sys/block/$short/slaves/"/*; do
                [ -d "$bd" ] || continue
                bd=${bd##*/}
                bd="/dev/${bd%[0-9]}" # FIXME: part2bd
                grubdevs_new[${#grubdevs_new[@]}]="$bd"
            done
        else
            grubdevs_new[${#grubdevs_new[@]}]="$grubdev"
        fi
    done
    grubdevs=( "${grubdevs_new[@]}" )

    if [ "$uefi" -ge 1 ]; then
        nvram="--no-nvram"
        if [ "$update_nvram" -ge 1 ]; then
            nvram=""
        fi    
        debug 1 "curtin uefi: installing ${grub_name} to: /boot/efi"
        chroot "$mp" env DEBIAN_FRONTEND=noninteractive sh -exc '
            echo "before grub-install efiboot settings"
            efibootmgr || echo "WARN: efibootmgr exited $?"
            dpkg-reconfigure "$1"
            update-grub
            # grub-install in 12.04 does not contain --no-nvram, --target,
            # or --efi-directory
            target="--target=$2"
            no_nvram="$3"
            efi_dir="--efi-directory=/boot/efi"
            gi_out=$(grub-install --help 2>&1)
            echo "$gi_out" | grep -q -- "$no_nvram" || no_nvram=""
            echo "$gi_out" | grep -q -- "--target" || target=""
            echo "$gi_out" | grep -q -- "--efi-directory" || efi_dir=""
            grub-install $target $efi_dir \
                --bootloader-id=ubuntu --recheck $no_nvram' -- \
            "${grub_name}" "${grub_target}" "$nvram" </dev/null ||
            { error "failed to install grub!"; return 1; }

        chroot "$mp" sh -exc '
            echo "after grub-install efiboot settings"
            efibootmgr || echo "WARN: efibootmgr exited $?"
            ' -- </dev/null ||
            { error "failed to list efi boot entries!"; return 1; }
    else
        # Note: dpkg-reconfigure calls grub-install on ppc64
        # this means that using '--no-nvram' below ends up
        # failing very oddly.  This is because grub's post-inst
        # runs grub-install with no target.  That ends up
        # updating nvram badly, and then the grub-install would
        # not fix it because of the no-nvram there.
        debug 1 "curtin non-uefi: installing ${grub_name} to: ${grubdevs[*]}"
        chroot "$mp" env DEBIAN_FRONTEND=noninteractive sh -exc '
            pkg=$1; shift;
            dpkg-reconfigure "$pkg"
            update-grub
            for d in "$@"; do grub-install "$d" || exit; done' \
            -- "${grub_name}" "${grubdevs[@]}" </dev/null ||
            { error "failed to install grub!"; return 1; }
    fi

    if [ -n "${mp_efi}" ]; then
        umount "$mp_efi" ||
            { error "failed to unmount $mp_efi"; return 1; }
    fi

    return
}

# vi: ts=4 expandtab syntax=sh