summaryrefslogtreecommitdiff
path: root/pkg/machine/vmconfigs/machine.go
blob: bbd0f6cc9be40e03f11222a562629ea889af15b6 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package vmconfigs

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/containers/common/pkg/strongunits"
	define2 "github.com/containers/podman/v5/libpod/define"
	"github.com/containers/podman/v5/pkg/errorhandling"
	"github.com/containers/podman/v5/pkg/machine/connection"
	"github.com/containers/podman/v5/pkg/machine/define"
	"github.com/containers/podman/v5/pkg/machine/env"
	"github.com/containers/podman/v5/pkg/machine/lock"
	"github.com/containers/podman/v5/pkg/machine/ports"
	"github.com/containers/storage/pkg/fileutils"
	"github.com/containers/storage/pkg/ioutils"
	"github.com/containers/storage/pkg/lockfile"
	"github.com/sirupsen/logrus"
)

/*
  info        Display machine host info					common
  init        Initialize a virtual machine				specific
  inspect     Inspect an existing machine				specific
  list        List machines								specific
  os          Manage a Podman virtual machine's OS		common
  rm          Remove an existing machine				specific
  set         Set a virtual machine setting				specific
  ssh         SSH into an existing machine				common
  start       Start an existing machine					specific
  stop        Stop an existing machine					specific
*/

var (
	SSHRemoteConnection     RemoteConnectionType = "ssh"
	DefaultIgnitionUserName                      = "core"
	ForwarderBinaryName                          = "gvproxy"
)

type RemoteConnectionType string

// NewMachineConfig creates the initial machine configuration file from cli options.
func NewMachineConfig(opts define.InitOptions, dirs *define.MachineDirs, sshIdentityPath string, vmtype define.VMType, machineLock *lockfile.LockFile) (*MachineConfig, error) {
	mc := new(MachineConfig)
	mc.Name = opts.Name
	mc.dirs = dirs
	mc.lock = machineLock

	// Assign Dirs
	cf, err := define.NewMachineFile(filepath.Join(dirs.ConfigDir.GetPath(), fmt.Sprintf("%s.json", opts.Name)), nil)
	if err != nil {
		return nil, err
	}
	mc.configPath = cf
	// Given that we are locked now and check again that the config file does not exists,
	// if it does it means the VM was already created and we should error.
	if err := fileutils.Exists(cf.Path); err == nil {
		return nil, fmt.Errorf("%s: %w", opts.Name, define.ErrVMAlreadyExists)
	}

	if vmtype != define.QemuVirt && len(opts.USBs) > 0 {
		return nil, fmt.Errorf("USB host passthrough not supported for %s machines", vmtype)
	}

	usbs, err := define.ParseUSBs(opts.USBs)
	if err != nil {
		return nil, err
	}

	// System Resources
	mrc := ResourceConfig{
		CPUs:     opts.CPUS,
		DiskSize: strongunits.GiB(opts.DiskSize),
		Memory:   strongunits.MiB(opts.Memory),
		USBs:     usbs,
	}
	mc.Resources = mrc

	sshPort, err := ports.AllocateMachinePort()
	if err != nil {
		return nil, err
	}

	sshConfig := SSHConfig{
		IdentityPath:   sshIdentityPath,
		Port:           sshPort,
		RemoteUsername: opts.Username,
	}

	mc.SSH = sshConfig
	mc.Created = time.Now()

	mc.HostUser = HostUser{UID: getHostUID(), Rootful: opts.Rootful}

	return mc, nil
}

// Lock creates a lock on the machine for single access
func (mc *MachineConfig) Lock() {
	mc.lock.Lock()
}

// Unlock removes an existing lock
func (mc *MachineConfig) Unlock() {
	mc.lock.Unlock()
}

// Refresh reloads the config file from disk
func (mc *MachineConfig) Refresh() error {
	content, err := os.ReadFile(mc.configPath.GetPath())
	if err != nil {
		return err
	}
	return json.Unmarshal(content, mc)
}

// write is a non-locking way to write the machine configuration file to disk
func (mc *MachineConfig) Write() error {
	if mc.configPath == nil {
		return fmt.Errorf("no configuration file associated with vm %q", mc.Name)
	}
	b, err := json.Marshal(mc)
	if err != nil {
		return err
	}
	logrus.Debugf("writing configuration file %q", mc.configPath.Path)
	return ioutils.AtomicWriteFile(mc.configPath.GetPath(), b, define.DefaultFilePerm)
}

