summaryrefslogtreecommitdiff
path: root/src/partvi
blob: 3b622edf8ef6080b564a96d92e7c93e917d37cde (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
#!/bin/bash

add_initrd()
{
    initrd_suffix=.samizdat
    $sudo mkdir -p "$mnt"/linux
    $sudo rsync  -aL --info=STATS "${1}vmlinuz${2}" "$mnt"/linux/vmlinuz
    $sudo rsync  -aL --info=STATS "${1}initrd.img${2}${2:+$initrd_suffix}" "$mnt"/linux/initrd.img
}

add_grub_cfg()
{
    $sudo mkdir -p "$mnt"/grub
    $sudo cp -aL "$GRUB_CONFIG" "$mnt"/grub
}

install_boot_dir()
{
    local mnt="$1"
    add_grub_cfg
    add_initrd "$samizdat_linux_dir"/ "${version_suffix}"
}

msg() { printf '%s: %s: %s\n' "$0" "$1" "$2" >&2; }
die() { msg Error "${*:-Exiting on fatal error.}"; exit 1; }
warn() { msg Warning "${*:-Something is wrong.}"; }
notice() { msg Notice "$*"; }
quietly() { "$@" >/dev/null 2>&1 || true; }
loudly() { (set -x; "$@"); }

validate_name()
{
        case "$1" in
        *[^a-zA-Z0-9_]*) false ;;
        *) true ;;
        esac
}

read_config_file()
{
        validate_name "$img" || { warn "invalid name: $img"; return 1; }
        while read line
        do
                line=${line%%#*} # ignore comments
                k=${line%%=*}
                v=${line#*=}
                [ "$k" -a "$k" != "$line" ] || return
                eval "conf_${img}_$k=\$v"
        done < "$img".conf
}

inquire_var() { _inquire_var "$img" "$1"; }
_inquire_var()
{
        local v
        v=conf_${1}_${2}
        v=${!v}
        if [ "$v" ]
        then
                eval "$2=\$v"
        else
                false
        fi
}

require_var() { _require_var "$img" "$1"; }
_require_var()
{
        _inquire_var "$@" || die "Missing required field '$2' for image file '$1'"
}

get_root_hash()
{
        sed -ne 's/^Root hash:[ \t]*//p' "$1"
}

require_exists()
{
        local f
        for f
        do
                [ -f "$f" ] || die "Not a file: $f"
        done
}

build_partition_image()
{
        if inquire_var rebuild
        then
                case "$rebuild" in
                        always|never|default) ;;
                        *) die "invalid value for field 'rebuild': $rebuild" ;;
                esac
        fi

        if [ "$rebuild" = 'always' ] || [ ! -e "$imgfile" -a "$rebuild" != 'never' ]
        then

                if [ -e "$imgfile" ]
                then
                        notice "Image file exists: $imgfile"
                fi

                case "$type" in
                        dm-verity-hashes|dm-verity-data)
                                require_var data_path
                                require_exists "$data_path" "$data_path".verity "$data_path".verity.log
                                root_hash=$(get_root_hash "$data_path".verity.log)
                                [ ${#root_hash} = 64 ]
                        ;;
                        *)
                                require_var allocation
                                tmp=$imgfile~tmp
                                fallocate -l "$allocation" "$tmp"
                        ;;
                esac

                case "$type" in
                        bios-grub)            ;;
                        efi-system-partition) mkfs.fat -F 32 -I "$tmp" || die "mkfs.vfat failed" ;;
                        boot|samizdat-keys)   mkfs.btrfs -q "$tmp" || die "mkfs.btrfs failed" ;;
                        dm-verity-data)
                                partuuid=${root_hash:0:32}
                                set_var partuuid
                                cp -f -T --reflink "$data_path" "$builddir"/"$partuuid"
                                ln -sfT "$partuuid" "$tmp"
                        ;;
                        dm-verity-hashes)
                                partuuid=${root_hash:32:32}
                                set_var partuuid
                                cp -f -T --reflink "$data_path".verity "$builddir"/"$partuuid"
                                ln -sfT "$partuuid" "$tmp"
                        ;;
                esac
                mv -T "$tmp" "$imgfile"
                notice "Successfully wrote $imgfile"
        fi
}

