make-initrd.sh 3.51 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
# PROFILE="$1"
# NAME="$2"
# WEBSITE="$3"
# TMPFS="$4"
TMP="$5"
CHROOTDIR="$6"
# SCRIPTSDIR="$7"
# PKGLIST="$8"
# SOURCESLIST="$9"
# REPO="${10}"
# KEYURL="${11}"
sulzbach's avatar
sulzbach committed
35
CACHEDIR="${12}"
sulzbach's avatar
sulzbach committed
36

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

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

41
# The most recent kernel available in the base system is used in the image
sulzbach's avatar
sulzbach committed
42 43 44 45 46 47
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"

sulzbach's avatar
sulzbach committed
48
PREVIOUS="$(find "${CACHEDIR}" -mindepth 1 -maxdepth 1 -type d | sort \
sulzbach's avatar
sulzbach committed
49 50 51 52 53 54 55 56 57 58 59 60
--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

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

# The initrd is converted from gzip to lzma
Lucas's avatar
Lucas committed
67 68 69
createIsoLogger --message "Using '$(basename "${initrd_path}")'" --type info
initrd_type="$(file "${initrd_path}")"
mkdir "${PREFIX}/initrd"
sulzbach's avatar
sulzbach committed
70 71
pushd "${PREFIX}/initrd" || createIsoLogger --message \
"Unable to change directory" --tag "pushd"
Lucas's avatar
Lucas committed
72
if grep --quiet --ignore-case "gzip" <<< "${initrd_type}"; then
sulzbach's avatar
sulzbach committed
73 74 75
    gzip --decompress --stdout "${initrd_path}" | cpio --extract \
    --make-directories || createIsoLogger --message \
    "Unable to decompress initrd" --tag "gzip"
Lucas's avatar
Lucas committed
76
elif grep --quiet --ignore-case "ascii" <<< "${initrd_type}"; then
sulzbach's avatar
sulzbach committed
77 78 79 80 81 82
    # 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
83 84
else
    createIsoLogger --message "Invalid initrd format"
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
85
fi
sulzbach's avatar
sulzbach committed
86 87
find . | cpio --quiet --create -H newc | lzma -7 > ../initrd.lz || \
createIsoLogger --message "Unable to compress initrd" --tag "lzma"
Lucas's avatar
Lucas committed
88
popd || createIsoLogger --message "Unable to change directory" --tag "popd"
sulzbach's avatar
sulzbach committed
89 90 91 92
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"
93

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