]> andersk Git - moira.git/blob - incremental/afs_rename.pl
026cdfc662eeba27bd0a05c96709f5cd9852b335
[moira.git] / incremental / afs_rename.pl
1 #!/usr/athena/bin/perl
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     # Lookup volume type for old locker
31     ($c = $oldcell) =~ s/\./_/g;
32     $vtype = eval "\$vtypes_$c{$oldltype}";
33     die "Cannot handle $oldltype volumes\n" unless $vtype;
34     $oldvname = $vtype . "." . $oldname;
35     $oldvname =~ s/[^-A-Za-z0-9_.]//g;  # strip out illegal characters
36
37     if (&check("X" . $oldvname) && &check("Xn." . $oldvname)) {
38         print STDERR "Cannot locate deactivated locker $oldname\n";
39         exit(1) if ($newtype eq "AFS");
40         exit(0);
41     }
42     $newvname = "n." . $newvname if ($oldvname =~ /^Xn\./);
43 } else {
44     $prefix = "";
45
46     open(FS, "$fs lsm $oldpath|");
47     chop($_ = <FS>);
48     close(FS);
49     die "Unable to locate locker $oldname\n" if ($?);
50     ($oldvname = $_) =~ s/^.* volume '.(.*)'$/\1/;
51     die "Unusual mountpoint encountered: $oldpath\n" if ($oldvname =~ /[ :]/);
52     $newvname = "n." . $newvname if ($oldvname =~ /^n\./);
53 }
54
55 $newvname = "X" . $newvname if ($newtype eq "ERR");
56 $newvname =~ s/[^-A-Za-z0-9_.]//g;      # strip out illegal characters
57
58 &run("$vos rename $oldvname $newvname -cell $newcell")
59     if ($oldvname ne $newvname);
60 &run("$vos remove $oldbackup $oldvname.backup -cell $oldcell")
61     if ($oldbackup && $newvname =~ /^n\./);
62
63 if ($oldtype eq "AFS") {
64     &run("$fs rmm $oldpath");
65     &release_parent($oldpath)
66         if ($newtype ne "AFS" || $oldpath ne $newpath);
67 }
68 if ($newtype eq "AFS") {
69     &run("$fs mkm $newpath $newvname");
70     &release_parent($newpath);
71 }
72
73 exit;
74
75
76 sub run
77 {
78     local(@cmd) = @_;
79     system("@cmd >/dev/null");
80     die "@cmd: FAILED\n" if ($?);
81     return 0;
82 }
83
84
85 sub check
86 {
87     local($vname) = @_;
88     local(@vos, @a);
89     
90     open (VOS, "$vos listvldb -name $vname -cell $oldcell 2>/dev/null|");
91     chop(@vos = <VOS>);
92     close(VOS);
93     return 1 if ($?);
94
95     $oldvname = $vname;
96     @a = split(/\s+/, $vos[$[ + 4]);
97     if (($_ = pop @a) eq "valid") {
98         splice(@vos, 0, 6);
99         for (@vos) {
100             ($oldbackup=$_) =~ s/^.*server (.*) partition (.*) RW .*$/\1 \2/
101                 if (/RW Site/);
102         }
103     }
104     return 0;
105 }
106
107 sub release_parent
108 {
109     local($p) = @_;
110
111     $p =~ s:/[^/]+$::;
112     open(FS, "$fs lv $p|") || &fatal("Can't get information about $p");
113     chop($_ = <FS>);
114     close(FS);
115     return if ($?);
116
117     local(@tmp) = (split(/ /,$_));
118     &run("$vos release $tmp[$#tmp] -cell $newcell");
119 }
This page took 0.12024 seconds and 3 git commands to generate.