[24319] | 1 | #!/usr/bin/perl |
---|
[23095] | 2 | # Usage: afs_change \ |
---|
| 3 | # oldname oldcell oldtype oldltype oldpath \ |
---|
| 4 | # newname newcell newtype newltype newpath |
---|
| 5 | |
---|
| 6 | require "/moira/bin/afs_utils.pl"; |
---|
| 7 | |
---|
| 8 | die "$0: Incorrect number of arguments\n" if (@ARGV != 10); |
---|
| 9 | |
---|
| 10 | ($oldname, $oldcell, $oldtype, $oldltype, $oldpath, |
---|
| 11 | $newname, $newcell, $newtype, $newltype, $newpath) = |
---|
| 12 | @ARGV; |
---|
| 13 | |
---|
| 14 | # Modify the paths, as only the read-write paths are used. |
---|
| 15 | $oldpath =~ s:^/afs/([^.]):/afs/.\1:; # use read-write path |
---|
| 16 | $newpath =~ s:^/afs/([^.]):/afs/.\1:; # use read-write path |
---|
| 17 | |
---|
| 18 | die "Cannot change cells\n" if ($oldcell ne $newcell); |
---|
| 19 | die "Can only handle AFS and ERR lockers\n" |
---|
| 20 | if (($oldtype !~ /^(AFS|ERR)$/) || |
---|
| 21 | ($newtype !~ /^(AFS|ERR)$/)); |
---|
| 22 | |
---|
| 23 | # Lookup volume type |
---|
| 24 | ($c = $newcell) =~ s/\./_/g; |
---|
| 25 | $vtype = eval "\$vtypes_${c}{$newltype}"; |
---|
| 26 | die "Cannot handle $newltype volumes\n" unless $vtype; |
---|
| 27 | $newvname = $vtype . "." . $newname; |
---|
| 28 | |
---|
| 29 | if ($oldtype eq "ERR") { |
---|
| 30 | # If volume was never unmounted, we don't need to do anything. |
---|
| 31 | system("$fs lsm $oldpath > /dev/null"); |
---|
| 32 | exit(0) if ($? == 0); |
---|
| 33 | |
---|
| 34 | # Lookup volume type for old locker |
---|
| 35 | ($c = $oldcell) =~ s/\./_/g; |
---|
| 36 | $vtype = eval "\$vtypes_${c}{$oldltype}"; |
---|
| 37 | die "Cannot handle $oldltype volumes\n" unless $vtype; |
---|
| 38 | $oldvname = $vtype . "." . $oldname; |
---|
| 39 | $oldvname =~ s/[^-A-Za-z0-9_.]//g; # strip out illegal characters |
---|
| 40 | |
---|
| 41 | if (&check("X" . $oldvname) && &check("Xn." . $oldvname)) { |
---|
| 42 | print STDERR "Cannot locate deactivated locker $oldname\n"; |
---|
| 43 | exit(1) if ($newtype eq "AFS"); |
---|
| 44 | exit(0); |
---|
| 45 | } |
---|
| 46 | $newvname = "n." . $newvname if ($oldvname =~ /^Xn\./); |
---|
| 47 | } else { |
---|
| 48 | $prefix = ""; |
---|
| 49 | |
---|
| 50 | open(FS, "$fs lsm $oldpath|"); |
---|
| 51 | chop($_ = <FS>); |
---|
| 52 | close(FS); |
---|
| 53 | die "Unable to locate locker $oldname\n" if ($?); |
---|
| 54 | ($oldvname = $_) =~ s/^.* volume '.(.*)'$/\1/; |
---|
| 55 | die "Unusual mountpoint encountered: $oldpath\n" if ($oldvname =~ /[ :]/); |
---|
| 56 | $newvname = "n." . $newvname if ($oldvname =~ /^n\./); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | $newvname = "X" . $newvname if ($newtype eq "ERR"); |
---|
| 60 | $newvname =~ s/[^-A-Za-z0-9_.]//g; # strip out illegal characters |
---|
| 61 | |
---|
| 62 | if ($oldbackup && $newvname =~ /^n\./) { |
---|
| 63 | system("$vos remove $oldbackup $oldvname.backup -cell $oldcell"); |
---|
| 64 | } |
---|
| 65 | if ($oldvname ne $newvname) { |
---|
| 66 | &run("$vos rename $oldvname $newvname -cell $newcell"); |
---|
| 67 | push(@clean, "$vos rename $newvname $oldvname -cell $newcell"); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | if ($oldtype eq "AFS") { |
---|
| 71 | &run("$fs rmm $oldpath"); |
---|
| 72 | push(@clean, "$fs mkm $oldpath $oldvname"); |
---|
| 73 | &release_parent($oldpath) |
---|
| 74 | if ($newtype ne "AFS" || $oldpath ne $newpath); |
---|
| 75 | } |
---|
| 76 | if ($newtype eq "AFS") { |
---|
| 77 | &run("$fs mkm $newpath $newvname"); |
---|
| 78 | push(@clean, "$fs rmm $newpath"); |
---|
| 79 | $oldfilespath = $newpath . "/OldFiles"; |
---|
| 80 | open(FS, "$fs lsm $oldfilespath|"); |
---|
| 81 | chop($_ = <FS>); |
---|
| 82 | close(FS); |
---|
| 83 | if (! $?) { |
---|
| 84 | ($oldofvname = $_) =~ s/^.* volume '.(.*)'$/\1/; |
---|
| 85 | $newofvname = $newvname . ".backup"; |
---|
| 86 | if ($newofvname ne $oldofvname) { |
---|
| 87 | &run("$fs sa $newpath sms all"); |
---|
| 88 | push(@clean, "$fs sa $newpath sms none"); |
---|
| 89 | &run("$fs rmm $oldfilespath"); |
---|
| 90 | push(@clean, "$fs mkm $oldfilespath $oldofvname"); |
---|
| 91 | &run("$fs mkm $oldfilespath $newofvname"); |
---|
| 92 | push(@clean, "$fs rmm $oldfilespath"); |
---|
| 93 | &run("$fs sa $newpath sms none"); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | &release_parent($newpath); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | &do_releases; |
---|
| 100 | exit; |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | sub run |
---|
| 104 | { |
---|
| 105 | local(@cmd) = @_; |
---|
| 106 | |
---|
| 107 | system("@cmd >/dev/null"); |
---|
| 108 | &fatal("@cmd: FAILED") if ($?); |
---|
| 109 | return 0; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | sub fatal |
---|
| 114 | { |
---|
| 115 | local($cmd); |
---|
| 116 | $_ = join(' ',@_); |
---|
| 117 | s/\n$//; |
---|
| 118 | |
---|
| 119 | while (@clean) { |
---|
| 120 | $cmd = pop(@clean); |
---|
| 121 | warn "$newname: Cleanup failed: $cmd\n" if (system("$cmd")); |
---|
| 122 | } |
---|
| 123 | die "$newname: $_\n"; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | sub check |
---|
| 128 | { |
---|
| 129 | local($vname) = @_; |
---|
| 130 | local(@vos, @a); |
---|
| 131 | |
---|
| 132 | open (VOS, "$vos listvldb -name $vname -cell $oldcell 2>/dev/null|"); |
---|
| 133 | chop(@vos = <VOS>); |
---|
| 134 | close(VOS); |
---|
| 135 | return 1 if ($?); |
---|
| 136 | |
---|
| 137 | $oldvname = $vname; |
---|
| 138 | @a = split(/\s+/, $vos[$[ + 4]); |
---|
| 139 | if (($_ = pop @a) eq "valid") { |
---|
| 140 | splice(@vos, 0, 6); |
---|
| 141 | for (@vos) { |
---|
| 142 | ($oldbackup=$_) =~ s/^.*server (.*) partition (.*) RW .*$/\1 \2/ |
---|
| 143 | if (/RW Site/); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | return 0; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | sub release_parent |
---|
| 150 | { |
---|
| 151 | local($p) = @_; |
---|
| 152 | |
---|
| 153 | $p =~ s:/[^/]+$::; |
---|
| 154 | open(FS, "$fs lv $p|") || &fatal("Can't get information about $p"); |
---|
| 155 | chop($_ = <FS>); |
---|
| 156 | close(FS); |
---|
| 157 | return if ($?); |
---|
| 158 | |
---|
| 159 | local(@tmp) = (split(/ /,$_)); |
---|
| 160 | push(@vrelease, "$tmp[$#tmp] -cell $newcell"); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | sub do_releases |
---|
| 165 | { |
---|
| 166 | local($lastv) = ""; |
---|
| 167 | local(@volumes) = sort @vrelease; |
---|
| 168 | while (@volumes) { |
---|
| 169 | $_ = shift(@volumes); |
---|
| 170 | next if ($_ eq $lastv); |
---|
| 171 | system("$vos release $_"); |
---|
| 172 | } |
---|
| 173 | } |
---|