Force Dolphin to mount partitions in /mnt

Moderator: BarryK

Post Reply
Caramel
Posts: 483
Joined: Sun Oct 02, 2022 6:25 pm
Location: France
Has thanked: 101 times
Been thanked: 81 times

Force Dolphin to mount partitions in /mnt

Post by Caramel »

To force Dolphin to mount a partition in /mnt, we can cheat with a fake /etc/fstab.

If the partition is written as mounted in /mnt in the file fstab, Dolphin mounts the partition at this place.

Dolphin in the lastest KDE edition of the Porteus distribution mount the partitions in /mnt

Here is the script udev-fstab-update used by Porteus (in /sbin)

Code: Select all

#!/bin/sh
# Handle /etc/fstab entries and /mnt directories for removable devices
# so they are mounted under /mnt directory.
# This script is called by /etc/udev/rules.d/10-porteus-fstab-update.rules

# Inspired by 'udev-fstab-update' from Slax 6.1.2 but rewritten from scratch
# for Porteus by fanthom <fanthom@porteus.org> and Ahau <ahau@porteus.org>

# Variables initialized by the udev environment:
# $ACTION (add, change, remove)
# $DEVNAME (device name including path)
# $ID_FS_TYPE (filesystem type)

# Exit if called during boot:
#ps a | grep -q 'bash /etc/rc.d/rc.S' && exit

# Exit if 'nohd' cheatcode is used:
egrep -qo " nohd( |\$)" /etc/bootcmd.cfg && exit

# Our variables:
FSTAB=/etc/fstab
DEVICE=`echo $DEVNAME | sed s_^/dev/__`
MNTPT=/mnt/$DEVICE
INITRD_PATH=/mnt/live
[ `/bin/ls -1 ${INITRD_PATH}/mnt | grep $DEVICE` ] && MNTPT=${INITRD_PATH}${MNTPT}
FS=$ID_FS_TYPE
[ "$FS" ] || FS=`/sbin/blkid $DEVNAME | egrep -o ' TYPE=[^ ]+' | cut -d'"' -f2`
cdtest=`echo $DEVICE | grep ^sr`
[ "$cdtest" ] && GVICON=',comment=x-gvfs-icon=media-optical-symbolic'

# Make sure the part of a script after 'mutex_lock' call is atomic,
# that means the 'locked' part of the script can never be execuetd 
# from several processes at the same time, in parallel.
# Every script waits until it gathers the lock.
# The lock directory is saved in /dev instead of /tmp, because /tmp may be
# readonly at the time when the lock is needed (eg. when udev is starting)
# $1 = name of the lock
#
mutex_lock()
{
   while ! mkdir "/dev/ll-mutex-lock-$1" 2>/dev/null; do
      usleep 100000;
   done
}

# Unlock the lock so another waiting process can reusse it and continue
# $1 = name of the lock
#
mutex_unlock()
{
   rmdir "/dev/ll-mutex-lock-$1" 2>/dev/null
}

fstab_add() {
# Create the mount point:
test ! -d $MNTPT && mkdir $MNTPT
# Create new entry as per the 'mopt=' or 'nmopt=' or 'rmopt=' cheatcode - or Porteus defaults:
MOPT=`egrep -o " mopt=[^ ]+" /etc/bootcmd.cfg | cut -d= -f2`
NMOPT=`egrep -o " nmopt=[^ ]+" /etc/bootcmd.cfg | cut -d= -f2`
RMOPT=`egrep -o " rmopt=[^ ]+" /etc/bootcmd.cfg | cut -d= -f2`

[ $MOPT ] || MOPT="users,noatime,nodiratime,suid,dev,exec,async"
[ $NMOPT ] || NMOPT="noatime,nodiratime,group,nodev,exec,async"
[ $RMOPT ] || RMOPT="noatime,nodiratime,suid,dev,exec,async,nofail"
if `test -e /lib/udev/rules.d/80-udisks2.rules`; then MOPT="$MOPT,comment=x-gvfs-show$GVICON"; NMOPT="$NMOPT,comment=x-gvfs-show$GVICON"; fi
if [ "$FS" = vfat ]; then sed -i '\?^'$DEVNAME'?d' $FSTAB; echo "$DEVNAME $MNTPT vfat $MOPT,umask=0,check=s,utf8 0 0" >> $FSTAB
elif [ "$FS" = ntfs ]; then sed -i '\?^'$DEVNAME'?d' $FSTAB; echo "$DEVNAME $MNTPT ntfs-3g $NMOPT 0 0" >> $FSTAB; chmod 777 $MNTPT
elif [ "$FS" = linux_raid_member ]; then sed -i '\?^'$DEVNAME'?d' $FSTAB; echo "$DEVNAME $MNTPT $FS $RMOPT 0 0" >> $FSTAB; chmod 777 $MNTPT
else sed -i '\?^'$DEVNAME'?d' $FSTAB; echo "$DEVNAME $MNTPT $FS $MOPT 0 0" >> $FSTAB
fi
}

