added warning (if kill own rootfs)
[enigma2.git] / lib / python / Components / Harddisk.py
1 import os
2
3 def tryOpen(filename):
4         try:
5                 procFile = open(filename)
6         except IOError:
7                 return ""
8         return procFile
9
10 def num2prochdx(num):
11         return "/proc/ide/hd" + ("a","b","c","d","e","f","g","h","i")[num] + "/"
12
13 class Harddisk:
14         def __init__(self, index):
15                 self.index = index
16
17                 host = self.index / 4
18                 bus = (self.index & 2)
19                 target = (self.index & 1)
20
21                 self.prochdx = num2prochdx(index)
22                 self.devidex = "/dev/ide/host%d/bus%d/target%d/lun0/" % (host, bus, target)
23
24         def getIndex(self):
25                 return self.index
26
27         def capacity(self):
28                 procfile = tryOpen(self.prochdx + "capacity")
29                 
30                 if procfile == "":
31                         return -1
32
33                 line = procfile.readline()
34                 procfile.close()
35                 
36                 try:
37                         cap = int(line)
38                 except:
39                         return -1
40                 
41                 return cap      
42                                                 
43         def model(self):
44                 procfile = tryOpen(self.prochdx + "model")
45                 
46                 if procfile == "":
47                         return ""
48
49                 line = procfile.readline()
50                 procfile.close()
51
52                 return line.strip()
53
54         def free(self):
55                 procfile = tryOpen("/proc/mounts")
56                 
57                 if procfile == "":
58                         return -1
59
60                 free = -1
61                 while 1:
62                         line = procfile.readline()
63                         if line == "":
64                                 break
65                         if line.startswith(self.devidex):
66                                 parts = line.strip().split(" ")
67                                 try:
68                                         stat = os.statvfs(parts[1])
69                                 except OSError:
70                                         continue
71                                 free = stat.f_bfree/1000 * stat.f_bsize/1000
72                                 break
73                 procfile.close()
74                 return free             
75
76         def numPartitions(self):
77                 try:
78                         idedir = os.listdir(self.devidex)
79                 except OSError:
80                         return -1
81                 numPart = -1
82                 for filename in idedir:
83                         if filename.startswith("disc"):
84                                 numPart += 1
85                         if filename.startswith("part"):
86                                 numPart += 1
87                 return numPart
88
89         def unmount(self):
90                 cmd = "/bin/umount " + self.devidex + "part*"
91                 os.system(cmd)
92
93         def createPartition(self):
94                 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
95                 sfdisk = os.popen(cmd, "w")
96                 sfdisk.write("0,\n;\n;\n;\ny\n")
97                 sfdisk.close()
98                 return 0
99
100         def mkfs(self):
101                 cmd = "/sbin/mkfs.ext3 -T largefile -m0 " + self.devidex + "part1"
102                 res = os.system(cmd)
103                 return (res >> 8)
104
105         def mount(self):
106                 cmd = "/bin/mount -t ext3 " + self.devidex + "part1 /hdd"
107                 res = os.system(cmd)
108                 return (res >> 8)
109
110         def createMovieFolder(self):
111                 res = os.system("mkdir /hdd/movie")
112                 return (res >> 8)
113                 
114         def initialize(self):
115                 self.unmount()
116
117                 if self.createPartition() != 0:
118                         return -1
119
120                 if self.mkfs() != 0:
121                         return -2
122
123                 if self.mount() != 0:
124                         return -3
125
126                 if self.createMovieFolder() != 0:
127                         return -4
128                 
129                 return 0
130                 
131 def existHDD(num):
132         mediafile = tryOpen(num2prochdx(num) + "media")
133
134         if mediafile == "":
135                 return -1
136
137         line = mediafile.readline()
138         mediafile.close()
139         
140         if line.startswith("disk"):
141                 return 1
142         
143         return -1
144
145 class HarddiskManager:
146         def __init__(self):
147                 hddNum = 0
148                 self.hdd = [ ]
149                 while 1:
150                         if existHDD(hddNum) == 1:
151                                 self.hdd.append(Harddisk(hddNum))
152                         hddNum += 1
153                         
154                         if hddNum > 8:
155                                 break
156
157         def HDDList(self):
158                 list = [ ]
159                 for hd in self.hdd:
160                         cap = hd.capacity() / 1000 * 512 / 1000
161                         print cap
162                         hdd = hd.model() + " (" 
163                         if hd.index & 1:
164                                 hdd += "slave"
165                         else:   
166                                 hdd += "master"
167                         if cap > 0:
168                                 hdd += ", %d.%03d GB" % (cap/1024, cap%1024)
169                         hdd += ")"
170
171                         print hdd
172                         
173 #                       if hd.index == 0:
174 #                               if hd.initialize() == 0:
175 #                                       print "hdd status ok"
176 #                               else:
177 #                                       print "hdd status ok"
178
179                         list.append((hdd, hd))
180                 return list
181
182
183 harddiskmanager = HarddiskManager()
184
185