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

5
6
set -e -u

7
8
export LANG=C

9
10
app_name=${0##*/}
arch=$(uname -m)
11
pkg_list=()
12
run_cmd=""
13
14
quiet="y"
pacman_conf="/etc/pacman.conf"
15
iso_label="ARCH_$(date +%Y%m)"
16
17
18
iso_publisher="Arch Linux <http://www.archlinux.org>"
iso_application="Arch Linux Live/Rescue CD"
install_dir="arch"
19
20
work_dir="work"
out_dir="out"
21
sfs_mode="sfs"
22
sfs_comp="xz"
23
gpg_key=
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# Show an INFO message
# $1: message string
_msg_info() {
    local _msg="${1}"
    echo "[mkarchiso] INFO: ${_msg}"
}

# 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}
    echo
    echo "[mkarchiso] ERROR: ${_msg}"
    echo
    if [[ ${_error} -gt 0 ]]; then
42
        exit "${_error}"
43
44
45
    fi
}

46
_chroot_init() {
47
    mkdir -p ${work_dir}/airootfs
48
    _pacman base syslinux
49
50
51
}

_chroot_run() {
52
    eval arch-chroot ${work_dir}/airootfs "${run_cmd}"
53
54
}

55
56
57
58
59
60
_mount_airootfs() {
    trap "_umount_airootfs" EXIT HUP INT TERM
    mkdir -p "${work_dir}/mnt/airootfs"
    _msg_info "Mounting '${work_dir}/airootfs.img' on '${work_dir}/mnt/airootfs'"
    mount "${work_dir}/airootfs.img" "${work_dir}/mnt/airootfs"
    _msg_info "Done!"
61
62
}

63
64
_umount_airootfs() {
    _msg_info "Unmounting '${work_dir}/mnt/airootfs'"
65
    umount -d "${work_dir}/mnt/airootfs"
66
67
    _msg_info "Done!"
    rmdir "${work_dir}/mnt/airootfs"
68
69
70
71
72
73
    trap - EXIT HUP INT TERM
}

# Show help usage, with an exit status.
# $1: exit status number.
_usage ()
74
{
75
    echo "usage ${app_name} [options] command <command options>"
76
    echo " general options:"
77
    echo "    -p PACKAGE(S)    Package(s) to install, can be used multiple times"
78
    echo "    -r <command>     Run <command> inside airootfs"
79
80
    echo "    -C <file>        Config file for pacman."
    echo "                     Default: '${pacman_conf}'"
81
    echo "    -L <label>       Set a label for the disk"
82
    echo "                     Default: '${iso_label}'"
83
    echo "    -P <publisher>   Set a publisher for the disk"
84
    echo "                     Default: '${iso_publisher}'"
85
    echo "    -A <application> Set an application name for the disk"
86
    echo "                     Default: '${iso_application}'"
87
    echo "    -D <install_dir> Set an install_dir. All files will by located here."
88
    echo "                     Default: '${install_dir}'"
89
    echo "                     NOTE: Max 8 characters, use only [a-z0-9]"
90
    echo "    -w <work_dir>    Set the working directory"
91
    echo "                     Default: '${work_dir}'"
92
    echo "    -o <out_dir>     Set the output directory"
93
    echo "                     Default: '${out_dir}'"
94
95
96
97
    echo "    -s <sfs_mode>    Set SquashFS image mode (img or sfs)"
    echo "                     img: prepare airootfs.sfs for dm-snapshot usage"
    echo "                     sfs: prepare airootfs.sfs for overlayfs usage"
    echo "                     Default: ${sfs_mode}"
98
    echo "    -c <comp_type>   Set SquashFS compression type (gzip, lzma, lzo, xz, zstd)"
99
    echo "                     Default: '${sfs_comp}'"
100
101
    echo "    -v               Enable verbose output"
    echo "    -h               This message"
102
    echo " commands:"
103
104
105
106
107
108
    echo "   init"
    echo "      Make base layout and install base group"
    echo "   install"
    echo "      Install all specified packages (-p)"
    echo "   run"
    echo "      run command specified by -r"
109
    echo "   prepare"
110
    echo "      build all images"
111
    echo "   pkglist"
112
    echo "      make a pkglist.txt of packages installed on airootfs"
113
    echo "   iso <image name>"
114
    echo "      build an iso image from the working dir"
115
    exit "${1}"
116
117
}

