public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Michael Köppl" <m.koeppl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH e2e-tests 17/38] tests: container: verify device passthrough
Date: Wed,  4 Nov 2026 20:03:24 +0100	[thread overview]
Message-ID: <20261104190345.2573464-18-m.koeppl@proxmox.com> (raw)
In-Reply-To: <20261104190345.2573464-1-m.koeppl@proxmox.com>

Pass two loop devices through to a container, one of them with
'deny-write' set, and check mode, read and write access inside the
container. Run it for an unprivileged container and for a privileged
one with the 'unmanaged' OS type.

Signed-off-by: Michael Köppl <m.koeppl@proxmox.com>
---
 tests/pve_ct_device_passthrough.pl | 162 +++++++++++++++++++++++++++++
 tests/testcases.ron                |   9 ++
 2 files changed, 171 insertions(+)
 create mode 100755 tests/pve_ct_device_passthrough.pl

diff --git a/tests/pve_ct_device_passthrough.pl b/tests/pve_ct_device_passthrough.pl
new file mode 100755
index 0000000..15ca4ab
--- /dev/null
+++ b/tests/pve_ct_device_passthrough.pl
@@ -0,0 +1,162 @@
+#!/usr/bin/perl
+
+=head1 NAME
+
+C<pve_ct_device_passthrough> - Test passing host devices through to a container.
+
+=head1 DESCRIPTION
+
+This testcase attaches two loop devices on the host, each backed by a small
+scratch file, and passes them through to a container, the second one with write
+access denied. It verifies that both devices show up inside the container with
+the configured mode, that data written to the writable one can be read back,
+and that writes to the other one are refused while reads still work.
+
+Loop devices are used because a container cannot access them unless they are
+passed through and they are easy to set up.
+
+It runs once for an unprivileged container and once for a privileged container
+with the 'unmanaged' OS type, for which LXC ships no OS-specific config.
+
+=cut
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use lib '.';
+use Proxmox::Test::Helper qw(CONTAINER_TEMPLATE get_pve_instance);
+use Proxmox::Test::Testcase;
+
+my $pve = get_pve_instance('pve1');
+my $node = $pve->get_nodename();
+my $client = $pve->client();
+
+my $backing_dir = '/var/lib/ct-passthrough';
+
+# The device type and number of $dev as reported by stat on the host.
+my $host_device_id = sub {
+    my ($dev) = @_;
+
+    my ($out) = $pve->run_command("stat -c '%F %t:%T' $dev");
+    chomp($out);
+    return $out;
+};
+
+Proxmox::Test::Testcase->new('pve_ct_device_passthrough')->with([
+    {
+        name => 'unprivileged',
+        unprivileged => 1,
+    },
+    {
+        name => 'privileged-unmanaged',
+        unprivileged => 0,
+        ostype => 'unmanaged',
+    },
+])->step(
+        'attach loop devices on the host',
+        sub {
+            my ($testcase) = @_;
+
+            $pve->run_command("mkdir -p $backing_dir");
+            for my $kind ('writable', 'readonly') {
+                my $file = "$backing_dir/$kind.img";
+                $pve->run_command("truncate -s 1M $file");
+                my ($dev) = $pve->run_command("losetup --find --show $file");
+                chomp($dev);
+                $testcase->{"${kind}_dev"} = $dev;
+            }
+        },
+        sub {
+            my ($testcase) = @_;
+
+            for my $kind ('writable', 'readonly') {
+                my $dev = $testcase->{"${kind}_dev"} // next;
+                $pve->run_command("losetup --detach $dev");
+            }
+            $pve->run_command("rm -rf $backing_dir");
+        },
+)->step(
+    'create container with passthrough devices',
+    sub {
+        my ($testcase) = @_;
+
+        $testcase->{vmid} = $client->get('/cluster/nextid');
+        my $upid = $client->post(
+            "/nodes/$node/lxc",
+            {
+                vmid => $testcase->{vmid},
+                ostemplate => CONTAINER_TEMPLATE,
+                rootfs => 'local-lvm:2',
+                storage => 'local-lvm',
+                memory => 256,
+                unprivileged => $testcase->{unprivileged},
+                features => 'nesting=1',
+                hostname => "ct-dev-$testcase->{name}",
+                ($testcase->{ostype} ? (ostype => $testcase->{ostype}) : ()),
+                dev0 => "$testcase->{writable_dev},mode=0660",
+                dev1 => "$testcase->{readonly_dev},mode=0640,deny-write=1",
+            },
+        );
+        $pve->task_ok($upid, "created $testcase->{name} container $testcase->{vmid}")
+            or die "creating container failed\n";
+    },
+    sub {
+        my ($testcase) = @_;
+        $pve->destroy_guest('lxc', $testcase->{vmid}) if defined($testcase->{vmid});
+    },
+)->step(
+    'start container',
+    sub {
+        my ($testcase) = @_;
+        my $vmid = $testcase->{vmid};
+
+        $pve->start_guest('lxc', $vmid);
+    },
+)->step(
+    'verify devices inside container',
+    sub {
+        my ($testcase) = @_;
+        my $vmid = $testcase->{vmid};
+        my $writable_dev = $testcase->{writable_dev};
+        my $readonly_dev = $testcase->{readonly_dev};
+
+        is(
+            $pve->ct_exec($vmid, "stat -c '%F %t:%T %a' $writable_dev"),
+            $host_device_id->($writable_dev) . " 660\n",
+            "$writable_dev is passed through with the configured mode",
+        );
+        is(
+            $pve->ct_exec($vmid, "stat -c '%F %t:%T %a' $readonly_dev"),
+            $host_device_id->($readonly_dev) . " 640\n",
+            "$readonly_dev is passed through with the configured mode",
+        );
+
+        my $data = 'test';
+
+        ok(
+            eval { $pve->ct_exec($vmid, "sh -c 'printf $data > $writable_dev'"); 1 },
+            "$writable_dev is writable",
+        );
+        is(
+            $pve->ct_exec($vmid, "head -c " . length($data) . " $writable_dev"),
+            $data,
+            "data written to $writable_dev can be read back",
+        );
+
+        # Opening for writing should already fail because the device cgroup checks access when the
+        # device is opened.
+        ok(
+            !eval { $pve->ct_exec($vmid, "sh -c 'printf $data > $readonly_dev'"); 1 },
+            "writing to $readonly_dev is denied",
+        );
+        is(
+            $pve->ct_exec($vmid, "sh -c 'head -c 4 $readonly_dev | wc -c'"),
+            "4\n",
+            "$readonly_dev is still readable",
+        );
+    },
+)->run();
+
+done_testing();
diff --git a/tests/testcases.ron b/tests/testcases.ron
index c0369e4..1d5b7ad 100644
--- a/tests/testcases.ron
+++ b/tests/testcases.ron
@@ -169,6 +169,15 @@ TestcaseConfig(
             ],
             tag: "extended",
         ),
