podman

Форк
0
105 строк · 2.3 Кб
1
//go:build windows
2

3
package main
4

5
import (
6
	"fmt"
7
	"os"
8
	"path"
9
	"syscall"
10
	"time"
11
	"unsafe"
12

13
	"github.com/containers/podman/v5/pkg/machine/wsl/wutil"
14
	"github.com/sirupsen/logrus"
15
	"golang.org/x/sys/windows/svc/eventlog"
16
)
17

18
const (
19
	//nolint:stylecheck
20
	MB_ICONWARNING = 0x00000030
21
	//nolint:stylecheck
22
	MB_OK = 0x00000000
23
	//nolint:stylecheck
24
	MB_DEFBUTTON1 = 0x00000000
25
)
26

27
const KernelWarning = "WSL Kernel installation did not complete successfully. " +
28
	"Podman machine will attempt to install this at a later time. " +
29
	"You can also manually complete the installation using the " +
30
	"\"wsl --update\" command."
31

32
func setupLogging(name string) (*eventlog.Log, error) {
33
	// Reuse the Built-in .NET Runtime Source so that we do not
34
	// have to provide a message table and modify the system
35
	// event configuration
36
	log, err := eventlog.Open(".NET Runtime")
37
	if err != nil {
38
		return nil, err
39
	}
40

41
	logrus.AddHook(NewEventHook(log, name))
42
	logrus.SetLevel(logrus.InfoLevel)
43

44
	return log, nil
45
}
46

47
func installWslKernel() error {
48
	logrus.Info("Installing WSL Kernel update")
49
	var (
50
		err error
51
	)
52
	backoff := 500 * time.Millisecond
53
	for i := 1; i < 6; i++ {
54
		err = wutil.SilentExec(wutil.FindWSL(), "--update")
55
		if err == nil {
56
			break
57
		}
58

59
		// In case of unusual circumstances (e.g. race with installer actions)
60
		// retry a few times
61
		logrus.Warn("An error occurred attempting the WSL Kernel update, retrying...")
62
		time.Sleep(backoff)
63
		backoff *= 2
64
	}
65

66
	if err != nil {
67
		err = fmt.Errorf("could not install WSL Kernel: %w", err)
68
	}
69

70
	return err
71
}
72

73
// Creates an "warn" style pop-up window
74
func warn(title string, caption string) int {
75
	format := MB_ICONWARNING | MB_OK | MB_DEFBUTTON1
76

77
	user32 := syscall.NewLazyDLL("user32.dll")
78
	captionPtr, _ := syscall.UTF16PtrFromString(caption)
79
	titlePtr, _ := syscall.UTF16PtrFromString(title)
80
	ret, _, _ := user32.NewProc("MessageBoxW").Call(
81
		uintptr(0),
82
		uintptr(unsafe.Pointer(captionPtr)),
83
		uintptr(unsafe.Pointer(titlePtr)),
84
		uintptr(format))
85

86
	return int(ret)
87
}
88

89
func main() {
90
	args := os.Args
91
	_, _ = setupLogging(path.Base(args[0]))
92
	if wutil.IsWSLInstalled() {
93
		// nothing to do
94
		logrus.Info("WSL Kernel already installed")
95
		return
96
	}
97

98
	result := installWslKernel()
99
	if result != nil {
100
		logrus.Error(result.Error())
101
		_ = warn("Podman Setup", KernelWarning)
102
	}
103

104
	logrus.Info("WSL Kernel update successful")
105
}
106

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

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

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

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