mkarchiso 14.3 KB
Newer Older
1
#!/bin/bash
2
3
#
# SPDX-License-Identifier: GPL-3.0-or-later
4

5
6
set -e -u

7
8
9
10
# Control the environment
umask 0022
export LANG="C"
export SOURCE_DATE_EPOCH="${SOURCE_DATE_EPOCH:-"$(date +%s)"}"
11

12
# mkarchiso defaults
13
app_name="${0##*/}"
14
pkg_list=()
15
run_cmd=""
16
quiet="y"
17
18
work_dir="work"
out_dir="out"
19
img_name="${app_name}.iso"
20
sfs_mode="sfs"
21
sfs_comp="xz"
22
gpg_key=""
23

24
25
26
27
28
29
30
31
32
33
34
35
36
# profile defaults
profile=""
iso_name="${app_name}"
iso_label="${app_name^^}"
iso_publisher="${app_name}"
iso_application="${app_name} iso"
iso_version=""
install_dir="${app_name}"
arch="$(uname -m)"
pacman_conf="/etc/pacman.conf"
bootmodes=()


37
38
39
40
# Show an INFO message
# $1: message string
_msg_info() {
    local _msg="${1}"
41
42
    [[ "${quiet}" == "y" ]] || printf '[%s] INFO: %s\n' "${app_name}" "${_msg}"

43
44
}

45
46
47
48
49
50
51
# Show a WARNING message
# $1: message string
_msg_warning() {
    local _msg="${1}"
    printf '\n[%s] WARNING: %s\n\n' "${app_name}" "${_msg}" >&2
}

52
53
54
55
56
57
# Show an ERROR message then exit with status
# $1: message string
# $2: exit code number (with 0 does not exit)
_msg_error() {
    local _msg="${1}"
    local _error=${2}
58
59
    printf '\n[%s] ERROR: %s\n\n' "${app_name}" "${_msg}" >&2
    if (( _error > 0 )); then
60
        exit "${_error}"
61
62
63
    fi
}

64
_chroot_init() {
65
    mkdir -p -- "${airootfs_dir}"
66
    _pacman base syslinux
67
68
69
}

_chroot_run() {
70
    eval -- arch-chroot "${airootfs_dir}" "${run_cmd}"
71
72
}

73
74
_mount_airootfs() {
    trap "_umount_airootfs" EXIT HUP INT TERM
75
    mkdir -p -- "${work_dir}/mnt/airootfs"
76
77
    _msg_info "Mounting '${airootfs_dir}.img' on '${work_dir}/mnt/airootfs'"
    mount -- "${airootfs_dir}.img" "${work_dir}/mnt/airootfs"
78
    _msg_info "Done!"
79
80
}

81
82
_umount_airootfs() {
    _msg_info "Unmounting '${work_dir}/mnt/airootfs'"
83
    umount -d -- "${work_dir}/mnt/airootfs"
84
    _msg_info "Done!"
85
    rmdir -- "${work_dir}/mnt/airootfs"
86
87
88
89
90
    trap - EXIT HUP INT TERM
}

# Show help usage, with an exit status.
# $1: exit status number.
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
_usage () {
    IFS='' read -r -d '' usagetext <<ENDUSAGETEXT || true
usage ${app_name} [options] command <command options>
 general options:
    -p PACKAGE(S)    Package(s) to install, can be used multiple times
    -r <command>     Run <command> inside airootfs
    -C <file>        Config file for pacman.
                     Default: '${pacman_conf}'
    -L <label>       Set a label for the disk
                     Default: '${iso_label}'
    -P <publisher>   Set a publisher for the disk
                     Default: '${iso_publisher}'
    -A <application> Set an application name for the disk
                     Default: '${iso_application}'
    -D <install_dir> Set an install_dir. All files will by located here.
                     Default: '${install_dir}'
                     NOTE: Max 8 characters, use only [a-z0-9]
    -w <work_dir>    Set the working directory
                     Default: '${work_dir}'
    -o <out_dir>     Set the output directory
                     Default: '${out_dir}'
    -s <sfs_mode>    Set SquashFS image mode (img or sfs)
                     img: prepare airootfs.sfs for dm-snapshot usage
                     sfs: prepare airootfs.sfs for overlayfs usage
                     Default: '${sfs_mode}'
    -c <comp_type>   Set SquashFS compression type (gzip, lzma, lzo, xz, zstd)
                     Default: '${sfs_comp}'
    -v               Enable verbose output
    -h               This message
 commands:
   init
      Make base layout and install base group
   install
      Install all specified packages (-p)
   run
      run command specified by -r
   prepare
      build all images
   pkglist
      make a pkglist.txt of packages installed on airootfs
   iso <image name>
      build an iso image from the working dir
ENDUSAGETEXT
    printf '%s\n' "${usagetext}"
135
    exit "${1}"
136
137
}

138
# Shows configuration according to command mode.
139
# $1: init | install | run | prepare | iso
140
141
_show_config () {
    local _mode="$1"
142
    printf '\n'
143
144
145
146
147
148
    _msg_info "Configuration settings"
    _msg_info "                  Command:   ${command_name}"
    _msg_info "             Architecture:   ${arch}"
    _msg_info "        Working directory:   ${work_dir}"
    _msg_info "   Installation directory:   ${install_dir}"
    case "${_mode}" in
149
150
151
152
        init)
            _msg_info "       Pacman config file:   ${pacman_conf}"
            ;;
        install)
153
            _msg_info "       Pacman config file:   ${pacman_conf}"
154
            _msg_info "                 Packages:   ${pkg_list[*]}"
155
            ;;
156
157
158
        run)
            _msg_info "              Run command:   ${run_cmd}"
            ;;
159
160
        prepare)
            ;;
161
162
        pkglist)
            ;;
163
164
165
166
167
168
        iso)
            _msg_info "               Image name:   ${img_name}"
            _msg_info "               Disk label:   ${iso_label}"
            _msg_info "           Disk publisher:   ${iso_publisher}"
            _msg_info "         Disk application:   ${iso_application}"
            ;;
169
    esac
170
    printf '\n'
171
}
172

173
# Install desired packages to airootfs
174
_pacman () {
175
    _msg_info "Installing packages to '${airootfs_dir}/'..."
176
177

    if [[ "${quiet}" = "y" ]]; then
178
        pacstrap -C "${pacman_conf}" -c -G -M -- "${airootfs_dir}" "$@" &> /dev/null
179
    else
180
        pacstrap -C "${pacman_conf}" -c -G -M -- "${airootfs_dir}" "$@"
181
    fi
182

183
    _msg_info "Packages installed successfully!"
184
185
}

186
# Cleanup airootfs
187
_cleanup () {
188
    _msg_info "Cleaning up what we can on airootfs..."
189

190
    # Delete initcpio image(s)
191
192
    if [[ -d "${airootfs_dir}/boot" ]]; then
        find "${airootfs_dir}/boot" -type f -name '*.img' -delete
193
194
    fi
    # Delete kernel(s)
195
196
    if [[ -d "${airootfs_dir}/boot" ]]; then
        find "${airootfs_dir}/boot" -type f -name 'vmlinuz*' -delete
197
198
    fi
    # Delete pacman database sync cache files (*.tar.gz)
199
200
    if [[ -d "${airootfs_dir}/var/lib/pacman" ]]; then
        find "${airootfs_dir}/var/lib/pacman" -maxdepth 1 -type f -delete
201
202
    fi
    # Delete pacman database sync cache
203
204
    if [[ -d "${airootfs_dir}/var/lib/pacman/sync" ]]; then
        find "${airootfs_dir}/var/lib/pacman/sync" -delete
205
206
    fi
    # Delete pacman package cache
207
208
    if [[ -d "${airootfs_dir}/var/cache/pacman/pkg" ]]; then
        find "${airootfs_dir}/var/cache/pacman/pkg" -type f -delete
209
210
    fi
    # Delete all log files, keeps empty dirs.
211
212
    if [[ -d "${airootfs_dir}/var/log" ]]; then
        find "${airootfs_dir}/var/log" -type f -delete
213
214
    fi
    # Delete all temporary files and dirs
215
216
    if [[ -d "${airootfs_dir}/var/tmp" ]]; then
        find "${airootfs_dir}/var/tmp" -mindepth 1 -delete
217
    fi
218
    # Delete package pacman related files.
219
    find "${work_dir}" \( -name '*.pacnew' -o -name '*.pacsave' -o -name '*.pacorig' \) -delete
220
    _msg_info "Done!"
221
}
222

