@@ -20,20 +20,20 @@ var hyperkitPath = "hyperkit"
2020type VM map [string ]* VMConfig
2121
2222type 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+
311336type Kexec struct {
312337 Kernel string `toml:"kernel"`
313338 Initrd string `toml:"initrd"`
@@ -319,7 +344,6 @@ func (k *Kexec) Cli() []string {
319344}
320345
321346func (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+
334368type 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+
347387type 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
363413type NetConf struct {
364414 IP string `toml:"ip"`
365415 MAC string `toml:"mac"`
@@ -401,7 +451,7 @@ func (n *NetConf) validate() error {
401451}
402452
403453func (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+
422478type 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
429491func fileExists (filename string ) bool {
430492 info , err := os .Stat (filename )
0 commit comments