runtime: support non-root for clh

This change enables to run cloud-hypervisor VMM using a non-root user
when rootless flag is set true in the configuration

Fixes: #2567

Signed-off-by: Feng Wang <fwang@confluent.io>
This commit is contained in:
Feng Wang
2023-01-31 09:48:49 -08:00
parent 44a780f262
commit cbe6ad9034
5 changed files with 45 additions and 4 deletions

View File

@@ -19,6 +19,7 @@ import (
"net/http/httputil"
"os"
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strconv"
@@ -37,6 +38,8 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
hv "github.com/kata-containers/kata-containers/src/runtime/pkg/hypervisors"
"github.com/kata-containers/kata-containers/src/runtime/pkg/katautils/katatrace"
pkgUtils "github.com/kata-containers/kata-containers/src/runtime/pkg/utils"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
)
@@ -653,7 +656,7 @@ func (clh *cloudHypervisor) StartVM(ctx context.Context, timeout int) error {
clh.Logger().WithField("function", "StartVM").Info("starting Sandbox")
vmPath := filepath.Join(clh.config.VMStorePath, clh.id)
err := os.MkdirAll(vmPath, DirMode)
err := utils.MkdirAllWithInheritedOwner(vmPath, DirMode)
if err != nil {
return err
}
@@ -1352,9 +1355,16 @@ func (clh *cloudHypervisor) launchClh() (int, error) {
cmdHypervisor.Stdout = clh.console
}
}
cmdHypervisor.Stderr = cmdHypervisor.Stdout
attr := syscall.SysProcAttr{}
attr.Credential = &syscall.Credential{
Uid: clh.config.Uid,
Gid: clh.config.Gid,
Groups: clh.config.Groups,
}
cmdHypervisor.SysProcAttr = &attr
err = utils.StartCmd(cmdHypervisor)
if err != nil {
return -1, err
@@ -1679,6 +1689,30 @@ func (clh *cloudHypervisor) cleanupVM(force bool) error {
clh.Logger().WithError(err).WithField("path", dir).Warnf("failed to remove vm path")
}
}
if rootless.IsRootless() {
if _, err := user.Lookup(clh.config.User); err != nil {
clh.Logger().WithError(err).WithFields(
log.Fields{
"user": clh.config.User,
"uid": clh.config.Uid,
}).Warn("failed to find the user, it might have been removed")
return nil
}
if err := pkgUtils.RemoveVmmUser(clh.config.User); err != nil {
clh.Logger().WithError(err).WithFields(
log.Fields{
"user": clh.config.User,
"uid": clh.config.Uid,
}).Warn("failed to delete the user")
return nil
}
clh.Logger().WithFields(
log.Fields{
"user": clh.config.User,
"uid": clh.config.Uid,
}).Debug("successfully removed the non root user")
}
clh.reset()

View File

@@ -1183,6 +1183,7 @@ func (q *qemu) cleanupVM() error {
"user": q.config.User,
"uid": q.config.Uid,
}).Warn("failed to delete the user")
return nil
}
q.Logger().WithFields(
logrus.Fields{