223
224
# Makes a ext4 filesystem inside a SquashFS from a source directory.
_mkairootfs_img () {
225
226
    if [[ ! -e "${airootfs_dir}" ]]; then
        _msg_error "The path '${airootfs_dir}' does not exist" 1
227
228
    fi

229
    _msg_info "Creating ext4 image of 32GiB..."
230
    truncate -s 32G -- "${airootfs_dir}.img"
231
    if [[ "${quiet}" == "y" ]]; then
232
        mkfs.ext4 -q -O '^has_journal,^resize_inode' -E 'lazy_itable_init=0' -m 0 -F -- "${airootfs_dir}.img"
233
    else
234
        mkfs.ext4 -O '^has_journal,^resize_inode' -E 'lazy_itable_init=0' -m 0 -F -- "${airootfs_dir}.img"
235
    fi
236
    tune2fs -c 0 -i 0 -- "${airootfs_dir}.img" &> /dev/null
237
    _msg_info "Done!"
238
    _mount_airootfs
239
240
    _msg_info "Copying '${airootfs_dir}/' to '${work_dir}/mnt/airootfs/'..."
    cp -aT -- "${airootfs_dir}/" "${work_dir}/mnt/airootfs/"
241
    chown root:root -- "${work_dir}/mnt/airootfs/"
242
    _msg_info "Done!"
243
    _umount_airootfs
244
    mkdir -p -- "${work_dir}/iso/${install_dir}/${arch}"
245
246
    _msg_info "Creating SquashFS image, this may take some time..."
    if [[ "${quiet}" = "y" ]]; then
247
        mksquashfs "${airootfs_dir}.img" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
248
            -comp "${sfs_comp}" -no-progress &> /dev/null
249
    else
250
        mksquashfs "${airootfs_dir}.img" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
251
            -comp "${sfs_comp}"
252
253
    fi
    _msg_info "Done!"
254
    rm -- "${airootfs_dir}.img"
255
256
}

257
258
# Makes a SquashFS filesystem from a source directory.
_mkairootfs_sfs () {
259
260
    if [[ ! -e "${airootfs_dir}" ]]; then
        _msg_error "The path '${airootfs_dir}' does not exist" 1
261
262
    fi

263
    mkdir -p -- "${work_dir}/iso/${install_dir}/${arch}"
264
265
    _msg_info "Creating SquashFS image, this may take some time..."
    if [[ "${quiet}" = "y" ]]; then
266
        mksquashfs "${airootfs_dir}" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
267
            -comp "${sfs_comp}" -no-progress &> /dev/null
268
    else
269
        mksquashfs "${airootfs_dir}" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
270
            -comp "${sfs_comp}"
271
272
273
274
    fi
    _msg_info "Done!"
}

275
276
_mkchecksum () {
    _msg_info "Creating checksum file for self-test..."
277
    cd -- "${work_dir}/iso/${install_dir}/${arch}"
278
    sha512sum airootfs.sfs > airootfs.sha512
279
    cd -- "${OLDPWD}"
280
    _msg_info "Done!"
281
282
}

283
284
_mksignature () {
    _msg_info "Creating signature file..."
285
    cd -- "${work_dir}/iso/${install_dir}/${arch}"
286
    gpg --detach-sign --default-key "${gpg_key}" airootfs.sfs
287
    cd -- "${OLDPWD}"
288
289
290
    _msg_info "Done!"
}

291

292
293
294
command_pkglist () {
    _show_config pkglist

295
    _msg_info "Creating a list of installed packages on live-enviroment..."
296
    pacman -Q --sysroot "${airootfs_dir}" > "${work_dir}/iso/${install_dir}/pkglist.${arch}.txt"
297
    _msg_info "Done!"
298
299
300

}

301
# Create an ISO9660 filesystem from "iso" directory.
302
command_iso () {
303
    local _iso_efi_boot_args=()
304

305
306
307
    if [[ ! -f "${work_dir}/iso/isolinux/isolinux.bin" ]]; then
         _msg_error "The file '${work_dir}/iso/isolinux/isolinux.bin' does not exist." 1
    fi
308
309
310
    if [[ ! -f "${work_dir}/iso/isolinux/isohdpfx.bin" ]]; then
         _msg_error "The file '${work_dir}/iso/isolinux/isohdpfx.bin' does not exist." 1
    fi
311

312
313
    # If exists, add an EFI "El Torito" boot image (FAT filesystem) to ISO-9660 image.
    if [[ -f "${work_dir}/iso/EFI/archiso/efiboot.img" ]]; then
314
315
316
317
318
319
        _iso_efi_boot_args+=(
            '-eltorito-alt-boot'
            '-e' 'EFI/archiso/efiboot.img'
            '-no-emul-boot'
            '-isohybrid-gpt-basdat'
        )
320
321
    fi

322
323
    _show_config iso

324
    mkdir -p -- "${out_dir}"
325
326
    _msg_info "Creating ISO image..."
    local _qflag=""
327
    if [[ "${quiet}" == "y" ]]; then
328
329
330
        xorriso -as mkisofs -quiet \
            -iso-level 3 \
            -full-iso9660-filenames \
331
            -rational-rock \
332
333
334
            -volid "${iso_label}" \
            -appid "${iso_application}" \
            -publisher "${iso_publisher}" \
335
            -preparer "prepared by ${app_name}" \
336
337
338
339
340
341
342
343
344
345
346
            -eltorito-boot isolinux/isolinux.bin \
            -eltorito-catalog isolinux/boot.cat \
            -no-emul-boot -boot-load-size 4 -boot-info-table \
            -isohybrid-mbr "${work_dir}/iso/isolinux/isohdpfx.bin" \
            "${_iso_efi_boot_args[@]}" \
            -output "${out_dir}/${img_name}" \
            "${work_dir}/iso/"
    else
        xorriso -as mkisofs \
            -iso-level 3 \
            -full-iso9660-filenames \
347
            -rational-rock \
348
349
350
            -volid "${iso_label}" \
            -appid "${iso_application}" \
            -publisher "${iso_publisher}" \
351
            -preparer "prepared by ${app_name}" \
352
353
354
355
356
357
358
            -eltorito-boot isolinux/isolinux.bin \
            -eltorito-catalog isolinux/boot.cat \
            -no-emul-boot -boot-load-size 4 -boot-info-table \
            -isohybrid-mbr "${work_dir}/iso/isolinux/isohdpfx.bin" \
            "${_iso_efi_boot_args[@]}" \
            -output "${out_dir}/${img_name}" \
            "${work_dir}/iso/"
359
    fi
360
    _msg_info "Done! | $(ls -sh -- "${out_dir}/${img_name}")"
361
362
}

