Skip to content

Commit

Permalink
Mount snapshots in a different location
Browse files Browse the repository at this point in the history
In preparation for using autofs to mount snapshots.
  • Loading branch information
marcone committed Apr 18, 2020
1 parent c2bc1b6 commit 00fe88d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
5 changes: 3 additions & 2 deletions run/archiveloop
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,13 @@ function mount_all_snapshots {
do
log "mounting snapshot $img"
imgdir=$(dirname "$img")
if mount | grep "$imgdir/mnt"
mntdir=/tmp/snapshots/$(basename "$imgdir")
if mount | grep "$mntdir"
then
echo "$img already mounted"
return
fi
/root/bin/mount_image.sh "$img" "$imgdir/mnt"
/root/bin/mount_image.sh "$img" "$mntdir"
done
log "mounted all snapshots"
}
Expand Down
21 changes: 11 additions & 10 deletions run/make_snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function snapshot {
# shellcheck disable=SC2012
oldest=$(ls -ldC1 /backingfiles/snapshots/snap-* | head -1)
log "low space, deleting $oldest"
/root/bin/release_snapshot.sh "$oldest/mnt"
/root/bin/release_snapshot.sh "$oldest"
rm -rf "$oldest"
done

Expand All @@ -133,26 +133,27 @@ function snapshot {
local newsnapdir
oldname=/backingfiles/snapshots/snap-$(printf "%06d" "$oldnum")/snap.bin
newsnapdir=/backingfiles/snapshots/snap-$(printf "%06d" $newnum)
newsnapmnt=/tmp/snapshots/snap-$(printf "%06d" $newnum)

local newname=$newsnapdir/snap.bin
local tmpsnapdir=/backingfiles/snapshots/newsnap
local tmpsnapname=$tmpsnapdir/snap.bin
local tmpsnapmnt=$tmpsnapdir/mnt
log "taking snapshot of cam disk: $newname"
log "taking snapshot of cam disk in $newsnapdir"
rm -rf "$tmpsnapdir"
/root/bin/mount_snapshot.sh /backingfiles/cam_disk.bin "$tmpsnapname" "$tmpsnapmnt"
/root/bin/mount_snapshot.sh /backingfiles/cam_disk.bin "$tmpsnapname" "$newsnapmnt"
log "took snapshot"

# check whether this snapshot is actually different from the previous one
find "$tmpsnapmnt/TeslaCam" -type f -printf '%s %P\n' > "$tmpsnapname.toc"
log "comparing $oldname.toc and $tmpsnapname.toc"
if [[ ! -e "$oldname.toc" ]] || diff "$oldname.toc" "$tmpsnapname.toc" | grep -e '^>'
find "$newsnapmnt/TeslaCam" -type f -printf '%s %P\n' > "/tmp/snap.bin.toc"
log "comparing new snapshot with $oldname"
if [[ ! -e "$oldname.toc" ]] || diff "$oldname.toc" "/tmp/snap.bin.toc" | grep -e '^>'
then
make_links_for_snapshot "$tmpsnapmnt" "$newsnapdir/mnt"
ln -s "$newsnapmnt" "$tmpsnapdir/mnt"
make_links_for_snapshot "$newsnapmnt" "$newsnapdir/mnt"
mv "$tmpsnapdir" "$newsnapdir"
mv /tmp/snap.bin.toc "$newsnapdir"
else
log "new snapshot is identical to previous one, discarding"
/root/bin/release_snapshot.sh "$tmpsnapmnt"
/root/bin/release_snapshot.sh "$newsnapmnt"
rm -rf "$tmpsnapdir"
fi
}
Expand Down
8 changes: 8 additions & 0 deletions run/mount_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ PARTLOOP=$(losetup -j "$SNAP" | awk '{print $1}' | sed 's/:/p1/')
echo mount "$PARTLOOP" "$MNT"
mount -o ro "$PARTLOOP" "$MNT"

# if the image was created using the old mounting scheme,
# switch it over to the new one
mntlink=$(dirname "$SNAP")/mnt
if [ ! -L "$mntlink" ] && [ -d "$mntlink" ]
then
rmdir "$mntlink"
ln -sf "$MNT" "$mntlink"
fi
15 changes: 10 additions & 5 deletions run/release_snapshot.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/bin/sh
#!/bin/bash -eu

# for some reason "umount -d" doesn't remove the loop device, so we have to remove it ourselves
MNT=$(echo "$1" | sed 's/\/$//')
LOOP=$(mount | grep -w "$MNT" | awk '{print $1}' | sed 's/p1$//')
NAME=$(basename "$1")
LOOPPART=$(mount | grep -w "$NAME" | awk '{print $1}')
LOOPROOT=${LOOPPART/p1/}
MNT=$(findmnt -o target -n $LOOPPART)
umount "$MNT"
losetup -d "$LOOP"
losetup -d "$LOOPROOT"

# delete the snapshot folders
rm -rf /backingfiles/snapshots/$NAME /tmp/snapshots/$NAME

# delete all dead links
find /backingfiles/TeslaCam/ -depth -xtype l -delete || true

# delete all Sentry, saved and recent folders that are now empty
find /backingfiles/TeslaCam/ -mindepth 2 -depth -type d -empty -exec rmdir "{}" \;
find /backingfiles/TeslaCam/ -mindepth 2 -depth -type d -empty -exec rmdir "{}" \;
2 changes: 1 addition & 1 deletion tools/checksnapshotlinks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function recentpathfor {
echo "RecentClips/$filedate/$filename"
}

find /backingfiles/snapshots/ -type f -name \*.mp4 | sort -r | {
find -L /backingfiles/snapshots/ -type f -name \*.mp4 | sort -r | {
while read -r path
do
name=${path##/*TeslaCam/}
Expand Down

0 comments on commit 00fe88d

Please sign in to comment.