fstab_change() {
# Change entry as per the 'mopt=' or 'nmopt=' cheatcode or porteus defaults:
MOPT=`egrep -o " mopt=[^ ]+" /etc/bootcmd.cfg | cut -d= -f2`
NMOPT=`egrep -o " nmopt=[^ ]+" /etc/bootcmd.cfg | cut -d= -f2`
[ $MOPT ] || MOPT="users,noatime,nodiratime,suid,dev,exec,async"
[ $NMOPT ] || NMOPT="noatime,nodiratime,group,nodev,exec,async"
if `test -e /lib/udev/rules.d/80-udisks2.rules`; then MOPT="$MOPT,comment=x-gvfs-show$GVICON"; NMOPT="$NMOPT,comment=x-gvfs-show$GVICON"; fi
if [ "$FS" = vfat ]; then sed -i "s_^$DEVNAME.*_$DEVNAME $MNTPT vfat $MOPT,umask=0,check=s,utf8 0 0_" $FSTAB; mkdir -p $MNTPT
elif [ "$FS" = ntfs ]; then sed -i "s_^$DEVNAME.*_$DEVNAME $MNTPT ntfs-3g $NMOPT 0 0_" $FSTAB; mkdir -p $MNTPT; chmod 777 $MNTPT
elif [ "$FS" = linux_raid_member ]; then sed -i "s_^$DEVNAME.*_$DEVNAME $MNTPT $FS $RMOPT 0 0_" $FSTAB; mkdir -p $MNTPT; chmod 777 $MNTPT
elif [ "$FS" = swap ]; then sed -i "s_^$DEVNAME.*_$DEVNAME none swap sw,pri=1 0 0_" $FSTAB
else sed -i "s_^$DEVNAME.*_$DEVNAME $MNTPT $FS $MOPT 0 0_" $FSTAB; mkdir -p $MNTPT
fi
}

mutex_lock udev-fstab-update

if [ "$ACTION" = add ]; then
    # Add new entry:
    [ "$FS" ] && fstab_add
elif [ "$ACTION" = change ]; then
    if grep -w "^/dev/$DEVICE" $FSTAB; then
	# Change existing entry:
	[ "$FS" ] && fstab_change
    else
	# Add new entry:
	[ "$FS" ] && fstab_add
    fi
elif [ "$ACTION" = remove ]; then
    # Delete fstab entry:
    sed -i "/$DEVICE /d" $FSTAB
    # Remove the mount point:
    rmdir $MNTPT 2>/dev/null
fi

mutex_unlock udev-fstab-update

I have removed the parts with "cheatcodes" that are Porteus boot options and created too from this script another script (makefakestab) that makes a fake fstab with all the partitions present at startup.

When a drive is plugged (or unplugged) the script udev-fstab-update is called by a file located in /etc/udev/rules.d

I made a pet file with these 3 files :

As one of the 3 files is in /root/Startup, a reboot is needed before test the pet

EDIT : pet removed

Last edited by Caramel on Sat Oct 19, 2024 3:16 pm, edited 2 times in total.
Caramel
Posts: 483
Joined: Sun Oct 02, 2022 6:25 pm
Location: France
Has thanked: 101 times
Been thanked: 81 times

Fix for fstab-manager

Post by Caramel »

In the previous version of udev-fstab-update, the line 89

Code: Select all

grep $DEVNAME /tmp/partitions && [ ! "$ACTION" = change ] && exit

prevents to add or remove an entry in /etc/fstab corresponding to a partition present at startup

This could cause a bug if a removable drive is plugged at startup and then replaced by a drive with a different type of partitionment
(For example a usb stick with a vfat partition replaced by a usb stick with a ext4 partition)

A solution considered is to still prevent the removal of a partition present at startup but to change the "add" action to a "change" action/

Code: Select all

grep $DEVNAME /tmp/partitions && [  "$ACTION" = remove ] && exit
grep $DEVNAME /tmp/partitions && [  "$ACTION" = add ] && $ACTION=change

