Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / scripts / zfsobj2fid
1 #!/usr/bin/env python3
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(data):
31     return hex(sum(data[i] << i * 8 for i in range(len(data))))
32
33 def main():
34     if len(sys.argv) != 3:
35         print('Usage:', sys.argv[0], '<dataset>', '<object>')
36         return 1
37
38     cmd = ['zdb', '-e', '-vvv', sys.argv[1], sys.argv[2]]
39     process = subprocess.Popen(cmd,
40                                stdout=subprocess.PIPE,
41                                stderr=subprocess.PIPE,
42                                universal_newlines=True)
43     stdout, stderr = process.communicate()
44     result = process.returncode
45
46     if result != 0:
47         msg = 'Error %d on %s: %s %s' % (result, cmd, stdout, stderr)
48         raise RuntimeError(msg)
49
50     lines = stdout.splitlines()
51     data = bytearray()
52     found_fid = False
53
54     for line in lines:
55         part = line.split()
56         if not part or part[0] != 'trusted.fid':
57             continue
58         fid = part[2]
59         found_fid = True
60         while len(fid) > 0:
61             val = fid[0]
62             fid = fid[1:]
63             if val == '\\':
64                 val = fid[0:3]
65                 fid = fid[3:]
66                 data.append(int(val, 8))
67             else:
68                 data.append(ord(val))
69         break
70
71     if not found_fid:
72         print('FID not found on', sys.argv[1], sys.argv[2])
73         return 1
74
75     print('[%s:%s:%s]' % (from_bytes(data[0:8]),
76                           from_bytes(data[8:12]),
77                           from_bytes(data[12:16])))
78     return 0
79
80 if __name__ == '__main__':
81     sys.exit(main())