make-initrd.sh 3.07 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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
# _DIST="${1}"
# _ARCH="${2}"
# _REPO="${3}"
CHROOTDIR="${4}"
# SCRIPT_NAME="${5}"
# DISTRO="${6}"
# DPLIST="${7}"
# INSTLIST="${8}"
# ISOLINUX="${9}"
# MOUNTLIST="${10}"
# OURSOURCESLIST="${11}"
# RMDSKT="${12}"
# RMLIST="${13}"
# SCRIPTSDIR="${14}"
# SHORTNAME="${15}"
# SOURCESLISTDEFAULT="${16}"
TMP="${17}"
# TMPFS="${18}"
# REPOPKGS="${19}"
PREFIX="${PWD}"
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
44

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

47 48 49 50
# The most recent kernel available in the base system is used in the image
initrd_path="$(find "${CHROOTDIR}/boot/" -maxdepth 1 -regextype egrep -name "initrd.img-[0-9,.,-]*-generic" | \
sort --version-sort --reverse | head --lines=1)"
version="$(basename "${initrd_path}" | cut --delimiter='-' --fields=2,3)"
Lucas Sulzbach's avatar
Lucas Sulzbach committed
51
cp "${CHROOTDIR}/boot/vmlinuz-${version}-generic" "${TMP}/image/casper/vmlinuz" || createIsoLogger --message "Unable to copy kernel to image" --tag "cp"
52 53

# The initrd is converted from gzip to lzma
Lucas's avatar
Lucas committed
54 55 56 57 58 59 60 61 62 63 64
createIsoLogger --message "Using '$(basename "${initrd_path}")'" --type info
initrd_type="$(file "${initrd_path}")"
mkdir "${PREFIX}/initrd"
pushd "${PREFIX}/initrd" || createIsoLogger --message "Unable to change directory" --tag "pushd"
if grep --quiet --ignore-case "gzip" <<< "${initrd_type}"; then
    gzip --decompress --stdout "${initrd_path}" | cpio --extract || createIsoLogger --message "Unable to decompress initrd" --tag "gzip"
elif grep --quiet --ignore-case "ascii" <<< "${initrd_type}"; then
    block_size="$(binwalk --include="compressed data" "${initrd_path}" | cut --delimiter=" " --fields=1 | grep -E "[0-9]+")"
    dd if="${initrd_path}" bs="${block_size}" skip=1 | gunzip | cpio --extract --make-directories
else
    createIsoLogger --message "Invalid initrd format"
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
65
fi
Lucas's avatar
Lucas committed
66 67 68
find . | cpio --quiet --create -H newc | lzma -7 > ../initrd.lz || createIsoLogger --message "Unable to compress initrd" --tag "lzma"
popd || createIsoLogger --message "Unable to change directory" --tag "popd"
rm --recursive --force "${PREFIX:?}/initrd" || createIsoLogger --message "Unable to remove dir \"${PREFIX}/initrd\"" --tag "rm"
Lucas Sulzbach's avatar
Lucas Sulzbach committed
69
mv initrd.lz "${TMP}/image/casper/initrd.lz" || createIsoLogger --message "Unable to move initrd to image" --tag "mv"
70

71

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