summaryrefslogtreecommitdiff
path: root/src/conf_mode/system_console.py
diff options
context:
space:
mode:
authorBrandon Stepler <brandon@stepler.net>2021-03-04 16:30:00 -0500
committerBrandon Stepler <brandon@stepler.net>2021-03-04 16:30:00 -0500
commit658456982ad4543790a3835f6ddbfbe3b583ec44 (patch)
treee37af23b28b3181194686b3116cc3fc1a3d8e9ed /src/conf_mode/system_console.py
parentd79c80ff078ac686ae2a4e0da66bfbd40d67f824 (diff)
downloadvyos-1x-658456982ad4543790a3835f6ddbfbe3b583ec44.tar.gz
vyos-1x-658456982ad4543790a3835f6ddbfbe3b583ec44.zip
grub: T3271: don't write grub.cfg if it hasn't changed
Diffstat (limited to 'src/conf_mode/system_console.py')
-rwxr-xr-xsrc/conf_mode/system_console.py25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/conf_mode/system_console.py b/src/conf_mode/system_console.py
index b17818797..33a546bd3 100755
--- a/src/conf_mode/system_console.py
+++ b/src/conf_mode/system_console.py
@@ -17,9 +17,8 @@
import os
import re
-from fileinput import input as replace_in_file
from vyos.config import Config
-from vyos.util import call
+from vyos.util import call, read_file, write_file
from vyos.template import render
from vyos import ConfigError, airbag
airbag.enable()
@@ -98,15 +97,27 @@ def generate(console):
if not os.path.isfile(grub_config):
return None
- # stdin/stdout are redirected in replace_in_file(), thus print() is fine
+ lines = read_file(grub_config).split('\n')
+
p = re.compile(r'^(.* console=ttyS0),[0-9]+(.*)$')
- for line in replace_in_file(grub_config, inplace=True):
+ write = False
+ newlines = []
+ for line in lines:
if line.startswith('serial --unit'):
- line = f'serial --unit=0 --speed={speed}\n'
+ newline = f'serial --unit=0 --speed={speed}'
elif p.match(line):
- line = '{},{}{}\n'.format(p.search(line)[1], speed, p.search(line)[2])
+ newline = '{},{}{}'.format(p.search(line)[1], speed, p.search(line)[2])
+ else:
+ newline = line
+
+ if newline != line:
+ write = True
+
+ newlines.append(newline)
+ newlines.append('')
- print(line, end='')
+ if write:
+ write_file(grub_config, '\n'.join(newlines))
return None