Skip to content

Commit

Permalink
Podman System Reset --run-root --graph-root
Browse files Browse the repository at this point in the history
added options to reinstantiate storage after resetting the podman system using
podman system reset. --run-root and --graph-root define the runRoot and graphRoot to initiate
a new runtime with. these values, once populated into a new libpd runtime, then are used to create a new storage.conf
file at the proper location (root or rootless default location).

These options allow for a more interactive and useful reset program and ill help eliminate many questions regarding how to configure the basic entities
of container storage.

depends on containers/storage#1096

Signed-off-by: cdoern <cdoern@redhat.com>
  • Loading branch information
cdoern authored and cdoern committed Feb 17, 2022
1 parent f918a94 commit ea8ea4b
Show file tree
Hide file tree
Showing 29 changed files with 367 additions and 121 deletions.
18 changes: 17 additions & 1 deletion cmd/podman/system/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var (
}

forceFlag bool
runDir string
graphDir string
)

func init() {
Expand All @@ -43,6 +45,12 @@ func init() {
})
flags := systemResetCommand.Flags()
flags.BoolVarP(&forceFlag, "force", "f", false, "Do not prompt for confirmation")

flags.StringVar(&runDir, "run-root", "", "run root directory to use for storage")
_ = systemResetCommand.RegisterFlagCompletionFunc("run-root", completion.AutocompleteNone)

flags.StringVar(&graphDir, "graph-root", "", "graph root directory to use")
_ = systemResetCommand.RegisterFlagCompletionFunc("graph-root", completion.AutocompleteNone)
}

