NutsOS/test/ndmt
2020-04-17 17:44:23 +02:00

129 lines
3.2 KiB
Python
Executable File

#!/usr/bin/python
import os
import subprocess
import time
# Variables globales
selectedDiskPath = ""
selectedMountPoint = ""
mountFlag = False
# Fonction SelectDisk : sélectionne une image disque
def SelectDisk(diskPath):
global selectedDiskPath
global mountFlag
if mountFlag == True:
print("Error : you must umount the current partition before select a new disk")
return -1
if os.path.isfile(diskPath) == True:
selectedDiskPath = diskPath
return 0
else:
print("Error : invalid disk path")
return -2
# Fonction SelectMountPoint : sélectionne un répertoire pour le montage
def SelectMountPoint(dirPath):
global selectedMountPoint
global mountFlag
if mountFlag == True:
print("Error : you must umount the current partition before select a new mount point")
return -1
if os.path.isdir(dirPath) == True:
selectedMountPoint = dirPath
return 0
else:
print("Error : invalid dir path")
return -2
# Fonction MountPart : Monte dans le répertoire sélectionné la partition <partNum> du disque sélectionné
def MountPart(partNum):
global selectedDiskPath
global selectedMountPoint
global mountFlag
if mountFlag == True:
print("Error : a partition is aready mounted")
return -1
if partNum >= 0 and partNum < 4:
uid = str(os.getuid())
gid = str(os.getgid())
off = subprocess.check_output(["tools/readpt", selectedDiskPath, str(partNum), "start_LBA"], stderr=None)
partOffset = str(int(off) * 512)
mountOptions = "loop,rw,offset={0},users,uid={1},gid={2}".format(partOffset, uid, gid)
if subprocess.call(["/usr/bin/sudo", "/bin/mount", "-o", mountOptions, selectedDiskPath, selectedMountPoint], stdin=None, stdout=None, stderr=None) == 0:
time.sleep(1)
print("Partition ", partNum, " mounted")
mountFlag = True
return 0
else:
print("Error : unable to mount partition ", partNum)
return -3
else:
return -2
# Fonction UmountPart : Démonte la partition montée
def UmountPart():
global selectedMountPoint
global mountFlag
if mountFlag == False:
print("Error : no partition mounted")
return -1
if subprocess.call(["/usr/bin/sudo", "/bin/umount", selectedMountPoint], stdin=None, stdout=None, stderr=None) == 0:
time.sleep(1)
print("Umount partition")
mountFlag = False
return 0
else:
print("Error : unable to umount partition")
return -2
# Programme principal
print("\n########################################")
print("### NutsOS Disk Management Tool v1.0 ###")
print("########################################\n")
quit = False
while quit != True:
cmd = input("#> ")
args = cmd.split(sep=' ')
if args[0] == "seldisk":
if len(args) == 2:
SelectDisk(args[1])
else:
print("Command error : seldisk <disk>")
elif args[0] == "selmnt":
if len(args) == 2:
SelectMountPoint(args[1])
else:
print("Command error : selmnt <mountpoint>")
elif args[0] == "mount":
if len(args) == 2:
MountPart(int(args[1]))
else:
print("Command error : mount <partition>")
elif args[0] == "umount":
if len(args) == 1:
UmountPart()
else:
print("Command error : umount")
elif args[0] == "quit":
quit = True
if mountFlag == True:
UmountPart()
else:
print("Command error : unknown command")