1

I'm designing a kiosk device with a Raspberry Pi.

The device is powered down by removing power without warning (not by graceful shut-down).

If a USB drive is mounted at the time (say "SanDisk"), at the next power-up, the "SanDisk" folder is still present in /media/pi/ (though inaccessible), and a new folder, "SanDisk1", appears.

After 100 times, there are 100 folders in /media/pi/, from "SanDisk1" to "SanDisk99".

What's the best way to clear the ghost USB folders from /media/pi/?

I can use a Python script.

3 Answers3

2

The "best" solution is to mount the drives explicitly, and unmount before shutting down, rather than relying on automount and tidying up the mess after.

Create a rule in /etc/fstab to mount the drives in a dedicated folder.

I use the following to mount one of my drives, although you will need to substitute settings appropriate for your drives.

UUID=94dc6686-0eda-41ba-87f7-494d7e37f913       /mnt/PiData     ext4    defaults,noatime,noauto  0     0

You should also implement some means of shutting down properly. If you continue to just turn power off, you will eventually corrupt the image, and possibly the mounted drive

Milliways
  • 62,573
  • 32
  • 113
  • 225
1

You could delete all the folders on reboot using a cronjob.

crontab -e

then adding

@Reboot * * * *  rm -rf /home/pi/*San*

Be careful as this will remove ALL folders and files starting with "San" and is also case sensitive

Dr.Rabbit
  • 1,016
  • 6
  • 10
0

This code deletes all the folders (ghosts or otherwise) from the media folder. A bit later, the system mounts the USB drive that is truly present

MEDIA_DIR = '/media/pi'

def clearGhostDrives():
    """Delete non-existent USB drives"""
    # When power is removed (no shut-down) and a USB drive is removed, 
    #  then the power is restored, a ghost folder is left in /media/pi/
    # Distinguishing between ghost folders and real ones has proven to be too much of a challenge
    # Instead, this function deletes all the USB folders
    # This is executed when powering up, before the USB drive is mounted
    # Therefore, after this removes all the folders, the system mounts the USB drive that actually exists

    # Locals
    DEL_DIR_CMD = 'sudo rm -r %s/%s'

    usbDrivesList = os.listdir(MEDIA_DIR)
    if len(usbDrivesList) > 1:
        for usbDriveName in usbDrivesList:
            doLinuxCmd(DEL_DIR_CMD % (MEDIA_DIR, usbDriveName))