func (mc *MachineConfig) SetRootful(rootful bool) error {
	if err := connection.UpdateConnectionIfDefault(rootful, mc.Name, mc.Name+"-root"); err != nil {
		return err
	}
	mc.HostUser.Rootful = rootful
	mc.HostUser.Modified = true
	return nil
}

func (mc *MachineConfig) removeSystemConnection() error { //nolint:unused
	return define2.ErrNotImplemented
}

// updateLastBoot writes the current time to the machine configuration file. it is
// an non-locking method and assumes it is being called locked
func (mc *MachineConfig) updateLastBoot() error { //nolint:unused
	mc.LastUp = time.Now()
	return mc.Write()
}

func (mc *MachineConfig) Remove(saveIgnition, saveImage bool) ([]string, func() error, error) {
	ignitionFile, err := mc.IgnitionFile()
	if err != nil {
		return nil, nil, err
	}

	readySocket, err := mc.ReadySocket()
	if err != nil {
		return nil, nil, err
	}

	gvProxySocket, err := mc.GVProxySocket()
	if err != nil {
		return nil, nil, err
	}

	apiSocket, err := mc.APISocket()
	if err != nil {
		return nil, nil, err
	}

	logPath, err := mc.LogFile()
	if err != nil {
		return nil, nil, err
	}

	rmFiles := []string{
		mc.configPath.GetPath(),
		readySocket.GetPath(),
		gvProxySocket.GetPath(),
		apiSocket.GetPath(),
		logPath.GetPath(),
	}
	if !saveImage {
		mc.ImagePath.GetPath()
	}
	if !saveIgnition {
		ignitionFile.GetPath()
	}

	mcRemove := func() error {
		var errs []error
		if err := connection.RemoveConnections(mc.Name, mc.Name+"-root"); err != nil {
			errs = append(errs, err)
		}

		if !saveIgnition {
			if err := ignitionFile.Delete(); err != nil {
				errs = append(errs, err)
			}
		}
		if !saveImage {
			if err := mc.ImagePath.Delete(); err != nil {
				errs = append(errs, err)
			}
		}
		if err := readySocket.Delete(); err != nil {
			errs = append(errs, err)
		}
		if err := gvProxySocket.Delete(); err != nil {
			errs = append(errs, err)
		}
		if err := apiSocket.Delete(); err != nil {
			errs = append(errs, err)
		}
		if err := logPath.Delete(); err != nil {
			errs = append(errs, err)
		}

		if err := mc.configPath.Delete(); err != nil {
			errs = append(errs, err)
		}

		if err := ports.ReleaseMachinePort(mc.SSH.Port); err != nil {
			errs = append(errs, err)
		}

		return errorhandling.JoinErrors(errs)
	}

	return rmFiles, mcRemove, nil
}

// ConfigDir is a simple helper to obtain the machine config dir
func (mc *MachineConfig) ConfigDir() (*define.VMFile, error) {
	if mc.dirs == nil || mc.dirs.ConfigDir == nil {
		return nil, errors.New("no configuration directory set")
	}
	return mc.dirs.ConfigDir, nil
}

// DataDir is a simple helper function to obtain the machine data dir
func (mc *MachineConfig) DataDir() (*define.VMFile, error) {
	if mc.dirs == nil || mc.dirs.DataDir == nil {
		return nil, errors.New("no data directory set")
	}
	return mc.dirs.DataDir, nil
}

// RuntimeDir is simple helper function to obtain the runtime dir
func (mc *MachineConfig) RuntimeDir() (*define.VMFile, error) {
	if mc.dirs == nil || mc.dirs.RuntimeDir == nil {
		return nil, errors.New("no runtime directory set")
	}
	return mc.dirs.RuntimeDir, nil
}

func (mc *MachineConfig) SetDirs(dirs *define.MachineDirs) {
	mc.dirs = dirs
}

func (mc *MachineConfig) IgnitionFile() (*define.VMFile, error) {
	configDir, err := mc.ConfigDir()
	if err != nil {
		return nil, err
	}
	return configDir.AppendToNewVMFile(mc.Name+".ign", nil)
}

func (mc *MachineConfig) ReadySocket() (*define.VMFile, error) {
	rtDir, err := mc.RuntimeDir()
	if err != nil {
		return nil, err
	}
	return readySocket(mc.Name, rtDir)
}

func (mc *MachineConfig) GVProxySocket() (*define.VMFile, error) {
	machineRuntimeDir, err := mc.RuntimeDir()
	if err != nil {
		return nil, err
	}
	return gvProxySocket(mc.Name, machineRuntimeDir)
}

