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