|
3 | 3 | package libpod |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "errors" |
6 | 7 | "fmt" |
| 8 | + "os" |
7 | 9 | "path/filepath" |
8 | 10 | "strings" |
9 | 11 |
|
10 | 12 | "github.com/sirupsen/logrus" |
| 13 | + "go.podman.io/common/pkg/config" |
11 | 14 | "go.podman.io/podman/v6/libpod/define" |
12 | 15 | "go.podman.io/podman/v6/pkg/namespaces" |
| 16 | + "go.podman.io/storage/pkg/fileutils" |
13 | 17 | ) |
14 | 18 |
|
15 | 19 | // Migrate stops the rootless pause process and performs any necessary database |
16 | 20 | // migrations that are required. It can also migrate all containers to a new OCI |
17 | 21 | // runtime, if requested. |
18 | | -func (r *Runtime) Migrate(newRuntime string) error { |
| 22 | +func (r *Runtime) Migrate(newRuntime string, migrateDB bool) error { |
19 | 23 | // Acquire the alive lock and hold it. |
20 | 24 | // Ensures that we don't let other Podman commands run while we are |
21 | 25 | // rewriting things in the DB. |
@@ -97,5 +101,161 @@ func (r *Runtime) Migrate(newRuntime string) error { |
97 | 101 | } |
98 | 102 | } |
99 | 103 |
|
| 104 | + if migrateDB { |
| 105 | + if err := r.checkCanMigrate(); err != nil { |
| 106 | + switch { |
| 107 | + case errors.Is(err, errCannotMigrateNoBolt): |
| 108 | + fmt.Printf("No migration is necessary: %v", err) |
| 109 | + return r.stopPauseProcess() |
| 110 | + case errors.Is(err, errCannotMigrateHardcodedBolt): |
| 111 | + logrus.Errorf("In containers.conf, database_backend is manually set to \"boltdb\" - comment this line out and run `podman system migrate --migrate-db` or restart the system to complete migration to SQLite") |
| 112 | + return fmt.Errorf("unable to migrate to SQLite database as database backend manually set") |
| 113 | + default: |
| 114 | + return err |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if err := r.migrateDB(); err != nil { |
| 119 | + return fmt.Errorf("migrating database from BoltDB to SQLite: %w", err) |
| 120 | + } |
| 121 | + } |
| 122 | + |
100 | 123 | return r.stopPauseProcess() |
101 | 124 | } |
| 125 | + |
| 126 | +var ( |
| 127 | + errCannotMigrateNoBolt = errors.New("no BoltDB database to migrate") |
| 128 | + errCannotMigrateHardcodedBolt = errors.New("database_backend in containers.conf is manually set to \"boltdb\"") |
| 129 | +) |
| 130 | + |
| 131 | +func (r *Runtime) checkCanMigrate() error { |
| 132 | + boltPath := getBoltDBPath(r) |
| 133 | + if err := fileutils.Exists(boltPath); err != nil { |
| 134 | + return errCannotMigrateNoBolt |
| 135 | + } |
| 136 | + |
| 137 | + // Necessary as database configuration is overwritten when the state is set up. |
| 138 | + // So we need a completely new state from disk to see what the user set. |
| 139 | + newCfg, err := config.New(nil) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("reloading configuration to check database backend in use") |
| 142 | + } |
| 143 | + backend, err := config.ParseDBBackend(newCfg.Engine.DBBackend) |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("invalid DB backend configured - please change containers.conf database_backend to \"sqlite\"") |
| 146 | + } |
| 147 | + if backend == config.DBBackendBoltDB { |
| 148 | + return errCannotMigrateHardcodedBolt |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func (r *Runtime) migrateDB() error { |
| 155 | + boltPath := getBoltDBPath(r) |
| 156 | + // Get us a Bolt database |
| 157 | + oldState, err := NewBoltState(boltPath, r) |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("opening legacy Bolt database at %s: %w", boltPath, err) |
| 160 | + } |
| 161 | + |
| 162 | + // Migrate volumes, then pods, then containers. |
| 163 | + // Containers must be last as the pods they are part of and volumes they use must already exist. |
| 164 | + allVolumes, err := oldState.AllVolumes() |
| 165 | + if err != nil { |
| 166 | + return fmt.Errorf("retrieving volumes from boltdb: %w", err) |
| 167 | + } |
| 168 | + for _, vol := range allVolumes { |
| 169 | + if err := r.state.AddVolume(vol); err != nil { |
| 170 | + if errors.Is(err, define.ErrVolumeExists) { |
| 171 | + logrus.Warnf("Volume with name %s already exists in the SQLite database; refusing to migrate from BoltDB", vol.Name()) |
| 172 | + continue |
| 173 | + } |
| 174 | + return err |
| 175 | + } |
| 176 | + if err := oldState.UpdateVolume(vol); err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + if err := r.state.SaveVolume(vol); err != nil { |
| 180 | + return err |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + allPods, err := oldState.AllPods() |
| 185 | + if err != nil { |
| 186 | + return fmt.Errorf("retrieving pods from boltdb: %w", err) |
| 187 | + } |
| 188 | + for _, pod := range allPods { |
| 189 | + if err := r.state.AddPod(pod); err != nil { |
| 190 | + if errors.Is(err, define.ErrPodExists) { |
| 191 | + logrus.Warnf("Pod with name %s already exists in the SQLite database; refusing to migrate from BoltDB", pod.Name()) |
| 192 | + continue |
| 193 | + } |
| 194 | + return err |
| 195 | + } |
| 196 | + if err := oldState.UpdatePod(pod); err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + if err := r.state.SavePod(pod); err != nil { |
| 200 | + return err |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // Containers must be done as a graph due to dependencies. |
| 205 | + // The state will error if we add a container before its dependencies. |
| 206 | + allCtrs, err := oldState.AllContainers(true) |
| 207 | + if err != nil { |
| 208 | + return fmt.Errorf("retrieving containers from boltdb: %w", err) |
| 209 | + } |
| 210 | + |
| 211 | + // BoltDB doesn't actually populate container networks on initial pull |
| 212 | + // from the database, that needs to be done separately. |
| 213 | + for _, ctr := range allCtrs { |
| 214 | + ctrNetworks, err := oldState.GetNetworks(ctr) |
| 215 | + if err != nil { |
| 216 | + return err |
| 217 | + } |
| 218 | + ctr.config.Networks = convertLegacyNetworks(ctrNetworks) |
| 219 | + } |
| 220 | + |
| 221 | + graph, err := BuildContainerGraph(allCtrs) |
| 222 | + if err != nil { |
| 223 | + return err |
| 224 | + } |
| 225 | + |
| 226 | + ctrErrors := make(map[string]error) |
| 227 | + ctrsVisited := make(map[string]bool) |
| 228 | + |
| 229 | + for _, node := range graph.noDepNodes { |
| 230 | + migrateNodeDatabase(node, false, ctrErrors, ctrsVisited, r.state) |
| 231 | + } |
| 232 | + var ctrError error |
| 233 | + for id, err := range ctrErrors { |
| 234 | + if ctrError != nil { |
| 235 | + logrus.Errorf("Migrating containers to SQLite: %v", ctrError) |
| 236 | + } |
| 237 | + ctrError = fmt.Errorf("migrating container %s: %w", id, err) |
| 238 | + } |
| 239 | + if ctrError != nil { |
| 240 | + return ctrError |
| 241 | + } |
| 242 | + |
| 243 | + oldState.Close() |
| 244 | + |
| 245 | + // Move the Bolt database so it is not reused, but preserve so data is not lost. |
| 246 | + newBoltDBPath := fmt.Sprintf("%s-old", boltPath) |
| 247 | + if err := os.Rename(boltPath, newBoltDBPath); err != nil { |
| 248 | + return fmt.Errorf("renaming old database %s to %s: %w", boltPath, newBoltDBPath, err) |
| 249 | + } |
| 250 | + fmt.Printf("Old database has been renamed to %s and will no longer be used\n", newBoltDBPath) |
| 251 | + |
| 252 | + return nil |
| 253 | +} |
| 254 | + |
| 255 | +func getBoltDBPath(runtime *Runtime) string { |
| 256 | + baseDir := runtime.config.Engine.StaticDir |
| 257 | + if runtime.storageConfig.TransientStore { |
| 258 | + baseDir = runtime.config.Engine.TmpDir |
| 259 | + } |
| 260 | + return filepath.Join(baseDir, "bolt_state.db") |
| 261 | +} |
0 commit comments