podman

Форк
0
/
runtime_migrate_linux.go 
120 строк · 3.2 Кб
1
//go:build !remote
2

3
package libpod
4

5
import (
6
	"fmt"
7
	"os"
8
	"path/filepath"
9
	"strconv"
10
	"syscall"
11

12
	"github.com/containers/podman/v5/libpod/define"
13
	"github.com/containers/podman/v5/pkg/rootless"
14
	"github.com/containers/podman/v5/pkg/util"
15
	"github.com/sirupsen/logrus"
16
)
17

18
func (r *Runtime) stopPauseProcess() error {
19
	if rootless.IsRootless() {
20
		pausePidPath, err := util.GetRootlessPauseProcessPidPath()
21
		if err != nil {
22
			return fmt.Errorf("could not get pause process pid file path: %w", err)
23
		}
24
		data, err := os.ReadFile(pausePidPath)
25
		if err != nil {
26
			if os.IsNotExist(err) {
27
				return nil
28
			}
29
			return fmt.Errorf("cannot read pause process pid file: %w", err)
30
		}
31
		pausePid, err := strconv.Atoi(string(data))
32
		if err != nil {
33
			return fmt.Errorf("cannot parse pause pid file %s: %w", pausePidPath, err)
34
		}
35
		if err := os.Remove(pausePidPath); err != nil {
36
			return fmt.Errorf("cannot delete pause pid file %s: %w", pausePidPath, err)
37
		}
38
		if err := syscall.Kill(pausePid, syscall.SIGKILL); err != nil {
39
			return err
40
		}
41
	}
42
	return nil
43
}
44

45
// Migrate stops the rootless pause process and performs any necessary database
46
// migrations that are required. It can also migrate all containers to a new OCI
47
// runtime, if requested.
48
func (r *Runtime) Migrate(newRuntime string) error {
49
	// Acquire the alive lock and hold it.
50
	// Ensures that we don't let other Podman commands run while we are
51
	// rewriting things in the DB.
52
	aliveLock, err := r.getRuntimeAliveLock()
53
	if err != nil {
54
		return fmt.Errorf("retrieving alive lock: %w", err)
55
	}
56
	aliveLock.Lock()
57
	defer aliveLock.Unlock()
58

59
	if !r.valid {
60
		return define.ErrRuntimeStopped
61
	}
62

63
	runningContainers, err := r.GetRunningContainers()
64
	if err != nil {
65
		return err
66
	}
67

68
	allCtrs, err := r.state.AllContainers(false)
69
	if err != nil {
70
		return err
71
	}
72

73
	logrus.Infof("Stopping all containers")
74
	for _, ctr := range runningContainers {
75
		fmt.Printf("stopped %s\n", ctr.ID())
76
		if err := ctr.Stop(); err != nil {
77
			return fmt.Errorf("cannot stop container %s: %w", ctr.ID(), err)
78
		}
79
	}
80

81
	// Did the user request a new runtime?
82
	runtimeChangeRequested := newRuntime != ""
83
	var requestedRuntime OCIRuntime
84
	if runtimeChangeRequested {
85
		runtime, exists := r.ociRuntimes[newRuntime]
86
		if !exists {
87
			return fmt.Errorf("change to runtime %q requested but no such runtime is defined: %w", newRuntime, define.ErrInvalidArg)
88
		}
89
		requestedRuntime = runtime
90
	}
91

92
	for _, ctr := range allCtrs {
93
		needsWrite := false
94

95
		// Reset pause process location
96
		oldLocation := filepath.Join(ctr.state.RunDir, "conmon.pid")
97
		if ctr.config.ConmonPidFile == oldLocation {
98
			logrus.Infof("Changing conmon PID file for %s", ctr.ID())
99
			ctr.config.ConmonPidFile = filepath.Join(ctr.config.StaticDir, "conmon.pid")
100
			needsWrite = true
101
		}
102

103
		// Reset runtime
104
		if runtimeChangeRequested && ctr.config.OCIRuntime != newRuntime {
105
			logrus.Infof("Resetting container %s runtime to runtime %s", ctr.ID(), newRuntime)
106
			ctr.config.OCIRuntime = newRuntime
107
			ctr.ociRuntime = requestedRuntime
108

109
			needsWrite = true
110
		}
111

112
		if needsWrite {
113
			if err := r.state.RewriteContainerConfig(ctr, ctr.config); err != nil {
114
				return fmt.Errorf("rewriting config for container %s: %w", ctr.ID(), err)
115
			}
116
		}
117
	}
118

119
	return r.stopPauseProcess()
120
}
121

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.