diff --git a/perl-xCAT/xCAT/Common.pm b/perl-xCAT/xCAT/Common.pm index ec14c8d1e..0d2e42fb2 100644 --- a/perl-xCAT/xCAT/Common.pm +++ b/perl-xCAT/xCAT/Common.pm @@ -4,6 +4,9 @@ package xCAT::Common; +use File::stat; +use File::Copy; + BEGIN { $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; @@ -130,4 +133,26 @@ sub usage_noderange { } } +# copy, overwriting only if the source file is newer +sub copy_if_newer { + my ($source, $dest) = @_; + + die "ERROR: source file doesn't exist\n" unless (-e $source); + + # resolve destination path + if ($dest =~ m/\/$/ || -d $dest) { + $dest .= '/' if ($dest !~ m/\/$/); + $dest .= $1 if $source =~ m/([^\/]+)$/; + } + + if (-e $dest) { + my $smtime = stat($source)->mtime; + my $dmtime = stat($dest)->mtime; + + return if ($smtime < $dmtime); + } + + copy($source, $dest); +} + 1; diff --git a/xCAT-server/lib/xcat/plugins/anaconda.pm b/xCAT-server/lib/xcat/plugins/anaconda.pm index 70a7aca9d..dfaf77835 100644 --- a/xCAT-server/lib/xcat/plugins/anaconda.pm +++ b/xCAT-server/lib/xcat/plugins/anaconda.pm @@ -19,6 +19,8 @@ Getopt::Long::Configure("bundling"); Getopt::Long::Configure("pass_through"); use File::Path; use File::Copy; +use xCAT::Common; + #use strict; my @cpiopid; @@ -292,18 +294,19 @@ sub mknetboot mkpath("/$tftpdir/xcat/netboot/$osver/$arch/$profile/"); - #TODO: only copy if newer... unless ($donetftp{$osver,$arch,$profile}) { - if (-f "$rootimgdir/hypervisor") { - copy("$rootimgdir/hypervisor", - "/$tftpdir/xcat/netboot/$osver/$arch/$profile/"); - $xenstyle=1; - } - copy("$rootimgdir/kernel", - "/$tftpdir/xcat/netboot/$osver/$arch/$profile/"); - copy("$rootimgdir/initrd.gz", - "/$tftpdir/xcat/netboot/$osver/$arch/$profile/"); - $donetftp{$osver,$arch,$profile} = 1; + eval { + if (-f "$rootimgdir/hypervisor") { + xCAT::Common::copy_if_newer("$rootimgdir/hypervisor", + "/$tftpdir/xcat/netboot/$osver/$arch/$profile/"); + $xenstyle=1; + } + xCAT::Common::copy_if_newer("$rootimgdir/kernel", + "/$tftpdir/xcat/netboot/$osver/$arch/$profile/"); + xCAT::Common::copy_if_newer("$rootimgdir/initrd.gz", + "/$tftpdir/xcat/netboot/$osver/$arch/$profile/"); + $donetftp{$osver,$arch,$profile} = 1; + }; } unless ( -r "/$tftpdir/xcat/netboot/$osver/$arch/$profile/kernel" and -r "/$tftpdir/xcat/netboot/$osver/$arch/$profile/initrd.gz") @@ -653,8 +656,21 @@ sub mkinstall unless ($doneimgs{"$os|$arch"}) { mkpath("/tftpboot/xcat/$os/$arch"); - copy($kernpath,"$tftpdir/xcat/$os/$arch"); - copy($initrdpath,"$tftpdir/xcat/$os/$arch/initrd.img"); + eval { + xCAT::Common::copy_if_newer($kernpath,"$tftpdir/xcat/$os/$arch"); + xCAT::Common::copy_if_newer($initrdpath,"$tftpdir/xcat/$os/$arch/initrd.img"); + }; + + if ($@) { + $callback->( + { + error => ["copying pxe files failed: $@"], + errorcode => [1], + } + ); + next; + } + $doneimgs{"$os|$arch"} = 1; }