iterate_partitions()
{
        local f
        for f in part*.conf
        do
                notice "Processing $f"
                img=${f%.conf}

                read_config_file || warn "Received error return from command: read_config_file $img"
                require_var name

                require_var type
                case "$type" in
                    dm-verity-hashes|dm-verity-data)
                            require_var data_path
                            [ ! "$SKIP_ROOTFS_COPY" ] || continue
                            ;;
                        efi-system-partition|bios-grub|boot|samizdat-*|partition-table) ;;
                        *) die "invalid type: $type" ;;
                esac

                imgfile=$builddir/$img
                "$@" || return
        done
}

set_var()
{
        eval "conf_${img}_${1}=\$${1}"
}

format_guid()
{
    set -- "$(tr -dc 0-9a-fA-F <<< "$1")"
    printf '%s\n' "${1:0:8}-${1:8:4}-${1:12:4}-${1:16:4}-${1:20:12}"
}

get_partition_size()
{
    case "$type" in
        dm-verity-hashes|dm-verity-data)
            stat -L -c '%s' "$builddir"/"${f%.conf}"
            ;;
        *)
            require_var allocation
            case "$allocation" in
                *K) echo $(( ${allocation%?} * 1024 )) ;;
                *M) echo $(( ${allocation%?} * 1024 * 1024 )) ;;
                *G) echo $(( ${allocation%?} * 1024 * 1024 * 1024 )) ;;
                *) return 1 ;;
            esac
            ;;
    esac
}

create_ptable_conf()
{
        unset partuuid
        inquire_var partuuid
        devsz=$(get_partition_size) || return

        [ "$start" -a "$devsz" ] || return

        [ "$((devsz % 512))" -eq 0 ]

        start_sectors=$((start / 512))
        size_sectors=$((devsz / 512))

        typecode=0FC63DAF-8483-4772-8E79-3D69D8477DE4
        case "$type" in
                partition-table) start=$((start + devsz)); return;;
                efi-system-partition) typecode=C12A7328-F81F-11D2-BA4B-00A0C93EC93B ;;
                dm-verity-data|dm-verity-hashes)
                    case "$name" in
                        samizdat-rootfs|samizdat-root-patch) typecode=4f68bce3-e8cd-4db1-96e7-fbcaf984b709 ;;
                        samizdat-root-patch-verity) typecode=2c7357ed-ebd2-46d9-aec1-23d437ec2bf5 ;;
                        samizdat-root-seed) ;; # keep default
                        samizdat-root-seed-verity) ;; # keep default
                    esac
                    ;;
                root) typecode=4f68bce3-e8cd-4db1-96e7-fbcaf984b709 ;;
                bios-grub)
                    typecode=21686148-6449-6E6F-744E-656564454649
                    printf 'start=1, size=%d, type=ee\n' \
                           "$((start_sectors - 1))" \
                           >> "$DOS_TABLE_FILE"
                    printf 'start=%d, size=%d, type=83, bootable\n' \
                           "$start_sectors" \
                           "$size_sectors" \
                           >> "$DOS_TABLE_FILE"
                    printf 'start=%d, type=ee\n' \
                           "$((start_sectors + size_sectors))" \
                           >> "$DOS_TABLE_FILE"
                    ;;
        esac

        (
            exec >> "$GPT_TABLE_FILE"
            printf '%d: start=%d, size=%d, type=%s, name="%s"' \
                    "$i" \
                    "$start_sectors" \
                    "$size_sectors" \
                    "$typecode" \
                    "$name"
            if [ "$partuuid" ]
            then
                printf ', uuid=%s' "$(format_guid "$partuuid")"
            fi
            echo
        )
        let ++i
        start=$((start + devsz))
}

