Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Prune linux-firmware non-greedy
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
pjp
Administrator
Administrator


Joined: 16 Apr 2002
Posts: 20067

PostPosted: Wed Aug 03, 2022 8:43 pm    Post subject: Prune linux-firmware non-greedy Reply with quote

Prerequisites:
File names of firmware to be retained.
Some ability to work with awk regular expressions (or an alternative tool, suitably exchanged for awk).

What it does:
Applies a filter based on a common portion of firmware filenames.
Intercepts the emerge process during the install phase, replacing contents of the "WHENCE" file to contain only filtered list of files.
Creates a linux-firmware-<version> savedconfig file during the instprep phase.
Portage continues the install as normal, only installing the desired firmware files and an appropriate savedconfig file.

Potential Issues:
If a filter were to exclude all versions of needed firmware, the system may have a loss of functionality.

Example: With no available wireless firmware, wireless networking might be unable to start. Therefore, it may be useful to keep the last known working versions available. See: emerge --buildpkg option (man emerge) and the location of PKGDIR (man make.conf).

Partial example:
NOTE:
* Pattern matching has been moved external from the script into the file referenced by "FILTER_FILE".
* awk is still used, which may have different syntax than expected. For example, an asterisk pattern match in awk should be a dot followed by an asterisk, as in: .*


(output shortened, including use of "wc -l")
Code:
$ ebuild /var/db/repos/gentoo/sys-kernel/linux-firmware/linux-firmware-20220708.ebuild install
$ cd /var/tmp/portage/sys-kernel/linux-firmware-20220708/temp/

awk pattern match keeping all rtl_nic files or select rtl_nic files:
Code:
$ awk '/rtl_nic/ { print $0 }' WHENCE |wc -l
31
$ awk '/rtl_nic\/rtl8(168|411)/ { print $0 }' WHENCE |wc -l
15
Modify the awk pattern match to suit your needs.

New hardware requires new files:
Either move the file from he env path location temporarily.
Or use the previously show ebuild command to identify new files (located in /var/tmp/portage/sys-kernel/linux-firmware-20220708/work/).

The code:
/etc/portage/env/sys-kernel/linux-firmware.filter:
rtl_nic/rtl8(168|411)
amd-ucode/microcode_amd(_fam16h)?.bin
/etc/portage/env/sys-kernel/linux-firmware:
# This may not be a good location. However, Portage warns about atom errors under package.env
FILTER_FILE="/etc/portage/env/sys-kernel/linux-firmware.filter"

# | Ebuild Phase: install                                                   |
if [ "${CATEGORY}" == "sys-kernel" ] && \
   [ "${PN}" == "linux-firmware" ]   && \
   [ "${EBUILD_PHASE}" == "install" ] && \
   [ -e ${FILTER_FILE} ]
then
        # WORKDIR    : ebuild root build directory: "${PORTAGE_BUILDDIR}/work"
        local wf
        # Per ebuild comment, MY_COMMIT is:
        #  - used for a "real snapshot"
        #  - or left blank for tagged releases
        if [[ -z ${MY_COMMIT} ]] ; then
            wf="${WORKDIR}/${P}/WHENCE"
        else
            wf="${WORKDIR}/${MY_COMMIT}/WHENCE"
        fi

        /bin/cp -a ${wf} ${T}/ \
            || die "Error copying ${wf} to temp directory."

        /usr/bin/awk 'NR==FNR{patterns[$1]; next} {for (p in patterns) if ($0 ~ p) print $0}' ${FILTER_FILE} "${T}/WHENCE" > "${wf}" \
            || die "Error creating new ${wf} file."
fi


# | Ebuild Phase: instprep                                                    |
if [ "${CATEGORY}" == "sys-kernel" ] && \
   [ "${PN}" == "linux-firmware" ]   && \
   [ "${EBUILD_PHASE}" == "instprep" ] && \
   [ -e ${FILTER_FILE} ]
then
        local sc

        # modify image directory
        sc="${D}/etc/portage/savedconfig/sys-kernel/${PF}"
        /bin/cp -a ${sc} ${T}/ \
            || die "Error copying ${sc} to temp directory."

        # Example:              "${T}/linux-firmware-20220708"
        awk '/^#/ { print $0 }' "${T}/${PF}" > "${sc}" \
            || die "Error preparing new ${sc} file."
        # Sort the file.
        awk '/^(amd|rtl)/ { print $0 }' "${T}/${PF}" |sort >> "${sc}" \
            || die "Error populating new ${sc} file."
fi



EDIT:
- Fixed a conditional logic error with the FILTER_FILE check.
- Moved awk patterns to FILTER_FILE location. Tested a few pattern variations using "ebuild ... install".
- Updated to accommodate versions using MY_COMMIT
- Replaced incorrect uses of ${P} with ${PF}
_________________
Quis separabit? Quo animo?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum