Skip to content

Instantly share code, notes, and snippets.

@zatricky
Created February 12, 2021 07:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zatricky/b195b474ed5f3c3c4c57e878eef4f0f5 to your computer and use it in GitHub Desktop.
Save zatricky/b195b474ed5f3c3c4c57e878eef4f0f5 to your computer and use it in GitHub Desktop.
#!/bin/bash
[[ "$(id -u)" -eq 0 ]] || (echo "Got root?" ; exit 5)
command -v btrfs > /dev/null || (echo "Command not found: btrfs" ; exit 4)
for folder in $* ; do
mount | awk '{print $3}' | grep "$(pwd)/${folder}" && echo "${folder} has a submount?" && exit 1
btrfs sub list . | grep "${PWD##*/}/${folder}" && echo "${folder} has a subvolume?" && exit 2
[ -e ${folder}.temp ] && echo "${folder}.temp already exists?" && exit 3 || true
mv ${folder} ${folder}.temp
btrfs sub create ${folder}
USER=$(stat -c '%U' ${folder}.temp)
GROUP=$(stat -c '%G' ${folder}.temp)
chown ${USER}.${GROUP} ${folder}
cp -a --reflink=always ${folder}.temp/. ${folder}/
echo "Delete ${folder}.temp/ if happy with ${folder}/"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment