Skip to content

Commit ab7429c

Browse files
authored
i/builtin: allow access to /var/lib/iscsi/nodes (#17350)
* i/builtin: allow access to /var/lib/icsi/nodes Newer openiSCSI stores its persistent node database in /var/lib/iscsi/nodes (https://salsa.debian.org/linux-blocks-team/open-iscsi/-/commit/74f7df689b2c53f9509bc2959804b30887038db9). This adapts the iscsi-initiator to allow access to that path and binds mounts the /var/lib/snapd/hostfs... path to the expected place. Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
1 parent 7da4ab0 commit ab7429c

3 files changed

Lines changed: 62 additions & 5 deletions

File tree

interfaces/builtin/iscsi_initiator.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919

2020
package builtin
2121

22+
import (
23+
"path/filepath"
24+
25+
"github.com/snapcore/snapd/interfaces"
26+
"github.com/snapcore/snapd/interfaces/apparmor"
27+
"github.com/snapcore/snapd/osutil"
28+
)
29+
2230
/*
2331
* The iscsi-initiator interface allows snaps to act as iSCSI initiators,
2432
* enabling them to discover, connect to, and manage iSCSI targets for
@@ -60,8 +68,8 @@ const iscsiInitiatorConnectedPlugAppArmor = `
6068
/etc/iscsi/isns/ rwk,
6169
/etc/iscsi/isns/** rw,
6270
# iSCSI target node information for persistent connections
63-
/etc/iscsi/nodes/ rwk,
64-
/etc/iscsi/nodes/** rw,
71+
/{etc,var/lib}/iscsi/nodes/ rwk,
72+
/{etc,var/lib}/iscsi/nodes/** rw,
6573
6674
# Runtime files and locks for iSCSI daemon operation
6775
/run/lock/iscsi/ rw,
@@ -103,7 +111,30 @@ func init() {
103111
implicitOnClassic: true,
104112
baseDeclarationSlots: iscsiInitiatorBaseDeclarationSlots,
105113
baseDeclarationPlugs: iscsiInitiatorBaseDeclarationPlugs,
106-
connectedPlugAppArmor: iscsiInitiatorConnectedPlugAppArmor,
107114
connectedPlugKModModules: iscsiInitiatorConnectedPlugKmod,
115+
// expose the host's iSCSI persistent node database at /var/lib/iscsi/nodes,
116+
// if one exists
117+
connectedPlugMount: []osutil.MountEntry{{
118+
Name: filepath.Join("/var/lib/snapd/hostfs", nodesDBDebianPath),
119+
Dir: nodesDBDebianPath,
120+
Options: []string{"bind", "rw"},
121+
}},
108122
}})
109123
}
124+
125+
const nodesDBDebianPath = "/var/lib/iscsi/nodes"
126+
127+
// AppArmorConnectedPlug updates the snap-update-ns rules to allow the bind
128+
// mount of the iscsi node DB to the correct location.
129+
func (iface *iscsiInitiatorInterface) AppArmorConnectedPlug(spec *apparmor.Specification, plug *interfaces.ConnectedPlug, slot *interfaces.ConnectedSlot) error {
130+
spec.AddSnippet(iscsiInitiatorConnectedPlugAppArmor)
131+
132+
emit := spec.AddUpdateNSf
133+
emit(" # Bind-mount host's iSCSI persistent node database\n")
134+
emit(" mount options=(bind, rw) /var/lib/snapd/hostfs%[1]s/ -> %[1]s/,\n", nodesDBDebianPath)
135+
emit(" umount %s/,\n", nodesDBDebianPath)
136+
// /var/lib/iscsi/nodes/ does not exist in the snap's base, so we need to
137+
// add writable-mimic rules for snap-update-ns to create it.
138+
apparmor.GenWritableProfile(emit, nodesDBDebianPath, 1)
139+
return nil
140+
}

interfaces/builtin/iscsi_initiator_test.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
package builtin_test
2121

2222
import (
23+
"strings"
24+
2325
. "gopkg.in/check.v1"
2426

2527
"github.com/snapcore/snapd/interfaces"
2628
"github.com/snapcore/snapd/interfaces/apparmor"
2729
"github.com/snapcore/snapd/interfaces/builtin"
2830
"github.com/snapcore/snapd/interfaces/kmod"
31+
"github.com/snapcore/snapd/interfaces/mount"
2932
"github.com/snapcore/snapd/interfaces/udev"
33+
"github.com/snapcore/snapd/osutil"
3034
"github.com/snapcore/snapd/snap"
3135
"github.com/snapcore/snapd/testutil"
3236
)
@@ -94,8 +98,8 @@ func (s *iscsiInitiatorInterfaceSuite) TestConnectedPlugSnippet(c *C) {
9498
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/etc/iscsi/static/** rw,")
9599
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/etc/iscsi/isns/ rwk,")
96100
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/etc/iscsi/isns/** rw,")
97-
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/etc/iscsi/nodes/ rwk,")
98-
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/etc/iscsi/nodes/** rw,")
101+
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/{etc,var/lib}/iscsi/nodes/ rwk,")
102+
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/{etc,var/lib}/iscsi/nodes/** rw,")
99103
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/run/lock/iscsi/** rwlk,")
100104
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/sys/class/iscsi_session/** rw,")
101105
c.Assert(apparmorSpec.SnippetForTag("snap.other.app"), testutil.Contains, "/sys/class/iscsi_host/** r,")
@@ -133,3 +137,22 @@ func (s *iscsiInitiatorInterfaceSuite) TestUDevConnectedPlug(c *C) {
133137
func (s *iscsiInitiatorInterfaceSuite) TestInterfaces(c *C) {
134138
c.Check(builtin.Interfaces(), testutil.DeepContains, s.iface)
135139
}
140+
141+
func (s *iscsiInitiatorInterfaceSuite) TestMountEntryAndNsRule(c *C) {
142+
const nsSnippet = "mount options=(bind, rw) /var/lib/snapd/hostfs/var/lib/iscsi/nodes/ -> /var/lib/iscsi/nodes/,"
143+
144+
mountSpec := &mount.Specification{}
145+
c.Assert(mountSpec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
146+
c.Check(mountSpec.MountEntries(), DeepEquals, []osutil.MountEntry{{
147+
Name: "/var/lib/snapd/hostfs/var/lib/iscsi/nodes",
148+
Dir: "/var/lib/iscsi/nodes",
149+
Options: []string{"bind", "rw"},
150+
}})
151+
152+
appSet, err := interfaces.NewSnapAppSet(s.plug.Snap(), nil)
153+
c.Assert(err, IsNil)
154+
aaSpec := apparmor.NewSpecification(appSet)
155+
c.Assert(aaSpec.AddConnectedPlug(s.iface, s.plug, s.slot), IsNil)
156+
157+
c.Check(strings.Join(aaSpec.UpdateNS(), "\n"), testutil.Contains, nsSnippet)
158+
}

tests/main/interfaces-iscsi-initiator/task.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ prepare: |
3535
tests.cleanup defer "$TESTSTOOLS"/fs-state restore-dir /etc/iscsi/static
3636
"$TESTSTOOLS"/fs-state mock-dir /etc/iscsi/isns
3737
tests.cleanup defer "$TESTSTOOLS"/fs-state restore-dir /etc/iscsi/isns
38+
"$TESTSTOOLS"/fs-state mock-dir /var/lib/iscsi/nodes
39+
tests.cleanup defer "$TESTSTOOLS"/fs-state restore-dir /var/lib/iscsi/nodes
3840
"$TESTSTOOLS"/fs-state mock-dir /etc/iscsi/nodes
3941
tests.cleanup defer "$TESTSTOOLS"/fs-state restore-dir /etc/iscsi/nodes
4042
"$TESTSTOOLS"/fs-state mock-file /etc/iscsi/initiatorname.iscsi
@@ -69,6 +71,7 @@ execute: |
6971
7072
echo "And the snap is able to access iSCSI nodes directory"
7173
"$TEST_SNAP".client -c "ls /etc/iscsi/nodes"
74+
"$TEST_SNAP".client -c "ls /var/lib/iscsi/nodes"
7275
7376
echo "And the snap is able to access additional Open-iSCSI persistent state"
7477
"$TEST_SNAP".client -c "ls /etc/iscsi/ifaces"

0 commit comments

Comments
 (0)