Add `KeepFiles` config
Host OS: Arch Linux
uname: Linux 6.6.3-arch1-1 SMP PREEMPT_DYNAMIC x86_64 GNU/Linux
mkosi: v19
Description
Similar to the KernelModules{Include,Exclude} config options, where "Include" overrides "Exclude", it'd be super useful to have a KeepFiles counterpart to RemoveFiles.
Right now, my mkosi.prepare & mkosi.finalize scripts are mostly taken up by functions similar to the following:
rm_charmaps() {
printf "Removing unused character encodings...\n" # Trims ~2.7MiB.
mkdir /usr/share/i18n/charmaps.tmp
cp /usr/share/i18n/charmaps/UTF-8.gz /usr/share/i18n/charmaps.tmp/
rm -r /usr/share/i18n/charmaps
mv /usr/share/i18n/charmaps.tmp -T /usr/share/i18n/charmaps
}
However, it would save a lot of scripting if I could instead do this in my config:
RemoveFiles = /usr/share/i18n/charmaps
KeepFiles = /usr/share/i18n/charmaps/UTF-8.*
I agree this would be useful but I'm not sure if we can implement this efficiently without introducing quite a bit of complexity. I don't see us adding this unless there's a more beefed up version of rm (what we currently use) that supports include/exclude behavior natively.
Would using a similar method to the cp2tmp -> rm -> mv as used in that script snippet help? It's the most efficient way I found of doing it, but I don't know how well it'd scale in mkosi.
Another option is rm !(file1.ext|file2.ext|file3.ext) with shopt -s extglob set, though I guess that depends on if bash is used internally by mkosi already. Or, as a probably terrible idea, using chattr -i on the files to keep and then removing the attribute afterwards.
find supports a -not specifier to invert selections though I am not sure how quick that is. Doing it in pure Python could also be sufficient depending on the complexity of the image/pattern
Could taking advantage of snapshots avoid some of the complexity? e.g.
- Before
RemoveFiles=is applied, take a snapshot of root A as root B. - Apply
RemoveFiles=to root A as it does now. - Then, apply
KeepFiles=by copying the specified files from root B to root A (and their parents where applicable), and finally, remove root B.
One issue is that this will be slower on hosts without snapshotting, however, that seems to already be the case with mkosi.