Skip to content

Commit 6a1f8ce

Browse files
committed
Make relative paths in a VM config relative to the run_dir, split out setting defaults
1 parent 520ca5e commit 6a1f8ce

File tree

4 files changed

+104
-24
lines changed

4 files changed

+104
-24
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Configuring Network: "net0"
55
cmd: ifconfig bridge1 192.168.99.1 netmask 0xffffff00
66
cmd: ifconfig bridge1 192.168.99.1 netmask 0xffffff00
77

8-
- Make relative paths in a VM config relative to the run_dir
98
- Add dependency graph ordering
109
- Debug logging
1110
- Remove all fmt.Printf and use log or similar
@@ -57,3 +56,4 @@ cmd: ifconfig bridge1 192.168.99.1 netmask 0xffffff00
5756
- Check that kernel and vmlinux exist in Up() of boot.kexec
5857
- If one of the tap interfaces doesn't come up, still add the ones that do come up to the bridge
5958
- Make relative paths be relative to the config file.
59+
- Make relative paths in a VM config relative to the run_dir

internal/config/config.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package config
22

3+
import (
4+
"path/filepath"
5+
)
6+
7+
// Config represents a hkmgr.toml config file
38
type Config struct {
49
Network Network `toml:"network"`
510
VM VM `toml:"vm"`
@@ -9,7 +14,18 @@ type Config struct {
914
// UpdateRelativePaths finds relative paths in the config and turns them into
1015
// fully qualified paths based on the config file path.
1116
func (c *Config) UpdateRelativePaths() {
17+
configDir := filepath.Dir(c.Path)
18+
19+
for name := range c.VM {
20+
c.VM[name].updateRelativePaths(configDir, name)
21+
}
22+
}
23+
24+
// Defaults sets default values for unset variables in the config.
25+
func (c *Config) Defaults() {
26+
configDir := filepath.Dir(c.Path)
27+
1228
for name := range c.VM {
13-
c.VM[name].updateRelativePaths(c.Path, name)
29+
c.VM[name].defaults(configDir, name)
1430
}
1531
}

internal/config/vm.go

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,20 @@ var hyperkitPath = "hyperkit"
2020
type VM map[string]*VMConfig
2121

2222
type VMConfig struct {
23-
Memory string `toml:"memory"`
24-
Cores int `toml:"cores"`
25-
UUID string `toml:"uuid"`
26-
SSHKey string `toml:"ssh_key"`
27-
ProvisionPre string `toml:"provision_pre"`
28-
ProvisionPost string `toml:"provision_post"`
29-
Before []string `toml:"before"`
30-
After []string `toml:"after"`
31-
Requires []string `toml:"requires"`
32-
RunDir string `toml:"run_dir"`
33-
Network []NetConf `toml:"network"`
34-
Boot Boot `toml:"boot"`
35-
HDD []HDD `toml:"hdd"`
36-
CDROM []CDROM `toml:"cdrom"`
23+
Memory string `toml:"memory"`
24+
Cores int `toml:"cores"`
25+
UUID string `toml:"uuid"`
26+
SSHKey string `toml:"ssh_key"`
27+
ProvisionPre string `toml:"provision_pre"`
28+
ProvisionPost string `toml:"provision_post"`
29+
Before []string `toml:"before"`
30+
After []string `toml:"after"`
31+
Requires []string `toml:"requires"`
32+
RunDir string `toml:"run_dir"`
33+
Network []*NetConf `toml:"network"`
34+
Boot Boot `toml:"boot"`
35+
HDD []*HDD `toml:"hdd"`
36+
CDROM []*CDROM `toml:"cdrom"`
3737
}
3838

3939
// Status is the status of a VM process
@@ -242,7 +242,7 @@ func (v *VMConfig) Validate() error {
242242
return errors.New("RunDir not specified")
243243
}
244244

245-
if err := v.Boot.Validate(); err != nil {
245+
if err := v.Boot.validate(); err != nil {
246246
return err
247247
}
248248

@@ -260,13 +260,24 @@ func (v *VMConfig) Validate() error {
260260
return nil
261261
}
262262

263-
func (v *VMConfig) updateRelativePaths(configPath string, name string) {
264-
configDir := filepath.Dir(configPath)
263+
func (v *VMConfig) defaults(configDir string, name string) {
265264
if v.RunDir == "" {
266265
v.RunDir = filepath.Join(configDir, ".run/vm/", name)
267-
} else if v.RunDir[:0] != "/" {
266+
}
267+
}
268+
269+
func (v *VMConfig) updateRelativePaths(configDir string, name string) {
270+
if v.RunDir[:1] != "/" {
268271
v.RunDir = filepath.Join(configDir, v.RunDir)
269272
}
273+
274+
v.Boot.updateRelativePaths(configDir)
275+
for i := range v.HDD {
276+
v.HDD[i].updateRelativePaths(v.RunDir)
277+
}
278+
for i := range v.CDROM {
279+
v.CDROM[i].updateRelativePaths(v.RunDir)
280+
}
270281
}
271282

272283
// Boot config
@@ -292,7 +303,7 @@ func (b *Boot) Cli() []string {
292303
return []string{}
293304
}
294305

295-
func (b *Boot) Validate() error {
306+
func (b *Boot) validate() error {
296307
if (Kexec{}) != b.Kexec {
297308
return b.Kexec.Validate()
298309
}
@@ -308,6 +319,20 @@ func (b *Boot) Validate() error {
308319
return nil
309320
}
310321

322+
func (b *Boot) updateRelativePaths(configDir string) {
323+
if (Kexec{}) != b.Kexec {
324+
b.Kexec.updateRelativePaths(configDir)
325+
}
326+
327+
if (Firmware{}) != b.Firmware {
328+
b.Firmware.updateRelativePaths(configDir)
329+
}
330+
331+
if (FBSD{}) != b.FBSD {
332+
b.FBSD.updateRelativePaths(configDir)
333+
}
334+
}
335+
311336
type Kexec struct {
312337
Kernel string `toml:"kernel"`
313338
Initrd string `toml:"initrd"`
@@ -319,7 +344,6 @@ func (k *Kexec) Cli() []string {
319344
}
320345

321346
func (k *Kexec) Validate() error {
322-
323347
if !fileExists(k.Kernel) {
324348
return fmt.Errorf("kernel not found: %s", k.Kernel)
325349
}
@@ -331,6 +355,16 @@ func (k *Kexec) Validate() error {
331355
return nil
332356
}
333357

358+
func (k *Kexec) updateRelativePaths(configDir string) {
359+
if k.Kernel[:1] != "/" {
360+
k.Kernel = filepath.Join(configDir, k.Kernel)
361+
}
362+
363+
if k.Initrd[:1] != "/" {
364+
k.Initrd = filepath.Join(configDir, k.Initrd)
365+
}
366+
}
367+
334368
type Firmware struct {
335369
Path string `toml:"path"`
336370
}
@@ -344,6 +378,12 @@ func (f *Firmware) Validate() error {
344378
return nil
345379
}
346380

381+
func (f *Firmware) updateRelativePaths(configDir string) {
382+
if f.Path[:1] != "/" {
383+
f.Path = filepath.Join(configDir, f.Path)
384+
}
385+
}
386+
347387
type FBSD struct {
348388
Userboot string `toml:"userboot"`
349389
BootVolume string `toml:"userboot"`
@@ -359,7 +399,17 @@ func (f *FBSD) Validate() error {
359399
return nil
360400
}
361401

362-
// VM Network Config
402+
func (f *FBSD) updateRelativePaths(configDir string) {
403+
if f.Userboot[:1] != "/" {
404+
f.Userboot = filepath.Join(configDir, f.Userboot)
405+
}
406+
407+
if f.Userboot[:1] != "/" {
408+
f.BootVolume = filepath.Join(configDir, f.BootVolume)
409+
}
410+
}
411+
412+
// NetConf is a VM network configuration
363413
type NetConf struct {
364414
IP string `toml:"ip"`
365415
MAC string `toml:"mac"`
@@ -401,7 +451,7 @@ func (n *NetConf) validate() error {
401451
}
402452

403453
func (n *NetConf) devicePath() string {
404-
if n.Device[0] == '/' {
454+
if n.Device[:1] == "/" {
405455
return n.Device
406456
}
407457
return "/dev/" + n.Device
@@ -419,12 +469,24 @@ func (h *HDD) create() error {
419469
return nil
420470
}
421471

472+
func (h *HDD) updateRelativePaths(runDir string) {
473+
if h.Path[:1] != "/" {
474+
h.Path = filepath.Join(runDir, h.Path)
475+
}
476+
}
477+
422478
type CDROM struct {
423479
Path string
424480
Driver string
425481
Extract bool
426482
}
427483

484+
func (c *CDROM) updateRelativePaths(runDir string) {
485+
if c.Path[:1] != "/" {
486+
c.Path = filepath.Join(runDir, c.Path)
487+
}
488+
}
489+
428490
// Check if a file exists and isn't a directory
429491
func fileExists(filename string) bool {
430492
info, err := os.Stat(filename)

internal/root/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ func Run() error {
105105
return err
106106
}
107107

108+
config.Defaults()
108109
config.UpdateRelativePaths()
110+
109111
if debug {
110112
fmt.Printf("Parsed config:\n\n%# v\n", pretty.Formatter(config))
111113
}

0 commit comments

Comments
 (0)