util-iso.sh 15 KB
Newer Older
udeved's avatar
udeved committed
1 2 3 4 5 6 7 8 9 10 11
#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

udeved's avatar
udeved committed
12 13
import ${LIBDIR}/util-iso-image.sh
import ${LIBDIR}/util-iso-boot.sh
14
import ${LIBDIR}/util-yaml.sh
udeved's avatar
udeved committed
15 16

error_function() {
udeved's avatar
udeved committed
17 18 19 20 21 22 23 24
    if [[ -p $logpipe ]]; then
        rm "$logpipe"
    fi
    # first exit all subshells, then print the error
    if (( ! BASH_SUBSHELL )); then
        error "A failure occurred in %s()." "$1"
        plain "Aborting..."
    fi
25
    umount_fs
26
    umount_img
udeved's avatar
udeved committed
27
    exit 2
udeved's avatar
udeved committed
28 29 30 31
}

# $1: function
run_log(){
udeved's avatar
udeved committed
32 33 34 35 36 37 38 39 40 41 42
    local func="$1"
    local tmpfile=${tmp_dir}/$func.ansi.log logfile=${log_dir}/$(gen_iso_fn).$func.log
    logpipe=$(mktemp -u "${tmp_dir}/$func.pipe.XXXXXXXX")
    mkfifo "$logpipe"
    tee "$tmpfile" < "$logpipe" &
    local teepid=$!
    $func &> "$logpipe"
    wait $teepid
    rm "$logpipe"
    cat $tmpfile | perl -pe 's/\e\[?.*?[\@-~]//g' > $logfile
    rm "$tmpfile"
udeved's avatar
udeved committed
43 44 45
}

run_safe() {
udeved's avatar
udeved committed
46 47 48 49 50
    local restoretrap func="$1"
    set -e
    set -E
    restoretrap=$(trap -p ERR)
    trap 'error_function $func' ERR
udeved's avatar
udeved committed
51

Bernhard Landauer's avatar
Bernhard Landauer committed
52
    if ${verbose}; then
udeved's avatar
udeved committed
53 54 55 56
        run_log "$func"
    else
        "$func"
    fi
udeved's avatar
udeved committed
57

udeved's avatar
udeved committed
58 59 60
    eval $restoretrap
    set +E
    set +e
udeved's avatar
udeved committed
61 62
}

udeved's avatar
udeved committed
63
trap_exit() {
udeved's avatar
udeved committed
64 65
    local sig=$1; shift
    error "$@"
66
    umount_fs
udeved's avatar
udeved committed
67 68
    trap -- "$sig"
    kill "-$sig" "$$"
udeved's avatar
udeved committed
69 70
}

71 72 73
make_sig () {
    msg2 "Creating signature file..."
    cd "$1"
udeved's avatar
udeved committed
74 75 76
    user_own "$1"
    su ${OWNER} -c "gpg --detach-sign --default-key ${gpgkey} $2.sfs"
    chown -R root "$1"
77
    cd ${OLDPWD}
udeved's avatar
udeved committed
78 79 80 81 82 83 84 85 86 87
}

# $1: file
make_checksum(){
    msg2 "Creating md5sum ..."
    cd $1
    md5sum $2.sfs > $2.md5
    cd ${OLDPWD}
}

