create-iso.sh 7.9 KB
Newer Older
1
#!/bin/bash
2
# shellcheck disable=SC1090
3 4 5 6 7 8
#
# Copyright (C) 2017 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of create-iso
#
9
# create-iso is free software; you can redistribute it and/or
10 11 12 13 14 15 16 17 18 19 20 21 22 23
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

sulzbach's avatar
sulzbach committed
24 25 26 27
# Get the prefix path (root) of create-iso
export PREFIX="$(realpath "$(dirname "$0")")"

# Load default values of parameters
sulzbach's avatar
sulzbach committed
28
source "${PREFIX}/conf/opts.conf"
29

30
while true; do
31
    case "${1}" in
sulzbach's avatar
sulzbach committed
32 33 34 35 36 37 38 39 40 41
      -h | --help     ) _HELP=true; break ;;
      -p | --profile  ) _PROF="$2"; shift; shift ;;
      -v | --version  ) _VERSION="$2"; shift; shift ;;
      -i | --input    ) _INPUT="$2"; shift; shift ;;
      -o | --output   ) _OUTPUT="$2"; shift; shift ;;
      -r | --repo     ) _REPO="$2"; shift; shift ;;
      -d | --debug    ) _DEBUG=true; shift ;;
      -s | --sendmail ) _MAIL=true; shift ;;
      -*              ) echo "Unrecognized option. Try with --help."; exit 1 ;;
      *               ) break ;;
42 43 44
    esac
done

sulzbach's avatar
sulzbach committed
45 46
if [[ "${_HELP}" = true ]]; then
    echo    "Usage: $0 [OPTION] [ARGUMENT] ..."
sulzbach's avatar
sulzbach committed
47 48
    echo    ""
    echo    "Options:"
sulzbach's avatar
sulzbach committed
49 50 51 52 53 54 55 56 57 58 59 60
    echo    "  -h, --help                               Show this help list"
    echo -n "  -p, --profile [PROF]                     Select create-iso "
    echo    "profile"
    echo -n "  -v, --version [VERSION]                  Set version of the "
    echo    "distribution to be built"
    echo -n "  -i, --input [PATH]                       Select ISO9660 file "
    echo    "to be used as base"
    echo -n "  -o, --output [PATH]                      Select output path of "
    echo    "the created ISO9660 image"
    echo -n "  -r, --repo [stable | testing | unstable] Select the base remote "
    echo    "repository of the distribution by codename"
    echo    "  -d, --debug                              Execute in debug mode"
sulzbach's avatar
sulzbach committed
61 62

    exit 0
63
fi
64

sulzbach's avatar
sulzbach committed
65
if [[ "$(id -u)" != "0" ]]; then
sulzbach's avatar
sulzbach committed
66 67
    echo "You must have more power to run this script. Are you root?"
    exit -1
68
fi
69

sulzbach's avatar
sulzbach committed
70 71
if [[ -z "${_PROF}" ]]; then
    echo "No profile specified. Try with --help."
72
    exit 1
73 74
fi

sulzbach's avatar
sulzbach committed
75
# Load profile config
sulzbach's avatar
sulzbach committed
76
if ! source "${PREFIX}/conf/${_PROF}.conf"; then
sulzbach's avatar
sulzbach committed
77
    echo "Unavailable profile. Try with --help." 
78 79 80
    exit 1
fi

sulzbach's avatar
sulzbach committed
81
# Load generic config
sulzbach's avatar
sulzbach committed
82 83
source "${PREFIX}/conf/general.conf"

sulzbach's avatar
sulzbach committed
84 85
if [[ -z "${_VERSION}" ]]; then
    echo "No version specified. Proceeding with unset version."
sulzbach's avatar
sulzbach committed
86 87
else
    NAME="${NAME} ${_VERSION}"
88
fi
89

sulzbach's avatar
sulzbach committed
90 91
if [[ "${_REPO}" != "stable" ]] && [[ "${_REPO}" != "testing" ]] && \
[[ "${_REPO}" != "unstable" ]]; then
sulzbach's avatar
sulzbach committed
92 93 94
    echo "Unrecognized remote repository codename. Try with --help."
    exit 1
fi
95

sulzbach's avatar
sulzbach committed
96
TIME_STAMP="$(date +'%Y-%m-%dT%H:%M:%S')"
97

sulzbach's avatar
sulzbach committed
98 99
mkdir --parents "${LOG_PATH}"
export LOG_FILE="${LOG_PATH}/${TIME_STAMP}.log"
100

