chenlong
Posts: 20
Joined: Thu Sep 22, 2022 8:14 am

Threre are some files On pi 4B

Thu Mar 23, 2023 9:29 am

Due to abnormal power failure, many directories have been added to the/media/pi directory, with names similar to those of a USB flash drive. Why are these directories generated。Why are these directories generated. What is their role and how to avoid it if I don't need it? :geek: :geek: :geek: :D

User avatar
kerry_s
Posts: 5422
Joined: Thu Jan 30, 2020 7:14 pm

Re: Threre are some files On pi 4B

Thu Mar 23, 2023 9:33 am

that happens from unclean disconnects, you need to use eject before removal.

chenlong
Posts: 20
Joined: Thu Sep 22, 2022 8:14 am

Re: Threre are some files On pi 4B

Fri Mar 24, 2023 2:23 am

I am working on a project where I use a camera to collect data and store it on a USB flash drive, but user irregularities may occur, such as cutting off the power supply without a safe shutdown. This will create these unnecessary directories, which will affect the operation of the program on the USB flash drive. So I want to know how to avoid generating these directories during an unsafe disconnect. :D :D

bjtheone
Posts: 2279
Joined: Mon May 20, 2019 11:28 pm
Location: The Frozen North (AKA Canada)

Re: Threre are some files On pi 4B

Fri Mar 24, 2023 12:03 pm

Avoid or deal with unsafe disconnects. Not being a jerk, but that is the issue that is causing the problem, since that is how the OS works. Your options are:

* UPS/battery backup so that you can (potentially automatically) cleanly shut down the Pi when you lose power
* fix your power situation

kaksi
Posts: 162
Joined: Tue Mar 10, 2015 6:19 am

Re: Threre are some files On pi 4B

Fri Mar 24, 2023 8:25 pm

I would advise you to solve the disconnect problem, too.

I live in an area where there were frequent power supply problems and unsafe shutting down often resulted in a write protected SD card.

So far, you are lucky to get off only with scrambled files.

User avatar
kerry_s
Posts: 5422
Joined: Thu Jan 30, 2020 7:14 pm

Re: Threre are some files On pi 4B

Fri Mar 24, 2023 8:47 pm

chenlong wrote:
Fri Mar 24, 2023 2:23 am
I am working on a project where I use a camera to collect data and store it on a USB flash drive, but user irregularities may occur, such as cutting off the power supply without a safe shutdown. This will create these unnecessary directories, which will affect the operation of the program on the USB flash drive. So I want to know how to avoid generating these directories during an unsafe disconnect. :D :D
you can add a startup to clean /media/you/(folders)

Code: Select all

mkdir ~/.config/autostart

Code: Select all

mousepad ~/.config/autostart/clean-media.desktop

Code: Select all

[Desktop Entry]
Type=Application
Exec=sudo rm -r /media/$USER/*

User avatar
thagrol
Posts: 9313
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Threre are some files On pi 4B

Sat Mar 25, 2023 1:10 am

kerry_s wrote:
Fri Mar 24, 2023 8:47 pm
chenlong wrote:
Fri Mar 24, 2023 2:23 am
I am working on a project where I use a camera to collect data and store it on a USB flash drive, but user irregularities may occur, such as cutting off the power supply without a safe shutdown. This will create these unnecessary directories, which will affect the operation of the program on the USB flash drive. So I want to know how to avoid generating these directories during an unsafe disconnect. :D :D
you can add a startup to clean /media/you/(folders)

Code: Select all

mkdir ~/.config/autostart

Code: Select all

mousepad ~/.config/autostart/clean-media.desktop

Code: Select all

[Desktop Entry]
Type=Application
Exec=sudo rm -r /media/$USER/*
An @reboot cron job or an entry in rc.local might be a better choice. Those should run before the desktop starts and the automounter has a chance to create yet another directory with a numbered suffix.

For cron:
  1. As the desired user run

    Code: Select all

    crontab -e
    Do not use sudo for this step.
  2. Insert the following at the end of the file:

    Code: Select all

    @reboot rmdir /media/$LOGNAME/*
For rc.local
  1. Open /etc/rc.local in your preferred text editor. You will need to be root or use sudo
  2. Insert the following immediately above the line that reads exit 0:

    Code: Select all

    rmdir --ignore-fail-on-non-empty /media/pi/*
    Replace "pi" with the required username.
Neither of the above will remove directories that are not empty but they will remove all other directories including any you created manually.

If your feeling flash you could use a one shot systemd service instead of cron or rc.local.
Knowledge, skills, & experience have value. If you expect to profit from someone's you should expect to pay for them.

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

peterlite
Posts: 934
Joined: Sun Apr 17, 2016 4:00 am

Re: Threre are some files On pi 4B

Thu Mar 30, 2023 2:34 am

/etc/fstab controls mounting of things specified there. You can sometimes set up regularly mounted devices there to remount properly after power outage. The remount might happen before the extra files are written.

Another problem you can get is USB 3 devices that are not really USB 3, only a USB 2.5. They need special care. You can use the lsusb command to find the device USB ID then look for known problems.

You can also reduce the time Linux caches writes before writing. If set to zero, you system might be slightly slower but all writes are instant. Unfortunately in Linux you cannot set zero just for plugged in devices.

A battery backup helps. A real UPS has the battery plus it can issue a signal to the OS to shut down before the battery runs out, ensuring all writes are complete.

For sensitive situations like that, I tend to reuse old notebooks instead of a Pi as the notebook has a charger and battery. For a project where I also need GPIO pins, I am using solar panel to charger to battery to USB power source to the Pi, reusing cheap items purchased on sale from a camping supplies place.

For a Pi 4, another option is to boot the Pi from SSD. Reuse an old mSATA/SATA SSD in a USB 3 enclosure. No need to fiddle with mounting something else.

Return to “Beginners”