github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap-update-ns/update.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package main
    21  
    22  import (
    23  	"github.com/snapcore/snapd/logger"
    24  	"github.com/snapcore/snapd/osutil"
    25  )
    26  
    27  // MountProfileUpdateContext provides the context of a mount namespace update.
    28  // The context provides a way to synchronize the operation with other users of
    29  // the snap system, to load and save the mount profiles and to provide the file
    30  // system assumptions with which the mount namespace will be modified.
    31  type MountProfileUpdateContext interface {
    32  	// Lock obtains locks appropriate for the update.
    33  	Lock() (unlock func(), err error)
    34  	// Assumptions computes filesystem assumptions under which the update shall operate.
    35  	Assumptions() *Assumptions
    36  	// LoadDesiredProfile loads the mount profile that should be constructed.
    37  	LoadDesiredProfile() (*osutil.MountProfile, error)
    38  	// LoadCurrentProfile loads the mount profile that is currently applied.
    39  	LoadCurrentProfile() (*osutil.MountProfile, error)
    40  	// SaveCurrentProfile saves the mount profile that is currently applied.
    41  	SaveCurrentProfile(*osutil.MountProfile) error
    42  }
    43  
    44  func executeMountProfileUpdate(upCtx MountProfileUpdateContext) error {
    45  	unlock, err := upCtx.Lock()
    46  	if err != nil {
    47  		return err
    48  	}
    49  	defer unlock()
    50  
    51  	desired, err := upCtx.LoadDesiredProfile()
    52  	if err != nil {
    53  		return err
    54  	}
    55  	debugShowProfile(desired, "desired mount profile")
    56  
    57  	currentBefore, err := upCtx.LoadCurrentProfile()
    58  	if err != nil {
    59  		return err
    60  	}
    61  	debugShowProfile(currentBefore, "current mount profile (before applying changes)")
    62  
    63  	// Synthesize mount changes that were applied before for the purpose of the tmpfs detector.
    64  	as := upCtx.Assumptions()
    65  	for _, entry := range currentBefore.Entries {
    66  		as.AddChange(&Change{Action: Mount, Entry: entry})
    67  	}
    68  
    69  	// Compute the needed changes and perform each change if
    70  	// needed, collecting those that we managed to perform or that
    71  	// were performed already.
    72  	changesNeeded := NeededChanges(currentBefore, desired)
    73  	debugShowChanges(changesNeeded, "mount changes needed")
    74  
    75  	logger.Debugf("performing mount changes:")
    76  	var changesMade []*Change
    77  	for _, change := range changesNeeded {
    78  		logger.Debugf("\t * %s", change)
    79  		synthesised, err := change.Perform(as)
    80  		changesMade = append(changesMade, synthesised...)
    81  		if len(synthesised) > 0 {
    82  			logger.Debugf("\tsynthesised additional mount changes:")
    83  			for _, synth := range synthesised {
    84  				logger.Debugf(" * \t\t%s", synth)
    85  			}
    86  		}
    87  		if err != nil {
    88  			// We may have done something even if Perform itself has
    89  			// failed. We need to collect synthesized changes and
    90  			// store them.
    91  			origin := change.Entry.XSnapdOrigin()
    92  			if origin == "layout" || origin == "overname" {
    93  				// TODO: convert the test to a method over origin.
    94  				return err
    95  			} else if err != ErrIgnoredMissingMount {
    96  				logger.Noticef("cannot change mount namespace according to change %s: %s", change, err)
    97  			}
    98  			continue
    99  		}
   100  
   101  		changesMade = append(changesMade, change)
   102  	}
   103  
   104  	// Compute the new current profile so that it contains only changes that were made
   105  	// and save it back for next runs.
   106  	var currentAfter osutil.MountProfile
   107  	for _, change := range changesMade {
   108  		if change.Action == Mount || change.Action == Keep {
   109  			currentAfter.Entries = append(currentAfter.Entries, change.Entry)
   110  		}
   111  	}
   112  	debugShowProfile(&currentAfter, "current mount profile (after applying changes)")
   113  	return upCtx.SaveCurrentProfile(&currentAfter)
   114  }