Created
June 6, 2020 22:31
-
-
Save lovesegfault/2c930244ccf41026e314d3be8f943a4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to create a directory in both the place where we want | |
# to bind mount it as well as making sure it exists in the location | |
# where persistence is located. This also enforces the correct ownership | |
# of the directory structure. | |
mkDirCreationSnippet = persistentStoragePath: dir: | |
'' | |
# capture the nix vars into bash to avoid escape hell | |
local sourceBase="${persistentStoragePath}" | |
local target="${dir}" | |
# iterate over each part of the target path | |
local previousPath="" | |
for pathPart in $(echo "$target" | tr "/" " "); do | |
# construct the incremental path, e.g. /var, /var/lib, /var/lib/iwd | |
local currentTargetPath="/$previousPart/$pathPart" | |
# construct the source path, e.g. /state/var, /state/var/lib | |
local currentSourcePath="$sourceBase$currentTargetPath" | |
if [ ! -e "$currentSourcePath" ]; then | |
printf "Bind source '%s' does not exist, creating it | |
mkdir "$currentSourcePath" | |
fi | |
if [ ! -e "$currentTargetPath" ]; then | |
mkdir "$currentTargetPath" | |
fi | |
# synchronize perms between the two, should be a noop if they were | |
# both just created. | |
chown -c --reference="$currentSourcePath" "$currentTargetPath" | |
unset currentSourcePath | |
unset currentTargetPath | |
done | |
unset sourceBase | |
unset target | |
''; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment