Skip to content

Instantly share code, notes, and snippets.

@lovesegfault
Created June 6, 2020 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovesegfault/2c930244ccf41026e314d3be8f943a4e to your computer and use it in GitHub Desktop.
Save lovesegfault/2c930244ccf41026e314d3be8f943a4e to your computer and use it in GitHub Desktop.
# 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