From 9e174565cee44286adcf35fff49a3d58af47ad6b Mon Sep 17 00:00:00 2001 From: Roman Vanicek Date: Tue, 19 Mar 2024 23:49:45 +0100 Subject: [PATCH] Use tmpfs for snapshots --- main.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 8f03d58..9e4c7cb 100644 --- a/main.go +++ b/main.go @@ -183,6 +183,12 @@ func umount(v *ofsVolume, rt *ofsVolumeRt) error { log.WithFields(log.Fields{"name": v.Volume.Name, "snapshot": i}).Trace("Snapshot unmounted on unmount.") } } + + if err := exec.Command("umount", snapshotsPath).Run(); err != nil { + log.WithFields(log.Fields{"name": v.Volume.Name}).Warn("Failed to unmount a snapshots tmpfs on unmount.") + } else { + log.WithFields(log.Fields{"name": v.Volume.Name}).Trace("Snapshot tmpfs unmounted on unmount.") + } } else { log.WithFields(log.Fields{"name": v.Volume.Name, "Error": err}).Warn("Cannot determine existing snapshot mounts on unmount") } @@ -339,6 +345,12 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) } } + // Mount a tmpfs there so it becomes machine specific and we can promptly delete directories + // that are outside of filteredSnapshots + if err := exec.Command("/bin/mount", "-t", "tmpfs", "-o", "size=8m", "tmpfs", snapshotsPath).Run(); err != nil { + log.WithFields(log.Fields{"name": r.Name, "directory": snapshotsDirectory}).Warn("Failed to create tmpfs in the snapshots mount directory.") + } + for cmd.ProcessState == nil { if snapshotRulesB, err := applyEnv(exec.Command("/sbin/mount.objectivefs", "snapshot", "-l", v.Fs), v.Env).Output(); err == nil { log.WithFields(log.Fields{"name": r.Name, "snapshotRules": string(snapshotRulesB)}).Debug("Current snapshot rules") @@ -367,8 +379,6 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) } // Unmount snapshots that are no longer in filteredSnapshots to conserve RAM - // Note: We cannot remove the directory as other machines may have different - // filteredSnapshots and may be using them. for i, path := range setMinus(existingMounts, setIntersect(expectedSnapshots, filteredSnapshots)) { if err := exec.Command("umount", path).Run(); err != nil { log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Warn("Failed to unmount an expired snapshot.") @@ -378,9 +388,9 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) } // Remove left-over dirs of expired snapshots - // Note: That is directories that no other machine would use as the snapshots are not - // mounted due to filteredSnapshots but because they are not even in expectedSnapshots. - for i, path := range setMinus(existingMountDirs, expectedSnapshots) { + // Note: As our root shall be on tmpfs deleting the directories does not affect + // other machines with different filteredSnapshots + for i, path := range setMinus(existingMountDirs, setIntersect(expectedSnapshots, filteredSnapshots)) { if err := os.Remove(path); err != nil { log.WithFields(log.Fields{"name": r.Name, "snapshot": i}).Warn("Failed to remove directory of an expired snapshot.") }