From a74b0c51adeb8fa4486da71af49d42809cb844eb Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Mon, 15 Jul 2019 12:03:27 -0700 Subject: [PATCH 01/20] [build]: change rc.local to j2 template * prepare for moving systemd service enablement to first boot instead of build time Signed-off-by: Lawrence Lee --- files/build_templates/sonic_debian_extension.j2 | 2 +- files/image_config/platform/{rc.local => rc.local.j2} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename files/image_config/platform/{rc.local => rc.local.j2} (100%) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 3930dcd54e2d..102fcbdaa7a8 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -293,7 +293,7 @@ sudo cp $IMAGE_CONFIGS/fstrim/* $FILESYSTEM_ROOT/etc/systemd/system/ sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable fstrim.timer ## copy platform rc.local -sudo cp $IMAGE_CONFIGS/platform/rc.local $FILESYSTEM_ROOT/etc/ +j2 $IMAGE_CONFIGS/platform/rc.local.j2 | sudo tee $FILESYSTEM_ROOT/etc/rc.local ## copy blacklist file sudo cp $IMAGE_CONFIGS/platform/linux_kernel_bde.conf $FILESYSTEM_ROOT/etc/modprobe.d/ diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local.j2 similarity index 100% rename from files/image_config/platform/rc.local rename to files/image_config/platform/rc.local.j2 From 3babce6588ef0e8fd24d8e70004690ae422d0861 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Tue, 16 Jul 2019 11:08:56 -0700 Subject: [PATCH 02/20] [build]: enable systemd services in rc.local * move enabling of systemd services from sonic_debian_extension to rc.local Signed-off-by: Lawrence Lee --- .../build_templates/sonic_debian_extension.j2 | 13 +------- files/image_config/platform/rc.local.j2 | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 102fcbdaa7a8..cba420c4262e 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -157,18 +157,15 @@ sudo cp -f $IMAGE_CONFIGS/cron.d/* $FILESYSTEM_ROOT/etc/cron.d/ # Copy NTP configuration files and templates sudo cp $IMAGE_CONFIGS/ntp/ntp-config.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable ntp-config.service sudo cp $IMAGE_CONFIGS/ntp/ntp-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/ntp/ntp.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ # Copy warmboot-finalizer files sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/finalize-warmboot.sh $FILESYSTEM_ROOT/usr/local/bin/finalize-warmboot.sh sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/warmboot-finalizer.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable warmboot-finalizer.service # Copy rsyslog configuration files and templates sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable rsyslog-config.service sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.d/* $FILESYSTEM_ROOT/etc/rsyslog.d/ @@ -181,7 +178,6 @@ sudo cp -f $IMAGE_CONFIGS/systemd/journald.conf $FILESYSTEM_ROOT/etc/systemd/ # Copy interfaces configuration files and templates sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable interfaces-config.service sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/interfaces/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ @@ -191,7 +187,6 @@ sudo mkdir -p $FILESYSTEM_ROOT/etc/network/interfaces.d # Copy hostcfgd files sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable hostcfgd.service sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/hostcfgd/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ @@ -203,12 +198,10 @@ sudo cp $BUILD_TEMPLATES/qos_config.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATE # Copy hostname configuration scripts sudo cp $IMAGE_CONFIGS/hostname/hostname-config.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable hostname-config.service sudo cp $IMAGE_CONFIGS/hostname/hostname-config.sh $FILESYSTEM_ROOT/usr/bin/ # Copy updategraph script and service file j2 files/build_templates/updategraph.service.j2 | sudo tee $FILESYSTEM_ROOT/etc/systemd/system/updategraph.service -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable updategraph.service sudo cp $IMAGE_CONFIGS/updategraph/updategraph $FILESYSTEM_ROOT/usr/bin/ {% if enable_dhcp_graph_service == "y" %} sudo bash -c "echo enabled=true > $FILESYSTEM_ROOT/etc/sonic/updategraph.conf" @@ -235,12 +228,10 @@ sudo cp $IMAGE_CONFIGS/sudoers/sudoers.lecture $FILESYSTEM_ROOT/etc/ # Copy control plane ACL management daemon files sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable caclmgrd.service sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd $FILESYSTEM_ROOT/usr/bin/ # Copy process-reboot-cause service files sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause.service $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable process-reboot-cause.service sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause $FILESYSTEM_ROOT/usr/bin/ ## Install package without starting service @@ -290,10 +281,10 @@ sudo rm -f $FILESYSTEM_ROOT/usr/sbin/policy-rc.d # Copy fstrim service and timer file, enable fstrim timer sudo cp $IMAGE_CONFIGS/fstrim/* $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable fstrim.timer ## copy platform rc.local j2 $IMAGE_CONFIGS/platform/rc.local.j2 | sudo tee $FILESYSTEM_ROOT/etc/rc.local +sudo chmod +x $FILESYSTEM_ROOT/etc/rc.local ## copy blacklist file sudo cp $IMAGE_CONFIGS/platform/linux_kernel_bde.conf $FILESYSTEM_ROOT/etc/modprobe.d/ @@ -331,7 +322,6 @@ sudo cp {{script}} $FILESYSTEM_ROOT/usr/bin/ {% for service in installer_services.split(' ') -%} if [ -f {{service}} ]; then sudo cp {{service}} $FILESYSTEM_ROOT/etc/systemd/system/ - sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable {{service}} fi {% endfor %} sudo LANG=C chroot $FILESYSTEM_ROOT fuser -km /sys || true @@ -345,7 +335,6 @@ sudo LANG=C cp $SCRIPTS_DIR/syncd.sh $FILESYSTEM_ROOT/usr/local/bin/syncd.sh # Copy systemd timer configuration # It implements delayed start of services sudo cp $BUILD_TEMPLATES/snmp.timer $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable snmp.timer sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get purge -y python-dev sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get clean -y diff --git a/files/image_config/platform/rc.local.j2 b/files/image_config/platform/rc.local.j2 index 76fe390be3cd..c744679f4911 100755 --- a/files/image_config/platform/rc.local.j2 +++ b/files/image_config/platform/rc.local.j2 @@ -319,6 +319,36 @@ if [ -f $FIRST_BOOT_FILE ]; then mv /host/grub.cfg /host/grub/grub.cfg fi + # Enable appropriate system services + + systemctl enable ntp-config.service + + systemctl enable warmboot-finalizer.service + + systemctl enable rsyslog-config.service + + systemctl enable interfaces-config.service + + systemctl enable hostcnfgd.service + + systemctl enable hostname-config.service + + systemctl enable updategraph.service + + systemctl enable caclmgrd.service + + systemctl enable process-reboot-cause.service + + systemctl enable fstrim.timer + + {% for service in installer_services.split(' ') -%} + if [ -f "/etc/systemd/system/{{service}}" ]; then + systemctl enable {{service}} + fi + {% endfor %} + + systemctl enable snmp.timer + firsttime_exit fi From 669895a7bc757e725fc1e5e90504b01c222f61b5 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Wed, 17 Jul 2019 12:23:41 -0700 Subject: [PATCH 03/20] [rc.local]: restart systemd daemon after first boot Signed-off-by: Lawrence Lee --- files/image_config/platform/rc.local.j2 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/files/image_config/platform/rc.local.j2 b/files/image_config/platform/rc.local.j2 index c744679f4911..5a0d25aa7b69 100755 --- a/files/image_config/platform/rc.local.j2 +++ b/files/image_config/platform/rc.local.j2 @@ -349,6 +349,8 @@ if [ -f $FIRST_BOOT_FILE ]; then systemctl enable snmp.timer + systemctl daemon-reexec + firsttime_exit fi From 4000ef318f0359fd33443b3d9aa43f7d37b9627e Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Thu, 18 Jul 2019 17:21:25 -0700 Subject: [PATCH 04/20] [files]: create generator for systemd services * remove systemd service enablement from rc.local * manually create symlinks for sonic with systemd generator Signed-off-by: Lawrence Lee --- .../build_templates/sonic_debian_extension.j2 | 5 ++ files/image_config/platform/rc.local.j2 | 33 --------- .../systemd/systemd-sonic-generator.j2 | 68 +++++++++++++++++++ 3 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 files/image_config/systemd/systemd-sonic-generator.j2 diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index cba420c4262e..36cbf49a54b1 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -286,6 +286,11 @@ sudo cp $IMAGE_CONFIGS/fstrim/* $FILESYSTEM_ROOT/etc/systemd/system/ j2 $IMAGE_CONFIGS/platform/rc.local.j2 | sudo tee $FILESYSTEM_ROOT/etc/rc.local sudo chmod +x $FILESYSTEM_ROOT/etc/rc.local +## Copy sonic generator + +j2 $IMAGE_CONFIGS/systemd/systemd-sonic-generator.j2 | sudo tee $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator +sudo chmod +x $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator + ## copy blacklist file sudo cp $IMAGE_CONFIGS/platform/linux_kernel_bde.conf $FILESYSTEM_ROOT/etc/modprobe.d/ diff --git a/files/image_config/platform/rc.local.j2 b/files/image_config/platform/rc.local.j2 index 5a0d25aa7b69..bdfb714d394e 100755 --- a/files/image_config/platform/rc.local.j2 +++ b/files/image_config/platform/rc.local.j2 @@ -319,39 +319,6 @@ if [ -f $FIRST_BOOT_FILE ]; then mv /host/grub.cfg /host/grub/grub.cfg fi - # Enable appropriate system services - - systemctl enable ntp-config.service - - systemctl enable warmboot-finalizer.service - - systemctl enable rsyslog-config.service - - systemctl enable interfaces-config.service - - systemctl enable hostcnfgd.service - - systemctl enable hostname-config.service - - systemctl enable updategraph.service - - systemctl enable caclmgrd.service - - systemctl enable process-reboot-cause.service - - systemctl enable fstrim.timer - - {% for service in installer_services.split(' ') -%} - if [ -f "/etc/systemd/system/{{service}}" ]; then - systemctl enable {{service}} - fi - {% endfor %} - - systemctl enable snmp.timer - - systemctl daemon-reexec - - firsttime_exit fi exit 0 diff --git a/files/image_config/systemd/systemd-sonic-generator.j2 b/files/image_config/systemd/systemd-sonic-generator.j2 new file mode 100644 index 000000000000..f7cd6a4d6eeb --- /dev/null +++ b/files/image_config/systemd/systemd-sonic-generator.j2 @@ -0,0 +1,68 @@ +#!/usr/bin/python + +import os +import sys + +UNIT_FILE_PREFIX = '/etc/systemd/system/' + +def parse_install_targets(unit_file): + '''Return installation information for a unit file.''' + + install_targets_raw = [] + + install_targets = [] + + with open(os.path.join(UNIT_FILE_PREFIX, unit_file), 'r') as f: + found_install = False + for line in f: + if '[Install]' in line: + found_install = True + elif found_install: + install_targets_raw.append(line) + + for line in install_targets_raw: + install_option = line.split('=')[0] + targets = line.split('=')[1].split() + if 'WantedBy' in install_option: + for target in targets: + install_targets.append(target + '.wants') + elif 'RequiredBy' in install_option: + for target in targets: + install_targets.append(target + '.requires') + + return install_targets + + +def install_unit(unit_file, install_dir, targets, instance = None): + '''Create symlinks for a unit file installation''' + + for target in targets: + if instance: + # Insert the instance into the symlink destination + tmp = unit_file.split('@') + unit_file_instance = '{}@{}{}'.format(tmp[0], instance, tmp[1]) + dst = os.path.join(install_dir, target, unit_file_instance) + else: + dst = os.path.join(install_dir, target, unit_file) + src = os.path.join(UNIT_FILE_PREFIX, unit_file) + os.symlink(src, dst) + + +def main(install_dir): + standalone_services = "ntp-config.service warmboot-finalizer.service rsyslog-config.service hostcnfgd.service hostname-config.service updategraph.service caclmgrd.service process-reboot-cause.service fstrim.timer snmp.timer".split() + + for service in standalone_services: + targets = parse_install_targets(service) + install_unit(service, install_dir, targets) + + {% for service in installer_services.split(' ') %} + if os.path.exists(os.path.join(UNIT_FILE_PREFIX, {{service}})): + targets = parse_install_targets({{service}}) + install_unit({{service}}, install_dir, targets) + {% endfor %} + + + +if __name__ == '__main__': + install_dir = sys.argv[1] + main(install_dir) From c4e9a74eeec710ef5dfe819770057189b551fa32 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Fri, 19 Jul 2019 09:27:09 -0700 Subject: [PATCH 05/20] Revert "[build]: change rc.local to j2 template" This reverts commit 5fbf30d2074bcd25e08f39132679f4d1ed6d0b54. --- files/build_templates/sonic_debian_extension.j2 | 4 +--- files/image_config/platform/{rc.local.j2 => rc.local} | 0 2 files changed, 1 insertion(+), 3 deletions(-) rename files/image_config/platform/{rc.local.j2 => rc.local} (100%) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 36cbf49a54b1..8981b2a97962 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -283,11 +283,9 @@ sudo rm -f $FILESYSTEM_ROOT/usr/sbin/policy-rc.d sudo cp $IMAGE_CONFIGS/fstrim/* $FILESYSTEM_ROOT/etc/systemd/system/ ## copy platform rc.local -j2 $IMAGE_CONFIGS/platform/rc.local.j2 | sudo tee $FILESYSTEM_ROOT/etc/rc.local -sudo chmod +x $FILESYSTEM_ROOT/etc/rc.local +sudo cp $IMAGE_CONFIGS/platform/rc.local $FILESYSTEM_ROOT/etc/ ## Copy sonic generator - j2 $IMAGE_CONFIGS/systemd/systemd-sonic-generator.j2 | sudo tee $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator sudo chmod +x $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator diff --git a/files/image_config/platform/rc.local.j2 b/files/image_config/platform/rc.local similarity index 100% rename from files/image_config/platform/rc.local.j2 rename to files/image_config/platform/rc.local From 5094522be849436cb5f3146a1cbef7634ebd2e7e Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Fri, 19 Jul 2019 09:28:52 -0700 Subject: [PATCH 06/20] [files]: revert changes to rc.local * restores original version of rc.local Signed-off-by: Lawrence Lee --- files/image_config/platform/rc.local | 1 + 1 file changed, 1 insertion(+) diff --git a/files/image_config/platform/rc.local b/files/image_config/platform/rc.local index bdfb714d394e..76fe390be3cd 100755 --- a/files/image_config/platform/rc.local +++ b/files/image_config/platform/rc.local @@ -319,6 +319,7 @@ if [ -f $FIRST_BOOT_FILE ]; then mv /host/grub.cfg /host/grub/grub.cfg fi + firsttime_exit fi exit 0 From a0a2d3ee5d26bf546d03f4f11b428912f3462cff Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Fri, 19 Jul 2019 14:10:43 -0700 Subject: [PATCH 07/20] [files]: update sonic generator documentation Signed-off-by: Lawrence Lee --- .../systemd/systemd-sonic-generator.j2 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/files/image_config/systemd/systemd-sonic-generator.j2 b/files/image_config/systemd/systemd-sonic-generator.j2 index f7cd6a4d6eeb..bc46838f8374 100644 --- a/files/image_config/systemd/systemd-sonic-generator.j2 +++ b/files/image_config/systemd/systemd-sonic-generator.j2 @@ -6,7 +6,12 @@ import sys UNIT_FILE_PREFIX = '/etc/systemd/system/' def parse_install_targets(unit_file): - '''Return installation information for a unit file.''' + ''' + Return installation information for a unit file. + + Parses the information in the [Install] section of a given unit file + to determine which directories to install the unit in + ''' install_targets_raw = [] @@ -34,7 +39,13 @@ def parse_install_targets(unit_file): def install_unit(unit_file, install_dir, targets, instance = None): - '''Create symlinks for a unit file installation''' + ''' + Create symlinks for a unit file installation + + For a given unit file and its installation targets, + create the appropriate symlinks in the target directory + to enable the unit and have it started when systemd starts + ''' for target in targets: if instance: From 4cfbed26e144bb283abf2203901fbb961458b5ac Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Fri, 19 Jul 2019 14:11:41 -0700 Subject: [PATCH 08/20] [files]: make sonic generator more robust * create installation directory if it doesn't exist already * check for existing symlink before creating a new one Signed-off-by: Lawrence Lee --- files/image_config/systemd/systemd-sonic-generator.j2 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/files/image_config/systemd/systemd-sonic-generator.j2 b/files/image_config/systemd/systemd-sonic-generator.j2 index bc46838f8374..84a35e2f2718 100644 --- a/files/image_config/systemd/systemd-sonic-generator.j2 +++ b/files/image_config/systemd/systemd-sonic-generator.j2 @@ -55,8 +55,12 @@ def install_unit(unit_file, install_dir, targets, instance = None): dst = os.path.join(install_dir, target, unit_file_instance) else: dst = os.path.join(install_dir, target, unit_file) + target_dir = os.path.join(install_dir, target) + if not os.path.exists(target_dir): + os.makedirs(target_dir) src = os.path.join(UNIT_FILE_PREFIX, unit_file) - os.symlink(src, dst) + if not os.path.exists(dst): + os.symlink(src, dst) def main(install_dir): From 5bce998fdd7e6e0ca11ddcbdd692aa32c512ffdd Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Fri, 19 Jul 2019 14:12:42 -0700 Subject: [PATCH 09/20] [files]: enable all services in sonic generator * store service list in environment variable in sonic_debian_extension * read all services from environment variable in systemd sonic generator Signed-off-by: Lawrence Lee --- .../build_templates/sonic_debian_extension.j2 | 21 +++++++++++++++---- .../systemd/systemd-sonic-generator.j2 | 13 ++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 8981b2a97962..65ba71d31594 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -157,18 +157,21 @@ sudo cp -f $IMAGE_CONFIGS/cron.d/* $FILESYSTEM_ROOT/etc/cron.d/ # Copy NTP configuration files and templates sudo cp $IMAGE_CONFIGS/ntp/ntp-config.service $FILESYSTEM_ROOT/etc/systemd/system/ +GENERATOR_SERVICES="$GENERATOR_SERVICES ntp-config.service" sudo cp $IMAGE_CONFIGS/ntp/ntp-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/ntp/ntp.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ # Copy warmboot-finalizer files sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/finalize-warmboot.sh $FILESYSTEM_ROOT/usr/local/bin/finalize-warmboot.sh sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/warmboot-finalizer.service $FILESYSTEM_ROOT/etc/systemd/system/ +GENERATOR_SERVICES="$GENERATOR_SERVICES warmboot-finalizer.service" # Copy rsyslog configuration files and templates sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.service $FILESYSTEM_ROOT/etc/systemd/system/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.d/* $FILESYSTEM_ROOT/etc/rsyslog.d/ +GENERATOR_SERVICES="rsyslog-config.service" # Copy logrotate.d configuration files sudo cp -f $IMAGE_CONFIGS/logrotate/logrotate.d/* $FILESYSTEM_ROOT/etc/logrotate.d/ @@ -180,6 +183,7 @@ sudo cp -f $IMAGE_CONFIGS/systemd/journald.conf $FILESYSTEM_ROOT/etc/systemd/ sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.service $FILESYSTEM_ROOT/etc/systemd/system/ sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/interfaces/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ +GENERATOR_SERVICES="$GENERATOR_SERVICES interfaces-config.service" # Copy initial interfaces configuration file, will be overwritten on first boot sudo cp $IMAGE_CONFIGS/interfaces/init_interfaces $FILESYSTEM_ROOT/etc/network/interfaces @@ -187,6 +191,7 @@ sudo mkdir -p $FILESYSTEM_ROOT/etc/network/interfaces.d # Copy hostcfgd files sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd.service $FILESYSTEM_ROOT/etc/systemd/system/ +GENERATOR_SERVICES="$GENERATOR_SERVICES hostcfgd.service" sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/hostcfgd/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ @@ -198,11 +203,13 @@ sudo cp $BUILD_TEMPLATES/qos_config.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATE # Copy hostname configuration scripts sudo cp $IMAGE_CONFIGS/hostname/hostname-config.service $FILESYSTEM_ROOT/etc/systemd/system/ +GENERATOR_SERVICES="$GENERATOR_SERVICES hostname-config.service" sudo cp $IMAGE_CONFIGS/hostname/hostname-config.sh $FILESYSTEM_ROOT/usr/bin/ # Copy updategraph script and service file j2 files/build_templates/updategraph.service.j2 | sudo tee $FILESYSTEM_ROOT/etc/systemd/system/updategraph.service sudo cp $IMAGE_CONFIGS/updategraph/updategraph $FILESYSTEM_ROOT/usr/bin/ +GENERATOR_SERVICES="$GENERATOR_SERVICES updategraph.service" {% if enable_dhcp_graph_service == "y" %} sudo bash -c "echo enabled=true > $FILESYSTEM_ROOT/etc/sonic/updategraph.conf" sudo bash -c "echo src=dhcp >> $FILESYSTEM_ROOT/etc/sonic/updategraph.conf" @@ -228,10 +235,12 @@ sudo cp $IMAGE_CONFIGS/sudoers/sudoers.lecture $FILESYSTEM_ROOT/etc/ # Copy control plane ACL management daemon files sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd.service $FILESYSTEM_ROOT/etc/systemd/system/ +GENERATOR_SERVICES="$GENERATOR_SERVICES caclmgrd.service" sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd $FILESYSTEM_ROOT/usr/bin/ # Copy process-reboot-cause service files sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause.service $FILESYSTEM_ROOT/etc/systemd/system/ +GENERATOR_SERVICES="$GENERATOR_SERVICES process-reboot-cause.service" sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause $FILESYSTEM_ROOT/usr/bin/ ## Install package without starting service @@ -281,14 +290,11 @@ sudo rm -f $FILESYSTEM_ROOT/usr/sbin/policy-rc.d # Copy fstrim service and timer file, enable fstrim timer sudo cp $IMAGE_CONFIGS/fstrim/* $FILESYSTEM_ROOT/etc/systemd/system/ +sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable fstrim.timer ## copy platform rc.local sudo cp $IMAGE_CONFIGS/platform/rc.local $FILESYSTEM_ROOT/etc/ -## Copy sonic generator -j2 $IMAGE_CONFIGS/systemd/systemd-sonic-generator.j2 | sudo tee $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator -sudo chmod +x $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator - ## copy blacklist file sudo cp $IMAGE_CONFIGS/platform/linux_kernel_bde.conf $FILESYSTEM_ROOT/etc/modprobe.d/ @@ -325,12 +331,18 @@ sudo cp {{script}} $FILESYSTEM_ROOT/usr/bin/ {% for service in installer_services.split(' ') -%} if [ -f {{service}} ]; then sudo cp {{service}} $FILESYSTEM_ROOT/etc/systemd/system/ + GENERATOR_SERVICES="$GENERATOR_SERVICES {{service}}" fi {% endfor %} sudo LANG=C chroot $FILESYSTEM_ROOT fuser -km /sys || true sudo LANG=C chroot $FILESYSTEM_ROOT umount -lf /sys {% endif %} +## Copy sonic generator +export generator_services=$GENERATOR_SERVICES +j2 $IMAGE_CONFIGS/systemd/systemd-sonic-generator.j2 | sudo tee $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator +sudo chmod +x $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator + # Copy swss and syncd service script sudo LANG=C cp $SCRIPTS_DIR/swss.sh $FILESYSTEM_ROOT/usr/local/bin/swss.sh sudo LANG=C cp $SCRIPTS_DIR/syncd.sh $FILESYSTEM_ROOT/usr/local/bin/syncd.sh @@ -338,6 +350,7 @@ sudo LANG=C cp $SCRIPTS_DIR/syncd.sh $FILESYSTEM_ROOT/usr/local/bin/syncd.sh # Copy systemd timer configuration # It implements delayed start of services sudo cp $BUILD_TEMPLATES/snmp.timer $FILESYSTEM_ROOT/etc/systemd/system/ +sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable snmp.timer sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get purge -y python-dev sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get clean -y diff --git a/files/image_config/systemd/systemd-sonic-generator.j2 b/files/image_config/systemd/systemd-sonic-generator.j2 index 84a35e2f2718..b809b956bd3d 100644 --- a/files/image_config/systemd/systemd-sonic-generator.j2 +++ b/files/image_config/systemd/systemd-sonic-generator.j2 @@ -64,16 +64,11 @@ def install_unit(unit_file, install_dir, targets, instance = None): def main(install_dir): - standalone_services = "ntp-config.service warmboot-finalizer.service rsyslog-config.service hostcnfgd.service hostname-config.service updategraph.service caclmgrd.service process-reboot-cause.service fstrim.timer snmp.timer".split() - for service in standalone_services: - targets = parse_install_targets(service) - install_unit(service, install_dir, targets) - - {% for service in installer_services.split(' ') %} - if os.path.exists(os.path.join(UNIT_FILE_PREFIX, {{service}})): - targets = parse_install_targets({{service}}) - install_unit({{service}}, install_dir, targets) + {% for service in generator_services.split(' ') %} + if os.path.exists(os.path.join(UNIT_FILE_PREFIX, '{{service}}')): + targets = parse_install_targets('{{service}}') + install_unit('{{service}}', install_dir, targets) {% endfor %} From dfd753d6786dfddcb5b574d2262793b31e238d42 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Fri, 19 Jul 2019 15:53:38 -0700 Subject: [PATCH 10/20] [files]: fix typo in generator services * stop ntp-config and warmboot-finalizer from being left out of generator_services in sonic_debian_extension Signed-off-by: Lawrence Lee --- files/build_templates/sonic_debian_extension.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 65ba71d31594..d5f57c7298b4 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -171,7 +171,7 @@ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.service $FILESYSTEM_ROOT/etc/syst sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.d/* $FILESYSTEM_ROOT/etc/rsyslog.d/ -GENERATOR_SERVICES="rsyslog-config.service" +GENERATOR_SERVICES="$GENERATOR_SERVICES rsyslog-config.service" # Copy logrotate.d configuration files sudo cp -f $IMAGE_CONFIGS/logrotate/logrotate.d/* $FILESYSTEM_ROOT/etc/logrotate.d/ From 1f809d087592daeed5853878f9d89793d675da21 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Tue, 23 Jul 2019 11:02:51 -0700 Subject: [PATCH 11/20] [files]: rewrite sonic systemd generator in C * Add rule and Makefile to build generator binary * Remove Python generator Signed-off-by: Lawrence Lee --- .../build_templates/sonic_debian_extension.j2 | 30 +- .../systemd/systemd-sonic-generator.j2 | 78 ------ rules/systemd-sonic-generator.mk | 3 + src/systemd-sonic-generator/Makefile | 9 + .../systemd-sonic-generator.c | 264 ++++++++++++++++++ 5 files changed, 291 insertions(+), 93 deletions(-) delete mode 100644 files/image_config/systemd/systemd-sonic-generator.j2 create mode 100644 rules/systemd-sonic-generator.mk create mode 100644 src/systemd-sonic-generator/Makefile create mode 100644 src/systemd-sonic-generator/systemd-sonic-generator.c diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index d5f57c7298b4..71ed08c1c75e 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -37,6 +37,8 @@ FILESYSTEM_ROOT_USR_SHARE="$FILESYSTEM_ROOT_USR/share" FILESYSTEM_ROOT_USR_SHARE_SONIC="$FILESYSTEM_ROOT_USR_SHARE/sonic" FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES="$FILESYSTEM_ROOT_USR_SHARE_SONIC/templates" +GENERATED_SERVICE_FILE="$FILESYSTEM_ROOT/etc/sonic/generated_services.conf" + clean_sys() { sudo umount $FILESYSTEM_ROOT/sys/fs/cgroup/* \ $FILESYSTEM_ROOT/sys/fs/cgroup \ @@ -157,21 +159,21 @@ sudo cp -f $IMAGE_CONFIGS/cron.d/* $FILESYSTEM_ROOT/etc/cron.d/ # Copy NTP configuration files and templates sudo cp $IMAGE_CONFIGS/ntp/ntp-config.service $FILESYSTEM_ROOT/etc/systemd/system/ -GENERATOR_SERVICES="$GENERATOR_SERVICES ntp-config.service" +echo "ntp-config.service" | sudo tee -a $GENERATED_SERVICE_FILE sudo cp $IMAGE_CONFIGS/ntp/ntp-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/ntp/ntp.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ # Copy warmboot-finalizer files sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/finalize-warmboot.sh $FILESYSTEM_ROOT/usr/local/bin/finalize-warmboot.sh sudo LANG=C cp $IMAGE_CONFIGS/warmboot-finalizer/warmboot-finalizer.service $FILESYSTEM_ROOT/etc/systemd/system/ -GENERATOR_SERVICES="$GENERATOR_SERVICES warmboot-finalizer.service" +echo "warmboot-finalizer.service" | sudo tee -a $GENERATED_SERVICE_FILE # Copy rsyslog configuration files and templates sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.service $FILESYSTEM_ROOT/etc/systemd/system/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ sudo cp $IMAGE_CONFIGS/rsyslog/rsyslog.d/* $FILESYSTEM_ROOT/etc/rsyslog.d/ -GENERATOR_SERVICES="$GENERATOR_SERVICES rsyslog-config.service" +echo "rsyslog-config.service" | sudo tee -a $GENERATED_SERVICE_FILE # Copy logrotate.d configuration files sudo cp -f $IMAGE_CONFIGS/logrotate/logrotate.d/* $FILESYSTEM_ROOT/etc/logrotate.d/ @@ -183,7 +185,7 @@ sudo cp -f $IMAGE_CONFIGS/systemd/journald.conf $FILESYSTEM_ROOT/etc/systemd/ sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.service $FILESYSTEM_ROOT/etc/systemd/system/ sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.sh $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/interfaces/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ -GENERATOR_SERVICES="$GENERATOR_SERVICES interfaces-config.service" +echo "interfaces-config.service" | sudo tee -a $GENERATED_SERVICE_FILE # Copy initial interfaces configuration file, will be overwritten on first boot sudo cp $IMAGE_CONFIGS/interfaces/init_interfaces $FILESYSTEM_ROOT/etc/network/interfaces @@ -191,7 +193,7 @@ sudo mkdir -p $FILESYSTEM_ROOT/etc/network/interfaces.d # Copy hostcfgd files sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd.service $FILESYSTEM_ROOT/etc/systemd/system/ -GENERATOR_SERVICES="$GENERATOR_SERVICES hostcfgd.service" +echo "hostcfgd.service" | sudo tee -a $GENERATED_SERVICE_FILE sudo cp $IMAGE_CONFIGS/hostcfgd/hostcfgd $FILESYSTEM_ROOT/usr/bin/ sudo cp $IMAGE_CONFIGS/hostcfgd/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/ @@ -203,13 +205,13 @@ sudo cp $BUILD_TEMPLATES/qos_config.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATE # Copy hostname configuration scripts sudo cp $IMAGE_CONFIGS/hostname/hostname-config.service $FILESYSTEM_ROOT/etc/systemd/system/ -GENERATOR_SERVICES="$GENERATOR_SERVICES hostname-config.service" +echo "hostname-config.service" | sudo tee -a $GENERATED_SERVICE_FILE sudo cp $IMAGE_CONFIGS/hostname/hostname-config.sh $FILESYSTEM_ROOT/usr/bin/ # Copy updategraph script and service file j2 files/build_templates/updategraph.service.j2 | sudo tee $FILESYSTEM_ROOT/etc/systemd/system/updategraph.service sudo cp $IMAGE_CONFIGS/updategraph/updategraph $FILESYSTEM_ROOT/usr/bin/ -GENERATOR_SERVICES="$GENERATOR_SERVICES updategraph.service" +echo "updategraph.service" | sudo tee -a $GENERATED_SERVICE_FILE {% if enable_dhcp_graph_service == "y" %} sudo bash -c "echo enabled=true > $FILESYSTEM_ROOT/etc/sonic/updategraph.conf" sudo bash -c "echo src=dhcp >> $FILESYSTEM_ROOT/etc/sonic/updategraph.conf" @@ -235,12 +237,12 @@ sudo cp $IMAGE_CONFIGS/sudoers/sudoers.lecture $FILESYSTEM_ROOT/etc/ # Copy control plane ACL management daemon files sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd.service $FILESYSTEM_ROOT/etc/systemd/system/ -GENERATOR_SERVICES="$GENERATOR_SERVICES caclmgrd.service" +echo "caclmgrd.service" | sudo tee -a $GENERATED_SERVICE_FILE sudo cp $IMAGE_CONFIGS/caclmgrd/caclmgrd $FILESYSTEM_ROOT/usr/bin/ # Copy process-reboot-cause service files sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause.service $FILESYSTEM_ROOT/etc/systemd/system/ -GENERATOR_SERVICES="$GENERATOR_SERVICES process-reboot-cause.service" +echo "process-reboot-cause.service" | sudo tee -a $GENERATED_SERVICE_FILE sudo cp $IMAGE_CONFIGS/process-reboot-cause/process-reboot-cause $FILESYSTEM_ROOT/usr/bin/ ## Install package without starting service @@ -331,17 +333,15 @@ sudo cp {{script}} $FILESYSTEM_ROOT/usr/bin/ {% for service in installer_services.split(' ') -%} if [ -f {{service}} ]; then sudo cp {{service}} $FILESYSTEM_ROOT/etc/systemd/system/ - GENERATOR_SERVICES="$GENERATOR_SERVICES {{service}}" + echo "{{service}}" | sudo tee -a $GENERATED_SERVICE_FILE fi {% endfor %} sudo LANG=C chroot $FILESYSTEM_ROOT fuser -km /sys || true sudo LANG=C chroot $FILESYSTEM_ROOT umount -lf /sys {% endif %} -## Copy sonic generator -export generator_services=$GENERATOR_SERVICES -j2 $IMAGE_CONFIGS/systemd/systemd-sonic-generator.j2 | sudo tee $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator -sudo chmod +x $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator +## Copy systemd generator for sonic services +sudo LANG=C cp $files_path/systemd-sonic-generator $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator # Copy swss and syncd service script sudo LANG=C cp $SCRIPTS_DIR/swss.sh $FILESYSTEM_ROOT/usr/local/bin/swss.sh @@ -350,7 +350,7 @@ sudo LANG=C cp $SCRIPTS_DIR/syncd.sh $FILESYSTEM_ROOT/usr/local/bin/syncd.sh # Copy systemd timer configuration # It implements delayed start of services sudo cp $BUILD_TEMPLATES/snmp.timer $FILESYSTEM_ROOT/etc/systemd/system/ -sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable snmp.timer +sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable snmp.timer sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get purge -y python-dev sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get clean -y diff --git a/files/image_config/systemd/systemd-sonic-generator.j2 b/files/image_config/systemd/systemd-sonic-generator.j2 deleted file mode 100644 index b809b956bd3d..000000000000 --- a/files/image_config/systemd/systemd-sonic-generator.j2 +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/python - -import os -import sys - -UNIT_FILE_PREFIX = '/etc/systemd/system/' - -def parse_install_targets(unit_file): - ''' - Return installation information for a unit file. - - Parses the information in the [Install] section of a given unit file - to determine which directories to install the unit in - ''' - - install_targets_raw = [] - - install_targets = [] - - with open(os.path.join(UNIT_FILE_PREFIX, unit_file), 'r') as f: - found_install = False - for line in f: - if '[Install]' in line: - found_install = True - elif found_install: - install_targets_raw.append(line) - - for line in install_targets_raw: - install_option = line.split('=')[0] - targets = line.split('=')[1].split() - if 'WantedBy' in install_option: - for target in targets: - install_targets.append(target + '.wants') - elif 'RequiredBy' in install_option: - for target in targets: - install_targets.append(target + '.requires') - - return install_targets - - -def install_unit(unit_file, install_dir, targets, instance = None): - ''' - Create symlinks for a unit file installation - - For a given unit file and its installation targets, - create the appropriate symlinks in the target directory - to enable the unit and have it started when systemd starts - ''' - - for target in targets: - if instance: - # Insert the instance into the symlink destination - tmp = unit_file.split('@') - unit_file_instance = '{}@{}{}'.format(tmp[0], instance, tmp[1]) - dst = os.path.join(install_dir, target, unit_file_instance) - else: - dst = os.path.join(install_dir, target, unit_file) - target_dir = os.path.join(install_dir, target) - if not os.path.exists(target_dir): - os.makedirs(target_dir) - src = os.path.join(UNIT_FILE_PREFIX, unit_file) - if not os.path.exists(dst): - os.symlink(src, dst) - - -def main(install_dir): - - {% for service in generator_services.split(' ') %} - if os.path.exists(os.path.join(UNIT_FILE_PREFIX, '{{service}}')): - targets = parse_install_targets('{{service}}') - install_unit('{{service}}', install_dir, targets) - {% endfor %} - - - -if __name__ == '__main__': - install_dir = sys.argv[1] - main(install_dir) diff --git a/rules/systemd-sonic-generator.mk b/rules/systemd-sonic-generator.mk new file mode 100644 index 000000000000..d23583eaf4f8 --- /dev/null +++ b/rules/systemd-sonic-generator.mk @@ -0,0 +1,3 @@ +SYSTEMD_SONIC_GENERATOR = systemd-sonic-generator +$(SYSTEMD_SONIC_GENERATOR)_SRC_PATH = $(SRC_PATH)/systemd-sonic-generator +SONIC_MAKE_FILES += $(SYSTEMD_SONIC_GENERATOR) diff --git a/src/systemd-sonic-generator/Makefile b/src/systemd-sonic-generator/Makefile new file mode 100644 index 000000000000..6c89b41047f0 --- /dev/null +++ b/src/systemd-sonic-generator/Makefile @@ -0,0 +1,9 @@ +CC=gcc +CFLAGS=-std=gnu99 + +MAIN_TARGET = systemd-sonic-generator + +$(addprefix $(DEST)/, $(MAIN_TARGET)): $(DEST)/% : + rm -f ./systemd-sonic-generator + + $(CC) $(CFLAGS) -o $@ systemd-sonic-generator.c diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c new file mode 100644 index 000000000000..3fc126e1eddd --- /dev/null +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -0,0 +1,264 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_NUM_TARGETS 5 +#define MAX_NUM_UNITS 128 + +static const char* UNIT_FILE_PREFIX = "/etc/systemd/system/"; +static const char* CONFIG_FILE = "/etc/sonic/generated_services.conf"; + + +void strip_trailing_newline(char* str) { + /*** + Strips trailing newline from a string if it exists + ***/ + + size_t l = strlen(str); + if (l > 0 && str[l-1] == '\n') + str[l-1] = '\0'; +} + + +static int get_target_lines(char* unit_file, char* target_lines[]) { + /*** + Gets installation information for a given unit file + + Returns lines in the [Install] section of a unit file + ***/ + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + bool found_install; + int num_target_lines; + + + fp = fopen(unit_file, "r"); + + if (fp == NULL) { + exit(EXIT_FAILURE); + } + + found_install = false; + num_target_lines = 0; + + while ((read = getline(&line, &len, fp)) != -1 ) { + // Assumes that [Install] is the last section of the unit file + if (strstr(line, "[Install]") != NULL) { + found_install = true; + } + else if (found_install) { + target_lines[num_target_lines] = strdup(line); + num_target_lines++; + } + } + + free(line); + + fclose(fp); + + return num_target_lines; +} + +static int get_install_targets_from_line(char* target_string, char* suffix, char* targets[]) { + /*** + Helper fuction for get_install_targets + + Given a space delimited string of target directories and a suffix, + puts each target directory plus the suffix into the targets array + ***/ + char* token; + char* target; + char final_target[PATH_MAX]; + int num_targets = 0; + + while ((token = strtok_r(target_string, " ", &target_string))) { + target = strdup(token); + strip_trailing_newline(target); + + strcpy(final_target, target); + strcat(final_target, suffix); + + free(target); + + targets[num_targets] = strdup(final_target); + num_targets++; + } + return num_targets; +} + +static int get_install_targets(char* unit_file, char* targets[]) { + /*** + Returns install targets for a unit file + + Parses the information in the [Install] section of a given + unit file to determine which directories to install the unit in + ***/ + char file_path[PATH_MAX]; + char *target_lines[5]; + int num_target_lines; + int num_targets; + char* token; + char* line = NULL; + bool first; + char* target_suffix; + + strcpy(file_path, UNIT_FILE_PREFIX); + strcat(file_path, unit_file); + + num_target_lines = get_target_lines(file_path, target_lines); + + num_targets = 0; + + for (int i = 0; i < num_target_lines; i++) { + line = target_lines[i]; + first = true; + + while ((token = strtok_r(line, "=", &line))) { + if (first) { + first = false; + + if (strstr(token, "RequiredBy") != NULL) { + target_suffix = ".requires"; + } + else if (strstr(token, "WantedBy") != NULL) { + target_suffix = ".wants"; + } + } + else { + num_targets += get_install_targets_from_line(token, target_suffix, &targets[num_targets]); + } + } + free(target_lines[i]); + } + return num_targets; +} + + +static int get_unit_files(char* unit_files[]){ + /*** + Reads a list of unit files to be installed from /etc/sonic/generated_services.conf + ***/ + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen(CONFIG_FILE, "r"); + + if (fp == NULL) { + exit(EXIT_FAILURE); + } + + int num_unit_files = 0; + + while ((read = getline(&line, &len, fp)) != -1) { + strip_trailing_newline(line); + unit_files[num_unit_files] = strdup(line); + num_unit_files++; + } + + free(line); + + fclose(fp); + + return num_unit_files; + +} + + +static int install_unit_file(char* unit_file, char* target, char* install_dir) { + /*** + Creates a symlink for a unit file installation + + For a given unit file and target directory, + create the appropriate symlink in the target directory + to enable the unit and have it started by Systemd + ***/ + char final_install_dir[PATH_MAX]; + char src_path[PATH_MAX]; + char dest_path[PATH_MAX]; + struct stat st; + int r; + + assert(unit_file); + assert(target); + + strcpy(final_install_dir, install_dir); + strcat(final_install_dir, target); + + strcpy(src_path, UNIT_FILE_PREFIX); + strcat(src_path, unit_file); + + if (stat(final_install_dir, &st) == -1) { + // If doesn't exist, create + mkdir(final_install_dir, 0755); + } + else if (S_ISREG(st.st_mode)) { + // If is regular file, remove and create + remove(final_install_dir); + mkdir(final_install_dir, 0755); + } + else if (S_ISDIR(st.st_mode)) { + // If directory, verify correct permissions + chmod(final_install_dir, 0755); + } + + + strcpy(dest_path, final_install_dir); + strcat(dest_path, "/"); + strcat(dest_path, unit_file); + + r = symlink(src_path, dest_path); + + if (r < 0) { + if (errno == EEXIST) + return 0; + + return 1; + } + + return 0; + +} + + +int main(int argc, char **argv) { + char* unit_files[MAX_NUM_UNITS]; + char install_dir[PATH_MAX]; + char* targets[MAX_NUM_TARGETS]; + int num_unit_files; + int num_targets; + + if (argc <= 1) { + fputs("Installation directory required as argument\n", stderr); + return 1; + } + + strcpy(install_dir, argv[1]); + strcat(install_dir, "/"); + + num_unit_files = get_unit_files(unit_files); + + // For each unit file, get the installation targets and install the unit + for (int i = 0; i < num_unit_files; i++) { + num_targets = get_install_targets(unit_files[i], targets); + for(int j = 0; j < num_targets; j++) { + if (install_unit_file(unit_files[i], targets[j], install_dir) != 0) { + return 1; + } + free(targets[j]); + } + + free(unit_files[i]); + } + return 0; +} From 14b8f6ed710406a980b213d7dcfa65adbc3d3f4c Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Tue, 23 Jul 2019 17:10:14 -0700 Subject: [PATCH 12/20] [systemd-sonic-generator]: build debian package * build a debian package containing systemd-sonic-generator Signed-off-by: Lawrence Lee --- .gitignore | 6 ++++++ .../build_templates/sonic_debian_extension.j2 | 4 +++- rules/systemd-sonic-generator.mk | 4 ++-- src/systemd-sonic-generator/Makefile | 19 ++++++++++++++++--- src/systemd-sonic-generator/debian/changelog | 3 +++ src/systemd-sonic-generator/debian/compat | 1 + src/systemd-sonic-generator/debian/control | 6 ++++++ src/systemd-sonic-generator/debian/rules | 18 ++++++++++++++++++ 8 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 src/systemd-sonic-generator/debian/changelog create mode 100644 src/systemd-sonic-generator/debian/compat create mode 100644 src/systemd-sonic-generator/debian/control create mode 100755 src/systemd-sonic-generator/debian/rules diff --git a/.gitignore b/.gitignore index 486dde110639..713246fd8793 100644 --- a/.gitignore +++ b/.gitignore @@ -81,6 +81,12 @@ src/supervisor/* !src/supervisor/patch/ src/swig/* !src/swig/Makefile +src/systemd-sonic-generator/systemd-sonic-generator +src/systemd-sonic-generator/debian/* +!src/systemd-sonic-generator/debian/changelog +!src/systemd-sonic-generator/debian/compat +!src/systemd-sonic-generator/debian/control +!src/systemd-sonic-generator/debian/rules src/telemetry/debian/* !src/telemetry/debian/changelog !src/telemetry/debian/compat diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 71ed08c1c75e..9193aaff014c 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -341,7 +341,9 @@ sudo LANG=C chroot $FILESYSTEM_ROOT umount -lf /sys {% endif %} ## Copy systemd generator for sonic services -sudo LANG=C cp $files_path/systemd-sonic-generator $FILESYSTEM_ROOT/lib/systemd/system-generators/systemd-sonic-generator +sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/systemd-sonic-generator*.deb || \ + sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f + # Copy swss and syncd service script sudo LANG=C cp $SCRIPTS_DIR/swss.sh $FILESYSTEM_ROOT/usr/local/bin/swss.sh diff --git a/rules/systemd-sonic-generator.mk b/rules/systemd-sonic-generator.mk index d23583eaf4f8..2ce82d572134 100644 --- a/rules/systemd-sonic-generator.mk +++ b/rules/systemd-sonic-generator.mk @@ -1,3 +1,3 @@ -SYSTEMD_SONIC_GENERATOR = systemd-sonic-generator +SYSTEMD_SONIC_GENERATOR = systemd-sonic-generator_1.0.0_amd64.deb $(SYSTEMD_SONIC_GENERATOR)_SRC_PATH = $(SRC_PATH)/systemd-sonic-generator -SONIC_MAKE_FILES += $(SYSTEMD_SONIC_GENERATOR) +SONIC_MAKE_DEBS += $(SYSTEMD_SONIC_GENERATOR) diff --git a/src/systemd-sonic-generator/Makefile b/src/systemd-sonic-generator/Makefile index 6c89b41047f0..c9ea0b13ac0d 100644 --- a/src/systemd-sonic-generator/Makefile +++ b/src/systemd-sonic-generator/Makefile @@ -1,9 +1,22 @@ CC=gcc CFLAGS=-std=gnu99 -MAIN_TARGET = systemd-sonic-generator +BINARY = systemd-sonic-generator +MAIN_TARGET = $(BINARY)_1.0.0_amd64.deb $(addprefix $(DEST)/, $(MAIN_TARGET)): $(DEST)/% : + dpkg-buildpackage -us -uc -b + mv ../$(MAIN_TARGET) $(DEST)/ + rm ../$(BINARY)-* ../$(BINARY)_* + +$(BINARY): systemd-sonic-generator.c rm -f ./systemd-sonic-generator - - $(CC) $(CFLAGS) -o $@ systemd-sonic-generator.c + + $(CC) $(CFLAGS) -o $@ $^ + +install: $(BINARY) + mkdir -p $(DESTDIR) + mkdir -p $(DESTDIR)/lib + mkdir -p $(DESTDIR)/lib/systemd + mkdir -p $(DESTDIR)/lib/systemd/system-generators + cp ./systemd-sonic-generator $(DESTDIR)/lib/systemd/system-generators diff --git a/src/systemd-sonic-generator/debian/changelog b/src/systemd-sonic-generator/debian/changelog new file mode 100644 index 000000000000..1cc6c755e6c8 --- /dev/null +++ b/src/systemd-sonic-generator/debian/changelog @@ -0,0 +1,3 @@ +systemd-sonic-generator (1.0.0) UNRELEASED; urgency=medium + * Initial version + -- Lawrence Lee Tue, 23 Jul 2019 16:00:00 -0800 diff --git a/src/systemd-sonic-generator/debian/compat b/src/systemd-sonic-generator/debian/compat new file mode 100644 index 000000000000..b4de39476753 --- /dev/null +++ b/src/systemd-sonic-generator/debian/compat @@ -0,0 +1 @@ +11 diff --git a/src/systemd-sonic-generator/debian/control b/src/systemd-sonic-generator/debian/control new file mode 100644 index 000000000000..cbf8989e8dfb --- /dev/null +++ b/src/systemd-sonic-generator/debian/control @@ -0,0 +1,6 @@ +Source: systemd-sonic-generator +Maintainer: Lawrence Lee + +Package: systemd-sonic-generator +Architecture: any +Description: Systemd generator for SONiC services diff --git a/src/systemd-sonic-generator/debian/rules b/src/systemd-sonic-generator/debian/rules new file mode 100755 index 000000000000..945fcf9d99b7 --- /dev/null +++ b/src/systemd-sonic-generator/debian/rules @@ -0,0 +1,18 @@ +#!/usr/bin/make -f + +DISTRIBUTION = $(shell sed -n "s/^VERSION_CODENAME=//p" /etc/os-release) +VERSION = 1.0.0 +PACKAGEVERSION = $(VERSION) + +%: + dh $@ + +override_dh_auto_clean: +override_dh_auto_test: +override_dh_auto_build: +override_dh_auto_install: + make systemd-sonic-generator + make install DESTDIR=debian/systemd-sonic-generator + +override_dh_gencontrol: + dh_gencontrol -- -v$(PACKAGEVERSION) From 193855f9d682819f24151469e999262220e05aa9 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Tue, 23 Jul 2019 17:19:29 -0700 Subject: [PATCH 13/20] [platform]: create dependency on systemd-sonic-generator * add systemd-sonic-generator as dependency for all platforms Signed-off-by: Lawrence Lee --- platform/barefoot/one-image.mk | 1 + platform/broadcom/one-image.mk | 1 + platform/cavium/one-image.mk | 1 + platform/centec/one-image.mk | 1 + platform/generic/onie-image.mk | 1 + platform/marvell/one-image.mk | 1 + platform/mellanox/one-image.mk | 1 + platform/nephos/one-image.mk | 1 + platform/p4/docker-sonic-p4.mk | 3 ++- platform/vs/kvm-image.mk | 1 + platform/vs/one-image.mk | 1 + 11 files changed, 12 insertions(+), 1 deletion(-) diff --git a/platform/barefoot/one-image.mk b/platform/barefoot/one-image.mk index 6fcb4d7ea51b..abc68c9475b6 100644 --- a/platform/barefoot/one-image.mk +++ b/platform/barefoot/one-image.mk @@ -1,6 +1,7 @@ # sonic one image installer SONIC_ONE_IMAGE = sonic-barefoot.bin +$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = barefoot $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(BFN_MODULE) $(PYTHON_THRIFT) diff --git a/platform/broadcom/one-image.mk b/platform/broadcom/one-image.mk index b87c6dc14706..bbdb851df1b5 100755 --- a/platform/broadcom/one-image.mk +++ b/platform/broadcom/one-image.mk @@ -1,6 +1,7 @@ # sonic broadcom one image installer SONIC_ONE_IMAGE = sonic-broadcom.bin +$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = broadcom $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(BRCM_OPENNSL_KERNEL) diff --git a/platform/cavium/one-image.mk b/platform/cavium/one-image.mk index 658c2d60b587..b95bad6417ea 100644 --- a/platform/cavium/one-image.mk +++ b/platform/cavium/one-image.mk @@ -1,6 +1,7 @@ # sonic cavium one image installer SONIC_ONE_IMAGE = sonic-cavium.bin +$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = cavium $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(CAVM_PLATFORM_DEB) diff --git a/platform/centec/one-image.mk b/platform/centec/one-image.mk index c5cc1f26f5fc..3b00de8c66df 100644 --- a/platform/centec/one-image.mk +++ b/platform/centec/one-image.mk @@ -1,6 +1,7 @@ # sonic centec one image installer SONIC_ONE_IMAGE = sonic-centec.bin +$(SONIC_ONE_IMAGE)_DEPENDS = $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = centec $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_LAZY_INSTALLS += $(CENTEC_E582_48X6Q_PLATFORM_MODULE) \ diff --git a/platform/generic/onie-image.mk b/platform/generic/onie-image.mk index 1a2ddb143505..06b4775cebc2 100644 --- a/platform/generic/onie-image.mk +++ b/platform/generic/onie-image.mk @@ -1,6 +1,7 @@ # sonic generic ONIE image installer SONIC_GENERIC_ONIE_IMAGE = sonic-generic.bin +$(SONIC_GENERIC_ONIE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_GENERIC_ONIE_IMAGE)_MACHINE = generic $(SONIC_GENERIC_ONIE_IMAGE)_IMAGE_TYPE = onie $(SONIC_GENERIC_ONIE_IMAGE)_INSTALLS = diff --git a/platform/marvell/one-image.mk b/platform/marvell/one-image.mk index 270a5307d0e1..521868a1a84b 100644 --- a/platform/marvell/one-image.mk +++ b/platform/marvell/one-image.mk @@ -1,6 +1,7 @@ # sonic marvell one image installer SONIC_ONE_IMAGE = sonic-marvell.bin +$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = marvell $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie ifeq ($(INSTALL_DEBUG_TOOLS),y) diff --git a/platform/mellanox/one-image.mk b/platform/mellanox/one-image.mk index 45a48c79bc33..2cc6994f1ea0 100644 --- a/platform/mellanox/one-image.mk +++ b/platform/mellanox/one-image.mk @@ -1,6 +1,7 @@ # sonic mellanox one image installer SONIC_ONE_IMAGE = sonic-mellanox.bin +$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = mellanox $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(SX_KERNEL) $(KERNEL_MFT) $(MFT_OEM) $(MFT) $(MLNX_HW_MANAGEMENT) diff --git a/platform/nephos/one-image.mk b/platform/nephos/one-image.mk index 8199401d26ad..040c7b3194e0 100644 --- a/platform/nephos/one-image.mk +++ b/platform/nephos/one-image.mk @@ -1,6 +1,7 @@ # sonic nephos one image installer SONIC_ONE_IMAGE = sonic-nephos.bin +$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = nephos $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(NEPHOS_MODULE) diff --git a/platform/p4/docker-sonic-p4.mk b/platform/p4/docker-sonic-p4.mk index 534d161631f5..588acefcd237 100644 --- a/platform/p4/docker-sonic-p4.mk +++ b/platform/p4/docker-sonic-p4.mk @@ -12,7 +12,8 @@ $(DOCKER_SONIC_P4)_DEPENDS += $(SWSS) \ $(LIBTEAM_UTILS) \ $(SONIC_DEVICE_DATA) \ $(SONIC_UTILS) \ - $(IPROUTE2) + $(IPROUTE2) \ + $(SYSTEMD_SONIC_GENERATOR) # ifeq ($(ROUTING_STACK), quagga) $(DOCKER_SONIC_P4)_DEPENDS += $(QUAGGA) diff --git a/platform/vs/kvm-image.mk b/platform/vs/kvm-image.mk index 90073749db55..01c25545ee6a 100644 --- a/platform/vs/kvm-image.mk +++ b/platform/vs/kvm-image.mk @@ -1,6 +1,7 @@ # sonic kvm image SONIC_KVM_IMAGE = sonic-vs.img.gz +$(SONIC_KVM_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_KVM_IMAGE)_MACHINE = vs $(SONIC_KVM_IMAGE)_IMAGE_TYPE = kvm ifeq ($(INSTALL_DEBUG_TOOLS),y) diff --git a/platform/vs/one-image.mk b/platform/vs/one-image.mk index 4f478ac11869..689120ec8475 100644 --- a/platform/vs/one-image.mk +++ b/platform/vs/one-image.mk @@ -1,6 +1,7 @@ # sonic vs one image installer SONIC_ONE_IMAGE = sonic-vs.bin +$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = vs $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie ifeq ($(INSTALL_DEBUG_TOOLS),y) From c65ff64dc04d6dcaedf314a92cf1a4d8400b9018 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Tue, 23 Jul 2019 21:23:02 -0700 Subject: [PATCH 14/20] [systemd-sonic-generator]: address review comments * check that array indices don't exceed array bounds * check return codes for system functions * define constant values * fix conflicting variable names Signed-off-by: Lawrence Lee --- .../systemd-sonic-generator.c | 53 ++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index 3fc126e1eddd..f40fdda1fe92 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -10,6 +10,7 @@ #include #define MAX_NUM_TARGETS 5 +#define MAX_NUM_INSTALL_LINES 5 #define MAX_NUM_UNITS 128 static const char* UNIT_FILE_PREFIX = "/etc/systemd/system/"; @@ -36,7 +37,7 @@ static int get_target_lines(char* unit_file, char* target_lines[]) { FILE *fp; char *line = NULL; size_t len = 0; - ssize_t read; + ssize_t nread; bool found_install; int num_target_lines; @@ -50,12 +51,16 @@ static int get_target_lines(char* unit_file, char* target_lines[]) { found_install = false; num_target_lines = 0; - while ((read = getline(&line, &len, fp)) != -1 ) { + while ((nread = getline(&line, &len, fp)) != -1 ) { // Assumes that [Install] is the last section of the unit file if (strstr(line, "[Install]") != NULL) { found_install = true; } else if (found_install) { + if (num_target_lines >= MAX_NUM_INSTALL_LINES) { + fputs("Number of lines in [Install] section exceeds MAX_NUM_INSTALL_LINES\n", stderr); + return -1; + } target_lines[num_target_lines] = strdup(line); num_target_lines++; } @@ -89,6 +94,11 @@ static int get_install_targets_from_line(char* target_string, char* suffix, char free(target); + if (num_targets >= MAX_NUM_TARGETS) { + fputs("Number of targets found exceeds MAX_NUM_TARGETS\n", stderr); + return -1; + } + targets[num_targets] = strdup(final_target); num_targets++; } @@ -103,9 +113,10 @@ static int get_install_targets(char* unit_file, char* targets[]) { unit file to determine which directories to install the unit in ***/ char file_path[PATH_MAX]; - char *target_lines[5]; + char *target_lines[MAX_NUM_INSTALL_LINES]; int num_target_lines; int num_targets; + int found_targets; char* token; char* line = NULL; bool first; @@ -115,6 +126,8 @@ static int get_install_targets(char* unit_file, char* targets[]) { strcat(file_path, unit_file); num_target_lines = get_target_lines(file_path, target_lines); + if (num_target_lines < 0) + return -1; num_targets = 0; @@ -134,7 +147,11 @@ static int get_install_targets(char* unit_file, char* targets[]) { } } else { - num_targets += get_install_targets_from_line(token, target_suffix, &targets[num_targets]); + found_targets = get_install_targets_from_line(token, target_suffix, &targets[num_targets]); + if (found_targets < 0) + return -1; + + num_targets += found_targets; } } free(target_lines[i]); @@ -200,16 +217,33 @@ static int install_unit_file(char* unit_file, char* target, char* install_dir) { if (stat(final_install_dir, &st) == -1) { // If doesn't exist, create - mkdir(final_install_dir, 0755); + r = mkdir(final_install_dir, 0755); + if (r == -1) { + fputs("Unable to create target directory\n", stderr); + return -1; + } } else if (S_ISREG(st.st_mode)) { // If is regular file, remove and create - remove(final_install_dir); - mkdir(final_install_dir, 0755); + r = remove(final_install_dir); + if (r == -1) { + fputs("Unable to remove file with same name as target directory\n", stderr); + return -1; + } + + r = mkdir(final_install_dir, 0755); + if (r == -1) { + fputs("Unable to create target direectory\n", stderr); + return -1; + } } else if (S_ISDIR(st.st_mode)) { // If directory, verify correct permissions - chmod(final_install_dir, 0755); + r = chmod(final_install_dir, 0755); + if (r == -1) { + fputs("Unable to change permissions of existing target directory\n", stderr); + return -1; + } } @@ -251,6 +285,9 @@ int main(int argc, char **argv) { // For each unit file, get the installation targets and install the unit for (int i = 0; i < num_unit_files; i++) { num_targets = get_install_targets(unit_files[i], targets); + if (num_targets < 0) + return 1; + for(int j = 0; j < num_targets; j++) { if (install_unit_file(unit_files[i], targets[j], install_dir) != 0) { return 1; From 03e02f11a0753012f3824c42c80e7ed2de6bc97d Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Wed, 24 Jul 2019 11:56:50 -0700 Subject: [PATCH 15/20] [systemd-sonic-generator]: address review comments * make formatting consistent in systemd-sonic-generator.c * make comments more accurate in sonic debian extension script Signed-off-by: Lawrence Lee --- files/build_templates/sonic_debian_extension.j2 | 2 +- src/systemd-sonic-generator/systemd-sonic-generator.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 9193aaff014c..3d2d1d9f61ac 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -340,7 +340,7 @@ sudo LANG=C chroot $FILESYSTEM_ROOT fuser -km /sys || true sudo LANG=C chroot $FILESYSTEM_ROOT umount -lf /sys {% endif %} -## Copy systemd generator for sonic services +## Install Systemd generator for SONiC services sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/systemd-sonic-generator*.deb || \ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index f40fdda1fe92..70aae9f0bd9c 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -160,7 +160,7 @@ static int get_install_targets(char* unit_file, char* targets[]) { } -static int get_unit_files(char* unit_files[]){ +static int get_unit_files(char* unit_files[]) { /*** Reads a list of unit files to be installed from /etc/sonic/generated_services.conf ***/ @@ -288,7 +288,7 @@ int main(int argc, char **argv) { if (num_targets < 0) return 1; - for(int j = 0; j < num_targets; j++) { + for (int j = 0; j < num_targets; j++) { if (install_unit_file(unit_files[i], targets[j], install_dir) != 0) { return 1; } From ccec6d444c3b50dea29d893881f68b8012d2e2d3 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Wed, 24 Jul 2019 22:36:49 -0700 Subject: [PATCH 16/20] [systemd-sonic-generator]: address review comments * fix formatting * remove unnecessary apt-get command Signed-off-by: Lawrence Lee --- files/build_templates/sonic_debian_extension.j2 | 3 +-- src/systemd-sonic-generator/systemd-sonic-generator.c | 2 -- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 3d2d1d9f61ac..8d58fb48c928 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -341,8 +341,7 @@ sudo LANG=C chroot $FILESYSTEM_ROOT umount -lf /sys {% endif %} ## Install Systemd generator for SONiC services -sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/systemd-sonic-generator*.deb || \ - sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f +sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/systemd-sonic-generator*.deb # Copy swss and syncd service script diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index 70aae9f0bd9c..563b394339bf 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -188,7 +188,6 @@ static int get_unit_files(char* unit_files[]) { fclose(fp); return num_unit_files; - } @@ -261,7 +260,6 @@ static int install_unit_file(char* unit_file, char* target, char* install_dir) { } return 0; - } From 093face2772a71ecc771a373da5a4b32d9711448 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Thu, 25 Jul 2019 10:34:28 -0700 Subject: [PATCH 17/20] [systemd-sonic-generator]: address review comments * make errors more verbose * change Makefile dependencies to INSTALLS Signed-off-by: Lawrence Lee --- .../build_templates/sonic_debian_extension.j2 | 4 -- platform/barefoot/one-image.mk | 2 +- platform/broadcom/one-image.mk | 2 +- platform/cavium/one-image.mk | 2 +- platform/centec/one-image.mk | 2 +- platform/generic/onie-image.mk | 3 +- platform/marvell/one-image.mk | 2 +- platform/mellanox/one-image.mk | 2 +- platform/nephos/one-image.mk | 2 +- platform/p4/docker-sonic-p4.mk | 2 +- platform/vs/kvm-image.mk | 2 +- .../systemd-sonic-generator.c | 56 +++++++++++-------- 12 files changed, 43 insertions(+), 38 deletions(-) diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2 index 8d58fb48c928..0f27c15273c1 100644 --- a/files/build_templates/sonic_debian_extension.j2 +++ b/files/build_templates/sonic_debian_extension.j2 @@ -340,10 +340,6 @@ sudo LANG=C chroot $FILESYSTEM_ROOT fuser -km /sys || true sudo LANG=C chroot $FILESYSTEM_ROOT umount -lf /sys {% endif %} -## Install Systemd generator for SONiC services -sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/systemd-sonic-generator*.deb - - # Copy swss and syncd service script sudo LANG=C cp $SCRIPTS_DIR/swss.sh $FILESYSTEM_ROOT/usr/local/bin/swss.sh sudo LANG=C cp $SCRIPTS_DIR/syncd.sh $FILESYSTEM_ROOT/usr/local/bin/syncd.sh diff --git a/platform/barefoot/one-image.mk b/platform/barefoot/one-image.mk index abc68c9475b6..36823baa8781 100644 --- a/platform/barefoot/one-image.mk +++ b/platform/barefoot/one-image.mk @@ -1,10 +1,10 @@ # sonic one image installer SONIC_ONE_IMAGE = sonic-barefoot.bin -$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = barefoot $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(BFN_MODULE) $(PYTHON_THRIFT) +$(SONIC_ONE_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_LAZY_INSTALLS += $(BFN_PLATFORM_MODULE) $(SONIC_ONE_IMAGE)_LAZY_INSTALLS += $(BFN_MONTARA_PLATFORM_MODULE) $(SONIC_ONE_IMAGE)_LAZY_INSTALLS += $(WNC_OSW1800_PLATFORM_MODULE) diff --git a/platform/broadcom/one-image.mk b/platform/broadcom/one-image.mk index bbdb851df1b5..966e22edbe5a 100755 --- a/platform/broadcom/one-image.mk +++ b/platform/broadcom/one-image.mk @@ -1,10 +1,10 @@ # sonic broadcom one image installer SONIC_ONE_IMAGE = sonic-broadcom.bin -$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = broadcom $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(BRCM_OPENNSL_KERNEL) +$(SONIC_ONE_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_LAZY_INSTALLS += $(DELL_S6000_PLATFORM_MODULE) \ $(DELL_Z9264F_PLATFORM_MODULE) \ $(DELL_S5232F_PLATFORM_MODULE) \ diff --git a/platform/cavium/one-image.mk b/platform/cavium/one-image.mk index b95bad6417ea..d81763430c05 100644 --- a/platform/cavium/one-image.mk +++ b/platform/cavium/one-image.mk @@ -1,11 +1,11 @@ # sonic cavium one image installer SONIC_ONE_IMAGE = sonic-cavium.bin -$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = cavium $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(CAVM_PLATFORM_DEB) $(SONIC_ONE_IMAGE)_INSTALLS += $(CAVM_XPNET_DEB) +$(SONIC_ONE_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) ifeq ($(INSTALL_DEBUG_TOOLS),y) $(SONIC_ONE_IMAGE)_DOCKERS += $(SONIC_INSTALL_DOCKER_DBG_IMAGES) $(SONIC_ONE_IMAGE)_DOCKERS += $(filter-out $(patsubst %-$(DBG_IMAGE_MARK).gz,%.gz, $(SONIC_INSTALL_DOCKER_DBG_IMAGES)), $(SONIC_INSTALL_DOCKER_IMAGES)) diff --git a/platform/centec/one-image.mk b/platform/centec/one-image.mk index 3b00de8c66df..0e057b24df03 100644 --- a/platform/centec/one-image.mk +++ b/platform/centec/one-image.mk @@ -1,9 +1,9 @@ # sonic centec one image installer SONIC_ONE_IMAGE = sonic-centec.bin -$(SONIC_ONE_IMAGE)_DEPENDS = $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = centec $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie +$(SONIC_ONE_IMAGE)_INSTALLS = $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_LAZY_INSTALLS += $(CENTEC_E582_48X6Q_PLATFORM_MODULE) \ $(CENTEC_E582_48X2Q4Z_PLATFORM_MODULE) ifeq ($(INSTALL_DEBUG_TOOLS),y) diff --git a/platform/generic/onie-image.mk b/platform/generic/onie-image.mk index 06b4775cebc2..5dc8bfb9f885 100644 --- a/platform/generic/onie-image.mk +++ b/platform/generic/onie-image.mk @@ -1,9 +1,8 @@ # sonic generic ONIE image installer SONIC_GENERIC_ONIE_IMAGE = sonic-generic.bin -$(SONIC_GENERIC_ONIE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_GENERIC_ONIE_IMAGE)_MACHINE = generic $(SONIC_GENERIC_ONIE_IMAGE)_IMAGE_TYPE = onie -$(SONIC_GENERIC_ONIE_IMAGE)_INSTALLS = +$(SONIC_GENERIC_ONIE_IMAGE)_INSTALLS = $(SYSTEMD_SONIC_GENERATOR) $(SONIC_GENERIC_ONIE_IMAGE)_DOCKERS = SONIC_INSTALLERS += $(SONIC_GENERIC_ONIE_IMAGE) diff --git a/platform/marvell/one-image.mk b/platform/marvell/one-image.mk index 521868a1a84b..fcac03854348 100644 --- a/platform/marvell/one-image.mk +++ b/platform/marvell/one-image.mk @@ -1,9 +1,9 @@ # sonic marvell one image installer SONIC_ONE_IMAGE = sonic-marvell.bin -$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = marvell $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie +$(SONIC_ONE_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) ifeq ($(INSTALL_DEBUG_TOOLS),y) $(SONIC_ONE_IMAGE)_DOCKERS += $(SONIC_INSTALL_DOCKER_DBG_IMAGES) $(SONIC_ONE_IMAGE)_DOCKERS += $(filter-out $(patsubst %-$(DBG_IMAGE_MARK).gz,%.gz, $(SONIC_INSTALL_DOCKER_DBG_IMAGES)), $(SONIC_INSTALL_DOCKER_IMAGES)) diff --git a/platform/mellanox/one-image.mk b/platform/mellanox/one-image.mk index 2cc6994f1ea0..2946ae53f47b 100644 --- a/platform/mellanox/one-image.mk +++ b/platform/mellanox/one-image.mk @@ -1,10 +1,10 @@ # sonic mellanox one image installer SONIC_ONE_IMAGE = sonic-mellanox.bin -$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = mellanox $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(SX_KERNEL) $(KERNEL_MFT) $(MFT_OEM) $(MFT) $(MLNX_HW_MANAGEMENT) +$(SONIC_ONE_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) ifeq ($(INSTALL_DEBUG_TOOLS),y) $(SONIC_ONE_IMAGE)_DOCKERS += $(SONIC_INSTALL_DOCKER_DBG_IMAGES) $(SONIC_ONE_IMAGE)_DOCKERS += $(filter-out $(patsubst %-$(DBG_IMAGE_MARK).gz,%.gz, $(SONIC_INSTALL_DOCKER_DBG_IMAGES)), $(SONIC_INSTALL_DOCKER_IMAGES)) diff --git a/platform/nephos/one-image.mk b/platform/nephos/one-image.mk index 040c7b3194e0..3651325456c7 100644 --- a/platform/nephos/one-image.mk +++ b/platform/nephos/one-image.mk @@ -1,10 +1,10 @@ # sonic nephos one image installer SONIC_ONE_IMAGE = sonic-nephos.bin -$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = nephos $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie $(SONIC_ONE_IMAGE)_INSTALLS += $(NEPHOS_MODULE) +$(SONIC_ONE_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_LAZY_INSTALLS += $(INGRASYS_S9130_32X_PLATFORM_MODULE) \ $(INGRASYS_S9230_64X_PLATFORM_MODULE) \ $(ACCTON_AS7116_54X_PLATFORM_MODULE) \ diff --git a/platform/p4/docker-sonic-p4.mk b/platform/p4/docker-sonic-p4.mk index 588acefcd237..00d1f36b2be0 100644 --- a/platform/p4/docker-sonic-p4.mk +++ b/platform/p4/docker-sonic-p4.mk @@ -13,7 +13,7 @@ $(DOCKER_SONIC_P4)_DEPENDS += $(SWSS) \ $(SONIC_DEVICE_DATA) \ $(SONIC_UTILS) \ $(IPROUTE2) \ - $(SYSTEMD_SONIC_GENERATOR) +$(DOCKER_SONIC_P4)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) # ifeq ($(ROUTING_STACK), quagga) $(DOCKER_SONIC_P4)_DEPENDS += $(QUAGGA) diff --git a/platform/vs/kvm-image.mk b/platform/vs/kvm-image.mk index 01c25545ee6a..439a6fc1e418 100644 --- a/platform/vs/kvm-image.mk +++ b/platform/vs/kvm-image.mk @@ -1,7 +1,7 @@ # sonic kvm image SONIC_KVM_IMAGE = sonic-vs.img.gz -$(SONIC_KVM_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) +$(SONIC_KVM_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_KVM_IMAGE)_MACHINE = vs $(SONIC_KVM_IMAGE)_IMAGE_TYPE = kvm ifeq ($(INSTALL_DEBUG_TOOLS),y) diff --git a/src/systemd-sonic-generator/systemd-sonic-generator.c b/src/systemd-sonic-generator/systemd-sonic-generator.c index 563b394339bf..a2723a28ff03 100644 --- a/src/systemd-sonic-generator/systemd-sonic-generator.c +++ b/src/systemd-sonic-generator/systemd-sonic-generator.c @@ -45,7 +45,8 @@ static int get_target_lines(char* unit_file, char* target_lines[]) { fp = fopen(unit_file, "r"); if (fp == NULL) { - exit(EXIT_FAILURE); + fprintf(stderr, "Failed to open file %s\n", unit_file); + return -1; } found_install = false; @@ -58,8 +59,9 @@ static int get_target_lines(char* unit_file, char* target_lines[]) { } else if (found_install) { if (num_target_lines >= MAX_NUM_INSTALL_LINES) { - fputs("Number of lines in [Install] section exceeds MAX_NUM_INSTALL_LINES\n", stderr); - return -1; + fprintf(stderr, "Number of lines in [Install] section of %s exceeds MAX_NUM_INSTALL_LINES\n", unit_file); + fputs("Extra [Install] lines will be ignored\n", stderr); + return num_target_lines; } target_lines[num_target_lines] = strdup(line); num_target_lines++; @@ -73,7 +75,7 @@ static int get_target_lines(char* unit_file, char* target_lines[]) { return num_target_lines; } -static int get_install_targets_from_line(char* target_string, char* suffix, char* targets[]) { +static int get_install_targets_from_line(char* target_string, char* suffix, char* targets[], int existing_targets) { /*** Helper fuction for get_install_targets @@ -94,12 +96,13 @@ static int get_install_targets_from_line(char* target_string, char* suffix, char free(target); - if (num_targets >= MAX_NUM_TARGETS) { + if (num_targets + existing_targets >= MAX_NUM_TARGETS) { fputs("Number of targets found exceeds MAX_NUM_TARGETS\n", stderr); - return -1; + fputs("Additional targets will be ignored \n", stderr); + return num_targets; } - targets[num_targets] = strdup(final_target); + targets[num_targets + existing_targets] = strdup(final_target); num_targets++; } return num_targets; @@ -126,8 +129,10 @@ static int get_install_targets(char* unit_file, char* targets[]) { strcat(file_path, unit_file); num_target_lines = get_target_lines(file_path, target_lines); - if (num_target_lines < 0) + if (num_target_lines < 0) { + fprintf(stderr, "Error parsing targets for %s\n", unit_file); return -1; + } num_targets = 0; @@ -147,10 +152,7 @@ static int get_install_targets(char* unit_file, char* targets[]) { } } else { - found_targets = get_install_targets_from_line(token, target_suffix, &targets[num_targets]); - if (found_targets < 0) - return -1; - + found_targets = get_install_targets_from_line(token, target_suffix, targets, num_targets); num_targets += found_targets; } } @@ -172,12 +174,17 @@ static int get_unit_files(char* unit_files[]) { fp = fopen(CONFIG_FILE, "r"); if (fp == NULL) { + fprintf(stderr, "Failed to open %s\n", CONFIG_FILE); exit(EXIT_FAILURE); } int num_unit_files = 0; while ((read = getline(&line, &len, fp)) != -1) { + if (num_unit_files >= MAX_NUM_UNITS) { + fprintf(stderr, "Maximum number of units exceeded, ignoring extras\n"); + return num_unit_files; + } strip_trailing_newline(line); unit_files[num_unit_files] = strdup(line); num_unit_files++; @@ -218,7 +225,7 @@ static int install_unit_file(char* unit_file, char* target, char* install_dir) { // If doesn't exist, create r = mkdir(final_install_dir, 0755); if (r == -1) { - fputs("Unable to create target directory\n", stderr); + fprintf(stderr, "Unable to create target directory %s\n", final_install_dir); return -1; } } @@ -226,13 +233,13 @@ static int install_unit_file(char* unit_file, char* target, char* install_dir) { // If is regular file, remove and create r = remove(final_install_dir); if (r == -1) { - fputs("Unable to remove file with same name as target directory\n", stderr); + fprintf(stderr, "Unable to remove file with same name as target directory %s\n", final_install_dir); return -1; } r = mkdir(final_install_dir, 0755); if (r == -1) { - fputs("Unable to create target direectory\n", stderr); + fprintf(stderr, "Unable to create target directory %s\n", final_install_dir); return -1; } } @@ -240,7 +247,7 @@ static int install_unit_file(char* unit_file, char* target, char* install_dir) { // If directory, verify correct permissions r = chmod(final_install_dir, 0755); if (r == -1) { - fputs("Unable to change permissions of existing target directory\n", stderr); + fprintf(stderr, "Unable to change permissions of existing target directory %s\n", final_install_dir); return -1; } } @@ -255,8 +262,8 @@ static int install_unit_file(char* unit_file, char* target, char* install_dir) { if (r < 0) { if (errno == EEXIST) return 0; - - return 1; + fprintf(stderr, "Error creating symlink %s from source %s\n", dest_path, src_path); + return -1; } return 0; @@ -283,13 +290,16 @@ int main(int argc, char **argv) { // For each unit file, get the installation targets and install the unit for (int i = 0; i < num_unit_files; i++) { num_targets = get_install_targets(unit_files[i], targets); - if (num_targets < 0) - return 1; + if (num_targets < 0) { + fprintf(stderr, "Error parsing %s\n", unit_files[i]); + free(unit_files[i]); + continue; + } for (int j = 0; j < num_targets; j++) { - if (install_unit_file(unit_files[i], targets[j], install_dir) != 0) { - return 1; - } + if (install_unit_file(unit_files[i], targets[j], install_dir) != 0) + fprintf(stderr, "Error installing %s to target directory %s\n", unit_files[i], targets[j]); + free(targets[j]); } From 52f1d7463d59fa3d3819c64ea8ada0dce6f6514f Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Thu, 25 Jul 2019 16:48:11 -0700 Subject: [PATCH 18/20] [platform]: add systemd generator as dependency for all images Signed-off-by: Lawrence Lee --- platform/barefoot/one-aboot.mk | 1 + platform/broadcom/one-aboot.mk | 1 + platform/broadcom/raw-image.mk | 1 + platform/generic/aboot-image.mk | 2 +- platform/vs/one-image.mk | 2 +- 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/platform/barefoot/one-aboot.mk b/platform/barefoot/one-aboot.mk index 71e3a95da3ab..4e5e808d2ab9 100644 --- a/platform/barefoot/one-aboot.mk +++ b/platform/barefoot/one-aboot.mk @@ -4,6 +4,7 @@ SONIC_ONE_ABOOT_IMAGE = sonic-aboot-barefoot.swi $(SONIC_ONE_ABOOT_IMAGE)_MACHINE = barefoot $(SONIC_ONE_ABOOT_IMAGE)_IMAGE_TYPE = aboot $(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(BFN_MODULE) +$(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_ABOOT_IMAGE)_LAZY_INSTALLS += $(BFN_PLATFORM_MODULE) $(SONIC_ONE_ABOOT_IMAGE)_LAZY_INSTALLS += $(BFN_MONTARA_PLATFORM_MODULE) $(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(ARISTA_PLATFORM_MODULE_DRIVERS) \ diff --git a/platform/broadcom/one-aboot.mk b/platform/broadcom/one-aboot.mk index 71829fca4013..66f3ca8797f4 100644 --- a/platform/broadcom/one-aboot.mk +++ b/platform/broadcom/one-aboot.mk @@ -4,5 +4,6 @@ SONIC_ONE_ABOOT_IMAGE = sonic-aboot-broadcom.swi $(SONIC_ONE_ABOOT_IMAGE)_MACHINE = broadcom $(SONIC_ONE_ABOOT_IMAGE)_IMAGE_TYPE = aboot $(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(BRCM_OPENNSL_KERNEL) $(ARISTA_PLATFORM_MODULE_DRIVERS) $(ARISTA_PLATFORM_MODULE_PYTHON2) $(ARISTA_PLATFORM_MODULE_PYTHON3) $(ARISTA_PLATFORM_MODULE) +$(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_ABOOT_IMAGE)_DOCKERS += $(SONIC_INSTALL_DOCKER_IMAGES) SONIC_INSTALLERS += $(SONIC_ONE_ABOOT_IMAGE) diff --git a/platform/broadcom/raw-image.mk b/platform/broadcom/raw-image.mk index 2591d32c1be8..1fe6a3f3f372 100644 --- a/platform/broadcom/raw-image.mk +++ b/platform/broadcom/raw-image.mk @@ -4,6 +4,7 @@ SONIC_RAW_IMAGE = sonic-broadcom.raw $(SONIC_RAW_IMAGE)_MACHINE = broadcom $(SONIC_RAW_IMAGE)_IMAGE_TYPE = raw $(SONIC_RAW_IMAGE)_INSTALLS += $(BRCM_OPENNSL_KERNEL) +$(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_RAW_IMAGE)_LAZY_INSTALLS += $($(SONIC_ONE_IMAGE)_LAZY_INSTALLS) $(SONIC_RAW_IMAGE)_DOCKERS += $(SONIC_INSTALL_DOCKER_IMAGES) SONIC_INSTALLERS += $(SONIC_RAW_IMAGE) diff --git a/platform/generic/aboot-image.mk b/platform/generic/aboot-image.mk index ecc91bba54c0..c3f21b616254 100644 --- a/platform/generic/aboot-image.mk +++ b/platform/generic/aboot-image.mk @@ -3,6 +3,6 @@ SONIC_GENERIC_ABOOT_IMAGE = sonic-aboot-generic.swi $(SONIC_GENERIC_ABOOT_IMAGE)_MACHINE = generic $(SONIC_GENERIC_ABOOT_IMAGE)_IMAGE_TYPE = aboot -$(SONIC_GENERIC_ABOOT_IMAGE)_INSTALLS = +$(SONIC_GENERIC_ABOOT_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_GENERIC_ABOOT_IMAGE)_DOCKERS = SONIC_INSTALLERS += $(SONIC_GENERIC_ABOOT_IMAGE) diff --git a/platform/vs/one-image.mk b/platform/vs/one-image.mk index 689120ec8475..cf4f535382bc 100644 --- a/platform/vs/one-image.mk +++ b/platform/vs/one-image.mk @@ -1,7 +1,7 @@ # sonic vs one image installer SONIC_ONE_IMAGE = sonic-vs.bin -$(SONIC_ONE_IMAGE)_DEPENDS += $(SYSTEMD_SONIC_GENERATOR) +$(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_ONE_IMAGE)_MACHINE = vs $(SONIC_ONE_IMAGE)_IMAGE_TYPE = onie ifeq ($(INSTALL_DEBUG_TOOLS),y) From 1d488196b8f3a94c2ab8df59f92b36e1fe5b18b4 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Fri, 26 Jul 2019 09:55:21 -0700 Subject: [PATCH 19/20] [systemd-sonic-generator]: address review comments * remove install dependency from p4 docker * use CONFIGURED_ARCH in Makefile Signed-off-by: Lawrence Lee --- platform/p4/docker-sonic-p4.mk | 3 +-- rules/systemd-sonic-generator.mk | 2 +- src/systemd-sonic-generator/Makefile | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/platform/p4/docker-sonic-p4.mk b/platform/p4/docker-sonic-p4.mk index 00d1f36b2be0..534d161631f5 100644 --- a/platform/p4/docker-sonic-p4.mk +++ b/platform/p4/docker-sonic-p4.mk @@ -12,8 +12,7 @@ $(DOCKER_SONIC_P4)_DEPENDS += $(SWSS) \ $(LIBTEAM_UTILS) \ $(SONIC_DEVICE_DATA) \ $(SONIC_UTILS) \ - $(IPROUTE2) \ -$(DOCKER_SONIC_P4)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) + $(IPROUTE2) # ifeq ($(ROUTING_STACK), quagga) $(DOCKER_SONIC_P4)_DEPENDS += $(QUAGGA) diff --git a/rules/systemd-sonic-generator.mk b/rules/systemd-sonic-generator.mk index 2ce82d572134..cea08099177f 100644 --- a/rules/systemd-sonic-generator.mk +++ b/rules/systemd-sonic-generator.mk @@ -1,3 +1,3 @@ -SYSTEMD_SONIC_GENERATOR = systemd-sonic-generator_1.0.0_amd64.deb +SYSTEMD_SONIC_GENERATOR = systemd-sonic-generator_1.0.0_$(CONFIGURED_ARCH).deb $(SYSTEMD_SONIC_GENERATOR)_SRC_PATH = $(SRC_PATH)/systemd-sonic-generator SONIC_MAKE_DEBS += $(SYSTEMD_SONIC_GENERATOR) diff --git a/src/systemd-sonic-generator/Makefile b/src/systemd-sonic-generator/Makefile index c9ea0b13ac0d..973c0d64eaed 100644 --- a/src/systemd-sonic-generator/Makefile +++ b/src/systemd-sonic-generator/Makefile @@ -2,7 +2,7 @@ CC=gcc CFLAGS=-std=gnu99 BINARY = systemd-sonic-generator -MAIN_TARGET = $(BINARY)_1.0.0_amd64.deb +MAIN_TARGET = $(BINARY)_1.0.0_$(CONFIGURED_ARCH).deb $(addprefix $(DEST)/, $(MAIN_TARGET)): $(DEST)/% : dpkg-buildpackage -us -uc -b From 75002b49b38032fa9d39496c991872c12cfd2717 Mon Sep 17 00:00:00 2001 From: Lawrence Lee Date: Mon, 29 Jul 2019 09:30:55 -0700 Subject: [PATCH 20/20] [platform/broadcom]: address review comments * fix Makefile typo Signed-off-by: Lawrence Lee --- platform/broadcom/raw-image.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/broadcom/raw-image.mk b/platform/broadcom/raw-image.mk index 1fe6a3f3f372..15534e893937 100644 --- a/platform/broadcom/raw-image.mk +++ b/platform/broadcom/raw-image.mk @@ -4,7 +4,7 @@ SONIC_RAW_IMAGE = sonic-broadcom.raw $(SONIC_RAW_IMAGE)_MACHINE = broadcom $(SONIC_RAW_IMAGE)_IMAGE_TYPE = raw $(SONIC_RAW_IMAGE)_INSTALLS += $(BRCM_OPENNSL_KERNEL) -$(SONIC_ONE_ABOOT_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) +$(SONIC_RAW_IMAGE)_INSTALLS += $(SYSTEMD_SONIC_GENERATOR) $(SONIC_RAW_IMAGE)_LAZY_INSTALLS += $($(SONIC_ONE_IMAGE)_LAZY_INSTALLS) $(SONIC_RAW_IMAGE)_DOCKERS += $(SONIC_INSTALL_DOCKER_IMAGES) SONIC_INSTALLERS += $(SONIC_RAW_IMAGE)