udeved's avatar
udeved committed
88
# $1: image path
89
make_sfs() {
udeved's avatar
udeved committed
90 91 92
    local src="$1"
    if [[ ! -e "${src}" ]]; then
        error "The path %s does not exist" "${src}"
93
        retrun 1
udeved's avatar
udeved committed
94
    fi
95
    local timer=$(get_timer) dest=${iso_root}/${iso_name}/${target_arch}
udeved's avatar
udeved committed
96
    local name=${1##*/}
udeved's avatar
udeved committed
97
    local sfs="${dest}/${name}.sfs"
98
    mkdir -p ${dest}
udeved's avatar
udeved committed
99 100 101 102
    msg "Generating SquashFS image for %s" "${src}"
    if [[ -f "${sfs}" ]]; then
        local has_changed_dir=$(find ${src} -newer ${sfs})
        msg2 "Possible changes for %s ..." "${src}"  >> ${tmp_dir}/buildiso.debug
udeved's avatar
udeved committed
103 104
        msg2 "%s" "${has_changed_dir}" >> ${tmp_dir}/buildiso.debug
        if [[ -n "${has_changed_dir}" ]]; then
udeved's avatar
udeved committed
105 106
            msg2 "SquashFS image %s is not up to date, rebuilding..." "${sfs}"
            rm "${sfs}"
udeved's avatar
udeved committed
107
        else
udeved's avatar
udeved committed
108
            msg2 "SquashFS image %s is up to date, skipping." "${sfs}"
udeved's avatar
udeved committed
109 110 111 112
            return
        fi
    fi

Bernhard Landauer's avatar
Bernhard Landauer committed
113
    if ${persist}; then
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        local size=32G
        local mnt="${mnt_dir}/${name}"
        msg2 "Creating ext4 image of %s ..." "${size}"
        truncate -s ${size} "${src}.img"
        local ext4_args=()
        ${verbose} && ext4_args+=(-q)
        ext4_args+=(-O ^has_journal,^resize_inode -E lazy_itable_init=0 -m 0)
        mkfs.ext4 ${ext4_args[@]} -F "${src}.img" &>/dev/null
        tune2fs -c 0 -i 0 "${src}.img" &> /dev/null
        mount_img "${work_dir}/${name}.img" "${mnt}"
        msg2 "Copying %s ..." "${src}/"
        cp -aT "${src}/" "${mnt}/"
        umount_img "${mnt}"

    fi
udeved's avatar
udeved committed
129 130

    msg2 "Creating SquashFS image, this may take some time..."
131
    local used_kernel=${kernel:5:1} mksfs_args=()
Bernhard Landauer's avatar
Bernhard Landauer committed
132
    if ${persist}; then
133 134 135 136 137 138 139
        mksfs_args+=(${work_dir}/${name}.img)
    else
        mksfs_args+=(${src})
    fi

    mksfs_args+=(${sfs} -noappend)

140 141 142
    local highcomp="-b 256K -Xbcj x86"
   # local comp='xz'
    [[ "${iso_compression}" != "xz" ]] && highcomp=""
udeved's avatar
udeved committed
143

144
    if [[ "${name}" == "mhwdfs" && ${used_kernel} < "4" ]]; then
145
        mksfs_args+=(-comp lz4)
udeved's avatar
udeved committed
146
    else
147
        mksfs_args+=(-comp ${iso_compression} ${highcomp})
148
    fi
Bernhard Landauer's avatar
Bernhard Landauer committed
149
    if ${verbose}; then
150 151 152
        mksquashfs "${mksfs_args[@]}" >/dev/null
    else
        mksquashfs "${mksfs_args[@]}"
udeved's avatar
udeved committed
153
    fi
udeved's avatar
udeved committed
154
    make_checksum "${dest}" "${name}"
udeved's avatar
udeved committed
155 156
    ${persist} && rm "${src}.img"

Bernhard Landauer's avatar
Bernhard Landauer committed
157
    if [[ -n ${gpgkey} ]]; then
udeved's avatar
udeved committed
158 159 160
        make_sig "${dest}" "${name}"
    fi

udeved's avatar
udeved committed
161
    show_elapsed_time "${FUNCNAME}" "${timer_start}"
udeved's avatar
udeved committed
162 163
}

udeved's avatar
udeved committed
164
assemble_iso(){
udeved's avatar
udeved committed
165
    msg "Creating ISO image..."
artoo's avatar
artoo committed
166 167 168
    local iso_publisher="$(get_osname) <$(get_disturl)>" \
        iso_app_id="$(get_osname) Live/Rescue CD" \
        mod_date=$(date -u +%Y-%m-%d-%H-%M-%S-00  | sed -e s/-//g)
169

udeved's avatar
udeved committed
170
    xorriso -as mkisofs \
artoo's avatar
artoo committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        --modification-date=${mod_date} \
        --protective-msdos-label \
        -volid "${iso_label}" \
        -appid "${iso_app_id}" \
        -publisher "${iso_publisher}" \
        -preparer "Prepared by manjaro-tools/${0##*/}" \
        -r -graft-points -no-pad \
        --sort-weight 0 / \
        --sort-weight 1 /boot \
        --grub2-mbr ${iso_root}/boot/grub/i386-pc/boot_hybrid.img \
        -partition_offset 16 \
        -b boot/grub/i386-pc/eltorito.img \
        -c boot.catalog \
        -no-emul-boot -boot-load-size 4 -boot-info-table --grub2-boot-info \
        -eltorito-alt-boot \
        -append_partition 2 0xef ${iso_root}/efi.img \
        -e --interval:appended_partition_2:all:: \
        -no-emul-boot \
        -iso-level 3 \
        -o ${iso_dir}/${iso_file} \
        ${iso_root}/

#         arg to add with xorriso-1.4.7
#         -iso_mbr_part_type 0x00
195 196 197 198
}

# Build ISO
make_iso() {
udeved's avatar
udeved committed
199
    msg "Start [Build ISO]"
200
    touch "${iso_root}/.miso"
201
    for sfs_dir in $(find "${work_dir}" -maxdepth 1 -type d); do
udeved's avatar
udeved committed
202 203
        if [[ "${sfs_dir}" != "${work_dir}" ]]; then
            make_sfs "${sfs_dir}"
udeved's avatar
udeved committed
204 205 206 207 208
        fi
    done

    msg "Making bootable image"
    # Sanity checks
209
    [[ ! -d "${iso_root}" ]] && return 1
udeved's avatar
udeved committed
210 211 212 213 214 215
    if [[ -f "${iso_dir}/${iso_file}" ]]; then
        msg2 "Removing existing bootable image..."
        rm -rf "${iso_dir}/${iso_file}"
    fi
    assemble_iso
    msg "Done [Build ISO]"
216 217
}

udeved's avatar
udeved committed
218
gen_iso_fn(){
udeved's avatar
udeved committed
219 220
    local vars=() name
    vars+=("${iso_name}")
Bernhard Landauer's avatar
Bernhard Landauer committed
221
    if ! ${chrootcfg}; then
udeved's avatar
udeved committed
222 223 224 225
        [[ -n ${profile} ]] && vars+=("${profile}")
    fi
    vars+=("${dist_release}")
    vars+=("${target_branch}")
Frede Hundewadt's avatar
Frede Hundewadt committed
226

227
    [[ ${extra} == 'false' ]] && vars+=("minimal")
Frede Hundewadt's avatar
Frede Hundewadt committed
228

udeved's avatar
udeved committed
229
    vars+=("${target_arch}")
Bernhard Landauer's avatar
Bernhard Landauer committed
230
    for n in ${vars[@]}; do
udeved's avatar
udeved committed
231 232 233
        name=${name:-}${name:+-}${n}
    done
    echo $name
udeved's avatar
udeved committed
234 235
}

udeved's avatar
udeved committed
236
reset_pac_conf(){
udeved's avatar
udeved committed
237 238 239 240
    info "Restoring [%s/etc/pacman.conf] ..." "$1"
    sed -e 's|^.*HoldPkg.*|HoldPkg      = pacman glibc manjaro-system|' \
        -e "s|^.*#CheckSpace|CheckSpace|" \
        -i "$1/etc/pacman.conf"
udeved's avatar
udeved committed
241 242
}

243
# Base installation (rootfs)
udeved's avatar
udeved committed
244
make_image_root() {
udeved's avatar
udeved committed
245
    if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
246 247
        msg "Prepare [Base installation] (rootfs)"
        local path="${work_dir}/rootfs"
udeved's avatar
udeved committed
248
        mkdir -p ${path}
udeved's avatar
udeved committed
249

udeved's avatar
udeved committed
250
        chroot_create "${path}" "${packages}" || die
udeved's avatar
udeved committed
251

252
        pacman -Qr "${path}" > "${path}/rootfs-pkgs.txt"
udeved's avatar
udeved committed
253
        copy_overlay "${profile_dir}/root-overlay" "${path}"
udeved's avatar
udeved committed
254

udeved's avatar
udeved committed
255
        reset_pac_conf "${path}"
udeved's avatar
udeved committed
256

257 258
        configure_lsb "${path}"

udeved's avatar
udeved committed
259 260
        clean_up_image "${path}"
        : > ${work_dir}/build.${FUNCNAME}
261
        msg "Done [Base installation] (rootfs)"
udeved's avatar
udeved committed
262
    fi
udeved's avatar
udeved committed
263 264
}

udeved's avatar
udeved committed
265
make_image_desktop() {
udeved's avatar
udeved committed
266
    if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
udeved's avatar
udeved committed
267 268
        msg "Prepare [Desktop installation] (desktopfs)"
        local path="${work_dir}/desktopfs"
udeved's avatar
udeved committed
269
        mkdir -p ${path}
270

udeved's avatar
udeved committed
271
        mount_fs_root "${path}"
272

udeved's avatar
udeved committed
273
        chroot_create "${path}" "${packages}"
udeved's avatar
udeved committed
274

udeved's avatar
udeved committed
275 276
        pacman -Qr "${path}" > "${path}/desktopfs-pkgs.txt"
        cp "${path}/desktopfs-pkgs.txt" ${iso_dir}/$(gen_iso_fn)-pkgs.txt
277
        [[ -e ${profile_dir}/desktop-overlay ]] && copy_overlay "${profile_dir}/desktop-overlay" "${path}"
278

udeved's avatar
udeved committed
279
        reset_pac_conf "${path}"
udeved's avatar
udeved committed
280

281
        umount_fs
udeved's avatar
udeved committed
282 283
        clean_up_image "${path}"
        : > ${work_dir}/build.${FUNCNAME}
udeved's avatar
udeved committed
284
        msg "Done [Desktop installation] (desktopfs)"
udeved's avatar
udeved committed
285
    fi
udeved's avatar
udeved committed
286 287
}

288
mount_fs_select(){
289
    if [[ -f "${packages_desktop}" ]]; then
290
        mount_fs_desktop "$1"
udeved's avatar
udeved committed
291
    else
292
        mount_fs_root "$1"
udeved's avatar
udeved committed
293
    fi
udeved's avatar
udeved committed
294 295
}

udeved's avatar
udeved committed
296
make_image_live() {
udeved's avatar
udeved committed
297
    if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
298 299
        msg "Prepare [Live installation] (livefs)"
        local path="${work_dir}/livefs"
udeved's avatar
udeved committed
300
        mkdir -p ${path}
301

302
        mount_fs_select "${path}"
303

udeved's avatar
udeved committed
304
        chroot_create "${path}" "${packages}"
udeved's avatar
udeved committed
305

306
        pacman -Qr "${path}" > "${path}/livefs-pkgs.txt"
udeved's avatar
udeved committed
307 308
        copy_overlay "${profile_dir}/live-overlay" "${path}"
        configure_live_image "${path}"
309

udeved's avatar
udeved committed
310
        reset_pac_conf "${path}"
udeved's avatar
udeved committed
311

312
        umount_fs
313

udeved's avatar
udeved committed
314 315 316 317
        # Clean up GnuPG keys
        rm -rf "${path}/etc/pacman.d/gnupg"
        clean_up_image "${path}"
        : > ${work_dir}/build.${FUNCNAME}
318
        msg "Done [Live installation] (livefs)"
udeved's avatar
udeved committed
319
    fi
udeved's avatar
udeved committed
320 321
}

322
make_image_mhwd() {
udeved's avatar
udeved committed
323
    if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
324 325
        msg "Prepare [drivers repository] (mhwdfs)"
        local path="${work_dir}/mhwdfs"
udeved's avatar
udeved committed
326
        mkdir -p ${path}${mhwd_repo}
327

328
        mount_fs_select "${path}"
udeved's avatar
udeved committed
329

udeved's avatar
udeved committed
330
        reset_pac_conf "${path}"
udeved's avatar
udeved committed
331

udeved's avatar
udeved committed
332
        copy_from_cache "${path}" "${packages}"
udeved's avatar
udeved committed
333

udeved's avatar
udeved committed
334 335 336 337 338 339 340 341
        if [[ -n "${packages_cleanup}" ]]; then
            for mhwd_clean in ${packages_cleanup}; do
                rm ${path}${mhwd_repo}/${mhwd_clean}
            done
        fi
        cp ${DATADIR}/pacman-mhwd.conf ${path}/opt
        make_repo "${path}"
        configure_mhwd_drivers "${path}"
342

343
        umount_fs
udeved's avatar
udeved committed
344 345
        clean_up_image "${path}"
        : > ${work_dir}/build.${FUNCNAME}
346
        msg "Done [drivers repository] (mhwdfs)"
udeved's avatar
udeved committed
347
    fi
udeved's avatar
udeved committed
348 349
}

udeved's avatar
udeved committed
350
make_image_boot() {
udeved's avatar
udeved committed
351
    if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
artoo's avatar
artoo committed
352 353
        msg "Prepare [/iso/boot]"
        local boot="${iso_root}/boot"
udeved's avatar
udeved committed
354

artoo's avatar
artoo committed
355
        mkdir -p ${boot}
udeved's avatar
udeved committed
356

artoo's avatar
artoo committed
357
        cp ${work_dir}/rootfs/boot/vmlinuz* ${boot}/vmlinuz-${target_arch}
udeved's avatar
udeved committed
358 359

        local path="${work_dir}/bootfs"
udeved's avatar
udeved committed
360
        mkdir -p ${path}
udeved's avatar
udeved committed
361

362 363 364 365 366
        if [[ -f "${packages_desktop}" ]]; then
            mount_fs_live "${path}"
        else
            mount_fs_net "${path}"
        fi
udeved's avatar
udeved committed
367

udeved's avatar
udeved committed
368
        prepare_initcpio "${path}"
artoo's avatar
artoo committed
369
        prepare_initramfs "${path}"
udeved's avatar
udeved committed
370

artoo's avatar
artoo committed
371
        cp ${path}/boot/initramfs.img ${boot}/initramfs-${target_arch}.img
372
        prepare_boot_extras "${path}" "${boot}"
udeved's avatar
udeved committed
373

374
        umount_fs
udeved's avatar
udeved committed
375

udeved's avatar
udeved committed
376 377
        rm -R ${path}
        : > ${work_dir}/build.${FUNCNAME}
artoo's avatar
artoo committed
378
        msg "Done [/iso/boot]"
udeved's avatar
udeved committed
379
    fi
udeved's avatar
udeved committed
380 381
}

udeved's avatar
udeved committed
382 383
configure_grub(){
    local default_args="misobasedir=${iso_name} misolabel=${iso_label}" \
Bernhard Landauer's avatar
Bernhard Landauer committed
384
        boot_args=('quiet' 'systemd.show_status=1')
udeved's avatar
udeved committed
385 386 387 388 389 390 391 392 393

    sed -e "s|@DIST_NAME@|${dist_name}|g" \
        -e "s|@ARCH@|${target_arch}|g" \
        -e "s|@DEFAULT_ARGS@|${default_args}|g" \
        -e "s|@BOOT_ARGS@|${boot_args[*]}|g" \
        -e "s|@PROFILE@|${profile}|g" \
        -i $1
}

artoo's avatar
artoo committed
394 395 396 397
configure_grub_theme(){
    sed -e "s|@ISO_NAME@|${iso_name}|" -i "$1"
}

artoo's avatar
artoo committed
398
make_grub(){
399
    if [[ ! -e ${work_dir}/build.${FUNCNAME} ]]; then
artoo's avatar
artoo committed
400
        msg "Prepare [/iso/boot/grub]"
udeved's avatar
udeved committed
401

artoo's avatar
artoo committed
402
        local path="${work_dir}/livefs"
udeved's avatar
udeved committed
403

artoo's avatar
artoo committed
404
        prepare_grub "${path}" "${iso_root}"
udeved's avatar
udeved committed
405 406

        configure_grub "${iso_root}/boot/grub/kernels.cfg"
artoo's avatar
artoo committed
407
        configure_grub_theme "${iso_root}/boot/grub/variable.cfg"
udeved's avatar
udeved committed
408

409
        : > ${work_dir}/build.${FUNCNAME}
artoo's avatar
artoo committed
410
        msg "Done [/iso/boot/grub]"
411 412 413
    fi
}

414
check_requirements(){
415
    [[ -f ${run_dir}/repo_info ]] || die "%s is not a valid iso profiles directory!" "${run_dir}"
Bernhard Landauer's avatar
Bernhard Landauer committed
416
    if ! $(is_valid_arch_iso ${target_arch}); then
udeved's avatar
udeved committed
417 418
        die "%s is not a valid arch!" "${target_arch}"
    fi
Bernhard Landauer's avatar
Bernhard Landauer committed
419
    if ! $(is_valid_branch ${target_branch}); then
udeved's avatar
udeved committed
420 421
        die "%s is not a valid branch!" "${target_branch}"
    fi
422

udeved's avatar
udeved committed
423
    local iso_kernel=${kernel:5:1} host_kernel=$(uname -r)
artoo's avatar
artoo committed
424
    if [[ ${iso_kernel} < "4" ]] \
Bernhard Landauer's avatar
Bernhard Landauer committed
425
    || [[ ${host_kernel%%*.} < "4" ]]; then
artoo's avatar
artoo committed
426
        die "The host and iso kernels must be version>=4.0!"
udeved's avatar
udeved committed
427
    fi
428 429
}

udeved's avatar
udeved committed
430
compress_images(){
udeved's avatar
udeved committed
431 432
    local timer=$(get_timer)
    run_safe "make_iso"
Bernhard Landauer's avatar
Bernhard Landauer committed
433
    user_own "${cache_dir_iso}" "-R"
udeved's avatar
udeved committed
434
    show_elapsed_time "${FUNCNAME}" "${timer}"
udeved's avatar
udeved committed
435 436
}

udeved's avatar
udeved committed
437
prepare_images(){
udeved's avatar
udeved committed
438 439 440
    local timer=$(get_timer)
    load_pkgs "${profile_dir}/Packages-Root"
    run_safe "make_image_root"
441 442
    if [[ -f "${packages_desktop}" ]] ; then
        load_pkgs "${packages_desktop}"
udeved's avatar
udeved committed
443
        run_safe "make_image_desktop"
udeved's avatar
udeved committed
444 445 446 447 448 449 450 451 452
    fi
    if [[ -f ${profile_dir}/Packages-Live ]]; then
        load_pkgs "${profile_dir}/Packages-Live"
        run_safe "make_image_live"
    fi
    if [[ -f ${packages_mhwd} ]] ; then
        load_pkgs "${packages_mhwd}"
        run_safe "make_image_mhwd"
    fi
453
    run_safe "make_image_boot"
artoo's avatar
artoo committed
454
    run_safe "make_grub"
udeved's avatar
udeved committed
455

udeved's avatar
udeved committed
456 457 458 459 460 461 462 463 464 465 466
    show_elapsed_time "${FUNCNAME}" "${timer}"
}

archive_logs(){
    local name=$(gen_iso_fn) ext=log.tar.xz src=${tmp_dir}/archives.list
    find ${log_dir} -maxdepth 1 -name "$name*.log" -printf "%f\n" > $src
    msg2 "Archiving log files [%s] ..." "$name.$ext"
    tar -cJf ${log_dir}/$name.$ext -C ${log_dir} -T $src
    msg2 "Cleaning log files ..."
    find ${log_dir} -maxdepth 1 -name "$name*.log" -delete
}
467

468
make_profile(){
udeved's avatar
udeved committed
469
    msg "Start building [%s]" "${profile}"
Bernhard Landauer's avatar
Bernhard Landauer committed
470
    if ${clean_first}; then
udeved's avatar
udeved committed
471
        chroot_clean "${chroots_iso}/${profile}/${target_arch}"
udeved's avatar
udeved committed
472

udeved's avatar
udeved committed
473 474 475 476 477
        local unused_arch=''
        case ${target_arch} in
            i686) unused_arch='x86_64' ;;
            x86_64) unused_arch='i686' ;;
        esac
Bernhard Landauer's avatar
Bernhard Landauer committed
478
        if [[ -d "${chroots_iso}/${profile}/${unused_arch}" ]]; then
udeved's avatar
udeved committed
479 480 481 482
            chroot_clean "${chroots_iso}/${profile}/${unused_arch}"
        fi
        clean_iso_root "${iso_root}"
    fi
udeved's avatar
udeved committed
483

udeved's avatar
udeved committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    if ${iso_only}; then
        [[ ! -d ${work_dir} ]] && die "Create images: buildiso -p %s -x" "${profile}"
        compress_images
        ${verbose} && archive_logs
        exit 1
    fi
    if ${images_only}; then
        prepare_images
        ${verbose} && archive_logs
        warning "Continue compress: buildiso -p %s -zc ..." "${profile}"
        exit 1
    else
        prepare_images
        compress_images
        ${verbose} && archive_logs
    fi
    reset_profile
    msg "Finished building [%s]" "${profile}"
    show_elapsed_time "${FUNCNAME}" "${timer_start}"
503
}
udeved's avatar
udeved committed
504

