mkarchiso 13.8 KB
Newer Older
1
#!/bin/bash
2

3
4
set -e -u

5
6
export LANG=C

7
8
9
app_name=${0##*/}
arch=$(uname -m)
pkg_list=""
10
run_cmd=""
11
12
13
14
15
16
quiet="y"
pacman_conf="/etc/pacman.conf"
export iso_label="ARCH_$(date +%Y%m)"
iso_publisher="Arch Linux <http://www.archlinux.org>"
iso_application="Arch Linux Live/Rescue CD"
install_dir="arch"
17
18
work_dir="work"
out_dir="out"
19
sfs_comp="xz"
20
21
22
23
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
        exit ${_error}
    fi
}

42
_chroot_init() {
43
44
45
    if [[ -f "${work_dir}/mkarchiso.init" ]]; then
        _msg_info "Initial enviroment already installed, skipping."
    else
46
        mkdir -p ${work_dir}/airootfs
47
        _pacman "base syslinux"
48
        : > "${work_dir}/mkarchiso.init"
49
50
51
52
    fi
}

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

56
57
58
59
60
61
_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!"
62
63
}

64
65
66
67
68
_umount_airootfs() {
    _msg_info "Unmounting '${work_dir}/mnt/airootfs'"
    umount "${work_dir}/mnt/airootfs"
    _msg_info "Done!"
    rmdir "${work_dir}/mnt/airootfs"
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
    trap - EXIT HUP INT TERM
}

# Compare if a file/directory (source) is newer than other file (target)
# $1: source file/directory
# $2: target file
# return: 0 if target does not exists or if target is older than source.
#         1 if target is newer than source
_is_directory_changed() {
    local _src="${1}"
    local _dst="${2}"

    if [ -e "${_dst}" ]; then
        if [[ $(find ${_src} -newer ${_dst} | wc -l) -gt 0 ]]; then
            _msg_info "Target '${_dst}' is older than '${_src}', updating."
            rm -f "${_dst}"
            return 0
        else
            _msg_info "Target '${_dst}' is up to date with '${_src}', skipping."
            return 1
        fi
    else
        _msg_info "Target '${_dst}' does not exist, making it from '${_src}'"
        return 0
    fi
}

# Show help usage, with an exit status.
# $1: exit status number.
_usage ()
99
{
100
    echo "usage ${app_name} [options] command <command options>"
101
    echo " general options:"
102
    echo "    -p PACKAGE(S)    Package(s) to install, can be used multiple times"
103
    echo "    -r <command>     Run <command> inside airootfs"
104
105
    echo "    -C <file>        Config file for pacman."
    echo "                     Default: '${pacman_conf}'"
106
    echo "    -L <label>       Set a label for the disk"
107
    echo "                     Default: '${iso_label}'"
108
    echo "    -P <publisher>   Set a publisher for the disk"
109
    echo "                     Default: '${iso_publisher}'"
110
    echo "    -A <application> Set an application name for the disk"
111
    echo "                     Default: '${iso_application}'"
112
    echo "    -D <install_dir> Set an install_dir. All files will by located here."
113
    echo "                     Default: '${install_dir}'"
114
    echo "                     NOTE: Max 8 characters, use only [a-z0-9]"
115
    echo "    -w <work_dir>    Set the working directory"
116
    echo "                     Default: '${work_dir}'"
117
    echo "    -o <out_dir>     Set the output directory"
118
    echo "                     Default: '${out_dir}'"
119
120
    echo "    -c <comp_type>   Set SquashFS compression type (gzip, lzma, lzo, xz)"
    echo "                     Default: '${sfs_comp}'"
121
122
    echo "    -v               Enable verbose output"
    echo "    -h               This message"
123
    echo " commands:"
124
125
126
127
128
129
    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"
130
    echo "   prepare"
131
    echo "      build all images"
132
    echo "   checksum"
133
    echo "      make a checksum.md5 for self-test"
134
    echo "   pkglist"
135
    echo "      make a pkglist.txt of packages installed on airootfs"
136
    echo "   iso <image name>"
137
    echo "      build an iso image from the working dir"
138
    exit ${1}
139
140
}

