func (s *TestSuite) SetUpSuite(c *C) {
	logrus.SetLevel(logrus.DebugLevel)
	logrus.SetOutput(os.Stderr)

	var err error

	err = exec.Command("mkdir", "-p", devRoot).Run()
	c.Assert(err, IsNil)

	err = exec.Command("mkdir", "-p", devMount).Run()
	c.Assert(err, IsNil)

	// Prepare base image
	s.imageFile = filepath.Join(devRoot, imageFile)
	err = exec.Command("truncate", "-s", strconv.Itoa(volumeSize), s.imageFile).Run()
	c.Assert(err, IsNil)

	tmpDev, err := util.AttachLoopbackDevice(s.imageFile, false)
	c.Assert(err, IsNil)

	err = exec.Command("mkfs", "-t", "ext4", tmpDev).Run()
	c.Assert(err, IsNil)

	err = exec.Command("mount", tmpDev, devMount).Run()
	c.Assert(err, IsNil)

	err = exec.Command("touch", filepath.Join(devMount, imageTestFile)).Run()
	c.Assert(err, IsNil)

	err = exec.Command("umount", devMount).Run()
	c.Assert(err, IsNil)

	err = util.DetachLoopbackDevice(s.imageFile, tmpDev)
	c.Assert(err, IsNil)
}
Ejemplo n.º 2
0
func (d *Driver) DeactivateImage(imageUUID string) error {
	image := d.loadImage(imageUUID)
	if image == nil {
		return generateError(logrus.Fields{
			LOG_FIELD_IMAGE: imageUUID,
		}, "Cannot find image")
	}
	for volumeUUID := range image.VolumeRef {
		if volume := d.loadVolume(volumeUUID); volume != nil {
			return generateError(logrus.Fields{
				LOG_FIELD_VOLUME: volumeUUID,
				LOG_FIELD_IMAGE:  imageUUID,
			}, "Volume based on the image hasn't been removed yet")
		}
	}
	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:     LOG_REASON_START,
		LOG_FIELD_EVENT:      LOG_EVENT_DEACTIVATE,
		LOG_FIELD_OBJECT:     LOG_OBJECT_IMAGE,
		LOG_FIELD_IMAGE:      imageUUID,
		LOG_FIELD_IMAGE_FILE: image.FilePath,
		LOG_FIELD_IMAGE_DEV:  image.Device,
	}).Debug()
	if err := util.DetachLoopbackDevice(image.FilePath, image.Device); err != nil {
		return err
	}
	if err := d.deleteImage(imageUUID); err != nil {
		return err
	}
	return nil
}