Recovering /etc/name_to_major

What do you do if you manage to delete or corrupt /etc/name_to_major? Assuming you don't have a backup a ZFS snapshot or an alternative boot environment, in which case you probably are in the wrong job, you would appear to be in trouble.

First thing is not to panic. Do not reboot the system. If you do that it won't boot and your day has just got a whole lot worse. The data needed to rebuild /etc/name_to_major is in the running kernel so it can be rebuilt from that. If your system an x86 system it is also in the boot archive.

However if you have no boot archive or have over written it with the bad name_to_system this script will extract it from the kernel, all be it slowly:

#!/bin/ksh
i=0
while ((i < 1000 ))
do
print "0t$i::major2name" | mdb -k | read x && echo $x $i
let i=i+1 
done

1Redirect that into a file then move the remains of your /etc/name_to_major out of the way and copy the file in place.

Next time make sure you have a back up or snapshot or alternative boot environment!

1You will see lots of errors of the form “mdb: failed to convert major number to name” these are to be expected. They can be limited to just one by adding “|| break” to the mdb line but that assumes that you have no holes in the major number listings which you may have if you have removed a device, so best to not risk that.

Comments:

Wow, that I should have known som 10 years ago... ;)

Posted by Thommy M. Malmström on September 08, 2009 at 12:25 AM BST #

Faster (runs in two seconds on my system):

i=0
while ((i < 100 ))
do
print "0t$i::major2name"
print "::echo $i"
((i+=1))
done | mdb -k 2>&1 | while read x
do
[[ "$x" = "mdb: failed to convert major number to name" ]] && break
if [[ "$x" != +([0-9]) ]]
then
major=$x
else
print "$major $x"
fi
done

Posted by Nico on September 10, 2009 at 05:17 PM BST #

Thanks nico.

However you do need to go higher than 100 and also you need to not stop on the first error as there can be holes in the name_to_major name space if when drivers are removed.

Although the gains in your script are from not forking so many copies of mdb, which is good.

So up the number to 1000 and change the "break" to "continue" and it is good.

Posted by Chris Gerhard on September 11, 2009 at 01:06 AM BST #

Post a Comment:
Comments are closed for this entry.
About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
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
    
       
Today