func (mc *MachineConfig) APISocket() (*define.VMFile, error) {
	machineRuntimeDir, err := mc.RuntimeDir()
	if err != nil {
		return nil, err
	}
	return apiSocket(mc.Name, machineRuntimeDir)
}

func (mc *MachineConfig) LogFile() (*define.VMFile, error) {
	rtDir, err := mc.RuntimeDir()
	if err != nil {
		return nil, err
	}
	return rtDir.AppendToNewVMFile(mc.Name+".log", nil)
}

func (mc *MachineConfig) Kind() (define.VMType, error) {
	// Not super in love with this approach
	if mc.QEMUHypervisor != nil {
		return define.QemuVirt, nil
	}
	if mc.AppleHypervisor != nil {
		return define.AppleHvVirt, nil
	}
	if mc.HyperVHypervisor != nil {
		return define.HyperVVirt, nil
	}
	if mc.WSLHypervisor != nil {
		return define.WSLVirt, nil
	}

	return define.UnknownVirt, nil
}

func (mc *MachineConfig) IsFirstBoot() (bool, error) {
	never, err := time.Parse(time.RFC3339, "0001-01-01T00:00:00Z")
	if err != nil {
		return false, err
	}
	return mc.LastUp == never, nil
}

func (mc *MachineConfig) ConnectionInfo(vmtype define.VMType) (*define.VMFile, *define.VMFile, error) {
	var (
		socket *define.VMFile
		pipe   *define.VMFile
	)

	if vmtype == define.HyperVVirt || vmtype == define.WSLVirt {
		pipeName := env.WithPodmanPrefix(mc.Name)
		pipe = &define.VMFile{Path: `\\.\pipe\` + pipeName}
		return nil, pipe, nil
	}

	socket, err := mc.APISocket()
	return socket, nil, err
}

// LoadMachineByName returns a machine config based on the vm name and provider
func LoadMachineByName(name string, dirs *define.MachineDirs) (*MachineConfig, error) {
	fullPath, err := dirs.ConfigDir.AppendToNewVMFile(name+".json", nil)
	if err != nil {
		return nil, err
	}
	mc, err := loadMachineFromFQPath(fullPath)
	if err != nil {
		if errors.Is(err, fs.ErrNotExist) {
			return nil, &define.ErrVMDoesNotExist{Name: name}
		}
		return nil, err
	}
	mc.dirs = dirs
	mc.configPath = fullPath

	// If we find an incompatible configuration, we return a hard
	// error because the user wants to deal directly with this
	// machine
	if mc.Version == 0 {
		return mc, &define.ErrIncompatibleMachineConfig{
			Name: name,
			Path: fullPath.GetPath(),
		}
	}
	return mc, nil
}

// loadMachineFromFQPath stub function for loading a JSON configuration file and returning
// a machineconfig.  this should only be called if you know what you are doing.
func loadMachineFromFQPath(path *define.VMFile) (*MachineConfig, error) {
	mc := new(MachineConfig)
	b, err := path.Read()
	if err != nil {
		return nil, err
	}

	if err = json.Unmarshal(b, mc); err != nil {
		return nil, fmt.Errorf("unable to load machine config file: %q", err)
	}
	lock, err := lock.GetMachineLock(mc.Name, filepath.Dir(path.GetPath()))
	mc.lock = lock
	return mc, err
}

// LoadMachinesInDir returns all the machineconfigs located in given dir
func LoadMachinesInDir(dirs *define.MachineDirs) (map[string]*MachineConfig, error) {
	mcs := make(map[string]*MachineConfig)
	if err := filepath.WalkDir(dirs.ConfigDir.GetPath(), func(path string, d fs.DirEntry, err error) error {
		if strings.HasSuffix(d.Name(), ".json") {
			fullPath, err := dirs.ConfigDir.AppendToNewVMFile(d.Name(), nil)
			if err != nil {
				return err
			}
			mc, err := loadMachineFromFQPath(fullPath)
			if err != nil {
				return err
			}
			// if we find an incompatible machine configuration file, we emit and error
			//
			if mc.Version == 0 {
				tmpErr := &define.ErrIncompatibleMachineConfig{
					Name: mc.Name,
					Path: fullPath.GetPath(),
				}
				logrus.Error(tmpErr)
				return nil
			}
			mc.configPath = fullPath
			mc.dirs = dirs
			mcs[mc.Name] = mc
		}
		return nil
	}); err != nil {
		return nil, err
	}
	return mcs, nil
}