pee_on_table()
{
        GPT_TABLE_FILE=$builddir/table.gpt
        DOS_TABLE_FILE=$builddir/table.mbr

        printf 'label: gpt\n\n' > "$GPT_TABLE_FILE"
        : > "$DOS_TABLE_FILE"

        local dev="$1"
        i=1
        start=0
        iterate_partitions create_ptable_conf

        loudly $sudo sfdisk --no-tell-kernel "$dev" < "$GPT_TABLE_FILE" || return
        loudly $sudo sfdisk --no-tell-kernel -Y dos "$dev" < "$DOS_TABLE_FILE" || return
}

clone_parts_to_target()
{

        f=$(readlink -e "$builddir"/"${f%.conf}") || return
        samizdat-ficlonerange "$f" "$target" 0 0 0
}

cleanup()
{
    for f in part*.conf
    do
        mnt=${f%.conf}.mnt
        if mountpoint -q "$mnt"
        then
            loudly $sudo umount "$mnt"
            $sudo rmdir "$mnt"
        fi
    done
    if [ "$whole" ]
    then
        loudly $sudo kpartx -sd "$whole"
        loudly $sudo losetup -d "$whole"
    fi
}

mount_part()
{
    local source_dev="$1"
    case "$type" in
        efi-system-partition|boot|samizdat-keys) ;;
        dm-verity-hashes|partition-table|bios-grub) return ;;
        *) notice "Not mounting $name"; return ;;
    esac

    img=${f%.conf}
    mnt=${f%.conf}.mnt

    mkdir -p "$mnt"
    loudly $sudo mount "$source_dev" "$mnt"
}

copy_data_to_mounted_target_filesystems()
{
    mount_part "/dev/mapper/${whole#/dev/}p${img#part}"
    case "$type" in
        boot)
            BOOT_DIR=$mnt
            install_boot_dir "$mnt"
            ;;
        samizdat-keys)
            $sudo rsync  -a  --info=STATS  "$GPG_INPUT_DIR"/ "$mnt"/gnupghome/
            ;;
        efi-system-partition)
            EFI_DIR=$mnt
            ;;
    esac
}

shopt -s nullglob
PATH=/sbin:$PATH

: ${GRUB_CONFIG:=../conf/grub.cfg}
samizdat_linux_dir=/
builddir=_build

if [ "$UID" = 0 ]
then sudo=
else sudo=sudo
fi

if [ "$GPG_INPUT_DIR" ]
then
    $sudo [ -d "$GPG_INPUT_DIR" ]
else
    for d in /root/.gnupg /cdrom/gnupghome
    do
        $sudo [ -d "$d" ] || continue
        GPG_INPUT_DIR=$d
        break
    done
fi

SKIP_ROOTFS_COPY=
if [ "$1" = 'key' ]
then
    SKIP_ROOTFS_COPY=y
fi

set -e

mkdir -p "$builddir"

if [ "$SKIP_ROOTFS_COPY" ]
then
    target=key.img
else
    target=whole.img
fi






if whole=$(losetup -j "$target" -O NAME --noheadings)
then
    cleanup
    whole=
fi
truncate -s0 "$target"
iterate_partitions build_partition_image




iterate_partitions clone_parts_to_target
$sudo losetup -L -f "$target"
whole=$(losetup -j "$target" -O NAME --noheadings)







pee_on_table "$whole"





trap cleanup EXIT
$sudo kpartx -su "$whole"

iterate_partitions copy_data_to_mounted_target_filesystems

time loudly $sudo eatmydata -- grub-install --boot-directory="$BOOT_DIR" "$whole" --target=i386-pc
time loudly $sudo eatmydata -- grub-install --boot-directory="$BOOT_DIR" "$whole" --target=x86_64-efi --removable --efi-directory="$EFI_DIR" || true