505
get_pacman_conf(){
udeved's avatar
udeved committed
506 507
    local user_conf=${profile_dir}/user-repos.conf pac_arch='default' conf
    [[ "${target_arch}" == 'x86_64' ]] && pac_arch='multilib'
Bernhard Landauer's avatar
Bernhard Landauer committed
508
    if [[ -f ${user_conf} ]]; then
udeved's avatar
udeved committed
509 510 511 512 513 514 515 516
        info "detected: %s" "user-repos.conf"
        check_user_repos_conf "${user_conf}"
        conf=${tmp_dir}/custom-pacman.conf
        cat ${DATADIR}/pacman-$pac_arch.conf ${user_conf} > "$conf"
    else
        conf="${DATADIR}/pacman-$pac_arch.conf"
    fi
    echo "$conf"
517 518
}

udeved's avatar
udeved committed
519
load_profile(){
udeved's avatar
udeved committed
520
    conf="$1/profile.conf"
521

udeved's avatar
udeved committed
522
    info "Profile: [%s]" "${profile}"
udeved's avatar
udeved committed
523

udeved's avatar
udeved committed
524
    load_profile_config "$conf"
udeved's avatar
udeved committed
525

udeved's avatar
udeved committed
526
    pacman_conf=$(get_pacman_conf)
527

udeved's avatar
udeved committed
528
    mirrors_conf=$(get_pac_mirrors_conf "${target_branch}")
udeved's avatar
udeved committed
529

udeved's avatar
udeved committed
530
    iso_file=$(gen_iso_fn).iso
udeved's avatar
udeved committed
531

udeved's avatar
udeved committed
532 533
    mkchroot_args+=(-C ${pacman_conf} -S ${mirrors_conf} -B "${build_mirror}/${target_branch}" -K)
    work_dir=${chroots_iso}/${profile}/${target_arch}
udeved's avatar
udeved committed
534

535
    iso_dir="${cache_dir_iso}/${edition}/${profile}/${dist_release}"
udeved's avatar
udeved committed
536

537
    iso_root=${chroots_iso}/${profile}/iso
udeved's avatar
udeved committed
538 539
    mnt_dir=${chroots_iso}/${profile}/mnt
    prepare_dir "${mnt_dir}"
540

udeved's avatar
udeved committed
541
    prepare_dir "${iso_dir}"
Bernhard Landauer's avatar
Bernhard Landauer committed
542
    user_own "${cache_dir_iso}" "-R"
udeved's avatar
udeved committed
543 544 545
}

prepare_profile(){
udeved's avatar
udeved committed
546 547 548
    profile=$1
    edition=$(get_edition ${profile})
    profile_dir=${run_dir}/${edition}/${profile}
udeved's avatar
udeved committed
549
    check_profile "${profile_dir}"
udeved's avatar
udeved committed
550
    load_profile "${profile_dir}"
udeved's avatar
udeved committed
551 552 553
}

build(){
udeved's avatar
udeved committed
554 555
    prepare_profile "$1"
    make_profile
udeved's avatar
udeved committed
556
}