File: fs.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (107 lines) | stat: -rw-r--r-- 3,104 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//go:build linux && cgo && !agent

package sys

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
)

// LocalDatabasePath returns the path of the local database file.
func (s *OS) LocalDatabasePath() string {
	return filepath.Join(s.VarDir, "database", "local.db")
}

// GlobalDatabaseDir returns the path of the global database directory.
func (s *OS) GlobalDatabaseDir() string {
	return filepath.Join(s.VarDir, "database", "global")
}

// GlobalDatabasePath returns the path of the global database SQLite file
// managed by dqlite.
func (s *OS) GlobalDatabasePath() string {
	return filepath.Join(s.GlobalDatabaseDir(), "db.bin")
}

// initDirs Make sure all our directories are available.
func (s *OS) initDirs() error {
	dirs := []struct {
		path string
		mode os.FileMode
	}{
		{s.VarDir, 0o711},

		// Instances are 0711 so the runtime can traverse to the data.
		{filepath.Join(s.VarDir, "containers"), 0o711},
		{filepath.Join(s.VarDir, "virtual-machines"), 0o711},

		// Snapshots are kept 0700 as the runtime doesn't need access.
		{filepath.Join(s.VarDir, "containers-snapshots"), 0o700},
		{filepath.Join(s.VarDir, "virtual-machines-snapshots"), 0o700},

		{filepath.Join(s.VarDir, "backups"), 0o700},
		{s.CacheDir, 0o700},
		{filepath.Join(s.CacheDir, "resources"), 0o700},
		{filepath.Join(s.VarDir, "database"), 0o700},
		{filepath.Join(s.VarDir, "devices"), 0o711},
		{filepath.Join(s.VarDir, "disks"), 0o700},
		{filepath.Join(s.VarDir, "guestapi"), 0o755},
		{filepath.Join(s.VarDir, "images"), 0o700},
		{s.LogDir, 0o700},
		{filepath.Join(s.VarDir, "networks"), 0o711},
		{s.RunDir, 0o711},
		{filepath.Join(s.VarDir, "security"), 0o700},
		{filepath.Join(s.VarDir, "security", "apparmor"), 0o700},
		{filepath.Join(s.VarDir, "security", "apparmor", "cache"), 0o700},
		{filepath.Join(s.VarDir, "security", "apparmor", "profiles"), 0o700},
		{filepath.Join(s.VarDir, "security", "seccomp"), 0o700},
		{filepath.Join(s.VarDir, "shmounts"), 0o711},
		{filepath.Join(s.VarDir, "storage-pools"), 0o711},
	}

	for _, dir := range dirs {
		err := os.Mkdir(dir.path, dir.mode)
		if err != nil {
			if !os.IsExist(err) {
				return fmt.Errorf("Failed to init dir %q: %w", dir.path, err)
			}

			err = os.Chmod(dir.path, dir.mode)
			if err != nil && !errors.Is(err, fs.ErrNotExist) {
				return fmt.Errorf("Failed to chmod dir %q: %w", dir.path, err)
			}
		}
	}

	return nil
}

// initStorageDirs make sure all our directories are on the storage layer (after storage is mounted).
func (s *OS) initStorageDirs() error {
	dirs := []struct {
		path string
		mode os.FileMode
	}{
		{filepath.Join(s.VarDir, "backups", "custom"), 0o700},
		{filepath.Join(s.VarDir, "backups", "instances"), 0o700},
	}

	for _, dir := range dirs {
		err := os.Mkdir(dir.path, dir.mode)
		if err != nil {
			if !os.IsExist(err) {
				return fmt.Errorf("Failed to init storage dir %q: %w", dir.path, err)
			}

			err = os.Chmod(dir.path, dir.mode)
			if err != nil && !errors.Is(err, fs.ErrNotExist) {
				return fmt.Errorf("Failed to chmod storage dir %q: %w", dir.path, err)
			}
		}
	}

	return nil
}