1 from os import system, listdir, statvfs, popen, makedirs, readlink, stat, major, minor
2 from Tools.Directories import SCOPE_HDD, resolveFilename
3 from Tools.CList import CList
4 from SystemInfo import SystemInfo
8 procFile = open(filename)
14 def __init__(self, device):
16 procfile = tryOpen("/sys/block/"+self.device+"/dev")
17 tmp = procfile.readline().split(':')
20 for disc in listdir("/dev/discs"):
21 path = readlink('/dev/discs/'+disc)
22 devidex = '/dev'+path[2:]+'/'
24 ret = stat(disc).st_rdev
25 if s_major == major(ret) and s_minor == minor(ret):
26 self.devidex = devidex
27 print "new Harddisk", device, self.devidex
31 return self.device < ob.device
34 ide_cf = self.device.find("hd") == 0 and self.devidex.find("host0") == -1 # 7025 specific
35 internal = self.device.find("hd") == 0 and self.devidex
45 procfile = tryOpen("/sys/block/"+self.device+"/size")
48 line = procfile.readline()
54 return cap / 1000 * 512 / 1000
60 return "%d.%03d GB" % (cap/1024, cap%1024)
63 if self.device.find("hd") == 0:
64 procfile = tryOpen("/proc/ide/"+self.device+"/model")
67 line = procfile.readline()
70 elif self.device.find("sd") == 0:
71 procfile = tryOpen("/sys/block/"+self.device+"/device/vendor")
74 vendor = procfile.readline().strip()
76 procfile = tryOpen("/sys/block/"+self.device+"/device/model")
77 model = procfile.readline().strip()
78 return vendor+'('+model+')'
80 assert False, "no hdX or sdX"
83 procfile = tryOpen("/proc/mounts")
90 line = procfile.readline()
93 if line.startswith(self.devidex):
94 parts = line.strip().split(" ")
96 stat = statvfs(parts[1])
99 free = stat.f_bfree/1000 * stat.f_bsize/1000
104 def numPartitions(self):
106 idedir = listdir(self.devidex)
110 for filename in idedir:
111 if filename.startswith("disc"):
113 if filename.startswith("part"):
118 procfile = tryOpen("/proc/mounts")
125 for line in procfile:
126 if line.startswith(self.devidex):
128 cmd = ' '.join([cmd, parts[1]])
135 def createPartition(self):
136 cmd = "/sbin/sfdisk -f " + self.devidex + "disc"
137 sfdisk = popen(cmd, "w")
138 sfdisk.write("0,\n;\n;\n;\ny\n")
143 cmd = "/sbin/mkfs.ext3 "
144 if self.diskSize() > 4 * 1024:
145 cmd += "-T largefile "
146 cmd += "-m0 " + self.devidex + "part1"
151 cmd = "/bin/mount -t ext3 " + self.devidex + "part1"
155 def createMovieFolder(self):
157 makedirs(resolveFilename(SCOPE_HDD))
163 # We autocorrect any failures
164 # TODO: we could check if the fs is actually ext3
165 cmd = "/sbin/fsck.ext3 -f -p " + self.devidex + "part1"
169 errorList = [ _("Everything is fine"), _("Creating partition failed"), _("Mkfs failed"), _("Mount failed"), _("Create movie folder failed"), _("Fsck failed"), _("Please Reboot"), _("Filesystem contains uncorrectable errors"), _("Unmount failed")]
171 def initialize(self):
174 if self.createPartition() != 0:
180 if self.mount() != 0:
183 if self.createMovieFolder() != 0:
198 if res != 0 and res != 1:
199 # A sum containing 1 will also include a failure
202 if self.mount() != 0:
207 def getDeviceDir(self):
210 def getDeviceName(self):
211 return self.getDeviceDir() + "disc"
214 def __init__(self, mountpoint, description = "", force_mounted = False):
215 self.mountpoint = mountpoint
216 self.description = description
217 self.force_mounted = force_mounted
218 self.is_hotplug = force_mounted # so far; this might change.
221 return statvfs(self.mountpoint)
226 return s.f_bavail * s.f_bsize
233 return s.f_blocks * s.f_bsize
238 # THANK YOU PYTHON FOR STRIPPING AWAY f_fsid.
239 # TODO: can os.path.ismount be used?
240 if self.force_mounted:
242 procfile = tryOpen("/proc/mounts")
243 for n in procfile.readlines():
244 if n.split(' ')[1] == self.mountpoint:
248 class HarddiskManager:
252 self.partitions = [ ]
254 self.on_partition_list_change = CList()
256 self.enumerateBlockDevices()
258 # currently, this is just an enumeration of what's possible,
259 # this probably has to be changed to support automount stuff.
260 # still, if stuff is mounted into the correct mountpoints by
261 # external tools, everything is fine (until somebody inserts
262 # a second usb stick.)
264 ("/media/hdd", _("Harddisk")),
265 ("/media/card", _("Card")),
266 ("/media/cf", _("Compact Flash")),
267 ("/media/mmc1", _("MMC Card")),
268 ("/media/net", _("Network Mount")),
269 ("/media/ram", _("Ram Disk")),
270 ("/media/usb", _("USB Stick")),
271 ("/", _("Internal Flash"))
275 self.partitions.append(Partition(mountpoint = x[0], description = x[1]))
277 def getBlockDevInfo(self, blockdev):
278 devpath = "/sys/block/" + blockdev
285 removable = bool(int(open(devpath + "/removable").read()))
286 dev = int(open(devpath + "/dev").read().split(':')[0])
287 if dev in [7, 31]: # loop, mtdblock
289 if blockdev[0:2] == 'sr':
291 if blockdev[0:2] == 'hd':
293 media = open("/proc/ide/%s/media" % blockdev).read()
294 if media.find("cdrom") != -1:
298 # check for partitions
300 for partition in listdir(devpath):
301 if partition[0:len(blockdev)] != blockdev:
303 partitions.append(partition)
308 return error, blacklisted, removable, is_cdrom, partitions
310 def enumerateBlockDevices(self):
311 print "enumerating block devices..."
312 for blockdev in listdir("/sys/block"):
313 error, blacklisted, removable, is_cdrom, partitions = self.getBlockDevInfo(blockdev)
314 print "found block device '%s':" % blockdev,
316 print "error querying properties"
320 print "ok, removable=%s, cdrom=%s, partitions=%s, device=%s" % (removable, is_cdrom, partitions, blockdev)
321 self.addHotplugPartition(blockdev, blockdev)
322 for part in partitions:
323 self.addHotplugPartition(part, part)
325 def getAutofsMountpoint(self, device):
326 return "/autofs/%s/" % (device)
328 def addHotplugPartition(self, device, description):
329 p = Partition(mountpoint = self.getAutofsMountpoint(device), description = description, force_mounted = True)
330 self.partitions.append(p)
331 self.on_partition_list_change("add", p)
333 if l and device[l-1] not in ('0','1','2','3','4','5','6','7','8','9'):
334 error, blacklisted, removable, is_cdrom, partitions = self.getBlockDevInfo(device)
335 if not blacklisted and not removable and not is_cdrom:
336 self.hdd.append(Harddisk(device))
338 SystemInfo["Harddisc"] = len(self.hdd) > 0
340 def removeHotplugPartition(self, device):
341 mountpoint = self.getAutofsMountpoint(device)
342 for x in self.partitions[:]:
343 if x.mountpoint == mountpoint:
344 self.partitions.remove(x)
345 self.on_partition_list_change("remove", x)
347 if l and device[l-1] not in ('0','1','2','3','4','5','6','7','8','9'):
350 if hdd.device == device:
353 SystemInfo["Harddisc"] = len(self.hdd) > 0
361 hdd = hd.model() + " - " + hd.bus()
364 hdd += " (" + cap + ")"
365 list.append((hdd, hd))
371 def getMountedPartitions(self, onlyhotplug = False):
372 return [x for x in self.partitions if (x.is_hotplug or not onlyhotplug) and x.mounted()]
374 harddiskmanager = HarddiskManager()