Example #1
0
// diskInjectCleanup handles unmounting, disconnecting nbd, and removing mount
// directory after diskInject.
func diskInjectCleanup(mntDir, nbdPath string) {
	log.Debug("cleaning up vm inject: %s %s", mntDir, nbdPath)

	out, err := processWrapper("umount", mntDir)
	if err != nil {
		log.Error("injectCleanup: %v, %v", out, err)
	}

	if err := nbd.DisconnectDevice(nbdPath); err != nil {
		log.Error("qemu nbd disconnect: %v", err)
		log.Warn("minimega was unable to disconnect %v", nbdPath)
	}

	err = os.Remove(mntDir)
	if err != nil {
		log.Error("rm mount dir: %v", err)
	}
}
Example #2
0
// Unmount, disconnect nbd, and remove mount directory
func vmInjectCleanup(mntDir, nbdPath string) {
	log.Debug("cleaning up vm inject: %s %s", mntDir, nbdPath)

	p := process("umount")
	cmd := exec.Command(p, mntDir)
	err := cmd.Run()
	if err != nil {
		log.Error("injectCleanup: %v", err)
	}

	err = nbd.DisconnectDevice(nbdPath)
	if err != nil {
		log.Error("qemu nbd disconnect: %v", err)
		log.Warn("minimega was unable to disconnect %v", nbdPath)
	}

	p = process("rm")
	cmd = exec.Command(p, "-r", mntDir)
	err = cmd.Run()
	if err != nil {
		log.Error("rm mount dir: %v", err)
	}
}
Example #3
0
// Buildqcow2 creates a qcow2 image using qemu-img, qemu-nbd, fdisk, mkfs.ext4,
// cp, and extlinux.
func Buildqcow2(buildPath string, c vmconfig.Config) error {
	targetName := strings.Split(filepath.Base(c.Path), ".")[0]
	log.Debugln("using target name:", targetName)

	err := nbd.Modprobe()
	if err != nil {
		return err
	}

	wd, err := os.Getwd()
	if err != nil {
		return err
	}

	// Final qcow2 target
	targetqcow2 := fmt.Sprintf("%v/%v.qcow2", wd, targetName)
	// Temporary file for building qcow2 file, will be renamed to targetqcow2
	tmpqcow2 := fmt.Sprintf("%v/%v.qcow2.tmp", wd, targetName)

	err = createQcow2(tmpqcow2, *f_qcowsize)
	if err != nil {
		return err
	}

	// Cleanup our temporary building file
	defer func() {
		// Check if file exists
		if _, err := os.Stat(tmpqcow2); err == nil {
			if err = os.Remove(tmpqcow2); err != nil {
				log.Errorln(err)
			}
		}
	}()

	dev, err := nbd.ConnectImage(tmpqcow2)
	if err != nil {
		return err
	}

	// Disconnect from the nbd device
	defer func() {
		if err := nbd.DisconnectDevice(dev); err != nil {
			log.Errorln(err)
		}
	}()

	err = partitionQcow2(dev)
	if err != nil {
		return err
	}

	err = formatQcow2(dev + "p1")
	if err != nil {
		return err
	}

	mountPath, err := mountQcow2(dev + "p1")
	if err != nil {
		return err
	}

	err = copyQcow2(buildPath, mountPath)
	if err != nil {
		err2 := umountQcow2(mountPath)
		if err2 != nil {
			log.Errorln(err2)
		}
		return err
	}

	err = extlinux(mountPath)
	if err != nil {
		err2 := umountQcow2(mountPath)
		if err2 != nil {
			log.Errorln(err2)
		}
		return err
	}

	err = umountQcow2(mountPath)
	if err != nil {
		return err
	}

	err = extlinuxMBR(dev, *f_mbr)
	if err != nil {
		return err
	}

	return os.Rename(tmpqcow2, targetqcow2)
}