make-initrd.sh 3.49 KB
Newer Older
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
1
#!/usr/bin/env 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
# 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.
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
23

sulzbach's avatar
sulzbach committed
24 25 26 27 28 29 30 31 32 33 34 35
# PROFILE="$1"
# NAME="$2"
# WEBSITE="$3"
# TMPFS="$4"
TMP="$5"
CHROOTDIR="$6"
# SCRIPTSDIR="$7"
# PKGLIST="$8"
# SOURCESLIST="$9"
# REPO="${10}"
# KEYURL="${11}"

36
PREFIX="${PWD}"
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
37

Lucas Sulzbach's avatar
Lucas Sulzbach committed
38
type createIsoLogger >/dev/null 2>&1 || source "${PREFIX}/helper/logging.incsh"
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
39

40
# The most recent kernel available in the base system is used in the image
sulzbach's avatar
sulzbach committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
initrd_path="$(find "${CHROOTDIR}/boot/" -maxdepth 1 -regextype egrep -name \
"initrd.img-[0-9,.,-]*-generic" | sort --version-sort | tail --lines=1)"

NEWMD5="$(md5sum "${initrd_path}")"
echo "${NEWMD5}" > "${TMP}/MD5SUMS"

PREVIOUS="$(find "${TMP}/cache" -mindepth 1 -maxdepth 1 -type d | sort \
--numeric | tail --lines=1)"

if [ -f "${PREVIOUS}/MD5SUMS" ]; then
    OLDMD5="$(cat "${PREVIOUS}/MD5SUMS")"

    if [ "${OLDMD5}" == "${NEWMD5}" ]; then
        createIsoLogger --message \
        "Caching '${PREVIOUS}/image/casper/initrd.lz'" --type info
        exit 0
    fi
fi

60
version="$(basename "${initrd_path}" | cut --delimiter='-' --fields=2,3)"
sulzbach's avatar
sulzbach committed
61 62 63
cp "${CHROOTDIR}/boot/vmlinuz-${version}-generic" \
"${TMP}/image/casper/vmlinuz" || createIsoLogger --message \
"Unable to copy kernel to image" --tag "cp"
64 65

# The initrd is converted from gzip to lzma
Lucas's avatar
Lucas committed
66 67 68
createIsoLogger --message "Using '$(basename "${initrd_path}")'" --type info
initrd_type="$(file "${initrd_path}")"
mkdir "${PREFIX}/initrd"
sulzbach's avatar
sulzbach committed
69 70
pushd "${PREFIX}/initrd" || createIsoLogger --message \
"Unable to change directory" --tag "pushd"
Lucas's avatar
Lucas committed
71
if grep --quiet --ignore-case "gzip" <<< "${initrd_type}"; then
sulzbach's avatar
sulzbach committed
72 73 74
    gzip --decompress --stdout "${initrd_path}" | cpio --extract \
    --make-directories || createIsoLogger --message \
    "Unable to decompress initrd" --tag "gzip"
Lucas's avatar
Lucas committed
75
elif grep --quiet --ignore-case "ascii" <<< "${initrd_type}"; then
sulzbach's avatar
sulzbach committed
76 77 78 79 80 81
    # TODO: Check what's happening here
    block_size="$(binwalk --include="compressed data" "${initrd_path}" | cut \
    --delimiter=" " --fields=1 | grep -E "[0-9]+" | head --lines=1)"
    dd if="${initrd_path}" bs="${block_size}" skip=1 | gunzip | cpio --extract \
    --make-directories || createIsoLogger --message \
    "Unable to decompress initrd" --tag "gunzip"
Lucas's avatar
Lucas committed
82 83
else
    createIsoLogger --message "Invalid initrd format"
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
84
fi
sulzbach's avatar
sulzbach committed
85 86
find . | cpio --quiet --create -H newc | lzma -7 > ../initrd.lz || \
createIsoLogger --message "Unable to compress initrd" --tag "lzma"
Lucas's avatar
Lucas committed
87
popd || createIsoLogger --message "Unable to change directory" --tag "popd"
sulzbach's avatar
sulzbach committed
88 89 90 91
rm --recursive --force "${PREFIX:?}/initrd" || createIsoLogger --message \
"Unable to remove dir \"${PREFIX}/initrd\"" --tag "rm"
mv initrd.lz "${TMP}/image/casper/initrd.lz" || createIsoLogger --message \
"Unable to move initrd to image" --tag "mv"
92

Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
93
exit 0