118
# Shows configuration according to command mode.
119
# $1: init | install | run | prepare | iso
120
121
122
123
124
125
126
127
128
_show_config () {
    local _mode="$1"
    echo
    _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
129
130
131
132
        init)
            _msg_info "       Pacman config file:   ${pacman_conf}"
            ;;
        install)
133
            _msg_info "       Pacman config file:   ${pacman_conf}"
134
            _msg_info "                 Packages:   ${pkg_list[*]}"
135
            ;;
136
137
138
        run)
            _msg_info "              Run command:   ${run_cmd}"
            ;;
139
140
        prepare)
            ;;
141
142
        pkglist)
            ;;
143
144
145
146
147
148
        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}"
            ;;
149
    esac
150
151
    echo
}
152

153
# Install desired packages to airootfs
154
155
_pacman ()
{
156
    _msg_info "Installing packages to '${work_dir}/airootfs/'..."
157
158

    if [[ "${quiet}" = "y" ]]; then
159
        pacstrap -C "${pacman_conf}" -c -G -M "${work_dir}/airootfs" "$@" &> /dev/null
160
    else
161
        pacstrap -C "${pacman_conf}" -c -G -M "${work_dir}/airootfs" "$@"
162
    fi
163

164
    _msg_info "Packages installed successfully!"
165
166
}

167
# Cleanup airootfs
168
_cleanup () {
169
    _msg_info "Cleaning up what we can on airootfs..."
170

171
    # Delete initcpio image(s)
172
173
    if [[ -d "${work_dir}/airootfs/boot" ]]; then
        find "${work_dir}/airootfs/boot" -type f -name '*.img' -delete
174
175
    fi
    # Delete kernel(s)
176
177
    if [[ -d "${work_dir}/airootfs/boot" ]]; then
        find "${work_dir}/airootfs/boot" -type f -name 'vmlinuz*' -delete
178
179
    fi
    # Delete pacman database sync cache files (*.tar.gz)
180
181
    if [[ -d "${work_dir}/airootfs/var/lib/pacman" ]]; then
        find "${work_dir}/airootfs/var/lib/pacman" -maxdepth 1 -type f -delete
182
183
    fi
    # Delete pacman database sync cache
184
185
    if [[ -d "${work_dir}/airootfs/var/lib/pacman/sync" ]]; then
        find "${work_dir}/airootfs/var/lib/pacman/sync" -delete
186
187
    fi
    # Delete pacman package cache
188
189
    if [[ -d "${work_dir}/airootfs/var/cache/pacman/pkg" ]]; then
        find "${work_dir}/airootfs/var/cache/pacman/pkg" -type f -delete
190
191
    fi
    # Delete all log files, keeps empty dirs.
192
193
    if [[ -d "${work_dir}/airootfs/var/log" ]]; then
        find "${work_dir}/airootfs/var/log" -type f -delete
194
195
    fi
    # Delete all temporary files and dirs
196
197
    if [[ -d "${work_dir}/airootfs/var/tmp" ]]; then
        find "${work_dir}/airootfs/var/tmp" -mindepth 1 -delete
198
    fi
199
    # Delete package pacman related files.
200
    find "${work_dir}" \( -name "*.pacnew" -o -name "*.pacsave" -o -name "*.pacorig" \) -delete
201
    _msg_info "Done!"
202
}
203

204
205
# Makes a ext4 filesystem inside a SquashFS from a source directory.
_mkairootfs_img () {
206
207
    if [[ ! -e "${work_dir}/airootfs" ]]; then
        _msg_error "The path '${work_dir}/airootfs' does not exist" 1
208
209
    fi

210
    _msg_info "Creating ext4 image of 32GiB..."
211
    truncate -s 32G "${work_dir}/airootfs.img"
212
    local _qflag=""
213
    if [[ "${quiet}" == "y" ]]; then
214
        _qflag="-q"
215
    fi
216
    mkfs.ext4 ${_qflag} -O ^has_journal,^resize_inode -E lazy_itable_init=0 -m 0 -F "${work_dir}/airootfs.img"
217
    tune2fs -c 0 -i 0 "${work_dir}/airootfs.img" &> /dev/null
218
    _msg_info "Done!"
219
    _mount_airootfs
220
221
    _msg_info "Copying '${work_dir}/airootfs/' to '${work_dir}/mnt/airootfs/'..."
    cp -aT "${work_dir}/airootfs/" "${work_dir}/mnt/airootfs/"
222
    chown root:root "${work_dir}/mnt/airootfs/"
223
    _msg_info "Done!"
224
    _umount_airootfs
225
    mkdir -p "${work_dir}/iso/${install_dir}/${arch}"
226
227
    _msg_info "Creating SquashFS image, this may take some time..."
    if [[ "${quiet}" = "y" ]]; then
228
229
        mksquashfs "${work_dir}/airootfs.img" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
            -comp "${sfs_comp}" -no-progress &> /dev/null
230
    else
231
        mksquashfs "${work_dir}/airootfs.img" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
232
            -comp "${sfs_comp}"
233
234
    fi
    _msg_info "Done!"
235
    rm "${work_dir}/airootfs.img"
236
237
}

238
239
240
241
242
243
244
245
246
# Makes a SquashFS filesystem from a source directory.
_mkairootfs_sfs () {
    if [[ ! -e "${work_dir}/airootfs" ]]; then
        _msg_error "The path '${work_dir}/airootfs' does not exist" 1
    fi

    mkdir -p "${work_dir}/iso/${install_dir}/${arch}"
    _msg_info "Creating SquashFS image, this may take some time..."
    if [[ "${quiet}" = "y" ]]; then
247
248
        mksquashfs "${work_dir}/airootfs" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
            -comp "${sfs_comp}" -no-progress &> /dev/null
249
    else
250
        mksquashfs "${work_dir}/airootfs" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs" -noappend \
251
            -comp "${sfs_comp}"
252
253
254
255
    fi
    _msg_info "Done!"
}

256
257
258
_mkchecksum () {
    _msg_info "Creating checksum file for self-test..."
    cd "${work_dir}/iso/${install_dir}/${arch}"
259
    sha512sum airootfs.sfs > airootfs.sha512
260
    cd "${OLDPWD}"
261
    _msg_info "Done!"
262
263
}

264
265
266
_mksignature () {
    _msg_info "Creating signature file..."
    cd "${work_dir}/iso/${install_dir}/${arch}"
267
268
    gpg --detach-sign --default-key "${gpg_key}" airootfs.sfs
    cd "${OLDPWD}"
269
270
271
    _msg_info "Done!"
}

272
273
274
command_pkglist () {
    _show_config pkglist

275
    _msg_info "Creating a list of installed packages on live-enviroment..."
276
    pacman -Q --sysroot "${work_dir}/airootfs" > \
277
278
        "${work_dir}/iso/${install_dir}/pkglist.${arch}.txt"
    _msg_info "Done!"
279
280
281

}

282
# Create an ISO9660 filesystem from "iso" directory.
283
command_iso () {
284
    local _iso_efi_boot_args=()
285

286
287
288
    if [[ ! -f "${work_dir}/iso/isolinux/isolinux.bin" ]]; then
         _msg_error "The file '${work_dir}/iso/isolinux/isolinux.bin' does not exist." 1
    fi
289
290
291
    if [[ ! -f "${work_dir}/iso/isolinux/isohdpfx.bin" ]]; then
         _msg_error "The file '${work_dir}/iso/isolinux/isohdpfx.bin' does not exist." 1
    fi
292

293
294
    # 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
295
296
297
298
299
300
        _iso_efi_boot_args+=(
            '-eltorito-alt-boot'
            '-e' 'EFI/archiso/efiboot.img'
            '-no-emul-boot'
            '-isohybrid-gpt-basdat'
        )
301
302
    fi

303
304
    _show_config iso

305
    mkdir -p "${out_dir}"
306
307
    _msg_info "Creating ISO image..."
    local _qflag=""
308
    if [[ "${quiet}" == "y" ]]; then
309
310
311
        xorriso -as mkisofs -quiet \
            -iso-level 3 \
            -full-iso9660-filenames \
312
            -rational-rock \
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
            -volid "${iso_label}" \
            -appid "${iso_application}" \
            -publisher "${iso_publisher}" \
            -preparer "prepared by mkarchiso" \
            -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 \
328
            -rational-rock \
329
330
331
332
333
334
335
336
337
338
339
            -volid "${iso_label}" \
            -appid "${iso_application}" \
            -publisher "${iso_publisher}" \
            -preparer "prepared by mkarchiso" \
            -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/"
340
    fi
341
    _msg_info "Done! | $(ls -sh "${out_dir}/${img_name}")"
342
343
}