+        Testcase(
+            name: "pve_ct_device_passthrough",
+            run: "./tests/pve_ct_device_passthrough.pl",
+            timeout: 600,
+            instances: [
+                "pve1",
+            ],
+            tag: "extended",
+        ),
         Testcase(
             name: "pve_ha_resource_config",
             run: "./tests/pve_ha_resource_config.pl",
-- 
2.47.3





  parent reply	other threads:[~2026-11-04 19:06 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 19:03 [PATCH e2e-tests/test-tools 00/38] tests: add tests for guests, Ceph, PBS and cluster workflows Michael Köppl
2026-11-04 19:03 ` [PATCH test-tools 01/38] ansible: rename snd_test bridge to tt_test as multi-purpose bridge Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 02/38] plans: rename sdn_test bridge to tt_test Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 03/38] debian: add missing jq dependency Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 04/38] test framework: move the default container template to the helper Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 05/38] lib: instance: add helper to check if an address answers ping Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 06/38] lib: pveinstance: look up disks by their SCSI serial Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 07/38] lib: pveinstance: add helper to find a task started by another task Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 08/38] lib: pveinstance: add helpers to start and stop guests Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 09/38] lib: pveinstance: add helper to run commands in containers Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 10/38] lib: pveinstance: add helper to wait for qmeventd's cleanup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 11/38] lib: pveinstance: add helper to check if guest exists Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 12/38] lib: add helpers to use a PBS datastore as PVE storage Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 13/38] scripts: retry container template download, fail if it fails Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 14/38] tests: container: verify lifecycle and guest setup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 15/38] tests: container: verify volume and bind mountpoints Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 16/38] tests: container: verify snapshots, clones, templates Michael Köppl
2026-11-04 19:03 ` Michael Köppl [this message]
2026-11-04 19:03 ` [PATCH e2e-tests 18/38] plans,scripts: build a minimal OCI image onto pve1 Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 19/38] tests: container: verify application containers from OCI images Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 20/38] tests: vm: verify snapshots, clones, templates Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 21/38] tests: vm: verify adding, resizing, and removing disks Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 22/38] plans,scripts: download a Debian cloud image onto pve1 Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 23/38] tests: vm: verify lifecycle operations and cloud-init guest setup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 24/38] plans,scripts: install Ceph Tentacle on the cluster nodes Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 25/38] test framework: add Ceph test helpers Michael Köppl
2026-11-06 12:24   ` Kefu Chai
2026-11-06 13:04     ` Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 26/38] tests: ceph: verify setting up a hyper-converged cluster Michael Köppl
2026-11-06 12:50   ` Kefu Chai
2026-11-06 13:05     ` Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 27/38] tests: ceph: verify guests on RBD storage Michael Köppl
2026-11-06 13:01   ` Kefu Chai
2026-11-06 13:07     ` Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 28/38] tests: ceph: verify setting up, using and destroying a CephFS Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 29/38] tests: ceph: verify replacing an OSD Michael Köppl
2026-11-06 13:14   ` Kefu Chai
2026-11-04 19:03 ` [PATCH e2e-tests 30/38] tests: ceph: drop the outdated Ceph setup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 31/38] tests: pbs: verify container backup and restore Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 32/38] tests: backup: verify VM backup and restore, locally and on PBS Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 33/38] tests: pbs: verify single file restore from a container backup Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 34/38] tests: storage: verify moving guest volumes between storages Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 35/38] tests: migration: verify container migration between nodes Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 36/38] tests: migration: verify VM migration with local disks Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 37/38] tests: ha: verify moving guests off a node for maintenance Michael Köppl
2026-11-04 19:03 ` [PATCH e2e-tests 38/38] tests: migration: verify migrating guests in bulk Michael Köppl
2026-11-06 13:25 ` [PATCH e2e-tests/test-tools 00/38] tests: add tests for guests, Ceph, PBS and cluster workflows Kefu Chai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261104190345.2573464-18-m.koeppl@proxmox.com \
    --to=m.koeppl@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal