2008-02-07 18:54:58 +00:00
package xCAT_plugin::packimage ;
2009-09-24 18:51:53 +00:00
BEGIN
{
$ ::XCATROOT = $ ENV { 'XCATROOT' } ? $ ENV { 'XCATROOT' } : '/opt/xcat' ;
}
use lib "$::XCATROOT/lib/perl" ;
2010-09-17 09:54:47 +00:00
use Data::Dumper ;
2008-02-07 18:54:58 +00:00
use xCAT::Table ;
use Getopt::Long ;
use File::Path ;
2008-04-23 20:03:51 +00:00
use File::Copy ;
2008-02-07 18:54:58 +00:00
use Cwd ;
use File::Temp ;
2010-04-07 15:14:29 +00:00
use File::Basename ;
use File::Path ;
2012-08-09 04:07:40 +00:00
#use xCAT::Utils qw(genpassword);
use xCAT::Utils ;
use xCAT::TableUtils ;
2009-07-22 05:59:11 +00:00
use xCAT::SvrUtils ;
2008-02-07 18:54:58 +00:00
Getopt::Long:: Configure ( "bundling" ) ;
Getopt::Long:: Configure ( "pass_through" ) ;
2010-09-17 09:54:47 +00:00
my $ verbose = 0 ;
#$verbose = 1;
2008-02-07 18:54:58 +00:00
sub handled_commands {
return {
packimage = > "packimage" ,
}
}
sub process_request {
my $ request = shift ;
my $ callback = shift ;
my $ doreq = shift ;
2012-08-09 04:07:40 +00:00
my $ installroot = xCAT::TableUtils - > getInstallDir ( ) ;
2008-02-07 18:54:58 +00:00
@ ARGV = @ { $ request - > { arg } } ;
2009-04-14 15:51:20 +00:00
my $ argc = scalar @ ARGV ;
if ( $ argc == 0 ) {
2009-10-02 18:55:54 +00:00
$ callback - > ( { info = > [ "packimage -h \npackimage -v \npackimage [-p profile] [-a architecture] [-o OS] [-m method]\npackimage imagename" ] } ) ;
2009-04-14 15:51:20 +00:00
return ;
}
2009-09-24 18:51:53 +00:00
my $ osver ;
my $ arch ;
my $ profile ;
my $ method = 'cpio' ;
my $ exlistloc ;
my $ syncfile ;
my $ rootimg_dir ;
my $ destdir ;
my $ imagename ;
2008-02-07 18:54:58 +00:00
GetOptions (
"profile|p=s" = > \ $ profile ,
"arch|a=s" = > \ $ arch ,
"osver|o=s" = > \ $ osver ,
2008-03-20 14:45:08 +00:00
"method|m=s" = > \ $ method ,
"help|h" = > \ $ help ,
"version|v" = > \ $ version
2008-02-07 18:54:58 +00:00
) ;
2008-03-20 14:45:08 +00:00
if ( $ version ) {
2008-07-07 18:29:55 +00:00
my $ version = xCAT::Utils - > Version ( ) ;
$ callback - > ( { info = > [ $ version ] } ) ;
2008-03-20 14:45:08 +00:00
return ;
}
2009-04-14 15:51:20 +00:00
if ( $ help ) {
2009-10-02 18:55:54 +00:00
$ callback - > ( { info = > [ "packimage -h \npackimage -v \npackimage [-p profile] [-a architecture] [-o OS] [-m method]\npackimage imagename" ] } ) ;
2008-03-20 14:45:08 +00:00
return ;
}
2009-09-24 18:51:53 +00:00
if ( @ ARGV > 0 ) {
$ imagename = $ ARGV [ 0 ] ;
if ( $ arch or $ osver or $ profile ) {
2010-09-17 09:54:47 +00:00
$ callback - > ( { error = > [ "-o, -p and -a options are not allowed when a image name is specified." ] , errorcode = > [ 1 ] } ) ;
return ;
2009-09-24 18:51:53 +00:00
}
2010-09-17 09:54:47 +00:00
# load the module in memory
2009-09-24 18:51:53 +00:00
eval { require ( "$::XCATROOT/lib/perl/xCAT/Table.pm" ) } ;
if ( $@ ) {
2010-09-17 09:54:47 +00:00
$ callback - > ( { error = > [ $@ ] , errorcode = > [ 1 ] } ) ;
return ;
2009-09-24 18:51:53 +00:00
}
2010-09-17 09:54:47 +00:00
# get the info from the osimage and linux
2009-09-24 18:51:53 +00:00
my $ osimagetab = xCAT::Table - > new ( 'osimage' , - create = > 1 ) ;
2010-09-17 09:54:47 +00:00
unless ( $ osimagetab ) {
$ callback - > ( { error = > [ "The osimage table cannot be opened." ] , errorcode = > [ 1 ] } ) ;
return ;
2009-09-24 18:51:53 +00:00
}
my $ linuximagetab = xCAT::Table - > new ( 'linuximage' , - create = > 1 ) ;
2010-09-17 09:54:47 +00:00
unless ( $ linuximagetab ) {
$ callback - > ( { error = > [ "The linuximage table cannot be opened." ] , errorcode = > [ 1 ] } ) ;
return ;
2009-09-24 18:51:53 +00:00
}
( my $ ref ) = $ osimagetab - > getAttribs ( { imagename = > $ imagename } , 'osvers' , 'osarch' , 'profile' , 'provmethod' , 'synclists' ) ;
2010-09-17 09:54:47 +00:00
unless ( $ ref ) {
$ callback - > ( { error = > [ "Cannot find image \'$imagename\' from the osimage table." ] , errorcode = > [ 1 ] } ) ;
return ;
2009-09-24 18:51:53 +00:00
}
( my $ ref1 ) = $ linuximagetab - > getAttribs ( { imagename = > $ imagename } , 'exlist' , 'rootimgdir' ) ;
2010-09-17 09:54:47 +00:00
unless ( $ ref1 ) {
$ callback - > ( { error = > [ "Cannot find $imagename from the linuximage table." ] , errorcode = > [ 1 ] } ) ;
return ;
2009-09-24 18:51:53 +00:00
}
$ osver = $ ref - > { 'osvers' } ;
$ arch = $ ref - > { 'osarch' } ;
$ profile = $ ref - > { 'profile' } ;
$ syncfile = $ ref - > { 'synclists' } ;
my $ provmethod = $ ref - > { 'provmethod' } ;
unless ( $ osver and $ arch and $ profile and $ provmethod ) {
2010-09-17 09:54:47 +00:00
$ callback - > ( { error = > [ "osimage.osvers, osimage.osarch, osimage.profile and osimage.provmethod must be specified for the image $imagename in the database." ] , errorcode = > [ 1 ] } ) ;
return ;
2009-09-24 18:51:53 +00:00
}
2010-12-15 08:36:53 +00:00
if ( $ provmethod ne 'netboot' ) {
$ callback - > ( { error = > [ "\'$imagename\' cannot be used to build diskless image. Make sure osimage.provmethod is 'netboot'." ] , errorcode = > [ 1 ] } ) ;
2010-09-17 09:54:47 +00:00
return ;
2009-09-24 18:51:53 +00:00
}
$ exlistloc = $ ref1 - > { 'exlist' } ;
$ destdir = $ ref1 - > { 'rootimgdir' } ;
2011-06-22 20:38:25 +00:00
} else {
$ provmethod = "netboot" ;
unless ( $ osver ) {
$ callback - > ( { error = > [ "Please specify a os version with the -o flag" ] , errorcode = > [ 1 ] } ) ;
return ;
}
unless ( $ arch ) {
$ arch = `uname -m` ;
chomp ( $ arch ) ;
$ arch = "x86" if ( $ arch =~ /i.86$/ ) ;
}
unless ( $ profile ) {
$ callback - > ( { error = > [ "Please specify a profile name with -p flag" ] , errorcode = > [ 1 ] } ) ;
return ;
}
2009-09-24 18:51:53 +00:00
}
2010-09-17 09:54:47 +00:00
unless ( $ destdir ) {
2009-09-24 18:51:53 +00:00
$ destdir = "$installroot/netboot/$osver/$arch/$profile" ;
}
$ rootimg_dir = "$destdir/rootimg" ;
2008-02-07 18:54:58 +00:00
my $ distname = $ osver ;
2008-02-12 19:10:33 +00:00
until ( - r "$::XCATROOT/share/xcat/netboot/$distname/" or not $ distname ) {
chop ( $ distname ) ;
}
unless ( $ distname ) {
$ callback - > ( { error = > [ "Unable to find $::XCATROOT/share/xcat/netboot directory for $osver" ] , errorcode = > [ 1 ] } ) ;
return ;
}
2011-06-22 20:38:25 +00:00
unless ( $ installroot ) {
$ callback - > ( { error = > [ "No installdir defined in site table" ] , errorcode = > [ 1 ] } ) ;
return ;
}
my $ oldpath = cwd ( ) ;
2010-09-17 09:54:47 +00:00
unless ( $ imagename ) {
2009-09-24 18:51:53 +00:00
$ exlistloc = xCAT::SvrUtils - > get_exlist_file_name ( "$installroot/custom/netboot/$distname" , $ profile , $ osver , $ arch ) ;
2010-09-17 09:54:47 +00:00
unless ( $ exlistloc ) { $ exlistloc = xCAT::SvrUtils - > get_exlist_file_name ( "$::XCATROOT/share/xcat/netboot/$distname" , $ profile , $ osver , $ arch ) ; }
2011-06-22 20:38:25 +00:00
#save the settings into DB, it will not update if the image already exist
my @ ret = xCAT::SvrUtils - > update_tables_with_diskless_image ( $ osver , $ arch , $ profile , "netboot" ) ;
unless ( $ ret [ 0 ] eq 0 ) {
$ callback - > ( { error = > [ "Error when updating the osimage tables: " . $ ret [ 1 ] ] , errorcode = > [ 1 ] } ) ;
return ;
}
2009-09-24 18:51:53 +00:00
}
2008-09-12 22:40:15 +00:00
2012-09-25 02:51:23 +00:00
#before generating rootimg.gz, copy $installroot/postscripts into the image at /xcatpost
if ( - e "$rootimg_dir/xcatpost" ) {
system ( "rm -rf $rootimg_dir/xcatpost" ) ;
}
system ( "mkdir -p $rootimg_dir/xcatpost" ) ;
system ( "cp -r $installroot/postscripts/* $rootimg_dir/xcatpost/" ) ;
2010-12-15 08:36:53 +00:00
# before generating rootimg.gz or rootimg.sfs, need to switch the rootimg to stateless mode if necessary
2010-09-17 09:54:47 +00:00
my $ rootimg_status = 0 ; # 0 means stateless mode, while 1 means statelite mode
$ rootimg_status = 1 if ( - f "$rootimg_dir/.statelite/litefile.save" ) ;
2010-12-15 08:36:53 +00:00
my $ ref_liteList ; # get the litefile entries
2010-09-17 09:54:47 +00:00
2010-12-15 08:36:53 +00:00
my @ ret = xCAT::Utils - > runcmd ( "ilitefile $osver-$arch-statelite-$profile" , 0 , 1 ) ;
$ ref_liteList = $ ret [ 0 ] ;
2010-09-17 09:54:47 +00:00
2010-12-15 08:36:53 +00:00
my % liteHash ; # create hash table for the entries in @listList
if ( parseLiteFiles ( $ ref_liteList , \ % liteHash ) ) {
$ callback - > ( { error = > [ "Failed for parsing litefile table!" ] , errorcode = > [ 1 ] } ) ;
return ;
}
2010-12-15 08:35:44 +00:00
2010-12-15 08:36:53 +00:00
$ verbose && $ callback - > ( { data = > [ "rootimg_status = $rootimg_status at line " . __LINE__ ] } ) ;
if ( $ rootimg_status ) {
xCAT::Utils - > runcmd ( "mkdir $rootimg_dir/.statebackup" , 0 , 1 ) ;
# read through the litefile table to decide which file/directory should be restore
my $ defaultloc = "$rootimg_dir/.default" ;
foreach my $ entry ( keys % liteHash ) {
my @ tmp = split /\s+/ , $ entry ;
my $ filename = $ tmp [ 1 ] ;
my $ fileopt = $ tmp [ 0 ] ;
if ( $ fileopt =~ m/link/ ) {
# backup them into .statebackup dirctory
# restore the files with "link" options
if ( $ filename =~ m/\/$/ ) {
chop $ filename ;
2010-09-17 09:54:47 +00:00
}
2010-12-15 08:36:53 +00:00
# create the parent directory if $filename's directory is not there,
my $ parent = dirname $ filename ;
unless ( - d "$rootimg_dir/.statebackup$parent" ) {
unlink "$rootimg_dir/.statebackup$parent" ;
$ verbose && $ callback - > ( { data = > [ "mkdir -p $rootimg_dir/.statebackup$parent" ] } ) ;
xCAT::Utils - > runcmd ( "mkdir -p $rootimg_dir/.statebackup$parent" , 0 , 1 ) ;
}
$ verbose && $ callback - > ( { data = > [ "backing up the file $filename.. at line " . __LINE__ ] } ) ;
$ verbose && print "++ $defaultloc$filename ++ $rootimg_dir$filename ++ at " . __LINE__ . "\n" ;
xCAT::Utils - > runcmd ( "mv $rootimg_dir$filename $rootimg_dir/.statebackup$filename" , 0 , 1 ) ;
xCAT::Utils - > runcmd ( "cp -r -a $defaultloc$filename $rootimg_dir$filename" , 0 , 1 ) ;
2010-09-17 09:54:47 +00:00
}
}
}
2010-12-15 08:36:53 +00:00
# TODO: following the old genimage code, to update the stateles-only files/directories
# # another file should be /opt/xcat/xcatdsklspost, but it seems not necessary
xCAT::Utils - > runcmd ( "mv $rootimg_dir/etc/init.d/statelite $rootimg_dir/.statebackup/statelite " , 0 , 1 ) if ( - e "$rootimg_dir/etc/init.d/statelite" ) ;
2010-09-17 09:54:47 +00:00
if ( - e "$rootimg_dir/usr/share/dracut" ) {
# currently only used for redhat families, not available for SuSE families
if ( - e "$rootimg_dir/etc/rc.sysinit.backup" ) {
xCAT::Utils - > runcmd ( "mv $rootimg_dir/etc/rc.sysinit.backup $rootimg_dir/etc/rc.sysinit" , 0 , 1 ) ;
}
}
2010-11-19 19:05:41 +00:00
my $ xcat_packimg_tmpfile = "/tmp/xcat_packimg.$$" ;
2012-04-14 14:19:34 +00:00
my $ excludestr = "find . -xdev " ;
2009-05-19 18:56:27 +00:00
my $ includestr ;
2009-04-13 14:39:55 +00:00
if ( $ exlistloc ) {
2012-10-29 12:39:54 +00:00
my @ excludeslist = split ',' , $ exlistloc ;
foreach my $ exlistlocname ( @ excludeslist ) {
my $ exlist ;
my $ excludetext ;
open ( $ exlist , "<" , $ exlistlocname ) ;
system ( "echo -n > $xcat_packimg_tmpfile" ) ;
while ( <$exlist> ) {
$ excludetext . = $ _ ;
}
close ( $ exlist ) ;
2010-04-15 13:06:52 +00:00
2012-10-29 12:39:54 +00:00
#handle the #INLCUDE# tag recursively
my $ idir = dirname ( $ exlistlocname ) ;
my $ doneincludes = 0 ;
while ( not $ doneincludes ) {
$ doneincludes = 1 ;
if ( $ excludetext =~ /#INCLUDE:[^#^\n]+#/ ) {
$ doneincludes = 0 ;
$ excludetext =~ s/#INCLUDE:([^#^\n]+)#/include_file($1,$idir)/eg ;
}
2010-04-15 13:06:52 +00:00
2012-10-29 12:39:54 +00:00
}
2010-04-07 15:14:29 +00:00
2012-10-29 12:39:54 +00:00
my @ tmp = split ( "\n" , $ excludetext ) ;
foreach ( @ tmp ) {
chomp $ _ ;
s/\s*#.*// ; #-- remove comments
next if /^\s*$/ ; #-- skip empty lines
if ( /^\+/ ) {
s/^\+// ; #remove '+'
$ includestr . = "-path '" . $ _ . "' -o " ;
} else {
s/^\-// ; #remove '-' if any
$ excludestr . = "'!' -path '" . $ _ . "' -a " ;
}
}
2009-04-13 14:39:55 +00:00
}
2012-10-29 12:39:54 +00:00
}
2010-09-17 09:54:47 +00:00
2010-12-15 08:36:53 +00:00
# the files specified for statelite should be excluded
my @ excludeStatelite = ( "./etc/init.d/statelite" , "./etc/rc.sysinit.backup" , "./.statelite*" , "./.default*" , "./.statebackup*" ) ;
foreach my $ entry ( @ excludeStatelite ) {
$ excludestr . = "'!' -path '" . $ entry . "' -a " ;
2010-09-17 09:54:47 +00:00
}
2009-05-19 18:56:27 +00:00
$ excludestr =~ s/-a $// ;
if ( $ includestr ) {
$ includestr =~ s/-o $// ;
2012-04-14 14:19:34 +00:00
$ includestr = "find . -xdev " . $ includestr ;
2009-05-19 18:56:27 +00:00
}
2010-09-17 09:54:47 +00:00
print "\nexcludestr=$excludestr\n\n includestr=$includestr\n\n" ; # debug
2008-04-23 20:03:51 +00:00
2009-05-19 18:56:27 +00:00
# add the xCAT post scripts to the image
2010-09-17 09:54:47 +00:00
unless ( - d "$rootimg_dir" ) {
$ callback - > ( { error = > [ "$rootimg_dir does not exist, run genimage -o $osver -p $profile on a server with matching architecture" ] , errorcode = > [ 1 ] } ) ;
2008-05-12 19:41:17 +00:00
return ;
}
2009-04-23 19:12:33 +00:00
2010-09-17 09:54:47 +00:00
# some rpms like atftp mount the rootimg/proc to /proc, we need to make sure rootimg/proc is free of junk
# before packaging the image
system ( "umount $rootimg_dir/proc" ) ;
copybootscript ( $ installroot , $ rootimg_dir , $ osver , $ arch , $ profile , $ callback ) ;
2008-05-07 14:51:12 +00:00
my $ passtab = xCAT::Table - > new ( 'passwd' ) ;
if ( $ passtab ) {
2011-12-21 06:45:37 +00:00
my $ pass = 'cluster' ;
( my $ pent ) = $ passtab - > getAttribs ( { key = > 'system' , username = > 'root' } , 'password' ) ;
if ( $ pent and defined ( $ pent - > { password } ) ) {
$ pass = $ pent - > { password } ;
}
my $ oldmask = umask ( 0077 ) ;
my $ shadow ;
open ( $ shadow , "<" , "$rootimg_dir/etc/shadow" ) ;
my @ shadents = <$shadow> ;
close ( $ shadow ) ;
open ( $ shadow , ">" , "$rootimg_dir/etc/shadow" ) ;
2013-01-14 14:56:44 +00:00
# 1 - MD5, 5 - SHA256, 6 - SHA512
unless ( ( $ pass =~ /^\$1\$/ ) || ( $ pass =~ /^\$5\$/ ) || ( $ pass =~ /^\$6\$/ ) ) {
2012-08-20 04:35:15 +00:00
$ pass = crypt ( $ pass , '$1$' . xCAT::Utils:: genpassword ( 8 ) ) ;
2011-12-21 06:45:37 +00:00
}
print $ shadow "root:$pass:13880:0:99999:7:::\n" ;
foreach ( @ shadents ) {
unless ( /^root:/ ) {
print $ shadow "$_" ;
}
}
close ( $ shadow ) ;
umask ( $ oldmask ) ;
2008-05-07 14:51:12 +00:00
}
2012-01-19 08:01:57 +00:00
# sync fils configured in the synclist to the rootimage
$ syncfile = xCAT::SvrUtils - > getsynclistfile ( undef , $ osver , $ arch , $ profile , "netboot" , $ imagename ) ;
if ( defined ( $ syncfile ) && - f $ syncfile
&& - d $ rootimg_dir ) {
print "sync files from $syncfile to the $rootimg_dir\n" ;
system ( "$::XCATROOT/bin/xdcp -i $rootimg_dir -F $syncfile" ) ;
2009-09-24 18:51:53 +00:00
}
2008-04-23 20:03:51 +00:00
2008-04-24 12:47:21 +00:00
my $ verb = "Packing" ;
2010-09-17 09:54:47 +00:00
2008-02-07 18:54:58 +00:00
my $ temppath ;
2011-01-06 06:54:06 +00:00
my $ oldmask ;
2010-09-17 09:54:47 +00:00
unless ( - d $ rootimg_dir ) {
2009-09-24 18:51:53 +00:00
$ callback - > ( { error = > [ "$rootimg_dir does not exist, run genimage -o $osver -p $profile on a server with matching architecture" ] } ) ;
2008-05-09 15:31:09 +00:00
return ;
}
2009-09-24 18:51:53 +00:00
$ callback - > ( { data = > [ "$verb contents of $rootimg_dir" ] } ) ;
unlink ( "$destdir/rootimg.gz" ) ;
unlink ( "$destdir/rootimg.sfs" ) ;
2010-12-15 08:36:53 +00:00
if ( $ method =~ /cpio/ ) {
if ( ! $ exlistloc ) {
2012-04-14 14:19:34 +00:00
$ excludestr = "find . -xdev |cpio -H newc -o | gzip -c - > ../rootimg.gz" ;
2010-12-15 08:36:53 +00:00
} else {
2010-09-17 09:54:47 +00:00
chdir ( "$rootimg_dir" ) ;
2010-11-19 19:05:41 +00:00
system ( "$excludestr >> $xcat_packimg_tmpfile" ) ;
2010-09-17 09:54:47 +00:00
if ( $ includestr ) {
2010-12-15 08:36:53 +00:00
system ( "$includestr >> $xcat_packimg_tmpfile" ) ;
2010-12-15 08:35:44 +00:00
}
2010-12-15 08:36:53 +00:00
#$excludestr =~ s!-a \z!|cpio -H newc -o | gzip -c - > ../rootimg.gz!;
$ excludestr = "cat $xcat_packimg_tmpfile|cpio -H newc -o | gzip -c - > ../rootimg.gz" ;
2010-12-15 08:35:44 +00:00
}
$ oldmask = umask 0077 ;
2010-12-15 08:36:53 +00:00
} elsif ( $ method =~ /squashfs/ ) {
$ temppath = mkdtemp ( "/tmp/packimage.$$.XXXXXXXX" ) ;
chmod 0755 , $ temppath ;
chdir ( "$rootimg_dir" ) ;
system ( "$excludestr >> $xcat_packimg_tmpfile" ) ;
if ( $ includestr ) {
system ( "$includestr >> $xcat_packimg_tmpfile" ) ;
}
$ excludestr = "cat $xcat_packimg_tmpfile|cpio -dump $temppath" ;
} else {
$ callback - > ( { error = > [ "Invalid method '$method' requested" ] , errorcode = > [ 1 ] } ) ;
}
chdir ( "$rootimg_dir" ) ;
`$excludestr` ;
if ( $ method =~ /cpio/ ) {
chmod 0644 , "$destdir/rootimg.gz" ;
2008-05-09 15:31:09 +00:00
umask $ oldmask ;
2010-12-15 08:36:53 +00:00
} elsif ( $ method =~ /squashfs/ ) {
my $ flags ;
if ( $ arch =~ /x86/ ) {
$ flags = "-le" ;
} elsif ( $ arch =~ /ppc/ ) {
$ flags = "-be" ;
}
if ( ! - x "/sbin/mksquashfs" ) {
$ callback - > ( { error = > [ "mksquashfs not found, squashfs-tools rpm should be installed on the management node" ] , errorcode = > [ 1 ] } ) ;
return ;
}
my $ rc = system ( "mksquashfs $temppath ../rootimg.sfs $flags" ) ;
if ( $ rc ) {
$ callback - > ( { error = > [ "mksquashfs could not be run successfully" ] , errorcode = > [ 1 ] } ) ;
return ;
}
$ rc = system ( "rm -rf $temppath" ) ;
if ( $ rc ) {
$ callback - > ( { error = > [ "Failed to clean up temp space" ] , errorcode = > [ 1 ] } ) ;
return ;
}
chmod ( 0644 , "../rootimg.sfs" ) ;
2008-02-07 18:54:58 +00:00
}
2010-12-15 08:36:53 +00:00
system ( "rm -f $xcat_packimg_tmpfile" ) ;
2010-11-19 19:05:41 +00:00
2010-12-15 08:36:53 +00:00
# move the files in /.statebackup back to rootimg_dir
if ( $ rootimg_status ) { # statelite mode
foreach my $ entry ( keys % liteHash ) {
my @ tmp = split /\s+/ , $ entry ;
my $ filename = $ tmp [ 1 ] ;
my $ fileopt = $ tmp [ 0 ] ;
if ( $ fileopt =~ m/link/ ) {
chop $ filename if ( $ filename =~ m/\/$/ ) ;
xCAT::Utils - > runcmd ( "rm -rf $rootimg_dir$filename" , 0 , 1 ) ;
xCAT::Utils - > runcmd ( "mv $rootimg_dir/.statebackup$filename $rootimg_dir$filename" , 0 , 1 ) ;
2010-09-17 09:54:47 +00:00
}
}
2010-12-15 08:36:53 +00:00
xCAT::Utils - > runcmd ( "mv $rootimg_dir/.statebackup/statelite $rootimg_dir/etc/init.d/statelite" , 0 , 1 ) ;
xCAT::Utils - > runcmd ( "rm -rf $rootimg_dir/.statebackup" , 0 , 1 ) ;
2010-09-17 09:54:47 +00:00
}
2009-09-19 17:03:14 +00:00
chdir ( $ oldpath ) ;
2008-02-07 18:54:58 +00:00
}
2008-04-23 20:03:51 +00:00
###########################################################
#
# copybootscript - copy the xCAT diskless init scripts to the image
#
#############################################################
sub copybootscript {
my $ installroot = shift ;
2009-09-24 18:51:53 +00:00
my $ rootimg_dir = shift ;
2008-04-23 20:03:51 +00:00
my $ osver = shift ;
my $ arch = shift ;
my $ profile = shift ;
my $ callback = shift ;
2012-08-09 04:07:40 +00:00
my @ timezone = xCAT::TableUtils - > get_site_attribute ( "timezone" ) ;
2008-05-01 20:52:16 +00:00
2008-04-23 20:03:51 +00:00
if ( - f "$installroot/postscripts/xcatdsklspost" ) {
# copy the xCAT diskless post script to the image
2009-09-24 18:51:53 +00:00
mkpath ( "$rootimg_dir/opt/xcat" ) ;
2008-04-23 20:03:51 +00:00
2009-09-24 18:51:53 +00:00
copy ( "$installroot/postscripts/xcatdsklspost" , "$rootimg_dir/opt/xcat/xcatdsklspost" ) ;
2010-04-04 04:01:05 +00:00
if ( $ timezone [ 0 ] ) {
copy ( "$rootimg_dir/usr/share/zoneinfo/$timezone[0]" , "$rootimg_dir/etc/localtime" ) ;
}
2008-04-23 20:03:51 +00:00
2009-09-24 18:51:53 +00:00
chmod ( 0755 , "$rootimg_dir/opt/xcat/xcatdsklspost" ) ;
2008-04-23 20:03:51 +00:00
} else {
my $ rsp ;
push @ { $ rsp - > { data } } , "Could not find the script $installroot/postscripts/xcatdsklspost.\n" ;
xCAT::MsgUtils - > message ( "E" , $ rsp , $ callback ) ;
return 1 ;
}
2010-04-04 04:01:05 +00:00
2008-04-23 20:03:51 +00:00
2010-06-07 19:42:44 +00:00
#if ( -f "$installroot/postscripts/xcatpostinit") {
2008-04-23 20:03:51 +00:00
# copy the linux diskless init script to the image
# - & set the permissions
2010-06-07 19:42:44 +00:00
#copy ("$installroot/postscripts/xcatpostinit","$rootimg_dir/etc/init.d/xcatpostinit");
2008-04-23 20:03:51 +00:00
2010-06-07 19:42:44 +00:00
#chmod(0755,"$rootimg_dir/etc/init.d/xcatpostinit");
2008-04-23 20:03:51 +00:00
# run chkconfig
2009-09-24 18:51:53 +00:00
#my $chkcmd = "chroot $rootimg_dir chkconfig --add xcatpostinit";
2010-06-07 19:42:44 +00:00
#symlink "/etc/init.d/xcatpostinit","$rootimg_dir/etc/rc3.d/S84xcatpostinit";
#symlink "/etc/init.d/xcatpostinit","$rootimg_dir/etc/rc4.d/S84xcatpostinit";
#symlink "/etc/init.d/xcatpostinit","$rootimg_dir/etc/rc5.d/S84xcatpostinit";
2008-05-07 19:11:13 +00:00
#my $rc = system($chkcmd);
#if ($rc) {
#my $rsp;
# push @{$rsp->{data}}, "Could not run the chkconfig command.\n";
# xCAT::MsgUtils->message("E", $rsp, $callback);
# return 1;
# }
2010-06-07 19:42:44 +00:00
#} else {
#my $rsp;
# push @{$rsp->{data}}, "Could not find the script $installroot/postscripts/xcatpostinit.\n";
# xCAT::MsgUtils->message("E", $rsp, $callback);
# return 1;
#}
2008-04-23 20:03:51 +00:00
return 0 ;
}
2010-04-07 15:14:29 +00:00
sub include_file
{
my $ file = shift ;
my $ idir = shift ;
my @ text = ( ) ;
unless ( $ file =~ /^\// ) {
$ file = $ idir . "/" . $ file ;
}
open ( INCLUDE , $ file ) || \
return "#INCLUDEBAD:cannot open $file#" ;
while ( <INCLUDE> ) {
chomp ( $ _ ) ;
s/\s+$// ; #remove trailing spaces
next if /^\s*$/ ; #-- skip empty lines
push ( @ text , $ _ ) ;
}
close ( INCLUDE ) ;
return join ( "\n" , @ text ) ;
}
2010-09-17 09:54:47 +00:00
= head3 parseLiteFiles
In the liteentry table , one directory and its sub - items ( including sub - directory and entries ) can co - exist ;
In order to handle such a scenario , one hash is generated to show the hirarachy relationship
For example , one array with entry names is used as the input:
my @ entries = (
"imagename bind,persistent /var/" ,
"imagename bind /var/tmp/" ,
"imagename tmpfs,rw /root/" ,
"imagename tmpfs,rw /root/.bashrc" ,
"imagename tmpfs,rw /root/test/" ,
"imagename bind /etc/resolv.conf" ,
"imagename bind /var/run/"
) ;
Then , one hash will generated as:
% hashentries = {
'bind,persistent /var/' = > [
'bind /var/tmp/' ,
'bind /var/run/'
] ,
'bind /etc/resolv.conf' = > undef ,
'tmpfs,rw /root/' = > [
'tmpfs,rw /root/.bashrc' ,
'tmpfs,rw /root/test/'
]
} ;
Arguments:
one array with entrynames ,
one hash to hold the entries parsed
Returns:
0 if sucucess
1 if fail
= cut
sub parseLiteFiles {
my ( $ flref , $ dhref ) = @ _ ;
my @ entries = @ { $ flref } ;
foreach ( @ entries ) {
my $ entry = $ _ ;
my @ str = split /\s+/ , $ entry ;
shift @ str ;
$ entry = join "\t" , @ str ;
my $ file = $ str [ 1 ] ;
chop $ file if ( $ file =~ m {/$} ) ;
unless ( exists $ dhref - > { "$entry" } ) {
my $ parent = dirname ( $ file ) ;
# to see whether $parent exists in @entries or not
unless ( $ parent =~ m/\/$/ ) {
$ parent . = "/" ;
}
my @ res = grep { $ _ =~ m/\Q$parent\E$/ } @ entries ;
my $ found = scalar @ res ;
if ( $ found eq 1 ) { # $parent is found in @entries
# handle $res[0];
my @ tmpresentry = split /\s+/ , $ res [ 0 ] ;
shift @ tmpresentry ;
$ res [ 0 ] = join "\t" , @ tmpresentry ;
chop $ parent ;
my @ keys = keys % { $ dhref } ;
my $ kfound = grep { $ _ =~ m/\Q$res[0]\E$/ } @ keys ;
if ( $ kfound eq 0 ) {
$ dhref - > { $ res [ 0 ] } = [] ;
}
push @ { $ dhref - > { "$res[0]" } } , $ entry ;
} else {
$ dhref - > { "$entry" } = ( ) ;
}
}
}
return 0 ;
}
1 ;