Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VM: Use virtiofsd chroot sandbox mode on pre pidfd_open kernels #13794

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lxd/device/device_utils_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/osarch"
"github.com/canonical/lxd/shared/revert"
"github.com/canonical/lxd/shared/version"
)

// RBDFormatPrefix is the prefix used in disk paths to identify RBD.
Expand Down Expand Up @@ -424,7 +425,7 @@ func DiskVMVirtfsProxyStop(pidPath string) error {
// Returns UnsupportedError error if the host system or instance does not support virtiosfd, returns normal error
// type if process cannot be started for other reasons.
// Returns revert function and listener file handle on success.
func DiskVMVirtiofsdStart(execPath string, inst instance.Instance, socketPath string, pidPath string, logPath string, sharePath string, idmaps []idmap.IdmapEntry) (func(), net.Listener, error) {
func DiskVMVirtiofsdStart(kernelVersion version.DottedVersion, inst instance.Instance, socketPath string, pidPath string, logPath string, sharePath string, idmaps []idmap.IdmapEntry) (func(), net.Listener, error) {
revert := revert.New()
defer revert.Fail()

Expand Down Expand Up @@ -502,7 +503,18 @@ func DiskVMVirtiofsdStart(execPath string, inst instance.Instance, socketPath st
defer func() { _ = unixFile.Close() }()

// Start the virtiofsd process in non-daemon mode.
args := []string{"--fd=3", "-o", fmt.Sprintf("source=%s", sharePath)}
args := []string{
"--fd=3",
"-o", fmt.Sprintf("source=%s", sharePath),
}

// Virtiofsd defaults to namespace sandbox mode which requires pidfd_open support.
// This was added in Linux 5.3, so if running an earlier kernel fallback to chroot sandbox mode.
minVer, _ := version.NewDottedVersion("5.3.0")
if kernelVersion.Compare(minVer) < 0 {
args = append(args, "--sandbox=chroot")
}

proc, err := subprocess.NewProcess(cmd, args, logPath, logPath)
if err != nil {
return nil, nil, err
Expand Down
2 changes: 1 addition & 1 deletion lxd/device/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ func (d *disk) startVM() (*deviceConfig.RunConfig, error) {
logPath := filepath.Join(d.inst.LogPath(), fmt.Sprintf("disk.%s.log", filesystem.PathNameEncode(d.name)))
_ = os.Remove(logPath) // Remove old log if needed.

revertFunc, unixListener, err := DiskVMVirtiofsdStart(d.state.OS.ExecPath, d.inst, sockPath, pidPath, logPath, mount.DevPath, rawIDMaps)
revertFunc, unixListener, err := DiskVMVirtiofsdStart(d.state.OS.KernelVersion, d.inst, sockPath, pidPath, logPath, mount.DevPath, rawIDMaps)
if err != nil {
var errUnsupported UnsupportedError
if errors.As(err, &errUnsupported) {
Expand Down
2 changes: 1 addition & 1 deletion lxd/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ func (d *qemu) start(stateful bool, op *operationlock.InstanceOperation) error {
// This is used by the lxd-agent in preference to 9p (due to its improved performance) and in scenarios
// where 9p isn't available in the VM guest OS.
configSockPath, configPIDPath := d.configVirtiofsdPaths()
revertFunc, unixListener, err := device.DiskVMVirtiofsdStart(d.state.OS.ExecPath, d, configSockPath, configPIDPath, "", configMntPath, nil)
revertFunc, unixListener, err := device.DiskVMVirtiofsdStart(d.state.OS.KernelVersion, d, configSockPath, configPIDPath, "", configMntPath, nil)
if err != nil {
var errUnsupported device.UnsupportedError
if !errors.As(err, &errUnsupported) {
Expand Down
Loading