Exemplo n.º 1
0
 def umount(self, fs, rm=False):
     if not fs.mount:
         return
     if fs.fstype in ['crypt', 'vdisk', 'loop']:
         dev = None
         l = losetup.get_loop_devices()
         for x in l:
             if l[x].is_used() and l[x].get_filename() == fs.img:
                 dev = l[x]
                 break
         if dev and fs.fstype == 'crypt':
             s = shell_cs('umount /dev/mapper/%s' % fs.name, stderr=True)
             if s[0] != 0:
                 raise Exception('Failed to unmount %s: %s' %
                                 (fs.name, s[1]))
             shell('cryptsetup luksClose %s' % fs.name)
             dev.unmount()
         elif dev:
             s = shell_cs('umount %s' % dev.device, stderr=True)
             if s[0] != 0:
                 raise Exception('Failed to unmount %s: %s' %
                                 (fs.name, s[1]))
             dev.unmount()
     else:
         s = shell_cs('umount %s' % fs.dev, stderr=True)
         if s[0] != 0:
             raise Exception('Failed to unmount %s: %s' % (fs.name, s[1]))
     apis.poicontrol(self.app).drop_by_path(fs.mount)
     if rm:
         shutil.rmtree(fs.mount)
Exemplo n.º 2
0
 def umount(self, fs, rm=False):
     if not fs.mount:
         return
     if fs.fstype in ['crypt', 'vdisk', 'loop']:
         dev = None
         l = losetup.get_loop_devices()
         for x in l:
             if l[x].is_used() and l[x].get_filename() == fs.img:
                 dev = l[x]
                 break
         if dev and fs.fstype == 'crypt':
             s = shell_cs('umount /dev/mapper/%s'%fs.name, stderr=True)
             if s[0] != 0:
                 raise Exception('Failed to unmount %s: %s'%(fs.name, s[1]))
             shell('cryptsetup luksClose %s'%fs.name)
             dev.unmount()
         elif dev:
             s = shell_cs('umount %s'%dev.device, stderr=True)
             if s[0] != 0:
                 raise Exception('Failed to unmount %s: %s'%(fs.name, s[1]))
             dev.unmount()
     else:
         s = shell_cs('umount %s'%fs.name, stderr=True)
         if s[0] != 0:
             raise Exception('Failed to unmount %s: %s'%(fs.name, s[1]))
     apis.poicontrol(self.app).drop_by_path(fs.mount)
     if rm:
         shutil.rmtree(fs.mount)
Exemplo n.º 3
0
 def encrypt_vdisk(self, fs, passwd, opts={'cipher': 'aes-xts-plain64', 'keysize': '256', 'hash': 'sha1'}, move=True, mount=False):
     opts = '-c %s -s %s -h %s'%(opts['cipher'], str(opts['keysize']), opts['hash'])
     l = losetup.get_loop_devices()
     if move:
         os.rename(os.path.join('/vdisk', fs.name+'.img'), os.path.join('/vdisk', fs.name+'.crypt'))
     dev = losetup.find_unused_loop_device()
     dev.mount(os.path.join('/vdisk', fs.name+'.crypt'))
     fs.img = os.path.join('/vdisk', fs.name+'.crypt')
     s = shell_cs('echo "%s" | cryptsetup %s luksFormat %s'%(passwd,opts,dev.device), stderr=True)
     if s[0] != 0:
         if move:
             dev.unmount()
             os.rename(os.path.join('/vdisk', fs.name+'.crypt'), os.path.join('/vdisk', fs.name+'.img'))
         raise Exception('Failed to encrypt %s: %s'%(fs.name, s[1]))
     fs.fstype = 'crypt'
     s = shell_cs('echo "%s" | cryptsetup luksOpen %s %s'%(passwd,dev.device,fs.name), stderr=True)
     if s[0] != 0:
         dev.unmount()
         raise Exception('Failed to decrypt %s: %s'%(fs.name, s[1]))
     s = shell_cs('mkfs.ext4 /dev/mapper/%s'%fs.name, stderr=True)
     shell('cryptsetup luksClose %s'%fs.name)
     dev.unmount()
     if s[0] != 0:
         raise Exception('Failed to format loop device: %s'%s[1])
     if mount:
         self.mount(fs, passwd)