101 102
exec 1<&-
exec 2<&-
103
exec 1<> "${LOG_FILE}"
104
exec 1>&1
105

sulzbach's avatar
sulzbach committed
106 107 108 109
source "${PREFIX}/include/base.sh"
source "${PREFIX}/include/chroot.sh"
source "${PREFIX}/include/logging.sh"
source "${PREFIX}/include/mount.sh"
sulzbach's avatar
sulzbach committed
110 111

# Disable debug logging if not in debug mode
sulzbach's avatar
sulzbach committed
112
[[ "${_DEBUG}" == false ]] && let VERBOSE_FLAGS="${ALL} ^ ${LDEBUG}"
113 114

# Installation of create-iso's dependencies
115
apt-get install isolinux syslinux squashfs-tools genisoimage xorriso reprepro \
sulzbach's avatar
sulzbach committed
116 117
binwalk --yes || \
create_iso_logger --message "Failed to install basic tools" --tag "apt-get"
sulzbach's avatar
sulzbach committed
118

sulzbach's avatar
sulzbach committed
119 120 121 122
# The default filename of the generated iso is a concatenation of lowered-cased 
# hyphen-delimited distribution version, repo codename and timestamp
[[ -z "${_OUTPUT}" ]] && _OUTPUT="${ISODIR}/output/$(sed -e 's/ /-/g' -e \
's/\(.*\)/\L\1/g' <<< "${NAME}")-${_REPO}-${TIME_STAMP}.iso"
sulzbach's avatar
sulzbach committed
123

sulzbach's avatar
sulzbach committed
124
cond_mount tmpfs "${TMPFS}" tmpfs "suid,dev,size=${TMPFS_SIZE}"
sulzbach's avatar
sulzbach committed
125

Lucas Sulzbach's avatar
Lucas Sulzbach committed
126 127 128 129 130 131 132 133 134
if [[ -z "${_INPUT}" ]]; then
    _INPUT="$(get_iso "${ISODIR}" "${ISOURL}")"
    # Dump the cache if the base image changed (Should be improved)
    rm --recursive --force "${CACHEDIR}"/../
fi

cond_mount "${_INPUT}" "${PREFIX}/isobase" iso9660 ro,loop
lowerdir="${PREFIX}/isobase"

sulzbach's avatar
sulzbach committed
135 136
mkdir --parents "${CACHEDIR}"
find="$(find "${CACHEDIR}" -mindepth 1 -maxdepth 1 -type d | sort --numeric)"
Lucas Sulzbach's avatar
Lucas Sulzbach committed
137

sulzbach's avatar
sulzbach committed
138 139
if [[ -z "${find}" ]]; then
    SQUASHFSPATH="${PREFIX}/isobase/casper/filesystem.squashfs"
sulzbach's avatar
sulzbach committed
140 141 142 143
else
    for dir in ${find}; do
        lowerdir="$(sed 's/:/\\:/g' <<< "${dir}")/image:${lowerdir}"
    done
sulzbach's avatar
sulzbach committed
144
    SQUASHFSPATH="${TMP}/image/casper/filesystem.squashfs"
sulzbach's avatar
sulzbach committed
145 146 147 148
fi

upperdir="${TMP}/upper"
workdir="${TMP}/work"
149

sulzbach's avatar
sulzbach committed
150 151 152
mkdir --parents "${upperdir}"
mkdir --parents "${workdir}"

sulzbach's avatar
sulzbach committed
153 154
cond_mount overlay "${TMP}/image" overlay \
"rw,lowerdir=${lowerdir},upperdir=${upperdir},workdir=${workdir}"
sulzbach's avatar
sulzbach committed
155

sulzbach's avatar
sulzbach committed
156
cond_mount "${SQUASHFSPATH}" "${PREFIX}/squashfsbase" squashfs ro,loop
sulzbach's avatar
sulzbach committed
157 158 159 160 161 162 163 164

lowerdir="${PREFIX}/squashfsbase"
upperdir="${TMPFS}/upper"
workdir="${TMPFS}/work"

mkdir --parents "${upperdir}"
mkdir --parents "${workdir}"

sulzbach's avatar
sulzbach committed
165 166
cond_mount overlay "${CHROOTDIR}" overlay \
"rw,lowerdir=${lowerdir},upperdir=${upperdir},workdir=${workdir}"
sulzbach's avatar
sulzbach committed
167

