#๐Ÿ”’ (Linux) Folder mounting after Formatting

15 messages ยท Page 1 of 1 (latest)

past tendon
#

So I am currently working on a Project for which I format the given USB Drive. It works just fine BUT when I formatted the drive and unplug the USB Drive the mount Folder stays there (in order to mount I have to create it). Without it there will be an error that it cannot re-mount since there is no mount path, but the main question is is there an "auto mount" feature" in some way that Linux does it automatically that I don't have to manually remove the Folder afterwards or without making a Watcher for it?

If you re-plug the USB Drive afterwards it will work just fine even when the folder has been deleted since Linux creates it temporary by itself.

I mean I could just tell the User to re-plug the USB drive but it would be nicer having it automatically do it by itself.

final ledgeBOT
#

@past tendon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

past tendon
#
def format_usb(usb_device: list) -> str:

    # Validating Path
    if not usb_device:
        logging.error(f"Invalid USB drive.")
        return None

#
  # Unmounting the drive to Format it
    logging.debug(f"Trying to unmount USB device.")
    try:
         result = subprocess.run(
            ['umount', usb_device[0]], #-> device path (/dev/sd..)
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=False
        )
         result = subprocess.run(
            ['umount', usb_device[1]], #-> mount path (/media/user/...)
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=False
        )
         logging.debug(f"Unmounted the following drive: {usb_device[2]} ({usb_device[0]})")
    except Exception as e:
        logging.error(f"Cannot unmount drive: {e}")
    # Formatting the USB Drive
    logging.debug(f"Trying to Format the USB Drive. ({usb_device[0]})")
    try:
         result = subprocess.run(
            ["mkfs.vfat", "-F", "32", "-n", "VAULT", usb_device[0]],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True
        )
         logging.debug(f"Formatted Successfully.")
    except subprocess.CalledProcessError as e:
        logging.error(f"Can't format USB Drive:\n{e.stderr.decode().strip()}")
    except Exception as e:
        logging.error(f"Can't format USB Drive: {e}")
    # Creating a Mount Point
    user = getpass.getuser()
    user_mount = f"/media/{user}/VAULT"
    #os.makedirs(user_mount, exist_ok=True)
    # Re-Mount the Drive
    logging.debug(f"Trying to re-mount {usb_device[0]}.")
    try:
         result = subprocess.run(
            ["mount", usb_device[0], usb_device[0]],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True
        )
         logging.debug(f"Re-mounted successfully to ({user_mount}).")

    except subprocess.CalledProcessError as e:
        logging.error(f"Can't mount USB Drive:\n{e.stderr.decode().strip()}")
    except Exception as e:
        logging.error(f"Can't mount USB Drive: {e}")
    return user_mount
#

LOGS:

taler@Tower:~/Desktop/Development/mdProtector/v3.0$ sudo python3 main.py 
[DEBUG]: Using selector: EpollSelector
1. List USB Devices
2. Check for Vault
3. Format USB Device
4. Exit
Choice: 3
[DEBUG]: Label: dddd Type: <class 'str'>
[DEBUG]: Mount Path: /media/taler/dddd Type: <class 'str'>
[DEBUG]: Mount Point: /dev/sdb Type: <class 'str'>
[DEBUG]: Raw Serial: Intenso_Alu_Line_B7DCEC48-0:0 Type: <class 'str'>
[DEBUG]: Hashed Serial: d57dc6da5e38fe4abfeb41e5002dad499892df7a01230ddf95d033bb24f05061 Type: <class 'str'>
************************************************************
[DEBUG]: Label: VAULT Type: <class 'str'>
[DEBUG]: Mount Path: /media/taler/VAULT Type: <class 'str'>
[DEBUG]: Mount Point: /dev/sda1 Type: <class 'str'>
[DEBUG]: Raw Serial: Flash_USB_Disk_372715409E59C88027818-0:0 Type: <class 'str'>
[DEBUG]: Hashed Serial: 8c285428cbce14df4f8e5da48a104aab0e0ffbae748970469bbddd8985262933 Type: <class 'str'>
************************************************************
1. Name: dddd (/media/taler/dddd)
2. Name: VAULT (/media/taler/VAULT)
Allowed choices: 2
Select USB Drive: 2
[DEBUG]: Trying to unmount USB device.
[DEBUG]: Unmounted the following drive: VAULT (/dev/sda1)
[DEBUG]: Trying to Format the USB Drive. (/dev/sda1)
[DEBUG]: Formatted Successfully.
[DEBUG]: Trying to re-mount /dev/sda1.
[ERROR]: Can't mount USB Drive:
mount: /dev/sda1: mount point is not a directory.
       dmesg(1) may have more information after failed mount system call.
formal cave
#

did you do lsblk to confirm that /dev/sda1 is the right one?

#

I guess you have [DEBUG]: Trying to Format the USB Drive. (/dev/sda1) [DEBUG]: Formatted Successfully.

past tendon
#

Yeah its the same

#

I currently create the mounting path myself with:

    #os.makedirs(user_mount, exist_ok=True)
#

but that leaves the folder there even after unplugging, but without it the program wont recognize it and cant mount the drive without re-plugging it.

#

Changing back to the previous code which uncomments the os.makedirs... and puts the correct device
result = subprocess.run(["mount", usb_device[0], user_mount],...

I get the following:

[DEBUG]: Trying to unmount USB device.
[DEBUG]: Unmounted the following drive: VAULT (/dev/sda1)
[DEBUG]: Trying to Format the USB Drive. (/dev/sda1)
[DEBUG]: Formatted Successfully.
[DEBUG]: Trying to re-mount /dev/sda1.
[DEBUG]: Re-mounted successfully to (/media/root/VAULT).

But that leaves me with the Folder that I would have to manually delete which stays even after USB Drive being unplugged.

past tendon
#

!close

final ledgeBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.