Exemplo n.º 4
0
def get(id=None):
    devs, mps = [], {}
    fstab = get_fstab()

    # Get mount data for all devices
    with open("/etc/mtab", "r") as f:
        for x in f.readlines():
            x = x.split()
            mps[x[0]] = x[1]

    # Get physical disks available
    for d in parted.getAllDevices():
        try:
            parts = parted.Disk(d).getPrimaryPartitions()
        except:
            continue
        for p in parts:
            if p.path.split("/")[-1].startswith("loop"):
                continue
            try:
                fstype = parted.probeFileSystem(p.geometry)
            except:
                fstype = "Unknown"
            try:
                dev = DiskPartition(id=p.path.split("/")[-1], path=p.path,
                    mountpoint=mps.get(p.path) or None, size=int(p.getSize("B")),
                    fstype=fstype, enabled=p.path in fstab, crypt=crypto.is_luks(p.path)==0)
                if id == dev.id:
                    return dev
                devs.append(dev)
            except:
                continue

    # Replace mount data for virtual disks with loopback id
    dd = losetup.get_loop_devices()
    for x in dd:
        try:
            s = dd[x].get_status()
        except:
            continue
        if "/dev/loop%s" % s.lo_number in mps:
            mps[s.lo_filename] = mps["/dev/loop%s" % s.lo_number]

    # Get virtual disks available
    for x in glob.glob(os.path.join(config.get("filesystems", "vdisk_dir"), "*")):
        if not x.endswith((".img", ".crypt")):
            continue
        dname = os.path.splitext(os.path.split(x)[1])[0]
        dev = VirtualDisk(id=dname, path=x, size=os.path.getsize(x),
            mountpoint=mps.get(x) or mps.get("/dev/mapper/%s" % dname) or None,
            enabled=x in fstab, crypt=x.endswith(".crypt"))
        if id == dev.id:
            return dev
        devs.append(dev)
    return devs if not id else None
Exemplo n.º 5
0
 def umount(self):
     if not self.mountpoint:
         return
     signals.emit("filesystems", "pre_umount", self)
     loops = losetup.get_loop_devices()
     for loop in loops:
         if loops[loop].is_used() and loops[loop].get_filename() == self.path:
             dev = loops[loop]
             break
     s = libc.umount2(ctypes.c_char_p(self.mountpoint), 0)
     if s == -1:
         raise Exception("Failed to unmount %s: %s" % (self.id, os.strerror(ctypes.get_errno())))
     if self.crypt:
         crypto.luks_close(self.id)
     if dev:
         dev.unmount()
     signals.emit("filesystems", "post_umount", self)
     self.mountpoint = None
Exemplo n.º 6
0
 def encrypt_vdisk(self,
                   fs,
                   passwd,
                   opts={
                       'cipher': 'aes-xts-plain64',
                       'keysize': '256',
                       'hash': 'sha1'
                   },
                   move=True,
                   mount=False):
     opts = '-c %s -s %s -h %s' % (opts['cipher'], str(
         opts['keysize']), opts['hash'])
     l = losetup.get_loop_devices()
     if move:
         os.rename(os.path.join('/vdisk', fs.name + '.img'),
                   os.path.join('/vdisk', fs.name + '.crypt'))
     dev = losetup.find_unused_loop_device()
     dev.mount(os.path.join('/vdisk', fs.name + '.crypt'))
     fs.img = os.path.join('/vdisk', fs.name + '.crypt')
     s = shell_csin('cryptsetup %s luksFormat %s' % (opts, dev.device),
                    passwd,
                    stderr=True)
     if s[0] != 0:
         if move:
             dev.unmount()
             os.rename(os.path.join('/vdisk', fs.name + '.crypt'),
                       os.path.join('/vdisk', fs.name + '.img'))
         raise Exception('Failed to encrypt %s: %s' % (fs.name, s[1]))
     fs.fstype = 'crypt'
     s = shell_csin('cryptsetup luksOpen %s %s' % (dev.device, fs.name),
                    passwd,
                    stderr=True)
     if s[0] != 0:
         dev.unmount()
         raise Exception('Failed to decrypt %s: %s' % (fs.name, s[1]))
     s = shell_cs('mkfs.ext4 /dev/mapper/%s' % fs.name, stderr=True)
     shell('cryptsetup luksClose %s' % fs.name)
     dev.unmount()
     if s[0] != 0:
         raise Exception('Failed to format loop device: %s' % s[1])
     if mount:
         self.mount(fs, passwd)