(EDIT : ERROR it's ACTION=change, not $ACTION=change)

EDIT : pet removed

Last edited by Caramel on Sat Oct 19, 2024 3:19 pm, edited 1 time in total.
User avatar
Federico
Posts: 292
Joined: Tue Jun 20, 2023 2:40 pm
Has thanked: 1 time
Been thanked: 36 times

Re: Force Dolphin to mount partitions in /mnt

Post by Federico »

Hi Caramel,
as far as I know, there's no need to "force" anything. At least since EasyOS Dunfell I just a use a simple script on /root/Startup of the type:

Code: Select all

#!/bin/sh
mkdir /mnt/nvme0n1p3
mkdir /mnt/nvme0n1p4
mount -t ntfs /dev/nvme0n1p3 /mnt/nvme0n1p3
mount -t ntfs /dev/nvme0n1p4 /mnt/nvme0n1p4

Partitions of the HDD or SSD (Windows partitions) will be automatically mounted when the system boots up.

For mounting and reading automatically FAT32 formatted USB sticks, scripts similar to the following can be used:

for mounting:

Code: Select all

#!/bin/bash
mkdir /mnt/usb_stick
mount -t vfat /dev/sdb1 /mnt/usb_stick
dolphin /mnt/usb_stick

for unmounting:

Code: Select all

#!/bin/bash
umount /mnt/usb_stick
rmdir /mnt/usb_stick 

The point of the question is, that this has to do with the system as a whole, not just with KDE Dolphin.
What the system can access can be also accessed by any file manager. Only in the case that a file manager runs as a flatpk or inside any sort of closed container there could be discrepancies.

Desktop PC
Case: Sharkoon S25-W MB: Asus Rog Strix B550-A PSU: XFX Pro 750W CPU: AMD Ryzen 5700X @ 4.6 GHz RAM: Corsair 32 GB DDR4 @ 3000 MHz Heatsink: Scythe Mugen 5 rev. B VGA: Asus Tuf RTX 3080 12 GB OC

Laptop PC: Asus Zenbook UX325E

Caramel
Posts: 483
Joined: Sun Oct 02, 2022 6:25 pm
Location: France
Has thanked: 101 times
Been thanked: 81 times

New fix for fstab-manager

Post by Caramel »

I made a big error in the last fix ("$ACTION=change" instead of "ACTION=change")

Sorry for the inconvenience.

fstab_manager-0.3.pet
(2.34 KiB) Downloaded 7 times
Federico wrote: Sat Oct 19, 2024 2:50 pm

Hi Caramel,
as far as I know, there's no need to "force" anything. At least since EasyOS Dunfell I just a use a simple script on /root/Startup of the type:

Code: Select all

#!/bin/sh
mkdir /mnt/nvme0n1p3
mkdir /mnt/nvme0n1p4
mount -t ntfs /dev/nvme0n1p3 /mnt/nvme0n1p3
mount -t ntfs /dev/nvme0n1p4 /mnt/nvme0n1p4

Partitions of the HDD or SSD (Windows partitions) will be automatically mounted when the system boots up.

For mounting and reading automatically FAT32 formatted USB sticks, scripts similar to the following can be used:

for mounting:

Code: Select all

#!/bin/bash
mkdir /mnt/usb_stick
mount -t vfat /dev/sdb1 /mnt/usb_stick
dolphin /mnt/usb_stick

for unmounting:

Code: Select all

#!/bin/bash
umount /mnt/usb_stick
rmdir /mnt/usb_stick 

The point of the question is, that this has to do with the system as a whole, not just with KDE Dolphin.
What the system can access can be also accessed by any file manager. Only in the case that a file manager runs as a flatpk or inside any sort of closed container there could be discrepancies.

I want a solution that works in all cases and without intervention of the user

Caramel
Posts: 483
Joined: Sun Oct 02, 2022 6:25 pm
Location: France
Has thanked: 101 times
Been thanked: 81 times

New version of fstab-manager

Post by Caramel »

The new version takes into account the case where an usb drive is plugged in, a partition of the drive is unmounted without the key being unplugged and then the partition is mounted again (by dolphin)
It is necessary that a partition is present in /etc/fstab before dolphin mount it.

The lines 80 and 81 in /usr/bin/udev-fstab-update

grep $DEVNAME /tmp/partitions && [ "$ACTION" = remove ] && exit
grep $DEVNAME /tmp/partitions && [ "$ACTION" = add ] && ACTION=change

are replaced by

Code: Select all

grep $DEVNAME /etc/fstab && [  "$ACTION" = remove ] && exit
grep $DEVNAME /etc/fstab && [  "$ACTION" = add ] && ACTION=change
fstab_manager-0.4.pet
(2.34 KiB) Downloaded 5 times
Post Reply

Return to “EasyOS”