func reset(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -85,14 +93,22 @@ func reset(cmd *cobra.Command, args []string) {
registry.ContainerEngine().Shutdown(registry.Context())
registry.ImageEngine().Shutdown(registry.Context())

cfg := registry.PodmanConfig()
if len(runDir) > 0 {
cfg.Runroot = runDir
}
if len(graphDir) > 0 {
cfg.Engine.StaticDir = graphDir
}

engine, err := infra.NewSystemEngine(entities.ResetMode, registry.PodmanConfig())
if err != nil {
logrus.Error(err)
os.Exit(define.ExecErrorCodeGeneric)
}
defer engine.Shutdown(registry.Context())

if err := engine.Reset(registry.Context()); err != nil {
if err := engine.Reset(registry.Context(), (len(runDir) > 0 || len(graphDir) > 0)); err != nil {
logrus.Error(err)
os.Exit(define.ExecErrorCodeGeneric)
}
Expand Down
14 changes: 13 additions & 1 deletion docs/source/markdown/podman-system-reset.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ podman\-system\-reset - Reset storage back to initial state
**podman system reset** [*options*]

## DESCRIPTION
**podman system reset** removes all pods, containers, images, networks and volumes.
**podman system reset** removes all pods, containers, images and volumes. can also be used to instantiate new directories for storing images and information about Podmans's state.

This command must be run **before** changing any of the following fields in the
`containers.conf` or `storage.conf` files: `driver`, `static_dir`, `tmp_dir`
Expand All @@ -26,7 +26,19 @@ Do not prompt for confirmation

Print usage statement

#### **--run-root**

Establishes a new run root directory and storage.conf file once the system is reset

#### **--graph-root**

Establishes a new graph root directory and storage.conf file once the system is reset

## EXAMPLES
Reset the system and create a new storage.conf file with the given run and graph root.
```
$ podman system reset --run-root=/run/user/1000/containers --graph-root=/home/charliedoern/.local/share/containers
```

```
$ podman system reset
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/containers/image/v5 v5.19.1
github.com/containers/ocicrypt v1.1.2
github.com/containers/psgo v1.7.2
github.com/containers/storage v1.38.2
github.com/containers/storage v1.38.3-0.20220214143206-77cf15bc7897
github.com/coreos/go-systemd/v22 v22.3.2
github.com/coreos/stream-metadata-go v0.0.0-20210225230131-70edb9eb47b3
github.com/cyphar/filepath-securejoin v0.2.3
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ github.com/containers/psgo v1.7.2 h1:WbCvsY9w+nCv3j4der0mbD3PSRUv/W8l+G0YrZrdSDc
github.com/containers/psgo v1.7.2/go.mod h1:SLpqxsPOHtTqRygjutCPXmeU2PoEFzV3gzJplN4BMx0=
github.com/containers/storage v1.37.0/go.mod h1:kqeJeS0b7DO2ZT1nVWs0XufrmPFbgV3c+Q/45RlH6r4=
github.com/containers/storage v1.38.0/go.mod h1:lBzt28gAk5ADZuRtwdndRJyqX22vnRaXmlF+7ktfMYc=
github.com/containers/storage v1.38.2 h1:8bAIxnVBGKzMw5EWCivVj24bztQT6IkDp4uHiyhnzwE=
github.com/containers/storage v1.38.2/go.mod h1:INP0RPLHWBxx+pTsO5uiHlDUGHDFvWZPWprAbAlQWPQ=
github.com/containers/storage v1.38.3-0.20220214143206-77cf15bc7897 h1:3h1ecuofBHeIfmG12bB4wF4jqvkcqiooGOKG453onew=
github.com/containers/storage v1.38.3-0.20220214143206-77cf15bc7897/go.mod h1:ugJrnJvpb6LrUshIYF/9g9YP9D4VTCYP+wShSiuqxN4=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
Expand Down Expand Up @@ -935,8 +936,9 @@ github.com/moby/sys/mount v0.2.0 h1:WhCW5B355jtxndN5ovugJlMFJawbUODuW8fSnEH6SSM=
github.com/moby/sys/mount v0.2.0/go.mod h1:aAivFE2LB3W4bACsUXChRHQ0qKWsetY4Y9V7sxOougM=
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
github.com/moby/sys/mountinfo v0.4.1/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
github.com/moby/sys/mountinfo v0.5.0 h1:2Ks8/r6lopsxWi9m58nlwjaeSzUX9iiL1vj5qB/9ObI=
github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU=
github.com/moby/sys/mountinfo v0.6.0 h1:gUDhXQx58YNrpHlK4nSL+7y2pxFZkUcXqzFDKWdC0Oo=
github.com/moby/sys/mountinfo v0.6.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU=
github.com/moby/sys/symlink v0.1.0/go.mod h1:GGDODQmbFOjFsXvfLVn3+ZRxkch54RkSiGqsZeMYowQ=
github.com/moby/term v0.0.0-20200312100748-672ec06f55cd/go.mod h1:DdlQx2hp0Ss5/fLikoLlEeIYiATotOjgB//nb973jeo=
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 h1:dcztxKSvZ4Id8iPpHERQBbIJfabdt4wUm5qy3wOL2Zc=
Expand Down
19 changes: 18 additions & 1 deletion libpod/boltdb_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,29 @@ func (s *BoltState) ValidateDBConfig(runtime *Runtime) error {
}
defer s.deferredCloseDBCon(db)

//runRootName := "storage temporary directory (runroot)"
//runRootKey := []byte(runRootName)
// Check runtime configuration
if err := checkRuntimeConfig(db, runtime); err != nil {
/* if runtime.newDB {
err = db.Update(func(tx *bolt.Tx) error {
rb, err := getRuntimeConfigBucket(tx)
if err != nil {
return err
}
dbValue := []byte(runtime.storageConfig.RunRoot)
fmt.Println("here", runtime.storageConfig.RunRoot)
if err := rb.Put(runRootKey, dbValue); err != nil {
return errors.Wrapf(err, "error updating %s in DB runtime config", runRootName)
}
return nil
})
return err
}*/
return err
}

return nil
return err
}

// SetNamespace sets the namespace that will be used for container and pod
Expand Down
5 changes: 2 additions & 3 deletions libpod/boltdb_state_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ func checkRuntimeConfig(db *bolt.DB, rt *Runtime) error {

for _, check := range checks {
exists, err := readOnlyValidateConfig(configBkt, check)
if err != nil {
if err != nil && (err != define.ErrDBBadConfig && !rt.newDB) {
return err
}
if !exists {
if !exists || (err != nil && rt.newDB) {
missingFields = append(missingFields, check)
}
}
Expand All @@ -187,7 +187,6 @@ func checkRuntimeConfig(db *bolt.DB, rt *Runtime) error {
if missing.runtimeValue == "" && missing.defaultValue != "" {
dbValue = []byte(missing.defaultValue)
}

if err := configBkt.Put(missing.key, dbValue); err != nil {
return errors.Wrapf(err, "error updating %s in DB runtime config", missing.name)
}
Expand Down
10 changes: 10 additions & 0 deletions libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,16 @@ func WithRuntimeFlags(runtimeFlags []string) RuntimeOption {
}
}

func WithNewDB() RuntimeOption {
return func(rt *Runtime) error {
if rt.valid {
return define.ErrRuntimeFinalized
}
rt.newDB = true
return nil
}
}

// Container Creation Options

// WithMaxLogSize sets the maximum size of container logs.
Expand Down
23 changes: 20 additions & 3 deletions libpod/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
"github.com/containers/podman/v4/pkg/rootless"
"github.com/containers/podman/v4/pkg/util"
"github.com/containers/storage"
storageTypes "github.com/containers/storage/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Reset removes all storage
func (r *Runtime) Reset(ctx context.Context) error {
func (r *Runtime) Reset(ctx context.Context, updateConf bool) error {
var timeout *uint
pods, err := r.GetAllPods()
if err != nil {
Expand Down Expand Up @@ -139,9 +140,25 @@ func (r *Runtime) Reset(ctx context.Context) error {
}
}
if storageConfPath, err := storage.DefaultConfigFile(rootless.IsRootless()); err == nil {
if _, err = os.Stat(storageConfPath); err == nil {
if _, err := os.Stat(storageConfPath); err == nil {
fmt.Printf("A storage.conf file exists at %s\n", storageConfPath)
fmt.Println("You should remove this file if you did not modify the configuration.")
fmt.Println("you can remove it or override it with --run-root and --graph-root")
if updateConf { // only update the config if we have a storage.conf, we do not want to create a new one
store, err := storageTypes.StorageConfig(rootless.IsRootless())
if err != nil {
return err
}
store.Storage.RunRoot = r.store.RunRoot()
store.Storage.GraphRoot = r.store.GraphRoot()
store.Storage.Driver = r.store.GraphDriverName()

err = storageTypes.Save(*store, rootless.IsRootless())
if err != nil {
return err
}

fmt.Println("\nWrote new storage config to", storageConfPath)
}
}
} else {
if prevError != nil {
Expand Down
9 changes: 5 additions & 4 deletions libpod/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ type Runtime struct {
noStore bool
// secretsManager manages secrets
secretsManager *secrets.SecretsManager
// newDB indicates that we do not need to merge the db config, just create a new one
newDB bool
}

// SetXdgDirs ensures the XDG_RUNTIME_DIR env and XDG_CONFIG_HOME variables are set.
Expand Down Expand Up @@ -575,7 +577,6 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
return err
}
}

// If we need to refresh the state, do it now - things are guaranteed to
// be set up by now.
if doRefresh {
Expand All @@ -590,7 +591,6 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
return err2
}
}

// Mark the runtime as valid - ready to be used, cannot be modified
// further
runtime.valid = true
Expand All @@ -600,7 +600,6 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (retErr error) {
return err
}
}

return nil
}

Expand Down Expand Up @@ -953,8 +952,10 @@ func (r *Runtime) configureStore() error {
if err != nil {
return err
}

r.store = store
if store.RunRoot() != r.storageConfig.RunRoot && store.GraphRoot() == r.storageConfig.GraphRoot {
logrus.Debug("config reset due to graph root being the default value ")
}
is.Transport.SetStore(store)

// Set up a storage service for creating container root filesystems from
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/entities/engine_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import (
type SystemEngine interface {
Renumber(ctx context.Context, flags *pflag.FlagSet, config *PodmanConfig) error
Migrate(ctx context.Context, flags *pflag.FlagSet, config *PodmanConfig, options SystemMigrateOptions) error
Reset(ctx context.Context) error
Reset(ctx context.Context, updateConf bool) error
Shutdown(ctx context.Context)
}
4 changes: 2 additions & 2 deletions pkg/domain/infra/abi/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ func sizeOfPath(path string) (int64, error) {
return size, err
}

func (se *SystemEngine) Reset(ctx context.Context) error {
return se.Libpod.Reset(ctx)
func (se *SystemEngine) Reset(ctx context.Context, updateConf bool) error {
return se.Libpod.Reset(ctx, updateConf)
}

func (se *SystemEngine) Renumber(ctx context.Context, flags *pflag.FlagSet, config *entities.PodmanConfig) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/domain/infra/runtime_abi.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !remote
// +build !remote

package infra
Expand Down
10 changes: 8 additions & 2 deletions pkg/domain/infra/runtime_libpod.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !remote
// +build !remote

package infra
Expand Down Expand Up @@ -126,12 +127,17 @@ func getRuntime(ctx context.Context, fs *flag.FlagSet, opts *engineOpts) (*libpo
storageSet = true
}

if fs.Changed("root") {
gr, _ := fs.GetString("graph-root")
if fs.Changed("root") || len(gr) > 0 {
options = append(options, libpod.WithNewDB())
storageSet = true
storageOpts.GraphRoot = cfg.Engine.StaticDir
storageOpts.GraphDriverOptions = []string{}
}
if fs.Changed("runroot") {

rr, _ := fs.GetString("run-root")
if fs.Changed("runroot") || len(rr) > 0 {
options = append(options, libpod.WithNewDB())
storageSet = true
storageOpts.RunRoot = cfg.Runroot
}
Expand Down
37 changes: 34 additions & 3 deletions test/e2e/system_reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,41 @@ var _ = Describe("podman system reset", func() {
Expect(session).Should(Exit(0))
Expect(session.OutputToStringArray()).To(BeEmpty())

session = podmanTest.Podman([]string{"network", "ls", "-q"})
var f *os.File
f, err = os.Create(podmanTest.Root + "/storage.conf")
if err != nil {
os.Exit(1)
}
err = os.Setenv("CONTAINERS_STORAGE_CONF", podmanTest.Root+"/storage.conf")
if err != nil {
os.Exit(1)
}

err = os.Setenv("XDG_RUNTIME_DIR", podmanTest.TempDir)
if err != nil {
os.Exit(1)
}

_, err = f.WriteString("[storage]\ndriver = \"overlay\"\nrunroot = \"" + podmanTest.RunRoot + "\"\ngraphroot = \"" + podmanTest.Root + "\"")
Expect(err).Should(BeNil())

session = podmanTest.Podman([]string{"system", "reset", "-f", "--run-root", "foo", "--graph-root", "bar"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
// default network should exists
Expect(session.OutputToStringArray()).To(HaveLen(1))

content, err := os.ReadFile(f.Name())
Expect(err).Should(BeNil())
Expect(string(content[:])).Should(ContainSubstring("foo"))
Expect(string(content[:])).Should(ContainSubstring("bar"))

err = os.Unsetenv("CONTAINERS_STORAGE_CONF")
if err != nil {
os.Exit(1)
}
err = os.Remove(podmanTest.Root + "/storage.conf")
if err != nil {
os.Exit(1)
}

})
})
2 changes: 1 addition & 1 deletion vendor/github.com/containers/storage/VERSION

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/containers/storage/containers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ea8ea4b

Please sign in to comment.