Exemplo n.º 7
0
    def get_filesystems(self):
        devs, vdevs = [], []
        fdisk = shell('lsblk -Ppnbo NAME,SIZE,TYPE,MOUNTPOINT,PKNAME').split(
            '\n')
        l = losetup.get_loop_devices()
        l = [l[x] for x in l if l[x].is_used()]

        for x in fdisk:
            if not x.split():
                continue
            x = x.split()
            for y in enumerate(x):
                x[y[0]] = y[1].split('"')[1::2][0]

            f = Filesystem()
            f.name = x[0].split('/')[-1]
            f.dev = x[0]
            f.size = int(x[1])
            f.fstype = x[2]
            if x[2] == 'part':
                f.icon = 'gen-arrow-down'
            elif x[2] == 'rom':
                f.icon = 'gen-cd'
            elif x[2] == 'crypt':
                f.icon = 'gen-lock'
                f.delete = False
            elif x[2] == 'loop':
                f.icon = 'gen-loop-2'
                f.delete = False
            else:
                f.icon = 'gen-storage'
            f.mount = x[3] if len(x) >= 4 else ''
            f.parent = x[4] if len(x) >= 5 else None
            for y in devs:
                if y.dev in f.dev:
                    f.parent = y
                    break
            if f.fstype in ['crypt', 'loop']:
                vdevs.append(f)
            else:
                devs.append(f)

        if not os.path.exists('/vdisk'):
            os.mkdir('/vdisk')
        for x in glob.glob('/vdisk/*.img'):
            f = Filesystem()
            found = False
            for y in l:
                if y.get_filename() == x:
                    for z in vdevs:
                        if z.dev == y.device:
                            found = True
                            z.name = os.path.splitext(os.path.split(x)[1])[0]
                            z.icon = 'gen-embed'
                            z.img = x
                            z.delete = True
            if not found:
                f.name = os.path.splitext(os.path.split(x)[1])[0]
                f.img = x
                f.fstype = 'vdisk'
                f.icon = 'gen-embed'
                f.size = os.path.getsize(x)
                vdevs.append(f)
        for x in glob.glob('/vdisk/*.crypt'):
            f = Filesystem()
            found = False
            for y in l:
                if y.get_filename() == x:
                    for z in vdevs:
                        if z.parent == y.device:
                            found = True
                            z.img = x
                            z.delete = True
                            vdevs.remove(
                                [i for i in vdevs if i.dev == z.parent][0])
            if not found:
                f.name = os.path.splitext(os.path.split(x)[1])[0]
                f.img = x
                f.fstype = 'crypt'
                f.icon = 'gen-lock'
                f.size = os.path.getsize(x)
                vdevs.append(f)
        devs = sorted(devs, key=lambda x: x.name)
        vdevs = sorted(vdevs, key=lambda x: x.name)
        return devs, vdevs
Exemplo n.º 8
0
    def get_filesystems(self):
        devs, vdevs = [],[]
        fdisk = shell('lsblk -Ppnbo NAME,SIZE,TYPE,MOUNTPOINT,PKNAME').split('\n')
        l = losetup.get_loop_devices()
        l = [l[x] for x in l if l[x].is_used()]

        for x in fdisk:
            if not x.split():
                continue
            x = x.split()
            for y in enumerate(x):
                x[y[0]] = y[1].split('"')[1::2][0]

            f = Filesystem()
            f.name = x[0].split('/')[-1]
            f.dev = x[0]
            f.size = int(x[1])
            f.fstype = x[2]
            if x[2] == 'part':
                f.icon = 'gen-arrow-down'
            elif x[2] == 'rom':
                f.icon = 'gen-cd'
            elif x[2] == 'crypt':
                f.icon = 'gen-lock'
                f.delete = False
            elif x[2] == 'loop':
                f.icon = 'gen-loop-2'
                f.delete = False
            else:
                f.icon = 'gen-storage'
            f.mount = x[3] if len(x) >= 4 else ''
            f.parent = x[4] if len(x) >= 5 else None
            for y in devs:
                if y.dev in f.dev:
                    f.parent = y
                    break
            if f.fstype in ['crypt', 'loop']:
                vdevs.append(f)
            else:
                devs.append(f)

        if not os.path.exists('/vdisk'):
            os.mkdir('/vdisk')
        for x in glob.glob('/vdisk/*.img'):
            f = Filesystem()
            found = False
            for y in l:
                if y.get_filename() == x:
                    for z in vdevs:
                        if z.dev == y.device:
                            found = True
                            z.name = os.path.splitext(os.path.split(x)[1])[0]
                            z.icon = 'gen-embed'
                            z.img = x
                            z.delete = True
            if not found:
                f.name = os.path.splitext(os.path.split(x)[1])[0]
                f.img = x
                f.fstype = 'vdisk'
                f.icon = 'gen-embed'
                f.size = os.path.getsize(x)
                vdevs.append(f)
        for x in glob.glob('/vdisk/*.crypt'):
            f = Filesystem()
            found = False
            for y in l:
                if y.get_filename() == x:
                    for z in vdevs:
                        if z.parent == y.device:
                            found = True
                            z.img = x
                            z.delete = True
                            vdevs.remove([i for i in vdevs if i.dev == z.parent][0])
            if not found:
                f.name = os.path.splitext(os.path.split(x)[1])[0]
                f.img = x
                f.fstype = 'crypt'
                f.icon = 'gen-lock'
                f.size = os.path.getsize(x)
                vdevs.append(f)
        devs = sorted(devs, key=lambda x: x.name)
        vdevs = sorted(vdevs, key=lambda x: x.name)
        return devs, vdevs