Skip to content

Commit 9bcccae

Browse files
Update the hostname in /boot/firmware/user-data (#19)
* Update the hostname in /boot/firmware/user-data as well as /etc/hostname to ensure it persists properly across reboots * Suppress error output about failure to preserve permissions across filesystems * Write the updated hostname to the turtlebot4/system file
1 parent 346a8c7 commit 9bcccae

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

turtlebot4_setup/conf.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def __init__(self) -> None:
130130
self.netplan_wifis_file = os.path.join(self.netplan_dir, '50-wifis.yaml')
131131
self.discovery_sh_file = os.path.join(self.setup_dir, 'discovery.sh')
132132
self.hostname_file = '/etc/hostname'
133+
self.fw_user_data_file = '/boot/firmware/user-data'
133134

134135
self.system_conf = copy.deepcopy(self.default_system_conf)
135136
self.wifi_conf = copy.deepcopy(self.default_wifi_conf)
@@ -213,7 +214,12 @@ def write_system(self):
213214
system = f.readlines()
214215
for i, line in enumerate(system):
215216
is_conf = False
216-
for k in [SystemOptions.MODEL, SystemOptions.VERSION, SystemOptions.ROS]:
217+
for k in [
218+
SystemOptions.MODEL,
219+
SystemOptions.VERSION,
220+
SystemOptions.ROS,
221+
SystemOptions.HOSTNAME,
222+
]:
217223
if k in line:
218224
system[i] = f'{k}:{self.system_conf[k]}\n'
219225
is_conf = True
@@ -230,6 +236,15 @@ def write_system(self):
230236
f.write(self.get(SystemOptions.HOSTNAME))
231237
subprocess.run(shlex.split('sudo mv /tmp' + self.hostname_file + ' ' + self.hostname_file))
232238

239+
# update /boot/firmware/user-data with the new hostname
240+
subprocess.run(shlex.split(f'cp {self.fw_user_data_file} /tmp/user-data'))
241+
subprocess.run(shlex.split(f'sed -i -E "s/^hostname:.+/hostname: {self.get(SystemOptions.HOSTNAME)}/" /tmp/user-data')) # noqa: E501
242+
subprocess.run(
243+
shlex.split(f'sudo mv /tmp/user-data {self.fw_user_data_file}'),
244+
stdout=subprocess.DEVNULL,
245+
stderr=subprocess.DEVNULL,
246+
)
247+
233248
def read_wifi(self):
234249
try:
235250
# Try to open the existing wifi configuration, but if it doesn't exist we can carry on

0 commit comments

Comments
 (0)