141
# Shows configuration according to command mode.
142
# $1: init | install | run | prepare | checksum | iso
143
144
145
146
147
148
149
150
151
_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
152
153
154
155
        init)
            _msg_info "       Pacman config file:   ${pacman_conf}"
            ;;
        install)
156
157
158
            _msg_info "       Pacman config file:   ${pacman_conf}"
            _msg_info "                 Packages:   ${pkg_list}"
            ;;
159
160
161
        run)
            _msg_info "              Run command:   ${run_cmd}"
            ;;
162
163
        prepare)
            ;;
164
165
        checksum)
            ;;
166
167
        pkglist)
            ;;
168
169
170
171
172
173
        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}"
            ;;
174
    esac
175
176
    echo
}
177

178
# Install desired packages to airootfs
179
180
_pacman ()
{
181
    _msg_info "Installing packages to '${work_dir}/airootfs/'..."
182
183

    if [[ "${quiet}" = "y" ]]; then
184
        pacstrap -C "${pacman_conf}" -c -d -G -M "${work_dir}/airootfs" $* &> /dev/null
185
    else
186
        pacstrap -C "${pacman_conf}" -c -d -G -M "${work_dir}/airootfs" $*
187
    fi
188

189
    _msg_info "Packages installed successfully!"
190
191
}

