Skip to content

Commit

Permalink
Fixes #35378 - Add systemd first boot service for host provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Dyrkon committed Oct 4, 2023
1 parent df3a0b0 commit 4eca119
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ sed -e 's/DEFAULTKERNEL=kernel-uek/DEFAULTKERNEL=kernel/g' -i /etc/sysconfig/ker

<%= snippet 'insights' if host_param_true?('host_registration_insights') && os_major < 9 -%>

<%= snippet 'first_boot_setup' %>

touch /tmp/foreman_built

chvt 1
Expand All @@ -382,10 +384,7 @@ The last post section halts Anaconda to prevent endless loop in case HTTP reques

<%= snippet 'eject_cdrom' -%>

if test -f /tmp/foreman_built; then
echo "calling home: build is done!"
<%= indent(2, skip1: true) { snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' }) } -%>
else
if ! test -f /tmp/foreman_built; then
echo "calling home: build failed!"
<%= indent(2, skip1: true) { snippet('built', :variables => { :endpoint => 'failed', :method => 'POST', :body_file => '/root/install.post.log' }) } -%>
fi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%#
kind: snippet
name: first_boot_service
model: ProvisioningTemplate
snippet: true
description: |
Post replacement service
-%>
[Unit]
Description=Initial setup callback
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/bin/bash /root/first_boot_script.sh
Type=oneshot
ExecStartPost=/usr/bin/systemctl disable first_boot_service

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%#
kind: snippet
name: first_boot_setup
model: ProvisioningTemplate
snippet: true
description: |
Post replacement for both systemd and non-systemd platforms
-%>
<%
os_major = @host.operatingsystem.major.to_i
rhel_compatible = @host.operatingsystem.family == 'Redhat' && @host.operatingsystem.name != 'Fedora'
has_systemd = (@host.operatingsystem.name == 'Fedora' && os_major >= 20) || (rhel_compatible && os_major >= 7) || (@host.operatingsystem.name == 'Ubuntu' && os_major >= 15) || (@host.operatingsystem.name == 'Debian' && os_major >= 8)
-%>
<% if has_systemd -%>
<%= save_to_file('/etc/systemd/system/first_boot_service.service', snippet('first_boot_service')) %>
<%= save_to_file('/root/first_boot_script.sh', snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' })) %>

systemctl enable first_boot_service
<% else -%>
<%= save_to_file('/root/first_boot_script.sh', snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' }) +
"\nmv /root/first_boot_script.sh /root/first_boot_script.disabled") %>
<%= save_to_file('/etc/init.d/first_boot_setup', snippet('first_boot_initd')) %>

chmod +x /etc/init.d/first_boot_setup
chkconfig --add /etc/init.d/first_boot_setup
chkconfig --level 2345 first_boot_setup on
<% end -%>

chmod +x /root/first_boot_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<%#
kind: snippet
name: first_boot_initd
model: ProvisioningTemplate
snippet: true
description: |
Post replacement initd script
-%>
#! /bin/bash

### BEGIN INIT INFO
# Provides: RedHat
# Required-Start: $local_fs $network
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Execute first boot script
# Description: Execute first boot script which executes after machine is successfully built for the first time.
### END INIT INFO

# Source function library
. /etc/rc.d/init.d/functions

##Service start/stop functions##
start() {
/tmp/first_boot_script
chkconfig --del first_boot_setup
}

start

exit 5
esac
exit $?

0 comments on commit 4eca119

Please sign in to comment.