summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2016-03-24 15:30:03 -0400
committerScott Moser <smoser@ubuntu.com>2016-03-24 15:30:03 -0400
commit3ad9929efcab614a6ffc170c75c1c6c81b57a2b8 (patch)
tree7e48c9a4969c4fad65b684fb1030a6d468906719 /cloudinit
parent841a773fd36968419354507fa45f44afa6eb8470 (diff)
downloadvyos-cloud-init-3ad9929efcab614a6ffc170c75c1c6c81b57a2b8.tar.gz
vyos-cloud-init-3ad9929efcab614a6ffc170c75c1c6c81b57a2b8.zip
make get_cmdline read /proc/1/cmdline if inside a container
This follows behavior of systemd/cloud-init-generator. This way you can feed a command line into lxc container.
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/util.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index 20916e53..0d21e11b 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -80,6 +80,8 @@ CONTAINER_TESTS = (['systemd-detect-virt', '--quiet', '--container'],
['running-in-container'],
['lxc-is-container'])
+PROC_CMDLINE = None
+
def decode_binary(blob, encoding='utf-8'):
# Converts a binary type into a text type using given encoding.
@@ -1191,12 +1193,27 @@ def load_file(fname, read_cb=None, quiet=False, decode=True):
def get_cmdline():
if 'DEBUG_PROC_CMDLINE' in os.environ:
- cmdline = os.environ["DEBUG_PROC_CMDLINE"]
+ return os.environ["DEBUG_PROC_CMDLINE"]
+
+ global PROC_CMDLINE
+ if PROC_CMDLINE is not None:
+ return PROC_CMDLINE
+
+ if is_container():
+ try:
+ contents = load_file("/proc/1/cmdline")
+ # replace nulls with space and drop trailing null
+ cmdline = contents.replace("\x00", " ")[:-1]
+ except Exception as e:
+ LOG.warn("failed reading /proc/1/cmdline: %s", e)
+ cmdline = ""
else:
try:
cmdline = load_file("/proc/cmdline").strip()
except:
cmdline = ""
+
+ PROC_CMDLINE = cmdline
return cmdline
@@ -1569,7 +1586,7 @@ def uptime():
try:
if os.path.exists("/proc/uptime"):
method = '/proc/uptime'
- contents = load_file("/proc/uptime").strip()
+ contents = load_file("/proc/uptime")
if contents:
uptime_str = contents.split()[0]
else: