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.

  • One obvious improvement beyond your specific question would be to add a button to turn this off gracefully. That's easy to do and well documented many places. Aside from that, can't you just write a bash script to remove the unwanted directory? Not clear what the problem is... – Brick Oct 2 '17 at 1:54
  • What have you tried? Just delete the folders! If you keep doing this without properly shutting down, you will corrupt the image. – Milliways Oct 2 '17 at 1:58
  • 2
    @DavideAndrea this is true, but what is also true is that your approach is wrong for application, you need to lock down linux to handle this use-case. You should not be using automount, but hardcoded mount paths. For a system like that you would typically run with read-only root fs and a seperate writeable partition for any data that you need to write back. USB Drive should not be in a location that can be removed and should be mounted RO and remounted RW by code when needs to be updated. You have much bigger risks then just repeated folder names - that is, complete file-system corruption. – crasic Oct 2 '17 at 22:48
  • 2
    In other words, if the user isn't expected to know what they are doing, you must be prepared to handle anything they would try to do, or make it obvious what they are expected to do. – crasic Oct 2 '17 at 22:52
  • 1
    Might consider using the Adafruit instructions to make the Pi read-only. Then much less chance of trouble when power is pulled. learn.adafruit.com/read-only-raspberry-pi – BowlOfRed Oct 5 '17 at 19:00
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

  • > implement some means of shutting down properly -- this is a kiosk application: users are not computer savvy, and are not expected to look for a "shut down " button; they'll just unplug the power cord. – Davide Andrea Oct 2 '17 at 14:56
  • 1
    This is the right answer for this HW. If it doesn't meet your use case, then you've got the wrong HW for your kiosk. – Brick Oct 3 '17 at 0:31
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

  • 1
    This will be problematic if he's mounting that same USB drive before this command runs in the re-boot sequence. I understand his question to imply that he is mounting it again - That's why the next-numbered directory appears. – Brick Oct 2 '17 at 2:11
  • If they're just empty directories, use rmdir. It won't do anything to an active mount. – BowlOfRed Oct 5 '17 at 18:57
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)) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.