sysconfig/cloudinit: check if disabled via kernel cmdline (#14034)

* sysconfig/cloudinit: Check if disabled via kernel commandline

As documented:

https://cloudinit.readthedocs.io/en/latest/howto/disable_cloud_init.html

Signed-off-by: Brett Holman <brett.holman@canonical.com>

* sysconfig: made kernel cmdline disable comment similar to that of disable file

---------

Signed-off-by: Brett Holman <brett.holman@canonical.com>
Co-authored-by: Brett Holman <brett.holman@canonical.com>
This commit is contained in:
Ernest Lotter
2024-06-03 14:42:31 +02:00
committed by GitHub
parent 2b3c68b024
commit 92c818d86b
2 changed files with 32 additions and 1 deletions

View File

@@ -35,6 +35,7 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/osutil/kcmdline"
"github.com/snapcore/snapd/strutil"
)
@@ -742,6 +743,15 @@ func CloudInitStatus() (CloudInitState, error) {
return CloudInitDisabledPermanently, nil
}
// if it was explicitly disabled via the kernel commandline, then
// return special status for that
cmdline, err := kcmdline.KeyValues("cloud-init")
if err != nil {
logger.Noticef("WARNING: cannot obtain cloud-init from kernel command line: %v", err)
} else if cmdline["cloud-init"] == "disabled" {
return CloudInitDisabledPermanently, nil
}
ciBinary, err := exec.LookPath("cloud-init")
if err != nil {
logger.Noticef("cannot locate cloud-init executable: %v", err)

View File

@@ -29,6 +29,7 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil/kcmdline"
"github.com/snapcore/snapd/sysconfig"
"github.com/snapcore/snapd/testutil"
)
@@ -54,6 +55,11 @@ func (s *sysconfigSuite) SetUpTest(c *C) {
oldTmpdir := os.Getenv("TMPDIR")
os.Setenv("TMPDIR", s.tmpdir)
s.AddCleanup(func() { os.Unsetenv(oldTmpdir) })
err := os.MkdirAll(filepath.Join(s.tmpdir, "proc"), 0755)
c.Assert(err, IsNil)
restore := kcmdline.MockProcCmdline(filepath.Join(s.tmpdir, "proc/cmdline"))
s.AddCleanup(restore)
}
func (s *sysconfigSuite) makeCloudCfgSrcDirFiles(c *C, cfgs ...string) (string, []string) {
@@ -579,6 +585,7 @@ func (s *sysconfigSuite) TestCloudInitStatus(c *C) {
exp sysconfig.CloudInitState
restrictedFile bool
disabledFile bool
disabledKernel bool
expError string
expectedLog string
}{
@@ -624,6 +631,11 @@ func (s *sysconfigSuite) TestCloudInitStatus(c *C) {
disabledFile: true,
exp: sysconfig.CloudInitDisabledPermanently,
},
{
comment: "disabled permanently via kernel commandline",
disabledKernel: true,
exp: sysconfig.CloudInitDisabledPermanently,
},
{
comment: "errored w/ exit code 0",
cloudInitOutput: "status: error",
@@ -677,6 +689,15 @@ fi
c.Assert(err, IsNil)
}
mockProcCmdline := filepath.Join(s.tmpdir, "proc/cmdline")
if t.disabledKernel {
err := os.WriteFile(mockProcCmdline, []byte("BOOT_IMAGE=/vmlinuz-6.1.53- root=UUID=63642d181-ad10-4457-80b0-14289c2183ef ro cloud-init=disabled panic_on_warn"), 0644)
c.Assert(err, IsNil)
} else {
err := os.WriteFile(mockProcCmdline, []byte("BOOT_IMAGE=/vmlinuz-6.1.53- root=UUID=63642d181-ad10-4457-80b0-14289c2183ef ro cloud-init=enabled panic_on_warn"), 0644)
c.Assert(err, IsNil)
}
if t.restrictedFile {
cloudDir := filepath.Join(dirs.GlobalRootDir, "etc/cloud/cloud.cfg.d")
err := os.MkdirAll(cloudDir, 0755)
@@ -698,7 +719,7 @@ fi
// if the restricted file was there we don't call cloud-init status
var expCalls [][]string
if !t.restrictedFile && !t.disabledFile {
if !t.restrictedFile && !t.disabledFile && !t.disabledKernel {
expCalls = [][]string{
{"cloud-init", "status"},
}