Whamcloud - gitweb
LU-9439 scripts: Change behavior of lustre_rmmod
[fs/lustre-release.git] / lustre / scripts / zfsobj2fid
1 #!/usr/bin/env python
2
3 # Copyright (c) 2014, Lawrence Livermore National Security, LLC.
4 # Produced at the Lawrence Livermore National Laboratory.
5 # Written by Christopher J. Morrone <morrone2@llnl.gov>
6 # LLNL-CODE-468512
7 #
8 # This file is part of lustre-tools-llnl.
9 #
10 # This program is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License (as published by the
12 # Free Software Foundation) version 2, dated June 1991.
13 #
14 # This program is distributed in the hope that it will be useful, but WITHOUT
15 # ANY WARRANTY; without even the IMPLIED WARRANTY OF
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # terms and conditions of the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software Foundation,
21 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 # Given a zfs dataset for a Lustre OST and the object number of a file
24 # in that dataset, this script will call zdb to retreive the Lustre FID
25 # and print it out in standard Lustre FID format.
26
27 import sys
28 import subprocess
29
30 def from_bytes(b):
31     return sum(b[i] << i*8 for i in range(len(b)))
32
33 def main():
34     if len(sys.argv) != 3:
35         print "Usage:", sys.argv[0], "<dataset> <object>"
36         return 1
37
38     p = subprocess.Popen(["zdb", "-e", "-vvv", sys.argv[1], sys.argv[2]],
39                           stdout=subprocess.PIPE)
40     pout, perr = p.communicate()
41
42     b = bytearray()
43     found_fid = False
44     for line in pout.split('\n'):
45         part = line.split()
46         if not part or part[0] != 'trusted.fid':
47             continue
48         fid = part[2]
49         found_fid = True
50         while len(fid) > 0:
51             val = fid[0]
52             fid = fid[1:]
53             if val == '\\':
54                 val = fid[0:3]
55                 fid = fid[3:]
56                 b.append(int(val, 8))
57             else:
58                 b.append(ord(val))
59         break
60
61     if not found_fid:
62         print "FID not found on", sys.argv[1], sys.argv[2]
63         return 1
64
65     print '[' \
66         + hex(from_bytes(b[0:8])) \
67         + ':' \
68         + hex(from_bytes(b[8:12])) \
69         + ':' \
70         + hex(from_bytes(b[12:16])) \
71         + ']'
72
73     return 0
74
75 if __name__ == '__main__':
76       sys.exit(main())