344
# create airootfs.sfs filesystem, and push it in "iso" directory.
345
346
347
348
command_prepare () {
    _show_config prepare

    _cleanup
349
    if [[ "${sfs_mode}" == "sfs" ]]; then
350
351
352
353
        _mkairootfs_sfs
    else
        _mkairootfs_img
    fi
354
    _mkchecksum
355
    if [[ "${gpg_key}" ]]; then
Gerardo Exequiel Pozzi's avatar
Gerardo Exequiel Pozzi committed
356
357
      _mksignature
    fi
358
359
}

360
# Install packages on airootfs.
361
# A basic check to avoid double execution/reinstallation is done via hashing package names.
362
command_install () {
363
364
365
366
    if [[ ! -f "${pacman_conf}" ]]; then
        _msg_error "Pacman config file '${pacman_conf}' does not exist" 1
    fi

367
    if [[ "${#pkg_list[@]}" -eq 0 ]]; then
368
369
370
371
        _msg_error "Packages must be specified" 0
        _usage 1
    fi

372
    _show_config install
373

374
    _pacman "${pkg_list[@]}"
375
376
}

377
378
379
380
381
382
383
384
385
command_init() {
    _show_config init
    _chroot_init
}

command_run() {
    _show_config run
    _chroot_run
}
386

387
if [[ "${EUID}" -ne 0 ]]; then
388
389
390
    _msg_error "This script must be run as root." 1
fi

391
392
umask 0022

393
while getopts 'p:r:C:L:P:A:D:w:o:s:c:g:vh' arg; do
394
    case "${arg}" in
395
396
        p)
            read -r -a opt_pkg_list <<< "${OPTARG}"
397
398
            pkg_list+=("${opt_pkg_list[@]}")
            ;;
399
        r) run_cmd="${OPTARG}" ;;
400
401
402
403
404
        C) pacman_conf="${OPTARG}" ;;
        L) iso_label="${OPTARG}" ;;
        P) iso_publisher="${OPTARG}" ;;
        A) iso_application="${OPTARG}" ;;
        D) install_dir="${OPTARG}" ;;
405
406
        w) work_dir="${OPTARG}" ;;
        o) out_dir="${OPTARG}" ;;
407
        s) sfs_mode="${OPTARG}" ;;
408
        c) sfs_comp="${OPTARG}" ;;
409
        g) gpg_key="${OPTARG}" ;;
410
411
412
413
414
415
416
417
418
419
420
421
422
423
        v) quiet="n" ;;
        h|?) _usage 0 ;;
        *)
            _msg_error "Invalid argument '${arg}'" 0
            _usage 1
            ;;
    esac
done

shift $((OPTIND - 1))

if [[ $# -lt 1 ]]; then
    _msg_error "No command specified" 0
    _usage 1
424
fi
425
426
427
command_name="${1}"

case "${command_name}" in
428
429
430
431
432
433
434
435
    init)
        command_init
        ;;
    install)
        command_install
        ;;
    run)
        command_run
436
437
438
439
        ;;
    prepare)
        command_prepare
        ;;
440
441
442
    pkglist)
        command_pkglist
        ;;
443
    iso)
444
        if [[ $# -lt 2 ]]; then
445
446
447
            _msg_error "No image specified" 0
            _usage 1
        fi
448
        img_name="${2}"
449
450
451
452
453
454
455
        command_iso
        ;;
    *)
        _msg_error "Invalid command name '${command_name}'" 0
        _usage 1
        ;;
esac
456
457

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