- try:
- if action == 'add' and device.is_hotplug and len(device.device) == 3:
- if device.device.startswith("sd"):
- newdevice = "/dev/" + device.device
- cmd = "sleep 3 && blkid -c /dev/null " + newdevice + "*"
- data = popen(cmd).read().split('\n')
-# print "[DeviceManagerHotplugDevice] blkid data : ",data
- for line in data:
- uuid = ""
- partitionType = ""
- if line.startswith(newdevice):
- if line.find(" UUID=") != -1:
- uuid = line.split(" UUID=")[1].split(' ')[0]
- if line.find(" TYPE=") != -1:
- partitionType = line.split(" TYPE=")[1].split(' ')[0].strip('"')
- mountdev = line.split(':')[0]
- devicemanagerconfig.loadConfig()
- configList = devicemanagerconfig.getConfigList()
- for c in configList:
- if uuid == c[0].strip():
- mountpoint = c[1].strip()
- if not os.path.ismount(mountpoint):
- if partitionType == "ntfs":
- cmd = "ntfs-3g %s %s"%(mountdev, mountpoint)
- else:
- cmd = "mount %s %s"%(mountdev, mountpoint)
- print "[DeviceManagerHotplugDevice] try..'%s'"%cmd
- os.system(cmd)
-
- elif action == 'remove' and device.device is not None and len(device.device) == 3:
- mounts = file('/proc/mounts').read().split('\n')
- for x in mounts:
- if not x.startswith('/'):
- continue
- if x.startswith("/dev/"+device.device):
- cmd = "umount "+x.split()[0]
- print "[DeviceManagerHotplugDevice] try.. '%s'"%cmd
- os.system(cmd)
- except:
- print "[DeviceManagerHotplug] hotplugDevice error."
+ def removeHotplugDevice(self, partition):
+ self.doUmount(partition.device, partition.mountpoint)
+
+ def getHotplugAction(self, action, partition):
+ if not self.hotplugActive or not config.plugins.devicemanager.hotplug_enable.value:
+ return
+ if partition.device is None or not partition.device.startswith("sd"):
+ return
+ print "[DeviceManagerHotplug] action : %s, device : %s"%(action, partition.device)
+
+ if action == 'add':
+ self.addHotPlugDevice(partition)
+ elif action == 'remove':
+ self.removeHotplugDevice(partition)
+
+ def addPartitionAutofsMountpoint(self, devpath, mountpoint):
+ device = path.basename(devpath)
+ autofsMountpoint = harddiskmanager.getAutofsMountpoint(device)
+# check already appended to partition list
+ for x in harddiskmanager.partitions:
+ if x.mountpoint == autofsMountpoint or x.mountpoint == mountpoint:
+ return
+#
+ from Components.Harddisk import Partition
+ physdev = path.realpath('/sys/block/' + device[:3] + '/device')[4:]
+ description = harddiskmanager.getUserfriendlyDeviceName(device, physdev)
+ p = Partition(mountpoint = autofsMountpoint, description = description, force_mounted = True, device = device)
+ harddiskmanager.partitions.append(p)
+ harddiskmanager.on_partition_list_change("add", p)
+
+ def autoMountOnStartup(self):
+ devicemanagerconfig.loadConfig()
+ configList = devicemanagerconfig.getConfigList()
+# get blkid info
+ blkiddata = []
+ data = os.popen("blkid -c /dev/NULL /dev/sd*").readlines()
+ for line in data:
+ devpath = uuid = filesystem = ""
+ devpath = line.split(':')[0]
+ if line.find(" UUID=") != -1:
+ uuid = line.split(" UUID=")[1].split(' ')[0]
+ if line.find(" TYPE=") != -1:
+ filesystem = line.split(" TYPE=")[1].split(' ')[0].strip('"')
+ blkiddata.append((devpath, uuid, filesystem))
+# check configList
+ for c in configList:
+ uuid_cfg = c[0].strip()
+ mountpoint_cfg = c[1].strip()
+ for (devpath, uuid, filesystem) in blkiddata:
+ if uuid_cfg == uuid:
+# do mount
+ if deviceinfo.isMounted(devpath, mountpoint_cfg):
+ print "[Devicemanager startup] already mounted"
+ self.addPartitionAutofsMountpoint(devpath, mountpoint_cfg)
+ else:
+# print "[autoMountOnStartup] do mount(%s %s %s)"%(devpath, configmountpoint, filesystem)
+ self.doMount(uuid, devpath, mountpoint_cfg, filesystem)
+
+ def umountOnShutdown(self):
+ devicemanagerconfig.loadConfig()
+ configList = devicemanagerconfig.getConfigList()
+# get mount info
+ mounts = []
+ data = file('/proc/mounts').read().split('\n')
+ for x in data:
+ if not x.startswith('/dev/sd'):
+ continue
+ devpath, mountpoint = x.split()[:2]
+ mounts.append((path.basename(devpath), mountpoint))
+# print "[DeviceManager] mounts : ",mounts
+ data = self.getBlkidInfo()
+# check configList
+ for c in configList:
+ uuid_cfg = c[0].strip()
+ mountpoint_cfg = c[1].strip()
+ device_cfg = None
+ if uuid_cfg in data.keys():
+ device_cfg = data[uuid_cfg]
+ if device_cfg is None:
+ return
+ for (device, mountpoint) in mounts:
+ if device_cfg == device:
+ if not deviceinfo.isFstabAutoMounted(uuid_cfg, "/dev/"+device_cfg, mountpoint_cfg):
+ self.doUmount(device, mountpoint)
+
+ def getBlkidInfo(self):
+ data = {}
+ blkid_lines = os.popen("blkid -c /dev/NULL /dev/sd*").readlines()
+ for line in blkid_lines:
+ device = uuid = ""
+ device = path.basename(line.split(':')[0])
+ if line.find(" UUID=") != -1:
+ blkid_uuid = line.split(" UUID=")[1].split(' ')[0]
+ data[blkid_uuid] = device
+ return data
+
+devicemanagerhotplug = deviceManagerHotplug()
+
+def DeviceManagerhotplugDeviceStart(action, device):
+ devicemanagerhotplug.getHotplugAction(action, device)