diff --git a/main.go b/main.go index 2f1c760..81cedf6 100644 --- a/main.go +++ b/main.go @@ -67,6 +67,7 @@ var objfsVersion = "7.2" const ( volumeBucket = "volumes" snapshotsDirectory = "snapshots" + backupDirectory = "backup" ) func (d *ofsDriver) Create(r *volume.CreateRequest) error { @@ -169,7 +170,7 @@ func (d *ofsDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) { return &volume.GetResponse{Volume: volumeInfo.Volume}, nil } -func umount(v *ofsVolume, rt *ofsVolumeRt) error { +func umount(v *ofsVolume, rt *ofsVolumeRt, isRemount bool) error { log.WithFields(log.Fields{"name": v.Volume.Name}).Info("Unmount ObjectiveFS Volume") if !rt.mounted { return fmt.Errorf("volume is not mounted") @@ -199,10 +200,14 @@ func umount(v *ofsVolume, rt *ofsVolumeRt) error { } } - if err := exec.Command("umount", v.Volume.Mountpoint).Run(); err != nil { - return err + var umountPath string + if v.Backup { + umountPath = path.Join(v.Volume.Mountpoint, backupDirectory) + } else { + umountPath = v.Volume.Mountpoint } - if err := os.Remove(v.Volume.Mountpoint); err != nil { + + if err := exec.Command("umount", umountPath).Run(); err != nil { return err } @@ -212,8 +217,19 @@ func umount(v *ofsVolume, rt *ofsVolumeRt) error { return err } v.BackupSnapshot = "" + + if !isRemount { + if err := os.Remove(umountPath); err != nil { + return err + } + } } + if !isRemount { + if err := os.Remove(v.Volume.Mountpoint); err != nil { + return err + } + } rt.mounted = false return nil } @@ -239,7 +255,7 @@ func (d *ofsDriver) Remove(r *volume.RemoveRequest) error { return fmt.Errorf("volume '%s' currently in use (%d unique)", r.Name, len(rt.use)) } if rt.mounted { - if err := umount(v, rt); err != nil { + if err := umount(v, rt, false); err != nil { return err } } @@ -294,7 +310,7 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) // Each mount request causes new snapshot to get created and the old one deleted if v.Backup && rt.mounted { log.WithFields(log.Fields{"name": r.Name, "snapshot": v.BackupSnapshot}).Info("Going to unmount and destroy the previous backup snapshot") - if err := umount(v, rt); err != nil { + if err := umount(v, rt, true); err != nil { return &volume.MountResponse{}, err } } @@ -304,9 +320,17 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) return &volume.MountResponse{}, err } - var fs string + var fs, mountPath string if v.Backup { log.WithFields(log.Fields{"name": r.Name}).Info("Going to create a backup snapshot") + mountPath := path.Join(v.Volume.Mountpoint, backupDirectory) + if _, err := os.Stat(mountPath); os.IsNotExist(err) { + log.WithFields(log.Fields{"name": r.Name, "directory": mountPath}).Info("Creating the backup mount directory") + if err := os.Mkdir(mountPath, os.ModePerm); err != nil { + log.WithFields(log.Fields{"name": r.Name}).Error("Failed to create the backup mount directory.") + return &volume.MountResponse{}, err + } + } if snapshotTextB, err := applyEnv(exec.Command("/sbin/mount.objectivefs", "snapshot", v.Fs), v.Env).CombinedOutput(); err != nil { log.WithFields(log.Fields{"name": v.Volume.Name, "output": string(snapshotTextB[:])}).Info("Failed to create a new backup snapshot") return &volume.MountResponse{}, err @@ -324,14 +348,15 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) } } else { fs = v.Fs + mountPath = v.Volume.Mountpoint } // Note: The first argument ("mount") causes running in the foreground, its absence in the background var cmd *exec.Cmd if len(v.Opts) == 0 { - cmd = exec.Command("/sbin/mount.objectivefs", "mount", fs, v.Volume.Mountpoint) + cmd = exec.Command("/sbin/mount.objectivefs", "mount", fs, mountPath) } else { - cmd = exec.Command("/sbin/mount.objectivefs", "mount", "-o"+v.Opts, fs, v.Volume.Mountpoint) + cmd = exec.Command("/sbin/mount.objectivefs", "mount", "-o"+v.Opts, fs, mountPath) } cmd.Env = v.Env @@ -370,7 +395,7 @@ func (d *ofsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) } // Check for mount - if isObjfs, err := isObjectiveFsMount(v.Volume.Mountpoint); err == nil && isObjfs { + if isObjfs, err := isObjectiveFsMount(mountPath); err == nil && isObjfs { break } } @@ -526,7 +551,7 @@ func (d *ofsDriver) Unmount(r *volume.UnmountRequest) error { delete(rt.use, r.ID) if len(rt.use) == 0 && v.Asap { - if err := umount(v, rt); err != nil { + if err := umount(v, rt, false); err != nil { return err } }