From 25138a3e6b94641876f77a107d484e9e0c275145 Mon Sep 17 00:00:00 2001 From: cridye Date: Mon, 23 Nov 2009 23:18:20 +0000 Subject: [PATCH] anaconda.pm: use copy if newer for tftp files. greatly reduces the likelyhood of truncating files while they are being read with tftp. otherwise, this happens frequently when xcat is being controlled programmatically Common.pm: add copy_if_newer utility function git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@4660 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- perl-xCAT/xCAT/Common.pm | 25 ++++++++++++++ xCAT-server/lib/xcat/plugins/anaconda.pm | 42 ++++++++++++++++-------- 2 files changed, 54 insertions(+), 13 deletions(-) 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; }