rangerdavid
Posts: 4
Joined: Wed Nov 29, 2017 6:47 pm

Clean Up USB Mounts on Boot

Thu Dec 20, 2018 3:16 am

All,

I need my Pi to reliably mount EXACTLY the same name for a USB thumbdrive every time it boots.

The drive is labeled "USBDRIVE" and if you unplug/plug it in while the Pi is running, it might get mounted the second time as "USBDRIVE(2)" or something like that. Oh well, that's okay, even though that breaks something I'm working on, but I DO want to be able to restart the PI and have the drive mapped as "USBDRIVE" again, without the added (1) or (2) or whatever...

Is there some code I could run on boot that:

- removes all records of previous USB drive mounts
- then mounts the drive again with that same label/name again?

The drive will physically be in the USB port for this whole reboot cycle.

THANK YOU FOR YOUR HELP! :D

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: Clean Up USB Mounts on Boot

Thu Dec 20, 2018 4:16 am

The place your thumb drive mounts to is

Code: Select all

/media/$USER/USBDRIVE
/media/$USER should be empty on boot.
Your problem is your pulling the thumb drive out without ejecting it. This is dangerous on linux and you will loose data or corrupt your drive. Use the eject tool (top right of the screen) or the eject button in file manager or right click select eject in file manager.

You must eject, linux performs disk updates in the background. this is not windows which writes immediately to removable drives

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Clean Up USB Mounts on Boot

Thu Dec 20, 2018 5:10 pm

rangerdavid wrote:
Thu Dec 20, 2018 3:16 am
I need my Pi to reliably mount EXACTLY the same name for a USB thumbdrive every time it boots.
1. Disable the automounter, it's a PITA.
2. Find the UUID/PARTUUID for your USB stick
sudo blkid should give you something like

Code: Select all

[email protected]:~ $ sudo blkid
/dev/sda1: LABEL="sticky_1" UUID="6228-7918" TYPE="vfat" PARTUUID="ba822123-01"
/dev/sda2: LABEL="sticky_2" UUID="6bfc8851-cf63-4362-abf1-045dda421aad" TYPE="ext4" PARTUUID="ba822123-02"
[email protected]:~ $
You can then use that PARTUUID for mounting it.
3. Create a directory where you'd like it mounted. For my example it's /media/sticky2
4. Create a /etc/systemd/system/media_sticky2.mount [Note how the name is the directory with slash (/) replaced with underscore (_)]

Code: Select all

[Unit]
Description=Mount /media/sticky2 directory
Requires=networking.service
Wants=networking.service
#
# Replaces this line in fstab
#PARTUUID=ba822123-02   /media/sticky2  ext4 defaults,noatime,x-systemd.automount 0 0
#
[Mount]
What=PARTUUID=ba822123-02
Where=/media/sticky2
Type=ext4
Options=defaults

[Install]
WantedBy=multi-user.target
5. Enable that with sudo systemctl enable media_sticky2.mount
6. Mount it for this boot with sudo systemctl start media_sticky2.mount
7. Grab a beer to celebrate the Winter Solstice and a job well done.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

rangerdavid
Posts: 4
Joined: Wed Nov 29, 2017 6:47 pm

Re: Clean Up USB Mounts on Boot

Fri Dec 21, 2018 7:08 pm

7. Grab a beer to celebrate the Winter Solstice and a job well done.

You know, maybe I'll make that step #1.... THANK YOU SO MUCH! I'll see if I can get this working.

TxRider
Posts: 15
Joined: Tue Aug 14, 2018 4:45 pm

Re: Clean Up USB Mounts on Boot

Fri Jan 25, 2019 6:20 am

Will this completely remove my mounted sd card so when I boot up with out it being installed, my PI will not crash? I've tried just doing an standard sudo umount /dev/sda1 but when ever I reboot without the sd card being plugged back it, by PI will crash and sometimes corrupt the rasp program? Thanks for any help!!

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Clean Up USB Mounts on Boot

Fri Jan 25, 2019 8:09 am

You can do it in fstab with a nofail directive.
Doing it with a systemd mount file means the mount can fail and nothing bad happens.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

TxRider
Posts: 15
Joined: Tue Aug 14, 2018 4:45 pm

Re: Clean Up USB Mounts on Boot

Fri Jan 25, 2019 8:36 pm

Thanks for the info Doug, but can you please send me a link to a video or code steps for the proper way that I need to do this?

Also, as soon as I umount the disk and then reboot, here is the error that pops up and I can no longer boot my Pi....is there a way that I can work around this boot up problem as well?

Thank you !!

Boot up Error: https://www.dropbox.com/s/el8qze9sk5rs1 ... t.jpg?dl=0

ConfuSomu
Posts: 17
Joined: Tue Dec 04, 2018 8:47 am
Location: Canada

Re: Clean Up USB Mounts on Boot

Sat Jan 26, 2019 7:21 am

In your /etc/fstab, add this line:

Code: Select all

UUID=<UUID of your device's partition> /media/$USER/USBDRIVE <partition type (ext4,ntfs,fat…)> defaults,nofail,user 0 0
Replace $USER with your Linux username (by default, it is pi). You can find out the UUID and partition type using sudo blkid. For more information about fstab view man 5 fstab. If you do all of this, I would recommend to remove the systemd service.

Wish you luck! ;)
If you are low on RAM, make sure to use zram! ;)
I found & use this zram setup script: https://github.com/novaspirit/rpi_zram
A post about zram: t=207304
Make sure to disable dphys-swapfile and mount the tmp folders in RAM. Also learn about SysRq!

TxRider
Posts: 15
Joined: Tue Aug 14, 2018 4:45 pm

Re: Clean Up USB Mounts on Boot

Sat Jan 26, 2019 8:00 pm

When I try to save the fstab file after the change, its saying that I do not have permission and cant be saved?

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Clean Up USB Mounts on Boot

Sat Jan 26, 2019 8:22 pm

sudo nano /etc/fstab
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

TxRider
Posts: 15
Joined: Tue Aug 14, 2018 4:45 pm

Re: Clean Up USB Mounts on Boot

Tue Jan 29, 2019 5:56 am

Thanks Doug, that did it !!

Return to “Troubleshooting”