How to Mount Drives with one click scripts

Moderator: Forum moderators

Post Reply
geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

How to Mount Drives with one click scripts

Post by geo_c »

Once I learned how to mount a drive from the command line, it occurred to me that I could make the process much faster by saving my commands in simple one line scripts which could be launched from any convenient location.

Attached is a set of scripts for the drives I normally might have attached to my computer: hard drive (sda1) sd-card (mmcblk0p1), usb drive (sdb1-3, sdc1)

Note: other forum members have added more sophisticated scripts to the thread below. In particular @wizard scripts are being updated in post #2.

How to use my simple scripts:

Method 1:

-unzip the attached script folder mntscripts and place them in your /mnt directory.

-attach a desired drive to your system and check to see if it is labeled either sda1,sdb1,sdb2,sdb3,sdc1, or mmcblk0p1, which can be observed using the desktop drive icons, if enabled, or by checking the dev/ directory where connected drives will be listed amongst a plethora of other devices.

-in /mnt click on the desired mounting script,

e.g. to mount sdb1, click the mntsdb1 script. Wait a brief second, and since you are already located in mnt, check the mnt/sdb1 directory to see if your files are there.

-to unmount, simply click the unmount script for the appropriate drive

e.g. to unmount sdb1 click the umntsdb1 script.

The advantage here is that once placed in the /mnt directory, they can be accessed from any file manager. They can be placed anywhere, linked from anywhere in the system, and also added to /root/my-applications/bin, so that one can simply type a command mntsdb1 from a terminal if desired.

Make sure they are executable on your system, I believe they should extract as executable, but it might be necessary to check.

In subsequent posts I will detail using symlinks to access the scripts for those who are less familiar with the process.

note: I have included the script called MOUNT_drv which will bring up the pmount gui but not launch a ROX window after mounting a drive. I would give credit to the author, but I don't know who wrote it!

Attachments
mntscripts.tar
(50 KiB) Downloaded 98 times
Last edited by geo_c on Fri Jul 22, 2022 2:41 pm, edited 4 times in total.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

Re: How to Mount Drives with one click scripts

Post by geo_c »

Before attempting to use my scripts, @wizard has come up with a single script that is much more elegant. It's a mount toggle. It mounts unmounted sdb1, and unmounts a mounted sdb1. If run with a terminal command, another drive other than sdb1 can be supplied with an argument as such: mntdrv sdc2 for instance.

@wizard's script updates can be found further down at:
https://forum.puppylinux.com/viewtopic. ... 313#p62313

Last edited by geo_c on Thu Jul 21, 2022 3:23 pm, edited 5 times in total.

geo_c
Old School Hipster, and Such

User avatar
rcrsn51
Posts: 1390
Joined: Sun Aug 23, 2020 4:26 pm
Been thanked: 357 times

Re: How to Mount Drives with one click scripts

Post by rcrsn51 »

Just out of curiosity, why not mount the drive just by clicking on the desktop drive icon?

geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

Re: How to Mount Drives with one click scripts

Post by geo_c »

rcrsn51 wrote: Wed Jul 20, 2022 2:08 pm

Just out of curiosity, why not mount the drive just by clicking on the desktop drive icon?

For me, I don't like to pop open a new ROX window. Maybe that can be disabled. Also this allows easy terminal based mounting and unmounting, as well as not having desktop drive icons as a matter of preference. Also, I spend a lot of time in the /mnt directory with my file managers, and this way I don't have to pop over to the desktop icons, it's all right there. Perhaps I should add the rationale in the original post.

geo_c
Old School Hipster, and Such

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

@geo_c

Mount_drv is a modification of pmount (coded by BK and other developers). Mount_drv was introduced here: https://forum.puppylinux.com/viewtopic. ... drv#p43817. It simply mounts the drive without opening Rox and can be executed from inside most file managers. :thumbup2:

Note that you could code your scripts to toggle mount/unmount with only one script per drive.

Thanks
wizard

Big pile of OLD computers

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

@geo_c

Hey geo, the more I thought about a mount/umount toggle, the more it sounded like an interesting code exercise. So here is something for you to play with:

EDIT: this is rev C of mntdrv, prior versions were writing a 0 byte file into directories.

mntdrv.gz
remove fake .gz before using
(569 Bytes) Downloaded 90 times

If you want to have a separate file for each drive open mntdrv and edit the first section of code, i.e. mydrv=sdb1, then rename the file to suit. When executed it will toggle, mount or unmount the drive.

You can also run it from the terminal, plus if you give it an argument in the terminal, i.e. mntdrv sdc1, the argument will override the drive in the first code section. In the example it would mount/umount sdc1.

Thanks for posting the topic, gave me something to do in the heat.

wizard

Last edited by wizard on Fri Jul 22, 2022 9:17 pm, edited 3 times in total.

Big pile of OLD computers

Dry Falls
Posts: 80
Joined: Thu Jul 29, 2021 7:40 pm
Has thanked: 22 times
Been thanked: 43 times

Re: How to Mount Drives with one click scripts

Post by Dry Falls »

Nice @wizard
If you run xfce4, there's a "places" panel-plugin which will find and mount/unmount/eject drives.
-df

geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

Re: How to Mount Drives with one click scripts

Post by geo_c »

wizard wrote: Wed Jul 20, 2022 8:30 pm

@geo_c

Hey geo, the more I thought about a mount/umount toggle, the more it sounded like an interesting code exercise. So here is something for you to play with:
mntdrv.gz

If you want to have a separate file for each drive open mntdrv and edit the first section of code, i.e. mydrv=sdb1, then rename the file to suit. When executed it will toggle, mount or unmount the drive.

You can also run it from the terminal, plus if you give it an argument in the terminal, i.e. mntdrv sdc1, the argument will override the drive in the first code section. In the example it would mount/umount sdc1.

Thanks for posting the topic, gave me something to do in the heat.

wizard

Yes very nice. I know my approach was crude (but effective). Yours does the trick much better as you don't need a collection of scripts. Now I know what to use the 'reserved' post for.

geo_c
Old School Hipster, and Such

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

@geo_c

I know my approach was crude

Nothing crude about it, it's how an idea evolves. You start with something simple that works and then add to it. You had the idea to mount/unmount with two different scripts. I just ask myself if both could be done with the same script. Using the mydrv variable in the first code section makes it easy to create a dedicated script for any drive if that's what works best for you. Just change the mydrv=sdb1 to sdc1 or any other drive and save the file to a new name and it's done. The command line argument thing is just a bonus that lets you specify the drive on the fly if needed.

Thanks
wizard

Big pile of OLD computers

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

@geo_c

Well didn't take me long to find a hole in my code. The original would only work if the mount point already existed. The revision will create the mount point if needed. The new version is in the post above. You should change your second post attachment accordingly.

Thanks
wizard

Big pile of OLD computers

geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

Re: How to Mount Drives with one click scripts

Post by geo_c »

file updated in second post

geo_c
Old School Hipster, and Such

User avatar
wiak
Posts: 4085
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 65 times
Been thanked: 1211 times
Contact:

Re: How to Mount Drives with one click scripts

Post by wiak »

wizard wrote: Wed Jul 20, 2022 8:30 pm

@geo_c

Hey geo, the more I thought about a mount/umount toggle, the more it sounded like an interesting code exercise. So here is something for you to play with:

EDIT: this is rev a of mntdrv
mntdrv.gz

I have a similar script (in functionality at least) to this in all WeeDog builds of the past couple of years and included with KLV-Airedale called wd_mount, but good idea to make the mount/umount a toggle action, which I'll adopt too, thanks. Simple though wd_mount is, I actually use it a lot!

Currently, in wd_mount, I have main code line:

Code: Select all

mountpoint -q /mnt/"${1}" || mount /dev/"${1}" /mnt/"${1}"

for toggle I just change that to:

Code: Select all

mountpoint -q /mnt/"${1}" && umount -l /dev/"${1}" || mount /dev/"${1}" /mnt/"${1}"

I haven't made a default drive to mount so usage of wd_mount is:

Code: Select all

wd_mount sdXX (or whatever)

On my computer, I have no sda (I have nvme SSD main drive) so my first usb stick occupies position sda1, so I didn't bother choosing a default since on other systems I have sdb1 as default, and mmc (SDcard) as default on another one! But it's a fair point that sdb1 usb stick probably is default for most people and so handy incorporating that.

Code: Select all

mountpoint -q /mnt/"${d}" && umount -l /dev/"${d}" || mount /dev/"${d}" /mnt/"${d}"

There is already a 'case' statement in wd_mount, which can be used to also set the variable d to appropriate default:

Code: Select all

#!/bin/sh
# wd_mount: Simple... mount tool by wiak. Licence MIT. Created 12Apr2020. Rev 0.0.3 22Jun2022
case "$1" in
  '-h'|'--help'|'-?') printf "usage: wd_mount <devicename> (e.g. wd_mount sdb1)>
Simple mounttool to mount/umount device to/from /mnt/devicename";exit;;
  '') d=sdb1;;  # 22Jun2022 thanks "wizard" for: idea to toggle the mount with a umount and include default d
  *) d=${1};;
esac
mkdir -p /mnt/"${d}"
mountpoint -q /mnt/"${d}" && umount -l /dev/"${d}" || mount /dev/"${d}" /mnt/"${d}"
mount | grep "${d}"

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

@wiak

Thanks for commenting. You're a far better coder, I am just a hack. Had to do some homework to understand some of your code and some of it I still don't follow, but certainly found it interesting.

Thanks
wizard

Big pile of OLD computers

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

@geo_c

Uploaded a rev B in post #6 above. It tidy's up the code. You may want to change your second post to just refer to post #6 since this may not be the last of it.

wizard's rule #4 "If you thinks it's done, it's not" :lol:

Thanks
wizard

Big pile of OLD computers

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

@geo_c

Uploaded a rev C in post #6 above. Prior versions were writing a 0 byte file into directories. :o

Thanks
wizard

Big pile of OLD computers

geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

Re: How to Mount Drives with one click scripts

Post by geo_c »

wizard wrote: Fri Jul 22, 2022 9:21 pm

@geo_c

Uploaded a rev C in post #6 above. Prior versions were writing a 0 byte file into directories. :o

Thanks
wizard

Yeah, that's right, I noticed that. But I didn't think much of it at the time.

And this is why I go with very, very, very simple scripts. ;)

geo_c
Old School Hipster, and Such

User avatar
mikewalsh
Moderator
Posts: 6185
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 803 times
Been thanked: 1994 times

Re: How to Mount Drives with one click scripts

Post by mikewalsh »

This is what I've used in the past with a number of older Puppies, before pMount's GUI became a thing. AFAIK, it was introduced with the mid 5-series Slackos, by 01micko.....

Mine is simply an adaptation of what Bill did years ago with the auto-mount script for the old Samba-TNG:-

Code: Select all

#!/bin/sh
#
#Auto mount partitions at boot
#
mount -U a37ce545-9fb7-4189-bed2-3fd6f2e20cf2 /mnt/sda3
mount -U 190eb203-07ac-490e-aa0f-d10419ca91bc /mnt/sdb1
mount -U 6d394fd7-aad2-4e20-90a9-c3ddcf07cc3e /mnt/sdb2

I've always auto-mounted certain partitions at boot-time, since they've contained a number of items that are sym-linked into most Puppies at start-up. The above usually sits in /etc/init.d. Short & simple.

