Example #1
0
// Merge guest volume/block device configuration with any volumes found in
// host container.
func mergeVolumeConfig(volumeconfig []volumeConfig, guest *gabs.Container) (err error) {
	var mounts *gabs.Container
	if guest.ExistsP("mount") {
		mounts = guest.S("mount")
	} else {
		mounts, _ = guest.ObjectP("mount")
	}
	mmap, _ := mounts.ChildrenMap()
	for _, mount := range mmap {
		var source string
		var path string
		var ok bool
		if source, ok = mount.Path("source").Data().(string); !ok {
			continue
		}
		if path, ok = mount.Path("path").Data().(string); !ok {
			continue
		}
		if source == "blk" && strings.HasPrefix(path, "/dev/ld") {
			return fmt.Errorf("Guest configuration defines " +
				"/dev/ld* block devices, cannot merge with " +
				"host volumes")
		}
	}
	blkIndex := 0
	for _, v := range volumeconfig {
		mp := "/" + v.name
		obj, _ := guest.Object("mount", mp)
		obj.Set("blk", "source")
		obj.Set(fmt.Sprintf("/dev/ld%va", blkIndex), "path")
		blkIndex += 1
	}
	return
}