363
# create airootfs.sfs filesystem, and push it in "iso" directory.
364
365
366
367
command_prepare () {
    _show_config prepare

    _cleanup
368
    if [[ "${sfs_mode}" == "sfs" ]]; then
369
370
371
372
        _mkairootfs_sfs
    else
        _mkairootfs_img
    fi
373
    _mkchecksum
374
    if [[ "${gpg_key}" ]]; then
Gerardo Exequiel Pozzi's avatar
Gerardo Exequiel Pozzi committed
375
376
      _mksignature
    fi
377
378
}

379
# Install packages on airootfs.
380
# A basic check to avoid double execution/reinstallation is done via hashing package names.
381
command_install () {
382
383
384
385
    if [[ ! -f "${pacman_conf}" ]]; then
        _msg_error "Pacman config file '${pacman_conf}' does not exist" 1
    fi

386
    if (( ${#pkg_list[@]} == 0 )); then
387
388
389
390
        _msg_error "Packages must be specified" 0
        _usage 1
    fi

391
    _show_config install
392

393
    _pacman "${pkg_list[@]}"
394
395
}

396
397
398
399
400
401
402
403
404
command_init() {
    _show_config init
    _chroot_init
}

command_run() {
    _show_config run
    _chroot_run
}
405

406
407
408
409
410
411
412
413
414
command_build_profile() {
    _msg_warning "The ${FUNCNAME[0]#command_} command is not fully implemented yet :("
    # Set up essential directory paths
    airootfs_dir="${work_dir}/${arch}/airootfs"

    exit 1
}

while getopts 'B:p:r:C:L:P:A:D:w:o:s:c:g:vh' arg; do
415
    case "${arg}" in
416
417
418
        B)
            profile="$(realpath -- "${OPTARG}")"
            ;;
419
420
        p)
            read -r -a opt_pkg_list <<< "${OPTARG}"
421
422
            pkg_list+=("${opt_pkg_list[@]}")
            ;;
423
        r) run_cmd="${OPTARG}" ;;
424
        C) pacman_conf="$(realpath -- "${OPTARG}")" ;;
425
426
427
428
        L) iso_label="${OPTARG}" ;;
        P) iso_publisher="${OPTARG}" ;;
        A) iso_application="${OPTARG}" ;;
        D) install_dir="${OPTARG}" ;;
429
430
        w) work_dir="$(realpath -- "${OPTARG}")" ;;
        o) out_dir="$(realpath -- "${OPTARG}")" ;;
431
        s) sfs_mode="${OPTARG}" ;;
432
        c) sfs_comp="${OPTARG}" ;;
433
        g) gpg_key="${OPTARG}" ;;
434
435
436
437
438
439
440
441
442
        v) quiet="n" ;;
        h|?) _usage 0 ;;
        *)
            _msg_error "Invalid argument '${arg}'" 0
            _usage 1
            ;;
    esac
done

443
444
445
446
if (( EUID != 0 )); then
    _msg_error "${app_name} must be run as root." 1
fi

447
448
shift $((OPTIND - 1))

449
if (( $# < 1 )); then
450
451
    _msg_error "No command specified" 0
    _usage 1
452
fi
453
454
command_name="${1}"

455
456
457
# Set directory path defaults
airootfs_dir="${work_dir}/airootfs"

458
case "${command_name}" in
459
460
461
462
463
464
465
466
    init)
        command_init
        ;;
    install)
        command_install
        ;;
    run)
        command_run
467
468
469
470
        ;;
    prepare)
        command_prepare
        ;;
471
472
473
    pkglist)
        command_pkglist
        ;;
474
    iso)
475
        if (( $# < 2 )); then
476
477
478
            _msg_error "No image specified" 0
            _usage 1
        fi
479
        img_name="${2}"
480
481
        command_iso
        ;;
482
483
484
    build_profile)
        command_build_profile
        ;;
485
486
487
488
489
    *)
        _msg_error "Invalid command name '${command_name}'" 0
        _usage 1
        ;;
esac
490
491

# vim:ts=4:sw=4:et: