common.sh 3.31 KB
Newer Older
1 2
#!/bin/bash

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
3
cleanTmpFiles() {
4 5 6 7
    rm -rf ${TMP}
    closeChroot
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
8
emergency_shell() {
9 10 11 12 13
    cleanTmpFiles
    echo "FATAL ERROR!"
    exit 1
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
14
log() {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    local _TYPE=$1
    local _TXT=$(echo $@ | cut -d' ' -f2-)
    local _FILE=${PREFIX}/log/${DIST}/${_TYPE}-create-iso.log
    if [ "$_TXT" ]; then
        if [ "$LOGINFO" ]; then
            echo "${_TYPE}: ${_TXT}" | tee -a $_FILE
        else
            echo "${_TYPE}: ${_TXT}"
        fi
    fi
    if [ "${_TYPE}" == "ERROR" ]; then
        emergency_shell
    fi
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
30
removeMountList() {
31 32 33 34
    grep -v "$1" ${MOUNTLIST} > ${MOUNTLIST}.tmp
    mv -f ${MOUNTLIST}.tmp ${MOUNTLIST}
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
35
initChroot() {
36 37 38
    local _DIST=$1

    mkdir -p ${CHROOTDIR}/var/log/create-iso/
39 40 41
    if [ ! -d ${PREFIX}/log/${_DIST} ]; then
        mkdir -p ${PREFIX}/log/${_DIST}
    fi
42 43 44 45 46 47 48 49 50 51

    cp /etc/resolv.conf ${CHROOTDIR}/etc/
    cp /etc/hosts ${CHROOTDIR}/etc/

    mount --bind /dev ${CHROOTDIR}/dev && echo "${CHROOTDIR}/dev" >> ${MOUNTLIST}
    mount --bind /sys ${CHROOTDIR}/sys && echo "${CHROOTDIR}/sys" >> ${MOUNTLIST}
    mount --bind /dev/pts ${CHROOTDIR}/dev/pts && echo "${CHROOTDIR}/dev/pts" \
        >> ${MOUNTLIST}
    mount --bind ${PREFIX}/log/${_DIST}/ ${CHROOTDIR}/var/log/create-iso/ && \
        echo "${CHROOTDIR}/var/log/create-iso/" >> ${MOUNTLIST} || \
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
52
        log ERROR "Failed to mount log dir."
53
    mount --bind /proc ${CHROOTDIR}/proc && echo "${CHROOTDIR}/proc" >> ${MOUNTLIST}
54

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
55
    # chroot ${CHROOTDIR} dbus-uuidgen > ${CHROOTDIR}/var/lib/dbus/machine-id
56 57 58 59 60 61 62 63 64 65 66 67 68

    # Prevent services from being started in the chrooted environment
    rm -rf ${CHROOTDIR}/fakebin
    chroot ${CHROOTDIR} mkdir /fakebin
    chroot ${CHROOTDIR} ln -s /bin/true /fakebin/initctl
    chroot ${CHROOTDIR} ln -s /bin/true /fakebin/invoke-rc.d
    chroot ${CHROOTDIR} ln -s /bin/true /fakebin/restart
    chroot ${CHROOTDIR} ln -s /bin/true /fakebin/start
    chroot ${CHROOTDIR} ln -s /bin/true /fakebin/stop
    chroot ${CHROOTDIR} ln -s /bin/true /fakebin/service
    chroot ${CHROOTDIR} ln -s /bin/true /fakebin/start-stop-daemon
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
69
closeChroot() {
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
    chroot ${CHROOTDIR} apt-get clean

    rm -rf ${CHROOTDIR}/fakebin
    rm -rf ${CHROOTDIR}/root/.bash_history ${CHROOTDIR}/var/lib/dbus/machine-id
    rm -rf ${CHROOTDIR}/tmp/*

    if grep -qs ${CHROOTDIR}/dev/pts ${MOUNTLIST}; then
        umount -l ${CHROOTDIR}/dev/pts && removeMountList ${CHROOTDIR}/dev/pts
    fi
    echo "1"
    if grep -qs ${CHROOTDIR}/sys ${MOUNTLIST}; then
        umount -l ${CHROOTDIR}/sys && removeMountList ${CHROOTDIR}/sys
    fi
    if grep -qs ${CHROOTDIR}/proc ${MOUNTLIST}; then
        umount -l ${CHROOTDIR}/proc && removeMountList ${CHROOTDIR}/proc
    fi
    if grep -qs ${CHROOTDIR}/dev ${MOUNTLIST}; then
        umount -l ${CHROOTDIR}/dev && removeMountList ${CHROOTDIR}/dev
    fi
    if grep -qs ${CHROOTDIR}/var/log/create-iso/ ${MOUNTLIST}; then
        umount -l ${CHROOTDIR}/var/log/create-iso/ \
            && removeMountList ${CHROOTDIR}/var/log/create-iso/
    fi
    rm -rf ${CHROOTDIR}/var/log/create-iso/
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
96
addToChrootFile() {
97 98
    local _FILE=$1
    local _CMD=$2
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
99
    echo "$_CMD \\ || /bin/bash" add no arquivo
100 101 102 103
    echo "$_CMD \\" >> "$_FILE"
    echo '|| /bin/bash' >> "$_FILE"
}

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
104
execChroot() {
105 106 107 108
    _FILE=$1
    _DIR=$2
    chmod +x $_FILE
    cp $_FILE ${CHROOTDIR}/tmp/exec.sh
109
    rm $_FILE
110 111
    chroot $_DIR /tmp/exec.sh
    return $?
Alessandro Elias's avatar
Alessandro Elias committed
112
}