192
# Cleanup airootfs
193
_cleanup () {
194
    _msg_info "Cleaning up what we can on airootfs..."
195

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

229
# Makes a filesystem from a source directory.
230
231
232
_mkairootfs () {
    if [[ ! -e "${work_dir}/airootfs" ]]; then
        _msg_error "The path '${work_dir}/airootfs' does not exist" 1
233
234
    fi

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

259
260
261
command_checksum () {
    _show_config checksum

262
263
264
265
    local _chk_arch

    for _chk_arch in i686 x86_64; do
        if _is_directory_changed "${work_dir}/iso/${install_dir}" "${work_dir}/iso/${install_dir}/checksum.${_chk_arch}.md5"; then
266
            _msg_info "Creating checksum file for self-test (${_chk_arch})..."
267
268
            cd "${work_dir}/iso/${install_dir}"
            if [[ -d "${_chk_arch}" ]]; then
269
                find ${_chk_arch} -type f -print0 | xargs -0 md5sum > checksum.${_chk_arch}.md5
270
271
272
273
274
            fi
            cd ${OLDPWD}
            _msg_info "Done!"
        fi
    done
275
276
}

277
278
279
command_pkglist () {
    _show_config pkglist

280
    if _is_directory_changed "${work_dir}/airootfs/var/lib/pacman/local" "${work_dir}/iso/${install_dir}/pkglist.${arch}.txt"; then
281
        _msg_info "Creating a list of installed packages on live-enviroment..."
282
        pacman -Sl -r "${work_dir}/airootfs" --config "${pacman_conf}" | \
283
284
285
286
287
288
289
            awk '/\[installed\]$/ {print $1 "/" $2 "-" $3}' > \
            "${work_dir}/iso/${install_dir}/pkglist.${arch}.txt"
        _msg_info "Done!"
    fi

}

290
# Create an ISO9660 filesystem from "iso" directory.
291
command_iso () {
292
293
    local _iso_efi_boot_args=""

294
295
296
    if [[ ! -f "${work_dir}/iso/isolinux/isolinux.bin" ]]; then
         _msg_error "The file '${work_dir}/iso/isolinux/isolinux.bin' does not exist." 1
    fi
297
298
299
    if [[ ! -f "${work_dir}/iso/isolinux/isohdpfx.bin" ]]; then
         _msg_error "The file '${work_dir}/iso/isolinux/isohdpfx.bin' does not exist." 1
    fi
300

301
302
    # 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
303
304
305
306
        _iso_efi_boot_args="-eltorito-alt-boot
                            -e EFI/archiso/efiboot.img
                            -no-emul-boot
                            -isohybrid-gpt-basdat"
307
308
    fi

309
310
    _show_config iso

311
312
    if _is_directory_changed "${work_dir}/iso" "${out_dir}/${img_name}"; then
        mkdir -p ${out_dir}
313
314
315
316
317
        _msg_info "Creating ISO image..."
        local _qflag=""
        if [[ ${quiet} == "y" ]]; then
            _qflag="-quiet"
        fi
318
        xorriso -as mkisofs ${_qflag} \
319
            -iso-level 3 \
320
321
322
323
324
325
326
            -full-iso9660-filenames \
            -volid "${iso_label}" \
            -appid "${iso_application}" \
            -publisher "${iso_publisher}" \
            -preparer "prepared by mkarchiso" \
            -eltorito-boot isolinux/isolinux.bin \
            -eltorito-catalog isolinux/boot.cat \
327
            -no-emul-boot -boot-load-size 4 -boot-info-table \
328
            -isohybrid-mbr ${work_dir}/iso/isolinux/isohdpfx.bin \
329
            ${_iso_efi_boot_args} \
330
331
            -output "${out_dir}/${img_name}" \
            "${work_dir}/iso/"
332
        _msg_info "Done! | $(ls -sh ${out_dir}/${img_name})"
333
334
335
    fi
}

336
# create airootfs.sfs filesystem, and push it in "iso" directory.
337
338
339
340
command_prepare () {
    _show_config prepare

    _cleanup
341

342
    mkdir -p "${work_dir}/iso/${install_dir}/${arch}"
343

344
345
    if _is_directory_changed "${work_dir}/airootfs" "${work_dir}/iso/${install_dir}/${arch}/airootfs.sfs"; then
        _mkairootfs
346
    fi
347
348
}

349
# Install packages on airootfs.
350
# A basic check to avoid double execution/reinstallation is done via hashing package names.
351
command_install () {
352
353
354
355
356
357
358
359
360
361
362
363
    if [[ ! -f "${pacman_conf}" ]]; then
        _msg_error "Pacman config file '${pacman_conf}' does not exist" 1
    fi

    #trim spaces
    pkg_list="$(echo ${pkg_list})"

    if [[ -z ${pkg_list} ]]; then
        _msg_error "Packages must be specified" 0
        _usage 1
    fi

364
    _show_config install
365
366
367

    local _pkg_list_hash
    _pkg_list_hash=$(echo ${pkg_list} | sort -u | md5sum | cut -c1-32)
368
    if [[ -f "${work_dir}/install.${_pkg_list_hash}" ]]; then
369
370
371
        _msg_info "These packages are already installed, skipping."
    else
        _pacman "${pkg_list}"
372
        : > "${work_dir}/install.${_pkg_list_hash}"
373
    fi
374
375
}

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

command_run() {
    _show_config run
    _chroot_run
}
385
386
387
388
389

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

390
while getopts 'p:r:C:L:P:A:D:w:o:c:vh' arg; do
391
392
    case "${arg}" in
        p) pkg_list="${pkg_list} ${OPTARG}" ;;
393
        r) run_cmd="${OPTARG}" ;;
394
395
396
397
398
        C) pacman_conf="${OPTARG}" ;;
        L) iso_label="${OPTARG}" ;;
        P) iso_publisher="${OPTARG}" ;;
        A) iso_application="${OPTARG}" ;;
        D) install_dir="${OPTARG}" ;;
399
400
        w) work_dir="${OPTARG}" ;;
        o) out_dir="${OPTARG}" ;;
401
        c) sfs_comp="${OPTARG}" ;;
402
403
404
405
406
407
408
409
410
411
412
413
414
415
        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
416
fi
417
418
419
command_name="${1}"

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

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