Clicking it would also run it manually if desired, but placing it in /etc/init.d will auto-run it earlier than if placed in /root/Startup.

----------------------------

I've long since learnt to take statements about 'simple' scripts with a fairly generous pinch of salt. Some that are described as 'simple' no doubt ARE to their creators (who fully understand what they've created).....though of course this is all relative to one's state of development vis-a-vis scripting in general. To me some such are like so much Greek; way beyond my comprehension!

In reality, it makes me look at my own scripting for the piss-poor efforts they often are...... :oops:

Never mind. At least they do what I want them to.....most of the time! :D

Mike. ;)

User avatar
wiak
Posts: 4085
Joined: Tue Dec 03, 2019 6:10 am
Location: Packing - big job
Has thanked: 65 times
Been thanked: 1211 times
Contact:

Re: How to Mount Drives with one click scripts

Post by wiak »

mikewalsh wrote: Fri Jul 22, 2022 11:53 pm

Never mind. At least they do what I want them to.....most of the time! :D

IMO, all things otherwise being equal, the best script is the simplest possible to get whatever job you want done. Most of the time we do indeed just need a quick list of commands to be auto-run when required. Occasionally, quite often, we need some selections and loops. Worst scripts IMO are those that unnecessarily use complex reg expressions (can't easily be avoided sometimes though, but difficult to get perfect and avoid unexpected exceptions) or unreadable condensed one-liner or so code formats - that's neither helpful for future maintenance, nor as instructional material, nor often allowing for easy additions/modification. Of course too much verbosity is a pain as well, as in fact is too much commenting; where possible much of the code should speak for itself in terms of operation aside from few general comments regarding what significant code sections do overall). Keep It Simple and, some might complain, Stupid is perfectly good I feel, as long as it reliably achieves what it is wanted for! My goal is always to simply get the code to work; only later do I sometimes clean it up a bit or make the implementation a bit more sophisticated or use a nested case instead of a nested if or vice versa depending what I myself find easier to maintain or read... Trickiest decision I often find is when I need to use almost (but not quite) the same piece of overall code many times in a script - sometimes it is more trouble than it is worth trying to make it into a function because the 'subtle' differences can be complex to account for, so then I just don't bother - doesn't slow the script down to re-use 'almost' the same code in various places in the script (in fact slightly faster since no function call involved). 'Almost the same', isn't the same as 'the same'...

A lot, in terms of 'style', is a matter of personal preference (despite some 'purists' having other more fixed ideas about what constitutes good code). I do advocate good indentation though (helps avoid missing end brackets and similar and aids readability for sure). For any kind of 'procedural' programming I was brought up with something ancient called Jackson Structured Programming; I rarely do anything but hack nowadays, but when some loop structure becomes too hard for my simple brain (usually related to some complex format of the input data) I still occasionally start drawing JSP diagrams to help me ensure I have everything accounted for and sorted out (as reliably as I can manage).

https://www.tinylinux.info/
DOWNLOAD wd_multi for hundreds of 'distros' at your fingertips: viewtopic.php?p=99154#p99154
Αξίζει να μεταφραστεί;

User avatar
wizard
Posts: 1993
Joined: Sun Aug 09, 2020 7:50 pm
Has thanked: 2667 times
Been thanked: 694 times

Re: How to Mount Drives with one click scripts

Post by wizard »

For me, simple is always better and I try to comment the code so others (and especially me a year later) can understand what it does. Sometimes even simple code does unexpected things, like the mntdrv script writing a 0 byte file. It was happening when the mount or umount command executed, but they were not the root cause. Turned out it was the IF statement that checks the $1 argument, something about my original syntax caused it. :?:
I always like it when someone like @wiak jumps in with "or you could do it like this" and I learn you could do in one line what I did in 5-10 lines. :thumbup2:

Thanks
wizard

Big pile of OLD computers

geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

Re: How to Mount Drives with one click scripts

