Example #1
0
// List all volumes.
func (c *Driver) List(lo storage.ListOptions) ([]storage.Volume, error) {
	poolName := lo.Params["pool"]

retry:
	er, err := executor.NewCapture(exec.Command("rbd", "ls", poolName, "--format", "json")).Run(context.Background())
	if err != nil {
		return nil, err
	}

	if er.ExitStatus != 0 {
		return nil, errored.Errorf("Listing pool %q: %v", poolName, er)
	}

	textList := []string{}

	if err := json.Unmarshal([]byte(er.Stdout), &textList); err != nil {
		logrus.Errorf("Unmarshalling ls for pool %q: %v. Retrying.", poolName, err)
		time.Sleep(100 * time.Millisecond)
		goto retry
	}

	list := []storage.Volume{}

	for _, name := range textList {
		list = append(list, storage.Volume{Name: c.externalName(strings.TrimSpace(name)), Params: storage.Params{"pool": poolName}})
	}

	return list, nil
}
Example #2
0
// ListSnapshots returns an array of snapshot names provided a maximum number
// of snapshots to be returned. Any error will be returned.
func (c *Driver) ListSnapshots(do storage.DriverOptions) ([]string, error) {
	intName, err := c.internalName(do.Volume.Name)
	if err != nil {
		return nil, err
	}

	poolName := do.Volume.Params["pool"]

	cmd := exec.Command("rbd", "snap", "ls", mkpool(poolName, intName))
	ctx, _ := context.WithTimeout(context.Background(), do.Timeout)
	er, err := executor.NewCapture(cmd).Run(ctx)
	if err != nil {
		return nil, err
	}

	if er.ExitStatus != 0 {
		return nil, errored.Errorf("Listing snapshots for (volume %q): %v", intName, er)
	}

	names := []string{}

	lines := strings.Split(er.Stdout, "\n")
	if len(lines) > 1 {
		for _, line := range lines[1:] {
			parts := spaceSplitRegex.Split(line, -1)
			if len(parts) < 3 {
				continue
			}

			names = append(names, parts[2])
		}
	}

	return names, nil
}
Example #3
0
func (c *Driver) showMapped(timeout time.Duration) (rbdMap, error) {
	var (
		er  *executor.ExecResult
		err error
	)

retry:
	rbdmap := rbdMap{}

	cmd := exec.Command("rbd", "showmapped", "--format", "json")
	ctx, _ := context.WithTimeout(context.Background(), timeout)
	er, err = executor.NewCapture(cmd).Run(ctx)
	if err != nil || er.ExitStatus != 0 || er.Stdout == "" {
		logrus.Warnf("Could not show mapped volumes. Retrying: %v", er.Stderr)
		time.Sleep(100 * time.Millisecond)
		goto retry
	}

	if err := json.Unmarshal([]byte(er.Stdout), &rbdmap); err != nil {
		logrus.Errorf("Could not parse RBD showmapped output, retrying: %s", er.Stderr)
		time.Sleep(100 * time.Millisecond)
		goto retry
	}

	return rbdmap, nil
}
Example #4
0
func runWithTimeout(cmd *exec.Cmd, timeout time.Duration) (*executor.ExecResult, error) {
	ctx, _ := context.WithTimeout(context.Background(), timeout)
	return executor.NewCapture(cmd).Run(ctx)
}