sulzbach's avatar
sulzbach committed
168
export NO_DBUS=true
sulzbach's avatar
sulzbach committed
169

170 171
# Each one of the available scripts is executed in numeric order
for script in "${SCRIPTSDIR}"/*; do
sulzbach's avatar
sulzbach committed
172
    if [[ -x "${script}" ]] && grep -E --quiet '^[0-9]{2}-[A-Za-z]+' \
sulzbach's avatar
sulzbach committed
173
    <<< "$(basename "${script}")"; then
sulzbach's avatar
sulzbach committed
174 175 176
        create_iso_logger --message "Running $(basename "${script}")..." \
        --type info
        if ! "${script}" \
sulzbach's avatar
sulzbach committed
177 178 179 180
             "${_PROF}" \
             "${NAME}" \
             "${WEBSITE}" \
             "${TMP}" \
181
             "${CHROOTDIR}" \
sulzbach's avatar
sulzbach committed
182
             "${CACHEDIR}" \
sulzbach's avatar
sulzbach committed
183 184 185 186
             "${PKGLIST}" \
             "${SOURCESLIST}" \
             "${_REPO}" \
             "${KEYURL}" \
187
             "${_DEBUG}"; then
sulzbach's avatar
sulzbach committed
188 189 190
			 cleanup "${CHROOTDIR}"
             create_iso_logger --message "Failed to run " \
             "$(basename "${script}")"
191
        fi
sulzbach's avatar
sulzbach committed
192
        # This warning is disabled because the literal '-' is needed:
193 194
        # shellcheck disable=SC1001
        if [[ "$(basename "${script}")" =~ [0-9]+\-chroot\.* ]]; then
sulzbach's avatar
sulzbach committed
195 196 197 198 199 200 201
            # If the script is marked as 'chroot', it means it performs
            # changes in the overlaid chroot filesystem, therefore, the
            # 'chroot script', filled with the required commands by the marked
            # script, is executed
            init_chroot "${CHROOTDIR}"
            exec_chroot "${CHROOTDIR}"
            close_chroot "${CHROOTDIR}"
202 203 204 205
        fi
    fi
done

206
# Finally generates the resulting iso image
sulzbach's avatar
sulzbach committed
207 208 209
mkdir --parents "$(dirname "${_OUTPUT}")"
pushd "${TMP}/image" || \
create_iso_logger --message "Unable to change directory" --tag "pushd"
sulzbach's avatar
sulzbach committed
210 211
xorriso -as mkisofs -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin -c \
isolinux/boot.cat -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 \
Lucas Sulzbach's avatar
Lucas Sulzbach committed
212 213
-boot-info-table -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot \
-isohybrid-gpt-basdat -o "${_OUTPUT}" . || \
sulzbach's avatar
sulzbach committed
214 215
create_iso_logger --message "Failed to generate iso image." --tag "xorriso"
popd || create_iso_logger --message "Unable to change directory" --tag "popd"
216

sulzbach's avatar
sulzbach committed
217 218
cleanup "${CHROOTDIR}"

sulzbach's avatar
sulzbach committed
219 220 221
cond_umount "${CHROOTDIR}"
rm --recursive --force "${CHROOTDIR}"
cond_umount "${PREFIX:?}/squashfsbase"
sulzbach's avatar
sulzbach committed
222
rm --recursive --force "${PREFIX:?}/squashfsbase"
sulzbach's avatar
sulzbach committed
223
cond_umount "${TMP:?}/image"
sulzbach's avatar
sulzbach committed
224

sulzbach's avatar
sulzbach committed
225 226 227 228 229
mkdir --parents "${CACHEDIR}/${TIME_STAMP}"
mv "${TMP}/MD5SUMS" "${CACHEDIR}/${TIME_STAMP}/" || \
create_iso_logger --message "Unable to move file" --tag "mv"
mv "${TMP}/upper" "${CACHEDIR}/${TIME_STAMP}/image" || \
create_iso_logger --message "Unable to move directory" --tag "mv"
sulzbach's avatar
sulzbach committed
230

sulzbach's avatar
sulzbach committed
231 232 233
cond_umount "${TMPFS}"
rm --recursive --force "${TMPFS}"
cond_umount "${PREFIX:?}/isobase"
sulzbach's avatar
sulzbach committed
234
rm --recursive --force "${PREFIX:?}/isobase"
235 236

exit 0