forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulation.go
186 lines (151 loc) · 5.18 KB
/
emulation.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package systemd
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/squashfs"
)
type emulation struct {
rootDir string
}
var errNotImplemented = errors.New("not implemented in emulation mode")
func (s *emulation) DaemonReload() error {
return errNotImplemented
}
func (s *emulation) DaemonReexec() error {
return errNotImplemented
}
func (s *emulation) Enable(service string) error {
_, err := systemctlCmd("--root", s.rootDir, "enable", service)
return err
}
func (s *emulation) Disable(service string) error {
_, err := systemctlCmd("--root", s.rootDir, "disable", service)
return err
}
func (s *emulation) Start(service ...string) error {
return errNotImplemented
}
func (s *emulation) StartNoBlock(service ...string) error {
return errNotImplemented
}
func (s *emulation) Stop(service string, timeout time.Duration) error {
return errNotImplemented
}
func (s *emulation) Kill(service, signal, who string) error {
return errNotImplemented
}
func (s *emulation) Restart(service string, timeout time.Duration) error {
return errNotImplemented
}
func (s *emulation) ReloadOrRestart(service string) error {
return errNotImplemented
}
func (s *emulation) RestartAll(service string) error {
return errNotImplemented
}
func (s *emulation) Status(units ...string) ([]*UnitStatus, error) {
return nil, errNotImplemented
}
func (s *emulation) IsEnabled(service string) (bool, error) {
return false, errNotImplemented
}
func (s *emulation) IsActive(service string) (bool, error) {
return false, errNotImplemented
}
func (s *emulation) LogReader(services []string, n int, follow bool) (io.ReadCloser, error) {
return nil, errNotImplemented
}
func (s *emulation) AddMountUnitFile(snapName, revision, what, where, fstype string) (string, error) {
if osutil.IsDirectory(what) {
return "", fmt.Errorf("bind-mounted directory is not supported in emulation mode")
}
// In emulation mode hostFsType is the fs we want to use to manually mount
// the snap below, but fstype is used for the created mount unit.
// This means that when preseeding in a lxd container, the snap will be
// mounted with fuse, but mount unit will use squashfs.
mountUnitOptions := append(fsMountOptions(fstype), squashfs.StandardOptions()...)
mountUnitName, err := writeMountUnitFile(snapName, revision, what, where, fstype, mountUnitOptions)
if err != nil {
return "", err
}
hostFsType, actualOptions := hostFsTypeAndMountOptions(fstype)
cmd := exec.Command("mount", "-t", hostFsType, what, where, "-o", strings.Join(actualOptions, ","))
if out, err := cmd.CombinedOutput(); err != nil {
return "", fmt.Errorf("cannot mount %s (%s) at %s in preseed mode: %s; %s", what, hostFsType, where, err, string(out))
}
multiUserTargetWantsDir := filepath.Join(dirs.SnapServicesDir, "multi-user.target.wants")
if err := os.MkdirAll(multiUserTargetWantsDir, 0755); err != nil {
return "", err
}
// cannot call systemd, so manually enable the unit by symlinking into multi-user.target.wants
mu := MountUnitPath(where)
enableUnitPath := filepath.Join(multiUserTargetWantsDir, mountUnitName)
if err := os.Symlink(mu, enableUnitPath); err != nil {
return "", fmt.Errorf("cannot enable mount unit %s: %v", mountUnitName, err)
}
return mountUnitName, nil
}
func (s *emulation) RemoveMountUnitFile(mountedDir string) error {
unit := MountUnitPath(dirs.StripRootDir(mountedDir))
if !osutil.FileExists(unit) {
return nil
}
isMounted, err := osutilIsMounted(mountedDir)
if err != nil {
return err
}
if isMounted {
// use detach-loop and lazy unmount
if output, err := exec.Command("umount", "-d", "-l", mountedDir).CombinedOutput(); err != nil {
return osutil.OutputErr(output, err)
}
}
multiUserTargetWantsDir := filepath.Join(dirs.SnapServicesDir, "multi-user.target.wants")
enableUnitPathSymlink := filepath.Join(multiUserTargetWantsDir, filepath.Base(unit))
if err := os.Remove(enableUnitPathSymlink); err != nil {
return err
}
if err := os.Remove(unit); err != nil {
return err
}
return nil
}
func (s *emulation) Mask(service string) error {
_, err := systemctlCmd("--root", s.rootDir, "mask", service)
return err
}
func (s *emulation) Unmask(service string) error {
_, err := systemctlCmd("--root", s.rootDir, "unmask", service)
return err
}
func (s *emulation) Mount(what, where string, options ...string) error {
return errNotImplemented
}
func (s *emulation) Umount(whatOrWhere string) error {
return errNotImplemented
}