Skip to content

Commit

Permalink
Drop mountpoints once no longer needed (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Aug 24, 2022
1 parent f03c1b8 commit 24c1a41
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions infra/storage/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"time"

"github.com/outofforest/go-zfs/v3"
Expand Down Expand Up @@ -100,8 +101,9 @@ func (d *zfsDriver) BuildID(ctx context.Context, buildKey types.BuildKey) (types

// CreateEmpty creates blank build
func (d *zfsDriver) CreateEmpty(ctx context.Context, imageName string, buildID types.BuildID) (FinalizeFn, string, error) {
buildDir := "/" + d.config.Root + "/" + string(buildID)
filesystem, err := zfs.CreateFilesystem(ctx, d.config.Root+"/"+string(buildID), zfs.CreateFilesystemOptions{Properties: map[string]string{
"mountpoint": "/" + d.config.Root + "/" + string(buildID) + "/root",
"mountpoint": buildDir + "/root",
propertyName: string(must.Bytes(json.Marshal(types.BuildInfo{
BuildID: buildID,
Name: imageName,
Expand All @@ -122,6 +124,9 @@ func (d *zfsDriver) CreateEmpty(ctx context.Context, imageName string, buildID t
if err := filesystem.SetProperty(ctx, "canmount", "off"); err != nil {
return err
}
if err := os.RemoveAll(buildDir); err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err)
}
_, err := filesystem.Snapshot(ctx, "image")
return err
}, filesystem.Info.Mountpoint, nil
Expand All @@ -135,9 +140,9 @@ func (d *zfsDriver) Clone(ctx context.Context, srcBuildID types.BuildID, dstImag
}

properties := dstBuildID.Type().Properties()

buildDir := "/" + d.config.Root + "/" + string(dstBuildID)
filesystem, err := snapshot.Clone(ctx, d.config.Root+"/"+string(dstBuildID), zfs.CloneOptions{Properties: map[string]string{
"mountpoint": "/" + d.config.Root + "/" + string(dstBuildID) + "/root",
"mountpoint": buildDir + "/root",
propertyName: string(must.Bytes(json.Marshal(types.BuildInfo{
BuildID: dstBuildID,
BasedOn: srcBuildID,
Expand All @@ -156,6 +161,9 @@ func (d *zfsDriver) Clone(ctx context.Context, srcBuildID types.BuildID, dstImag
if err := filesystem.SetProperty(ctx, "mountpoint", "none"); err != nil {
return err
}
if err := os.RemoveAll(buildDir); err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err)
}
}
if !properties.Mountable {
if err := filesystem.SetProperty(ctx, "canmount", "off"); err != nil {
Expand Down Expand Up @@ -246,6 +254,9 @@ func (d *zfsDriver) Drop(ctx context.Context, buildID types.BuildID) error {
if err := filesystem.Destroy(ctx, zfs.DestroyRecursive); err != nil {
return errors.WithStack(fmt.Errorf("build %s have children: %w", buildID, ErrImageHasChildren))
}
if err := os.RemoveAll("/" + d.config.Root + "/" + string(buildID)); err != nil && !errors.Is(err, os.ErrNotExist) {
return errors.WithStack(err)
}
return nil
}

Expand Down

0 comments on commit 24c1a41

Please sign in to comment.