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

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

47 48 49 50 51 52 53
# 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)"
cp "${CHROOTDIR}/boot/vmlinuz-${version}-generic" "${TMP}/image/casper/vmlinuz" || perror "Unable to copy kernel to image"

# The initrd is converted from gzip to lzma
54
if ! test -f initrd.lz; then
55 56
    if [ ! -d "${PREFIX}/initrd" ]; then
        mkdir "${PREFIX}/initrd"
57
    else
58
        rm --recursive --force "${PREFIX:?}"/initrd/*
59
    fi
60 61 62 63 64 65 66 67
    pinfo "Using '$(basename "${initrd_path}")'"
    initrd_type="$(file "${initrd_path}")"
    pushd "${PREFIX}/initrd" || perror "Unable to change directory"
    if grep --quiet --ignore-case "gzip" <<< "${initrd_type}"; then
        gzip --decompress --stdout "${initrd_path}" | cpio --extract || perror "Unable to decompress initrd"
    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
68
    else
69
        perror "Invalid initrd format"
70
    fi
71 72 73
    find . | cpio --quiet --create -H newc | lzma -7 > ../initrd.lz || perror "Unable to compress initrd"
    popd || perror "Unable to change directory"
    rm --recursive --force "${PREFIX:?}/initrd"
Daniel Bissani Furlin's avatar
Daniel Bissani Furlin committed
74
fi
75
mv initrd.lz "${TMP}/image/casper/initrd.lz" || perror "Unable to copy initrd to image"
76

77

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