1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
#!/usr/bin/perl
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
use Getopt::Std;
use Cwd;
use strict;
package MDesc;
use constant {
MDEND => 0x45,
MDNODE => 0x4e,
MDARC => 0x61,
MDDATA => 0x64,
MDSTR => 0x73,
MDVAL => 0x76,
};
sub new {
my $class = shift;
my $self = {};
$self->{FILE} = undef;
$self->{MAJOR} = undef;
$self->{MINOR} = undef;
$self->{NODE_SEC_SZ} = undef;
$self->{NAME_SEC_SZ} = undef;
$self->{DATA_SEC_SZ} = undef;
$self->{NODES} = undef;
$self->{NAMES} = undef;
$self->{DATA} = undef;
bless($self, $class);
return $self;
}
sub open {
my $self = shift;
my ($mdhdr, $size);
if (@_) {
$self->{NAME} = shift;
} else {
$self->{NAME} = '/dev/mdesc';
}
return unless open(MD, "$self->{NAME}");
# Read and parse MD header
unless (read(MD, $mdhdr, 16) == 16) {
close (MD);
return;
}
($self->{MAJOR}, $self->{MINOR},
$self->{NODE_SEC_SZ},
$self->{NAME_SEC_SZ},
$self->{DATA_SEC_SZ}) = unpack("nnNNN", $mdhdr);
$size = read(MD, $self->{NODES}, $self->{NODE_SEC_SZ});
$size = read(MD, $self->{NAMES}, $self->{NAME_SEC_SZ});
$size = read(MD, $self->{DATA}, $self->{DATA_SEC_SZ});
1;
}
#
# return hash of given node's information
#
sub getnode {
my ($self, $nodeid) = @_;
my ($tag, $name, $namelen, $nameoff, $datalen, $dataoff, %node);
($tag, $namelen, $nameoff, $datalen, $dataoff) =
unpack("CCx2NNN", substr($self->{NODES}, $nodeid * 16, 16));
$name = substr($self->{NAMES}, $nameoff, $namelen);
%node = (tag => $tag, name => $name, nameid => $nameoff);
if ($tag == MDSTR || $tag == MDDATA) {
$node{'datalen'} = $datalen;
$node{'dataoff'} = $dataoff;
} elsif ($tag == MDVAL) {
$node{'val'} = ($datalen << 32) | $dataoff;
} elsif ($tag == MDARC || $tag == MDNODE) {
$node{'idx'} = ($datalen << 32) | $dataoff;
}
return %node;
}
#
# return hash of given property's information
#
sub getprop {
my ($self, $propid) = @_;
my (%node, $tag, %prop);
%node = $self->getnode($propid);
$tag = $node{'tag'};
%prop = (name => $node{'name'}, tag => $tag);
if ($tag == MDSTR) {
$prop{'string'} =
substr($self->{DATA}, $node{'dataoff'}, $node{'datalen'} - 1);
} elsif ($tag == MDARC) {
$prop{'arc'} = $node{'idx'};
} elsif ($tag == MDVAL) {
$prop{'val'} = $node{'val'};
} elsif ($tag == MDDATA) {
$prop{'length'} = $node{'datalen'};
$prop{'offset'} = $node{'dataoff'};
} else {
return undef;
}
return %prop;
}
#
# find name table index of given name
#
sub findname {
my ($self, $name) = @_;
my ($idx, $next, $p);
for ($idx = 0; $idx < $self->{NAME_SEC_SZ}; $idx = $next + 1) {
$next = index($self->{NAMES}, "\0", $idx);
$p = substr($self->{NAMES}, $idx, $next - $idx);
return $idx if ($p eq $name);
}
return -1;
}
#
# find given property in node
#
sub findprop {
my ($self, $nodeid, $propname, $type) = @_;
my (%node, $nameid);
%node = $self->getnode($nodeid);
return -1 if ($node{'tag'} != MDNODE);
$nameid = $self->findname($propname);
return -1 if ($nameid == -1);
do {
$nodeid++;
%node = $self->getnode($nodeid);
if ($node{'tag'} == $type && $node{'nameid'} == $nameid) {
return $nodeid;
}
} while ($node{'tag'} != MDEND);
return -1;
}
#
# lookup property in node and return its hash
#
sub lookup {
my ($self, $nodeid, $propname, $type) = @_;
my ($propid);
$propid = $self->findprop($nodeid, $propname, $type);
return undef if ($propid == -1);
return $self->getprop($propid);
}
sub scan_node {
my ($self, $nodeid, $nameid, $arcid, $ret, $seen) = @_;
my (%node);
return if ($seen->[$nodeid] == 1);
$seen->[$nodeid] = 1;
%node = $self->getnode($nodeid);
return if ($node{'tag'} != MDNODE);
push(@$ret, $nodeid) if ($node{'nameid'} == $nameid);
do {
$nodeid++;
%node = $self->getnode($nodeid);
if ($node{'tag'} == MDARC && $node{'nameid'} == $arcid) {
$self->scan_node($node{'idx'}, $nameid, $arcid, $ret, $seen);
}
} while ($node{'tag'} != MDEND);
}
#
# scan dag from 'start' via 'arcname'
# return list of nodes named 'nodename'
#
sub scan {
my ($self, $start, $nodename, $arcname) = @_;
my ($nameid, $arcid, @ret, @seen);
$nameid = $self->findname($nodename);
$arcid = $self->findname($arcname);
$self->scan_node($start, $nameid, $arcid, \@ret, \@seen);
return @ret;
}
package main;
#
# 'find' needs to use globals anyway,
# so we might as well use the same ones
# everywhere
#
our ($old, $new);
our %opts;
#
# fix path_to_inst
#
sub fixinst {
use File::Copy;
my ($oldpat, $newpat);
my ($in, $out);
$oldpat = '"' . $old . '/';
$newpat = '"' . $new . '/';
$in = "etc/path_to_inst";
$out = "/tmp/path$$";
open(IN, "<", $in) or die "can't open $in\n";
open(OUT, ">", $out) or die "can't open $out\n";
my ($found, $path);
#
# first pass
# see if there are any old paths that need to be re-written
#
$found = 0;
while (<IN>) {
($path, undef, undef) = split;
if ($path =~ /^$oldpat/) {
$found = 1;
last;
}
}
# return if no old paths found
if ($found == 0) {
close(IN);
close(OUT);
unlink $out;
return 0;
}
print "replacing $old with $new in /etc/path_to_inst\n";
#
# 2nd pass
# substitute new for old
#
seek(IN, 0, 0);
while (<IN>) {
($path, undef, undef) = split;
if ($path =~ /^$oldpat/) {
s/$oldpat/$newpat/;
}
print OUT;
}
close(IN);
close(OUT);
if ($opts{v}) {
print "path_to_inst changes:\n";
system("/usr/bin/diff", $in, $out);
print "\n";
}
move $out, $in or die "can't modify $in\n";
return 1;
}
our $oldpat;
sub wanted {
my $targ;
-l or return;
$targ = readlink;
if ($targ =~ /$oldpat/) {
$targ =~ s/$old/$new/;
unlink;
symlink $targ, $_;
print "symlink $_ changed to $targ\n" if ($opts{v});
}
}
#
# fix symlinks
#
sub fixdev {
use File::Find;
$oldpat = "/devices" . $old;
print "updating /dev symlinks\n";
find \&wanted, "dev";
}
#
# fixup path_to_inst and /dev symlinks
#
sub fixup {
# setup globals
($old, $new) = @_;
# if fixinst finds no matches, no need to run fixdev
return if (fixinst == 0);
fixdev;
print "\n" if ($opts{v});
}
#
# remove caches
#
sub rmcache {
unlink "etc/devices/devid_cache";
unlink "etc/devices/devname_cache";
unlink <etc/devices/mdi_*_cache>;
unlink "etc/devices/retire_store";
unlink "etc/devices/snapshot_cache";
unlink "dev/.devlink_db";
}
# $< == 0 or die "$0: must be run as root\n";
getopts("vR:", \%opts);
if ($opts{R}) {
chdir $opts{R} or die "can't chdir to $opts{R}\n";
}
cwd() ne "/" or die "can't run on root directory\n";
if ($#ARGV == 1) {
#
# manual run (no MD needed)
#
fixup @ARGV;
rmcache;
exit;
}
my ($md, @nodes, $nodeid, @aliases, $alias);
my (%newpath, %roots);
#
# scan MD for ioaliases
#
$md = MDesc->new;
$md->open;
@nodes = $md->scan(0, "ioaliases", "fwd");
$#nodes == 0 or die "missing ioaliases node\n";
#
# foreach ioalias node, replace any 'alias' paths
# with the 'current' one
#
# complicating this is that the alias paths can be
# substrings of each other, which can cause false
# hits in /etc/path_to_inst, so first gather all
# aliases with the same root into a list, then sort
# it by length so we always fix the longer alias
# paths before the shorter ones
#
@nodes = $md->scan(@nodes[0], "ioalias", "fwd");
foreach $nodeid (@nodes) {
my (%prop, $current);
%prop = $md->lookup($nodeid, "aliases", $md->MDSTR);
@aliases = split(/ /, $prop{'string'});
%prop = $md->lookup($nodeid, "current", $md->MDSTR);
$current = $prop{'string'};
foreach $alias (@aliases) {
next if ($alias eq $current);
my ($slash, $root);
$newpath{$alias} = $current;
$slash = index($alias, '/', 1);
if ($slash == -1) {
$root = $alias;
} else {
$root = substr($alias, 0, $slash);
}
push(@{ $roots{$root} }, $alias);
}
}
my $aref;
foreach $aref (values %roots) {
@aliases = sort { length($b) <=> length($a) } @$aref;
foreach $alias (@aliases) {
fixup $alias, $newpath{$alias};
}
}
rmcache;
|