File: ownsense.py

package info (click to toggle)
live-clone 3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,104 kB
  • sloc: python: 2,333; xml: 440; makefile: 61; sh: 45
file content (24 lines) | stat: -rw-r--r-- 987 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
#!/usr/bin/python3

"""
sense the own system when cloning from a Freeduc drive
"""
from subprocess import run

def ownDevice(blockSize = 4194304):
    """
    Find the device path of the USB stick itself, when cloning in "own" mode
    
    @param blockSize the size of wanted blocks; defaults to 4194304 == 4M
    @return the path of the USB stick's device and its size in blocks unit
    """
    cmd="mount | grep /usr/lib/live/mount | grep /dev/sd | awk '{print $1}'| head -n 1"
    completed = run(cmd, shell=True, capture_output=True, encoding="utf-8")
    device = completed.stdout.strip()
    if device == "":
        raise Exception("No own device, are we running a non-Freeduc system?")
    cmd=f"LANG=C sfdisk -l {device} | head -n1|  awk '{{print $7}}'"
    completed = run(cmd, shell=True, capture_output=True, encoding="utf-8")
    sectors=int(completed.stdout.strip())
    count = 1 + sectors*512//blockSize # convert 512 B sectors to 4 MiB blocks
    return device, count