common.sh 2.91 KB
Newer Older
1
#!/bin/bash
2
# shellcheck disable=SC1090
3

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
4
initChroot() {
5
    local chrootdir="${1}"
6

7
    # Create the log file; this file will be bind to appear in "${VARLOG}"
Alessandro Elias's avatar
Alessandro Elias committed
8
    # directory in the target machine that generates the iso.
9
    touch "${chrootdir}/${LOG_FILE}"
10
    touch "${LOG_FILE}"
Alessandro Elias's avatar
Alessandro Elias committed
11 12 13 14 15

    mount --bind /dev "${chrootdir}/dev"
    mount --bind /dev/pts "${chrootdir}/dev/pts"
    mount --bind /proc "${chrootdir}/proc"
    mount --bind /sys "${chrootdir}/sys"
16

17
    mount --bind "${LOG_FILE}" "${chrootdir}/${LOG_FILE}"
18

Alessandro Elias's avatar
Alessandro Elias committed
19
    mount --bind "/etc/resolv.conf" "${chrootdir}/etc/resolv.conf"
20 21

    # Prevent services from being started in the chrooted environment
22 23 24 25 26 27 28 29 30
    rm -rf "${chrootdir:?}/fakebin"
    chroot "${chrootdir}" mkdir /fakebin
    chroot "${chrootdir}" ln --symbolic /bin/true /fakebin/initctl
    chroot "${chrootdir}" ln --symbolic /bin/true /fakebin/invoke-rc.d
    chroot "${chrootdir}" ln --symbolic /bin/true /fakebin/restart
    chroot "${chrootdir}" ln --symbolic /bin/true /fakebin/start
    chroot "${chrootdir}" ln --symbolic /bin/true /fakebin/stop
    chroot "${chrootdir}" ln --symbolic /bin/true /fakebin/service
    chroot "${chrootdir}" ln --symbolic /bin/true /fakebin/start-stop-daemon
31 32
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
33
closeChroot() {
34
    local chrootdir="${1}"
35

Alessandro Elias's avatar
Alessandro Elias committed
36
    umount --lazy "${chrootdir}/etc/resolv.conf"
37
    umount --lazy "${chrootdir}/${LOG_FILE}"
38
    umount --lazy "${chrootdir}"/{dev/pts,proc,sys,dev}
Alessandro Elias's avatar
Alessandro Elias committed
39
}
40

Alessandro Elias's avatar
Alessandro Elias committed
41
function create_stub() {
42
    local script="${1}"
Alessandro Elias's avatar
Alessandro Elias committed
43

44
    cat << EOF > "${script}"
Alessandro Elias's avatar
Alessandro Elias committed
45 46 47 48 49 50 51
#!/usr/bin/env bash

# THIS SCRIPT WAS GENERATE DINAMICALLY BY CREATE-ISO
# DO NOT EDIT IT.


EOF
52
    cat "${PREFIX}/helper/logging.incsh" >> "${script}"
53 54
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
55
addToChrootFile() {
56 57 58
    local chrootdir="${1}"
    local script_name="${2}"
    local cmd="${3}"
Alessandro Elias's avatar
Alessandro Elias committed
59

60 61
    if [ ! -f "${chrootdir}/tmp/${script_name}" ]; then
        create_stub "${chrootdir}/tmp/${script_name}"
Alessandro Elias's avatar
Alessandro Elias committed
62 63
    fi

64
    echo "${cmd}" >> "${chrootdir}/tmp/${script_name}"
65 66
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
67
execChroot() {
68 69
    local chrootdir="${1}"
    local script_name="${2}"
Alessandro Elias's avatar
Alessandro Elias committed
70

71 72
    [ ! -x "${chrootdir}/tmp/${script_name}" ] && chmod +x "${chrootdir}/tmp/${script_name}"
    chroot "${chrootdir}" "/tmp/${script_name}"
73
    rm --recursive --force "${chrootdir}/tmp/${script_name:?}" &>/dev/null
Alessandro Elias's avatar
Alessandro Elias committed
74

75
    return $?
Alessandro Elias's avatar
Alessandro Elias committed
76
}
77 78

function cleanup() {
79 80
    local chrootdir="${1}"
    local tmp="${2}"
81

82 83 84 85
    if mountpoint --quiet "${chrootdir}/etc/resolv.conf"; then
        umount --lazy "${chrootdir}/etc/resolv.conf"
    fi
    if mountpoint --quiet "${chrootdir}/${LOG_FILE}"; then
86
        umount --lazy "${chrootdir}/${LOG_FILE}"
87 88 89 90 91 92 93 94 95 96 97 98 99
    fi
    if mountpoint --quiet "${chrootdir}/dev/pts"; then
       umount --lazy "${chrootdir}/dev/pts"
    fi
    if mountpoint --quiet "${chrootdir}/dev"; then
       umount --lazy "${chrootdir}/dev"
    fi
    if mountpoint --quiet "${chrootdir}/sys"; then
       umount --lazy "${chrootdir}/sys"
    fi
    if mountpoint --quiet "${chrootdir}/proc"; then
       umount --lazy "${chrootdir}/proc"
    fi
100
    rm --recursive --force "${tmp:?}"
101
}