Post by geo_c »

@wiak, @wizard, @mikewalsh,

When I say very,very,very simple, I mean for example:

Code: Select all

#!/bin/sh

mount /dev/sda1 /mnt/sda1

As this one is impossible to screw up, unless it's 3:45 am and I can't see straight anymore!

~geo

Last edited by geo_c on Sat Jul 23, 2022 11:52 pm, edited 1 time in total.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2883
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2208 times
Been thanked: 880 times

Re: How to Mount Drives with one click scripts

Post by geo_c »

geo_c wrote: Sat Jul 23, 2022 3:09 pm

Code: Select all

#!/bin/sh

mount /dev/sda1 /mnt/sda1

As this one is impossible to screw up, unless it 3:45 am and I can't see straight anymore!

~geo

But perhaps it would be more sophisticated if I wrote it like this:

Code: Select all

#!/bin/sh

# Mounting script to be used on linux filesystem
# use of mount command conditional to an available drive
# use of mount command requires an existing mount point
# for list of available options use command - mount --help
# mount points can be found in /mnt
# available drives can be found in /dev
 
mount /dev/sda1 /mnt/sda1

# following confirmation only displayed when script is run from command line
echo "sda1 mounted and ready for use"
Last edited by geo_c on Sat Jul 23, 2022 3:26 pm, edited 1 time in total.

geo_c
Old School Hipster, and Such

User avatar
mikewalsh
Moderator
Posts: 6185
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 803 times
Been thanked: 1994 times

Re: How to Mount Drives with one click scripts

Post by mikewalsh »

@geo_c :-

The only difference twixt mine & yours is that instead of using the 'device' label (/dev/sdx) - which can, on occasion, change - I am instead using the partition's 'unique identifier' (the UUID) which is generated & assigned to every partition by gParted when it creates it. This will NOT change (unless you re-format the partition, when a new UUID will be generated).

Sdb - my secondary, internal 'data drive' - used to be an external, USB 3.0 desktop Seagate 'Expansion' drive until I ripped the Barracuda HDD out of the plastic casing and installed it internally into the new HP instead. Those two partitions on sdb must have been formatted originally nearly 6 years ago now.

I've been using those same two UUIDs for years.

Mike. :thumbup:

tom22251
Posts: 5
Joined: Mon Jun 27, 2022 1:50 pm
Been thanked: 3 times

Re: How to Mount Drives with one click scripts

Post by tom22251 »

Hi guys,
great stuff!!!
Few years ago I came up with something similar, but as a rox-app, with the icon changing when clicked on, and opening a rox window.
Here's the script and a zip file if you want to dowload it. It is intended to be placed in the /root folder, but of course can be put anywhere if you change the location parameter accordingly.

#!/bin/sh
#########################################################
D="/dev/sda1" #device
M="/mnt/sda1" #mountpoint
L="/root/A1" #to be placed at this location

A=${L}/.DirIcon #roxapp icon
B=${L}/.1.png #roxapp icon 1
C=${L}/.2.png #roxapp icon 2
P="aplay -q ${L}/1.au" #clicking sound
R=${L}/sign #on/off sign to be read

${P}; . ${R} #reading
if [ $k = "OFF" ];then
mount ${D} ${M}; sync
rox ${M} #opening a rox window
mv -T $A $B #renaming the icons
mv -T $C $A
mv -T $B $C
echo "k=ON" > ${R} #registering the ON state
else
rox -D ${M} #closing the rox window
umount ${M}; sync
echo "k=OFF" > ${R} #registering the OFF state
mv -T $A $B #renaming the icons
mv -T $C $A
mv -T $B $C
fi
exit
#########################################################

drive sda1.jpg
drive sda1.jpg (12.66 KiB) Viewed 2870 times
rox-app.zip
(12.78 KiB) Downloaded 90 times

Happy Sunday!
Tom

Post Reply

Return to “File Management”