"Fossies" - the Fresh Open Source Software Archive

Member "usr/share/mysql-zrm/plugins/lvm-snapshot.pl" (26 Aug 2013, 4805 Bytes) of package /linux/privat/MySQL-zrm-3.0-release.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "lvm-snapshot.pl" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/perl
    2 #
    3 #
    4 # Copyright (c) 2006 Zmanda Inc.  All Rights Reserved.
    5 #
    6 # This program is free software; you can redistribute it and/or modify it
    7 # under the terms of the GNU General Public License version 2 as published
    8 # by the Free Software Foundation.
    9 #
   10 # This program is distributed in the hope that it will be useful, but
   11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   13 # for more details.
   14 #
   15 # You should have received a copy of the GNU General Public License along
   16 # with this program; if not, write to the Free Software Foundation, Inc.,
   17 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   18 #
   19 # Contact information: Zmanda Inc, 505 N Mathlida Ave, Suite 120
   20 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
   21 #
   22 
   23 use strict;
   24 use warnings;
   25 use Getopt::Long;
   26 use File::Path;
   27 use lib '/usr/lib/mysql-zrm';
   28 use ZRM::SnapshotCommon;
   29 
   30 
   31 $SIG{'TERM'} = sub { &printAndDie("TERM signal"); };
   32 
   33 my $LVCREATE="lvcreate";
   34 my $LVDISPLAY="lvdisplay";
   35 my $LVREMOVE="lvremove -f";
   36 my $NOUUID=" -o nouuid ";
   37 my $DEV="/dev/";
   38 my $MAPPER="/dev/mapper/";
   39 
   40 # Uses df to get the device name and filesystem type
   41 # Uses lvdisplay to see if this device is an lvm volume
   42 # $_[0] directory name
   43 # $_[1] snapshot name
   44 sub getSnapshotDeviceDetails()
   45 {
   46     my $snapshotName = $_[1];
   47         my @ret = `sudo $DF '$_[0]'`;
   48         if( $? != 0 ) {
   49                 &printAndDie( "Failed to find database: ".$_[0] );
   50         }
   51     chomp( @ret );
   52         my @fs = split(" " , $ret[1]);
   53         my $dev = $fs[0];
   54         my $volumeGroup;
   55         my $volumeName;
   56 
   57     # If mapper is being used the device 
   58     # output from df will show /dev/mapper/volume-group/volume-name. 
   59     # Split this up and create the actual device path
   60         if( $fs[0]=~/^$MAPPER/ ){
   61         my $x = $';
   62         $x =~ /([^-])-([^-])/;
   63         $volumeGroup = $`.$1;
   64         $volumeName = $2.$';
   65         $volumeGroup =~ s/--/-/g;
   66         $volumeName =~ s/--/-/g;
   67                 $dev = "/dev/".$volumeGroup."/".$volumeName;
   68         } else {
   69                 if( $fs[0]=~/^$DEV/ ){
   70                         ($volumeGroup, $volumeName) = split( "/", $' );
   71                 }
   72         }
   73         @ret = `sudo $LVDISPLAY $dev 2>&1`;
   74         if( $? == 0 ) {
   75         print "device=$dev\n";
   76         print "snapshot-device=/dev/$volumeGroup/$snapshotName\n";
   77         print "device-mount-point=$fs[6]\n";
   78         print "filesystem-type=$fs[1]\n";
   79                 my $str = &getCommonDetails( $_[0], $_[1], $fs[6] );
   80                 print $str;
   81         }
   82 }
   83 
   84 sub doGetSnapshotdeviceDetails()
   85 {
   86     if( !defined $opt{"directory"} ){
   87         &printAndDie( "Please supply --directory" );
   88     } 
   89     if( !defined  $opt{"sname"} ) {
   90         &printAndDie( "Please supply --sname" );
   91     }
   92     &getSnapshotDeviceDetails( $opt{"directory"}, $opt{"sname"} );
   93 }
   94 
   95 sub doCreateSnapshot()
   96 {
   97     if( !defined $opt{"dev"} ){
   98         &printAndDie( "Please input --dev" );
   99     }
  100     if( !defined $opt{"sname"} ){
  101         &printAndDie( "Please input --sname" );
  102     }
  103     if( !defined $opt{"size"} ){
  104         &printAndDie( "Please input --size" );
  105     }
  106 
  107     my $cmd = "$LVCREATE -L";
  108     $cmd .= $opt{"size"};
  109     $cmd .= " -s -n ";
  110     $cmd .= $opt{"sname"};
  111     $cmd .= " ".$opt{"dev"};
  112     my $r = system( "sudo ".$cmd );
  113     if( $r != 0 ){
  114         &printAndDie( "Snapshot create failed" );
  115     }
  116 }
  117 
  118 sub doMount()
  119 {
  120     if( !defined $opt{"directory"} ){
  121         &printAndDie("Please input mount directory using --directory");
  122     }
  123     if( !defined $opt{"dev"} ){
  124         &printAndDie("Please input device to be mounted using --dev");
  125     }
  126     if( !defined $opt{"fstype"} ){
  127         &printAndDie("Please input filesystem type using --fstype");
  128     }
  129     mkpath( $opt{'directory'} );
  130         my $command = $MOUNT;
  131         if( $opt{"fstype"} eq "xfs" ){
  132             $command .= $NOUUID;
  133         }
  134     $command .= " \"$opt{dev}\" $opt{directory}";
  135     my $r = system( "sudo ".$command ); 
  136     if( $r != 0 ){
  137         &printAndDie( "mount of $opt{dev} on $opt{directory} failed" );
  138     }
  139 }
  140 
  141 sub doUmount()
  142 {
  143     if( !defined $opt{"directory"} ){
  144         &printAndDie("Please specify the directory using --directory");
  145     }
  146     my $command = "$UMOUNT $opt{directory}";
  147     my $r = system( "sudo ".$command ); 
  148     if( $r != 0 ){
  149         &printAndDie( "umount of $opt{directory} failed" );
  150     }
  151     rmtree( $opt{directory}, 0, 0 );
  152 }
  153 
  154 sub doRemoveSnapshot()
  155 {
  156     if( !defined $opt{"dev"} ){
  157         &printAndDie("Please specify the snapshot device using --dev");
  158     }
  159     my $command = $LVREMOVE." \"$opt{dev}\"";  
  160     my $r = system( "sudo ".$command ); 
  161     if( $r != 0 ){
  162         &printAndDie( "remove of $opt{dev} failed" );
  163     }
  164 }
  165 
  166 &initSnapshotPlugin();
  167 if( $action eq "get-vm-device-details" ){
  168     &doGetSnapshotdeviceDetails();
  169 }elsif( $action eq "create-snapshot" ){
  170     &doCreateSnapshot();
  171 }elsif( $action eq "mount" ){
  172     &doMount();
  173 }elsif( $action eq "umount" ){
  174     &doUmount();
  175 }elsif( $action eq "remove-snapshot" ){
  176     &doRemoveSnapshot();
  177 }
  178 exit( 0 );
  179 
  180