/
run_linux.go
68 lines (56 loc) · 1.54 KB
/
run_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
//go:build linux
package kvm
import (
"os"
"os/exec"
"os/user"
"strconv"
"syscall"
"github.com/juju/errors"
)
// Run the command as user libvirt-qemu and return the combined output.
// If dir is non-empty, use it as the working directory.
func runAsLibvirt(dir, command string, args ...string) (string, error) {
uid, gid, err := getUserUIDGID(libvirtUser)
if err != nil {
return "", errors.Trace(err)
}
cmd := exec.Command(command, args...)
if dir != "" {
cmd.Dir = dir
}
if dir == "" {
dir, _ = os.Getwd()
}
logger.Debugf("running: %s %v from %s", command, args, dir)
logger.Debugf("running as uid: %d, gid: %d\n", uid, gid)
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{
Uid: uint32(uid),
Gid: uint32(gid),
}
out, err := cmd.CombinedOutput()
output := string(out)
logger.Debugf("output: %v", output)
return output, err
}
// getUserUIDGID returns integer vals for uid and gid for the user. It returns
// -1 when there's an error so no one accidentally thinks 0 is the appropriate
// uid/gid when there's an error.
func getUserUIDGID(_ string) (int, int, error) {
u, err := user.Lookup(libvirtUser)
if err != nil {
return -1, -1, errors.Trace(err)
}
uid, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return -1, -1, errors.Trace(err)
}
gid, err := strconv.ParseUint(u.Gid, 10, 32)
if err != nil {
return -1, -1, errors.Trace(err)
}
return int(uid), int(gid), nil
}