From ebc04e4cbcdbb275ea61b5ae6cd80eaf949bbc8b Mon Sep 17 00:00:00 2001 From: yangsong Date: Fri, 17 Aug 2018 11:23:56 +0800 Subject: [PATCH] support args in postinstallscripts (#5462) * support args in postinstallscripts * add the script to inject systemd service unit --- .../references/man5/linuximage.5.rst | 2 +- perl-xCAT/xCAT/Schema.pm | 2 +- xCAT-server/lib/xcat/plugins/genimage.pm | 5 +-- xCAT-server/share/xcat/netboot/rh/genimage | 6 ++-- xCAT/postscripts/injectservice | 34 +++++++++++++++++++ 5 files changed, 42 insertions(+), 7 deletions(-) create mode 100755 xCAT/postscripts/injectservice diff --git a/docs/source/guides/admin-guides/references/man5/linuximage.5.rst b/docs/source/guides/admin-guides/references/man5/linuximage.5.rst index 532eafc1b..c39c9ff3d 100644 --- a/docs/source/guides/admin-guides/references/man5/linuximage.5.rst +++ b/docs/source/guides/admin-guides/references/man5/linuximage.5.rst @@ -92,7 +92,7 @@ linuximage Attributes: \ **postinstall**\ - Supported in diskless image only. The fully qualified name of the scripts running in non-chroot mode after the package installation but before initrd generation during genimage. If multiple scripts are specified, they should be seperated with comma ",". A set of osimage attributes are exported as the environment variables to be used in the postinstall scripts: + Supported in diskless image only. The fully qualified name of the scripts and the user-specified arguments running in non-chroot mode after the package installation but before initrd generation during genimage. If multiple scripts are specified, they should be seperated with comma ",". The arguments passed to each postinstall script include 4 implicit arguments(,,,) and the user-specified arguments. A set of osimage attributes are exported as the environment variables to be used in the postinstall scripts: .. code-block:: perl diff --git a/perl-xCAT/xCAT/Schema.pm b/perl-xCAT/xCAT/Schema.pm index 3a78f33a8..f5479013c 100755 --- a/perl-xCAT/xCAT/Schema.pm +++ b/perl-xCAT/xCAT/Schema.pm @@ -815,7 +815,7 @@ passed as argument rather than by table value', otherpkglist => 'The fully qualified name of the file that stores non-distro package lists that will be included in the image. It could be set to multiple paths. The multiple paths must be separated by ",".', otherpkgdir => 'The base directory and urls of internet repos from which the non-distro packages are retrived. Only 1 local directory is supported at present. The entries should be delimited with comma ",". Currently, the internet repos are only supported on Ubuntu and Redhat.', exlist => 'The fully qualified name of the file that stores the file names and directory names that will be excluded from the image during packimage command. It is used for diskless image only.', - postinstall => 'Supported in diskless image only. The fully qualified name of the scripts running in non-chroot mode after the package installation but before initrd generation during genimage. If multiple scripts are specified, they should be seperated with comma ",". A set of osimage attributes are exported as the environment variables to be used in the postinstall scripts: + postinstall => 'Supported in diskless image only. The fully qualified name of the scripts and the user-specified arguments running in non-chroot mode after the package installation but before initrd generation during genimage. If multiple scripts are specified, they should be seperated with comma ",". The arguments passed to each postinstall script include 4 implicit arguments(,,,) and the user-specified arguments. A set of osimage attributes are exported as the environment variables to be used in the postinstall scripts: IMG_ARCH(The architecture of the osimage, such as "ppc64le","x86_64"), IMG_NAME(The name of the osimage, such as "rhels7.3-ppc64le-netboot-compute"), diff --git a/xCAT-server/lib/xcat/plugins/genimage.pm b/xCAT-server/lib/xcat/plugins/genimage.pm index 69d0c4c6d..3a7364eb7 100644 --- a/xCAT-server/lib/xcat/plugins/genimage.pm +++ b/xCAT-server/lib/xcat/plugins/genimage.pm @@ -195,7 +195,8 @@ sub process_request { } $postinstall_filename = $ref_linuximage_tab->{'postinstall'}; if ($postinstall_filename ne "") { - foreach my $file (split ',', $postinstall_filename) { + foreach my $rawfile (split ',', $postinstall_filename) { + my ($file,@args)=split(" ",$rawfile); if (!-r $file) { $callback->({ error => ["The postinstall_filename specified \'$file\' does not exist!"], errorcode => [1] }); return 1; @@ -325,7 +326,7 @@ sub process_request { if ($pkglist) { $cmd .= " --pkglist $pkglist"; } if ($srcdir_otherpkgs) { $cmd .= " --otherpkgdir \"$srcdir_otherpkgs\""; } if ($otherpkglist) { $cmd .= " --otherpkglist $otherpkglist"; } - if ($postinstall_filename) { $cmd .= " --postinstall $postinstall_filename"; } + if ($postinstall_filename) { $cmd .= " --postinstall \"$postinstall_filename\""; } if ($destdir) { $cmd .= " --rootimgdir $destdir"; } if ($tempfile) { if (!$dryrun) { $cmd .= " --tempfile $tempfile"; } diff --git a/xCAT-server/share/xcat/netboot/rh/genimage b/xCAT-server/share/xcat/netboot/rh/genimage index b119c7ca8..205537957 100755 --- a/xCAT-server/share/xcat/netboot/rh/genimage +++ b/xCAT-server/share/xcat/netboot/rh/genimage @@ -771,7 +771,6 @@ unless ($imagename) { $postinstall_filename = imgutils::get_profile_def_filename($osver, $profile, $arch, $pathtofiles, "postinstall"); } } - if ($postinstall_filename) { use_hackuname($arch, $kernelver); @@ -792,14 +791,15 @@ if ($postinstall_filename) { $ENV{IMG_ROOTIMGDIR}=$rootimg_dir if("" ne $rootimg_dir); - foreach my $postinstall (split /,/, $postinstall_filename) { + foreach my $rawpostinstall (split /,/, $postinstall_filename) { + my ($postinstall,@args)=split(" ",$rawpostinstall) ; if (!-x $postinstall) { print "postinstall script $postinstall is not executable\n"; unuse_hackuname(); exit 1; } - my $rc = system($postinstall, $rootimg_dir, $osver, $arch, $profile); + my $rc = system($postinstall, $rootimg_dir, $osver, $arch, $profile,@args); if ($rc) { print "postinstall script $postinstall failed\n"; unuse_hackuname(); diff --git a/xCAT/postscripts/injectservice b/xCAT/postscripts/injectservice new file mode 100755 index 000000000..f25cb0e69 --- /dev/null +++ b/xCAT/postscripts/injectservice @@ -0,0 +1,34 @@ +#!/bin/bash + +#4 implicit arguments passed by genimage +rootimg_dir=$1 +osver=$2 +arch=$3 +profile=$4 + +#user-specified arguments in "postinstall" attribute +#the name of the service to inject +servicename=$5 +#the full path of the service unit file to inject on management node(non-chroot) +servicefile=$6 + +echo servicename=$servicename +echo servicefile=$servicefile + +#append the suffix ".service" to service name if not specified +echo "$servicename" |grep -q -e "\.service$" || servicename=$servicename.service + +if [ -e "$rootimg_dir/usr/lib/systemd/" ]; then + #if systemd is the service management framework for the diskless image + #enable the xcatpostinit1.service + destfile="$rootimg_dir/etc/systemd/system/$servicename" + if [ -e "$rootimg_dir/etc/systemd/system/$servicename" ]; then + rm -rf $destfile + fi + cp -f $servicefile $destfile + rm -rf "$rootimg_dir/etc/systemd/system/multi-user.target.wants/$servicename" + oldpwd=$(pwd) + cd $rootimg_dir/etc/systemd/system/multi-user.target.wants/ + ln -s ../$servicename "$rootimg_dir/etc/systemd/system/multi-user.target.wants/$servicename" + cd $oldpwd +fi