From 138b9ec92831ae152b685a0ed58bcae385a5367b Mon Sep 17 00:00:00 2001 From: ligc Date: Tue, 14 Sep 2010 08:22:36 +0000 Subject: [PATCH] rmimage code drop git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@7444 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- xCAT-client/pods/man1/rmimage.1.pod | 89 +++++++++++ xCAT-client/xCAT-client.spec | 1 + xCAT-server/lib/xcat/plugins/rmimage.pm | 197 ++++++++++++++++++++++++ 3 files changed, 287 insertions(+) create mode 100644 xCAT-client/pods/man1/rmimage.1.pod create mode 100644 xCAT-server/lib/xcat/plugins/rmimage.pm diff --git a/xCAT-client/pods/man1/rmimage.1.pod b/xCAT-client/pods/man1/rmimage.1.pod new file mode 100644 index 000000000..b3556c4fa --- /dev/null +++ b/xCAT-client/pods/man1/rmimage.1.pod @@ -0,0 +1,89 @@ +=head1 NAME + +B - Removes the Linux stateless or statelite image from the file system. + +=head1 SYNOPSIS + +I + + +I I<-o > I<-a > I<-p > + +I + +=head1 DESCRIPTION + +Removes the Linux stateless or statelite image from the file system. +The install dir is setup by using "installdir" attribute set in the site table. + +If I is specified, this command uses the information in the I +to calculate the image root directory; otherwise, this command uses the operating system name, +architecture and profile name to calculate the image root directory. + +The osimage definition will not be removed from the xCAT tables by default, +specifying the flag --xcatdef will remove the osimage definition, +or you can use rmdef -t osimage to remove the osimage definition. + +The statelite image files on the diskful service nodes will not be removed, +remove the image files on the service nodes manually if necessary, +for example, use command "rsync -az --delete /install :/" to remove the image files on the service nodes, +where the is the hostname of the service node. + +=head1 Parameters + +I specifies the name of an os image definition to be used. The specification for the image is stored in the I table and I table. + +=head1 OPTIONS + + +B<-h | --help> Display usage message. + +B<-V | --verbose> Verbose mode. + +B<-o> Operating system (fedora9,rhels5.5,sles11,etc) + +B<-p> Profile (compute,service,etc) + +B<-a> Architecture (ppc64,x86_64,etc) + +B<--xcatdef> Remove the xCAT osimage definition + + +=head1 RETURN VALUE + +0 The command completed successfully. + +1 An error has occurred. + +=head1 EXAMPLES + +1. To remove a fedora9 stateless image for a compute node architecture x86_64, enter: + +I + +or + +I + +2. To remove a rhels5.5 statelite image for a compute node architecture ppc64 and the osimage definition, enter: + +I + +or + +I + +=head1 FILES + +/opt/xcat/sbin/rmimage + + +=head1 NOTES + +This command is part of the xCAT software product. + +=head1 SEE ALSO + +L, L + + diff --git a/xCAT-client/xCAT-client.spec b/xCAT-client/xCAT-client.spec index d20cf6c19..e130768e9 100644 --- a/xCAT-client/xCAT-client.spec +++ b/xCAT-client/xCAT-client.spec @@ -147,6 +147,7 @@ ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/nodegrpch ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/sbin/tabdump ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/sbin/tabprune ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/sbin/packimage +ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/sbin/rmimage ln -sf ../bin/xcatclient $RPM_BUILD_ROOT/%{prefix}/sbin/makedns ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/bin/gettab ln -sf ../bin/xcatclientnnr $RPM_BUILD_ROOT/%{prefix}/sbin/nodeadd diff --git a/xCAT-server/lib/xcat/plugins/rmimage.pm b/xCAT-server/lib/xcat/plugins/rmimage.pm new file mode 100644 index 000000000..79773163f --- /dev/null +++ b/xCAT-server/lib/xcat/plugins/rmimage.pm @@ -0,0 +1,197 @@ +#!/usr/bin/env perl +# IBM(c) 2010 EPL license http://www.eclipse.org/legal/epl-v10.html +package xCAT_plugin::rmimage; + +BEGIN +{ + $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat'; +} + +use lib "$::XCATROOT/lib/perl"; +use Getopt::Long; +use File::Path; +use xCAT::Utils; +use xCAT::DBobjUtils; + +Getopt::Long::Configure("bundling"); +Getopt::Long::Configure("pass_through"); + +sub handled_commands { + return { + rmimage => "rmimage", + } +} + +sub process_request { + my $request = shift; + my $callback = shift; + my $doreq = shift; + my $installroot = xCAT::Utils->getInstallDir(); + my $tftproot = xCAT::Utils->getTftpDir(); + + my $usage = "\nUsage:\n rmimage [-h | --help]\n rmimage [-V | --verbose] -o -a -p \n rmimage [-V | --verbose] imagename [--xcatde]"; + + @ARGV = @{$request->{arg}}; + + my $osver; + my $arch; + my $profile; + my $method; + my $xcatdef; + my $imagename; + my $imagedir; + + if (!xCAT::Utils->isLinux()) { + $callback->({error=>["The rmimage command is only supported on Linux."],errorcode=>[1]}); + return; + } + + if (!GetOptions( + 'o=s' => \$osver, + 'a=s' => \$arch, + 'p=s' => \$profile, + 'h|help' => \$help, + 'V|verbose' => \$verbose, + 'xcatdef' => \$xcatdef + )) { + $callback->({error=>["$usage"],errorcode=>[1]}); + exit 1; + } + + if ($help) { + $callback->({info=>["$usage"]}); + return; + } + + if (@ARGV > 0) { + $imagename=$ARGV[0]; + if($verbose) { + $callback->({info=>["image name is $imagename"]}); + } + + if ($arch or $osver or $profile) { + $callback->({error=>["-o, -a and -p options are not allowed when a image name is specified."],errorcode=>[1]}); + return; + } + + } else { + + if (!$osver or !$arch or !$profile) { + $callback->({error=>["Missing flag -o, -a or -p"],errorcode=>[1]}); + return; + } + + if ($xcatdef) { + $callback->({error=>["--xcatdef can not be used with the -o, -a or -p"],errorcode=>[1]}); + return; + } + } + + if ($imagename) { + #Check the provemethod when imagename is specified + my $osimagetab = xCAT::Table->new('osimage', -create=>1); + if (!$osimagetab) { + $callback->({error=>["Can not open osimage table."],errorcode=>[1]}); + return; + } + (my $ref) = $osimagetab->getAttribs({imagename => $imagename}, 'osvers', 'osarch', 'profile', 'provmethod'); + if ($ref) { + $osver = $ref->{'osvers'}; + $arch = $ref->{'osarch'}; + $profile = $ref->{'profile'}; + $method = $ref->{'provmethod'};; + } + if($verbose) { + $callback->({info=>["For osimage $imagename: osver = $osver, arch = $arch, profile = $profile, method = $method in osimage table"]}); + } + if (($method) && ($method ne "netboot") && ($method ne "statelite")) { + $callback->({error=>["Invalid method \"$method\", the rmimage command can only be used to remove the netboot or statelite image files"],errorcode=>[1]}); + return; + } + + #Check the rootimgdir when imagename is specified + my $linuximagetab = xCAT::Table->new('linuximage', -create=>1); + if (!$linuximagetab) { + $callback->({error=>["Can not open linuximage table."],errorcode=>[1]}); + return; + } + (my $ref1) = $linuximagetab->getAttribs({imagename => $imagename}, 'rootimgdir'); + if (($ref1) && ($ref1->{'rootimgdir'})) { + $imagedir = $ref1->{'rootimgdir'}; + } + + if($verbose) { + $callback->({info=>["For osimage $imagename: rootimgdir = $imagedir in linuximage table"]}); + } + + # If the rootimgdir is empty, use the osver, arch and profile to form the rootimgdir + if (!$imagedir) { + # If any of the osver, arch or profile is empty in osimage table, + # use the imagename to get the attributes + if (!$osver or !$arch or !$profile) { + #split the imagename + ($osver, $arch, $method, $profile) = split(/-/, $imagename, 4); + if (!$osver or !$arch or !$profile or !$method) { + $callback->({error=>["Invalid image name $imagename"],errorcode=>[1]}); + return; + } + if (($method ne "netboot") && ($method ne "statelite")) { + $callback->({error=>["Invalid method \"$method\", the rmimage command can only be used to remove the netboot or statelite image files"],errorcode=>[1]}); + return; + } + } + $imagedir = "$installroot/netboot/$osver/$arch/$profile"; + } + } else { # imagename is not specified + $imagedir = "$installroot/netboot/$osver/$arch/$profile"; + } + + if($verbose) { + $callback->({info=>["image directory is $imagedir"]}); + } + + if (! -d $imagedir) { + $callback->({error=>["Image directory $imagedir does not exist"],errorcode=>[1]}); + return; + } + + my $tftpdir = "$tftproot/xcat/netboot/$osver/$arch/$profile"; + my @filestoremove = ("$imagedir/rootimg.gz", "$imagedir/kernel", "$imagedir/initrd.gz", "$tftpdir/initrd.gz", "$tftpdir/kernel"); + + #some rpms like atftp mount the rootimg/proc to /proc, we need to make sure rootimg/proc is free of junk + `umount -l $imagedir/rootimg/proc 2>&1 1>/dev/null`; + + #Start removing the rootimg directory and files + if (-d "$imagedir/rootimg") { + $callback->({info=>["Removing directory $imagedir/rootimg"]}); + rmtree "$imagedir/rootimg"; + } + + foreach my $fremove (@filestoremove) { + if (-f $fremove) { + $callback->({info=>["Removing file $fremove"]}); + unlink("$fremove"); + } + } + + $callback->({info=>["Image files have been removed successfully from this management node."]}); + + if (!$imagename || ($method eq "statelite")) { + $callback->({info=>["Please be aware that the statelite image files on the diskful service nodes are not removed, you can remove the image files on the service nodes manually if necessary, for example, use command \"rsync -az --delete $installroot :/\" to remove the image files on the service nodes, where the is the hostname of the service node."]}); + } + + if ($xcatdef && $imagename) { + $callback->({info=>["Removing osimage definition for $imagename."]}); + my %objhash = (); + $objhash{$imagename} = 'osimage'; + # remove the objects + if (xCAT::DBobjUtils->rmobjdefs(\%objhash) != 0) + { + $callback->({error=>["Failed to remove the osimage definition for $imagename"],errorcode=>[1]}); + } else { + $callback->({info=>["osimage definition for $imagename is removed successfully."]}); + } + } +} + +1;