summaryrefslogtreecommitdiff
path: root/lib/hdd/__init__.py
blob: 8c63933f7afd57ab6bf422982f497b30f2ec8518 (plain)
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
#!/usr/bin/python

import subprocess
import sys
import re
import pprint

def humanCapasity(b):
    b = float(b)
    G = ['%.0f B', '%.2f KB', '%.2f MB', '%.2f GB', '%.2f TB']
    for p in G:
        if b < 1024:
            return p % b
        else:
            b /= 1024
    raise 'Too large hard drive ;-)'


class Disk(object):
    # fdisk -G /dev/rdsk/c0t0d0p0
    # * Physical geometry for device /dev/rdsk/c0t0d0p0
    # * PCYL     NCYL     ACYL     BCYL     NHEAD NSECT SECSIZ
    #   2088     2088     0        0        255   63    512
    _fdiskG = re.compile('\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*')

    # fdisk -W - /dev/rdsk/c0t0d0p0
    #* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl    Rsect      Numsect
    #  191   128  65     2      0       168    44     380     4096       6111232   
    #  0     0    0      0      0       0      0      0       0          0         
    #  0     0    0      0      0       0      0      0       0          0         
    #  0     0    0      0      0       0      0      0       0          0         
    _fdiskW = re.compile('\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*')

    _name = None
    _desc = None
    _geometry = {}
    _capacity = 0
    _capacity_human = None
    _partitions = []
    _raw_device = None

    @property
    def name(self):
        return self._name

    @property
    def desc(self):
        return self._desc

    @desc.setter
    def desc(self, desc):
        self._desc = desc
    
    def read_partitions():
        self._partitions = {}
        out = subprocess.Popen(['fdisk', '-W', '-', self._raw_device],
                stdout=subprocess.PIPE).stdout
        for line in out:
            m = self._fdiskW.match(line)
            if m:
                self._partitions.append({
                    'id'      : int(m.group(1)),
                    'rsect'   : int(m.group(9)),
                    'numsect' : int(m.group(10)),
                    'cap'     : humanCapasity(int(m.group(10))*self._geometry['secsiz']),
                    })

    @name.setter
    def name(self, name):
        """ Set disk name and update disk's info: capacity, geometry etc."""
        if name != self._name:
            self._raw_device = '/dev/rdsk/{}p0'.format(name)
            self._name = name
            out = subprocess.Popen(['fdisk', '-G', self._raw_device],
                    stdout=subprocess.PIPE).stdout
            for line in out:
                m = self._fdiskG.match(line)
                if m:
                    self._geometry = {
                            'pcyl'   : int(m.group(1)),
                            'ncyl'   : int(m.group(2)),
                            'acyl'   : int(m.group(3)),
                            'bcyl'   : int(m.group(4)),
                            'nhead'  : int(m.group(5)),
                            'nsect'  : int(m.group(6)),
                            'secsiz' : int(m.group(7)),
                            }
            self._capacity = ( self._geometry['ncyl'] * self._geometry['nhead'] *
                             self._geometry['nsect'] * self._geometry['secsiz'] )
            assert self._capacity != 0
            self._capacity_human = humanCapasity(self._capacity)

    @property
    def geometry(self):
        return self._geometry

    @property
    def partitions(self):
        return self._partitions

    @property
    def capacity(self):
        """ Capacity in bytes """
        return self._capacity

    @property
    def cap(self):
        """ Human readable capacity string, e. g. 30 GB """
        return self._capacity_human

    def __init__(self, name, desc=None):
        self.name = name
        self.desc = desc
        pass