Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep directories when SIGINT sent to daemon #1526

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,20 +471,23 @@ func (o *snapshotter) getCleanupDirectories(ctx context.Context, t storage.Trans
}

func (o *snapshotter) cleanupSnapshotDirectory(ctx context.Context, dir string) error {

// On a remote snapshot, the layer is mounted on the "fs" directory.
// We use Filesystem's Unmount API so that it can do necessary finalization
// before/after the unmount.
mp := filepath.Join(dir, "fs")
if err := o.fs.Unmount(ctx, mp); err != nil {
log.G(ctx).WithError(err).WithField("dir", mp).Debug("failed to unmount")
if err := o.unmountSnapshotDirectory(ctx, dir); err != nil {
log.G(ctx).WithError(err).WithField("dir", dir).Debug("failed to unmount")
}
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("failed to remove directory %q: %w", dir, err)
}
return nil
}

func (o *snapshotter) unmountSnapshotDirectory(ctx context.Context, dir string) error {
// On a remote snapshot, the layer is mounted on the "fs" directory.
// We use Filesystem's Unmount API so that it can do necessary finalization
// before/after the unmount.
mp := filepath.Join(dir, "fs")
return o.fs.Unmount(ctx, mp)
}

func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, key, parent string, opts []snapshots.Opt) (_ storage.Snapshot, err error) {
ctx, t, err := o.ms.TransactionContext(ctx, true)
if err != nil {
Expand Down Expand Up @@ -656,12 +659,28 @@ func (o *snapshotter) Close() error {
// unmount all mounts including Committed
const cleanupCommitted = true
ctx := context.Background()
if err := o.cleanup(ctx, cleanupCommitted); err != nil {
log.G(ctx).WithError(err).Warn("failed to cleanup")
if err := o.unmountAllSnapshots(ctx, cleanupCommitted); err != nil {
log.G(ctx).WithError(err).Warn("failed to unmount snapshots on close")
}
return o.ms.Close()
}

func (o *snapshotter) unmountAllSnapshots(ctx context.Context, cleanupCommitted bool) error {
cleanup, err := o.cleanupDirectories(ctx, cleanupCommitted)
if err != nil {
return err
}

log.G(ctx).Debugf("unmount: dirs=%v", cleanup)
for _, dir := range cleanup {
if err := o.unmountSnapshotDirectory(ctx, dir); err != nil {
log.G(ctx).WithError(err).WithField("path", dir).Warn("failed to unmount directory")
}
}

return nil
}

// prepareRemoteSnapshot tries to prepare the snapshot as a remote snapshot
// using filesystems registered in this snapshotter.
func (o *snapshotter) prepareRemoteSnapshot(ctx context.Context, key string, labels map[string]string) error {
Expand Down
Loading