File: disks.py

package info (click to toggle)
python3-dmm 0.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 540 kB
  • sloc: python: 441; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,790 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
disk and partition functions for dmm.
"""

import pathlib
from command_runner import command_runner

def run_parted_commands(disk, commands):
    """
    Iterate through parted commands and return an error if applicable.
    """
    for partedcmd in commands:
        ecode, result = command_runner("parted -s -- %s %s" % (disk, partedcmd))
        if ecode != 0:
            return ecode, result


def losetup(diskimg, mountpoint, loopdev):
    """
    Sets up a loopback device.
    """
    ecode, result = command_runner("losetup -P /dev/%s %s" % (loopdev, diskimg))
    return ecode, result


def losetup_teardown(loopdev):
    """
    Unmounts a loopback device.
    """
    ecode, result = command_runner("losetup -d /dev/%s" % (loopdev))
    return ecode, result


def create_filesystems(partitions):
    """
    Creates a new filesystem on a device.
    """
    for partition in partitions:
        ecode, result = command_runner("mkfs.%s %s %s" % (partition['fstype'], partition['options'], partition['partition']))
        if ecode != 0:
            return ecode, result


def mount(filesystems):
    """
    Mounts filesystems
    """
    for filesystem in filesystems:
        path = pathlib.Path(filesystem['mountpoint'])
        path.mkdir(parents=True, exist_ok=True)
        ecode, result = command_runner("mount -t %s %s %s %s" % (filesystem['fstype'], filesystem['mountopts'],
                  filesystem['source'], filesystem['mountpoint']))
        if ecode != 0:
            return ecode, result

def umount(mounts):
    """
    Unmounts filesystems
    """
    for mount in mounts:
        path = pathlib.Path(mount['mountpoint'])
        ecode, result = command_runner("umount %s" % (mount['mountpoint']))
        if ecode != 0:
            return ecode, result