github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/libpodruntime/runtime.go (about) 1 package libpodruntime 2 3 import ( 4 "context" 5 6 "github.com/containers/libpod/cmd/podman/cliconfig" 7 "github.com/containers/libpod/libpod" 8 "github.com/containers/libpod/pkg/cgroups" 9 "github.com/containers/libpod/pkg/namespaces" 10 "github.com/containers/libpod/pkg/rootless" 11 "github.com/containers/libpod/pkg/util" 12 "github.com/containers/storage" 13 "github.com/pkg/errors" 14 ) 15 16 type runtimeOptions struct { 17 name string 18 renumber bool 19 migrate bool 20 noStore bool 21 withFDS bool 22 } 23 24 // GetRuntimeMigrate gets a libpod runtime that will perform a migration of existing containers 25 func GetRuntimeMigrate(ctx context.Context, c *cliconfig.PodmanCommand, newRuntime string) (*libpod.Runtime, error) { 26 return getRuntime(ctx, c, &runtimeOptions{ 27 name: newRuntime, 28 renumber: false, 29 migrate: true, 30 noStore: false, 31 withFDS: true, 32 }) 33 } 34 35 // GetRuntimeDisableFDs gets a libpod runtime that will disable sd notify 36 func GetRuntimeDisableFDs(ctx context.Context, c *cliconfig.PodmanCommand) (*libpod.Runtime, error) { 37 return getRuntime(ctx, c, &runtimeOptions{ 38 renumber: false, 39 migrate: false, 40 noStore: false, 41 withFDS: false, 42 }) 43 } 44 45 // GetRuntimeRenumber gets a libpod runtime that will perform a lock renumber 46 func GetRuntimeRenumber(ctx context.Context, c *cliconfig.PodmanCommand) (*libpod.Runtime, error) { 47 return getRuntime(ctx, c, &runtimeOptions{ 48 renumber: true, 49 migrate: false, 50 noStore: false, 51 withFDS: true, 52 }) 53 } 54 55 // GetRuntime generates a new libpod runtime configured by command line options 56 func GetRuntime(ctx context.Context, c *cliconfig.PodmanCommand) (*libpod.Runtime, error) { 57 return getRuntime(ctx, c, &runtimeOptions{ 58 renumber: false, 59 migrate: false, 60 noStore: false, 61 withFDS: true, 62 }) 63 } 64 65 // GetRuntimeNoStore generates a new libpod runtime configured by command line options 66 func GetRuntimeNoStore(ctx context.Context, c *cliconfig.PodmanCommand) (*libpod.Runtime, error) { 67 return getRuntime(ctx, c, &runtimeOptions{ 68 renumber: false, 69 migrate: false, 70 noStore: true, 71 withFDS: true, 72 }) 73 } 74 75 func getRuntime(ctx context.Context, c *cliconfig.PodmanCommand, opts *runtimeOptions) (*libpod.Runtime, error) { 76 options := []libpod.RuntimeOption{} 77 storageOpts := storage.StoreOptions{} 78 storageSet := false 79 80 uidmapFlag := c.Flags().Lookup("uidmap") 81 gidmapFlag := c.Flags().Lookup("gidmap") 82 subuidname := c.Flags().Lookup("subuidname") 83 subgidname := c.Flags().Lookup("subgidname") 84 if (uidmapFlag != nil && gidmapFlag != nil && subuidname != nil && subgidname != nil) && 85 (uidmapFlag.Changed || gidmapFlag.Changed || subuidname.Changed || subgidname.Changed) { 86 userns, _ := c.Flags().GetString("userns") 87 uidmapVal, _ := c.Flags().GetStringSlice("uidmap") 88 gidmapVal, _ := c.Flags().GetStringSlice("gidmap") 89 subuidVal, _ := c.Flags().GetString("subuidname") 90 subgidVal, _ := c.Flags().GetString("subgidname") 91 mappings, err := util.ParseIDMapping(namespaces.UsernsMode(userns), uidmapVal, gidmapVal, subuidVal, subgidVal) 92 if err != nil { 93 return nil, err 94 } 95 storageOpts.UIDMap = mappings.UIDMap 96 storageOpts.GIDMap = mappings.GIDMap 97 98 storageSet = true 99 } 100 101 if c.Flags().Changed("root") { 102 storageSet = true 103 storageOpts.GraphRoot = c.GlobalFlags.Root 104 } 105 if c.Flags().Changed("runroot") { 106 storageSet = true 107 storageOpts.RunRoot = c.GlobalFlags.Runroot 108 } 109 if len(storageOpts.RunRoot) > 50 { 110 return nil, errors.New("the specified runroot is longer than 50 characters") 111 } 112 if c.Flags().Changed("storage-driver") { 113 storageSet = true 114 storageOpts.GraphDriverName = c.GlobalFlags.StorageDriver 115 // Overriding the default storage driver caused GraphDriverOptions from storage.conf to be ignored 116 storageOpts.GraphDriverOptions = []string{} 117 } 118 // This should always be checked after storage-driver is checked 119 if len(c.GlobalFlags.StorageOpts) > 0 { 120 storageSet = true 121 storageOpts.GraphDriverOptions = c.GlobalFlags.StorageOpts 122 } 123 if opts.migrate { 124 options = append(options, libpod.WithMigrate()) 125 if opts.name != "" { 126 options = append(options, libpod.WithMigrateRuntime(opts.name)) 127 } 128 } 129 130 if opts.renumber { 131 options = append(options, libpod.WithRenumber()) 132 } 133 134 // Only set this if the user changes storage config on the command line 135 if storageSet { 136 options = append(options, libpod.WithStorageConfig(storageOpts)) 137 } 138 139 if !storageSet && opts.noStore { 140 options = append(options, libpod.WithNoStore()) 141 } 142 // TODO CLI flags for image config? 143 // TODO CLI flag for signature policy? 144 145 if len(c.GlobalFlags.Namespace) > 0 { 146 options = append(options, libpod.WithNamespace(c.GlobalFlags.Namespace)) 147 } 148 149 if c.Flags().Changed("runtime") { 150 options = append(options, libpod.WithOCIRuntime(c.GlobalFlags.Runtime)) 151 } 152 153 if c.Flags().Changed("conmon") { 154 options = append(options, libpod.WithConmonPath(c.GlobalFlags.ConmonPath)) 155 } 156 if c.Flags().Changed("tmpdir") { 157 options = append(options, libpod.WithTmpDir(c.GlobalFlags.TmpDir)) 158 } 159 if c.Flags().Changed("network-cmd-path") { 160 options = append(options, libpod.WithNetworkCmdPath(c.GlobalFlags.NetworkCmdPath)) 161 } 162 163 if c.Flags().Changed("events-backend") { 164 options = append(options, libpod.WithEventsLogger(c.GlobalFlags.EventsBackend)) 165 } 166 167 if c.Flags().Changed("cgroup-manager") { 168 options = append(options, libpod.WithCgroupManager(c.GlobalFlags.CGroupManager)) 169 } else { 170 unified, err := cgroups.IsCgroup2UnifiedMode() 171 if err != nil { 172 return nil, err 173 } 174 if rootless.IsRootless() && !unified { 175 options = append(options, libpod.WithCgroupManager("cgroupfs")) 176 } 177 } 178 179 // TODO flag to set libpod static dir? 180 // TODO flag to set libpod tmp dir? 181 182 if c.Flags().Changed("cni-config-dir") { 183 options = append(options, libpod.WithCNIConfigDir(c.GlobalFlags.CniConfigDir)) 184 } 185 if c.Flags().Changed("default-mounts-file") { 186 options = append(options, libpod.WithDefaultMountsFile(c.GlobalFlags.DefaultMountsFile)) 187 } 188 if c.Flags().Changed("hooks-dir") { 189 options = append(options, libpod.WithHooksDir(c.GlobalFlags.HooksDir...)) 190 } 191 192 // TODO flag to set CNI plugins dir? 193 194 // TODO I don't think these belong here? 195 // Will follow up with a different PR to address 196 // 197 // Pod create options 198 199 infraImageFlag := c.Flags().Lookup("infra-image") 200 if infraImageFlag != nil && infraImageFlag.Changed { 201 infraImage, _ := c.Flags().GetString("infra-image") 202 options = append(options, libpod.WithDefaultInfraImage(infraImage)) 203 } 204 205 infraCommandFlag := c.Flags().Lookup("infra-command") 206 if infraCommandFlag != nil && infraImageFlag.Changed { 207 infraCommand, _ := c.Flags().GetString("infra-command") 208 options = append(options, libpod.WithDefaultInfraCommand(infraCommand)) 209 } 210 211 if !opts.withFDS { 212 options = append(options, libpod.WithEnableSDNotify()) 213 } 214 215 return libpod.NewRuntime(ctx, options...) 216 }