Home | History | Annotate | Download | only in scripts
      1  5184  ek110237 #!/usr/bin/perl
      2  5184  ek110237 #
      3  5184  ek110237 # CDDL HEADER START
      4  5184  ek110237 #
      5  5184  ek110237 # The contents of this file are subject to the terms of the
      6  5184  ek110237 # Common Development and Distribution License (the "License").
      7  5184  ek110237 # You may not use this file except in compliance with the License.
      8  5184  ek110237 #
      9  5184  ek110237 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     10  5184  ek110237 # or http://www.opensolaris.org/os/licensing.
     11  5184  ek110237 # See the License for the specific language governing permissions
     12  5184  ek110237 # and limitations under the License.
     13  5184  ek110237 #
     14  5184  ek110237 # When distributing Covered Code, include this CDDL HEADER in each
     15  5184  ek110237 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     16  5184  ek110237 # If applicable, add the following below this CDDL HEADER, with the
     17  5184  ek110237 # fields enclosed by brackets "[]" replaced with your own identifying
     18  5184  ek110237 # information: Portions Copyright [yyyy] [name of copyright owner]
     19  5184  ek110237 #
     20  5184  ek110237 # CDDL HEADER END
     21  5184  ek110237 #
     22  5184  ek110237 #
     23  9513    Andrew # Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  5184  ek110237 # Use is subject to license terms.
     25  5184  ek110237 
     26  5184  ek110237 #
     27  5184  ek110237 # Put commands in  here to flush the file system cache after
     28  5184  ek110237 # file set creation but prior to steady state
     29  5184  ek110237 #
     30  5184  ek110237 # For most file systems, filebench already handles fs cache flushing
     31  5184  ek110237 # For ZFS, it needs some help, so this script does
     32  5184  ek110237 #    "zpool export <poolname>" then "zpool import <poolname>"
     33  5184  ek110237 #
     34  5184  ek110237 
     35  5184  ek110237 $fs = $ARGV[0];
     36  5184  ek110237 $dir = $ARGV[1];
     37  5184  ek110237 
     38  5184  ek110237 #
     39  5184  ek110237 # if not zfs, inform user and exit.
     40  5184  ek110237 #
     41  5184  ek110237 if (($fs =~ m/^zfs$/) != 1) {
     42  5184  ek110237         print "filesystem type is: $fs, no action required, so exiting\n";
     43  5184  ek110237         exit(0);
     44  5184  ek110237 }
     45  5184  ek110237 
     46  5184  ek110237 #
     47  5184  ek110237 # It is zfs. Find name of pool to export/import from supplied
     48  5184  ek110237 # directory path name $dir
     49  5184  ek110237 #
     50  5184  ek110237 # Example:
     51  5184  ek110237 # # zfs list -H
     52  5184  ek110237 # tank    164K    24.0G   19K     /tank
     53  5184  ek110237 # tank/a  18K     24.0G   18K     /tank/a
     54  5184  ek110237 # tank/b  18K     24.0G   18K     /wombat
     55  5184  ek110237 # # 
     56  5184  ek110237 # # ./fs_flush zfs /wombat
     57  5184  ek110237 # 'zpool export tank'
     58  5184  ek110237 # 'zpool import tank'
     59  5184  ek110237 # # 
     60  5184  ek110237 #
     61  5184  ek110237 
     62  5184  ek110237 # Get a list of zfs file systems mounted locally
     63  5184  ek110237 @zlist = `/usr/sbin/zfs list -H`;
     64  5184  ek110237 
     65  5184  ek110237 #
     66  5184  ek110237 # Compare the list with the supplied directory path name
     67  5184  ek110237 #
     68  5184  ek110237 chomp @zlist;
     69  5184  ek110237 foreach ( @zlist ) {
     70  5184  ek110237 	#
     71  5184  ek110237 	# For listed zfs file systems, extract the root and
     72  5184  ek110237 	# mount point information
     73  5184  ek110237 	#
     74  5184  ek110237         my $zline = $_;
     75  5184  ek110237         ($root, $b, $c, $d, $mntpnt) = split /\t/, $zline, 5;
     76  5184  ek110237 
     77  5184  ek110237 	# See if the supplied directory path includes this mount point
     78  9513    Andrew         if(($mntpnt ne "\/") && ($dir =~/^$mntpnt/)) {
     79  5184  ek110237 
     80  5184  ek110237 		#
     81  5184  ek110237 		# We have a winner! The root name up to the
     82  5184  ek110237 		# first forward slash is the pool name
     83  5184  ek110237 		#
     84  5184  ek110237                 ($pool) = split /\//, $root;
     85  5184  ek110237 
     86  9513    Andrew 		# save zpool.cache
     87  9513    Andrew 		my $tempfile = "/tmp/zpool.cache$$";
     88  9513    Andrew 		my $cachefile = "";
     89  9513    Andrew 
     90  9513    Andrew 		if (run_prog("cp /etc/zfs/zpool.cache $tempfile") == 0) {
     91  9513    Andrew 			$cachefile = "-c $tempfile "
     92  9513    Andrew 		}
     93  9513    Andrew 
     94  5184  ek110237 		# Do the cache flushing
     95  9513    Andrew 		
     96  9513    Andrew                 print "'zpool export \/ import $pool'\n";
     97  9513    Andrew 		if (run_prog("zpool export $pool") == 0) {
     98  9513    Andrew 			system("zpool import $cachefile$pool");
     99  9513    Andrew 		}
    100  9513    Andrew 		system("rm -f $tempfile")	if ($cachefile ne "");
    101  5184  ek110237                 exit(0);
    102  5184  ek110237         }
    103  5184  ek110237 }
    104  9513    Andrew exit(-1);
    105  9513    Andrew 
    106  9513    Andrew sub run_prog
    107  9513    Andrew {
    108  9513    Andrew 	my $progname = shift;
    109  9513    Andrew 
    110  9513    Andrew 	my $res = system($progname);
    111  9513    Andrew 
    112  9513    Andrew 	#return error if failed for any reason
    113  9513    Andrew 	return -1 if (($res == -1) || ($res & 127));
    114  9513    Andrew 
    115  9513    Andrew 	return ($res >> 8);
    116  9513    Andrew }
    117