How to mount USB drive in udev rule?2019 Community Moderator Electionmount is not executed when called by udevWhy does Linux mark FAT as 'dirty' simply due to mounting it?Why is my udev-fired script not working?udev rule for usb attach/detach not triggeringudev triggers rule but script fails to execute properlyUDEV-Rule to mount USB-Stick / execute scriptWhy is my udev mounted device not staying put?udev won't mount the plugged in deviceWhy is my udev rule not working?run mount as root inside udev ruleUDEV issue.. USB triggers script but USB doesn't mount properlyUdev inhibits auto mountMount fuse.mergerfs in udev script

Output visual diagram of picture

python displays `n` instead of breaking a line

Why does the frost depth increase when the surface temperature warms up?

How do I lift the insulation blower into the attic?

Capacitor electron flow

Amorphous proper classes in MK

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

Mortal danger in mid-grade literature

How to get directions in deep space?

Why can't I get pgrep output right to variable on bash script?

Checking @@ROWCOUNT failing

Writing in a Christian voice

Not hide and seek

Strange behavior in TikZ draw command

Started in 1987 vs. Starting in 1987

Has the laser at Magurele, Romania reached the tenth of the Sun power?

Why does a 97 / 92 key piano exist by Bosendorfer?

How do you say "Trust your struggle." in French?

When is the exact date for EOL of Ubuntu 14.04 LTS?

Taking the numerator and the denominator

Offset in split text content

Hashing password to increase entropy

Do I have to take mana from my deck or hand when tapping this card?

What do the positive and negative (+/-) transmit and receive pins mean on Ethernet cables?



How to mount USB drive in udev rule?



2019 Community Moderator Electionmount is not executed when called by udevWhy does Linux mark FAT as 'dirty' simply due to mounting it?Why is my udev-fired script not working?udev rule for usb attach/detach not triggeringudev triggers rule but script fails to execute properlyUDEV-Rule to mount USB-Stick / execute scriptWhy is my udev mounted device not staying put?udev won't mount the plugged in deviceWhy is my udev rule not working?run mount as root inside udev ruleUDEV issue.. USB triggers script but USB doesn't mount properlyUdev inhibits auto mountMount fuse.mergerfs in udev script










1















I setup a rule in udev to run a script in background to automount the USB drive. It managed to run the script fine, but the mounting is failing for some reason.



RULE:



ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/etc/mntUsbChk.sh &"


In this script:



#!/bin/sh

sleep 2

mkdir /mnt/usb

foundUsb=false

if [ -e /dev/sda1 ]; then
mount /dev/sda1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdb1 ]; then
mount /dev/sdb1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdc1 ]; then
mount /dev/sdc1 /mnt/usb
$foundUsb=true
fi

if [ $foundUsb -eq false ]; then
exit
fi

echo "USB MOUNTED"



[ 1610.868626] FAT-fs (sdb1): Volume was not properly unmounted. Some
data may be corrupt. Please run fsck.




EDIT: More details: The script was being executed for sure because the /mnt/usb folder was created after I plugged in the USB.



And also, if I manually type mount /dev/sda1 /mnt/usb after I plug in USB before I enable the rule, it was able to mount it just fine. I just don't get it why it cannot mount in the rules udev.










share|improve this question



















  • 2





    See my answer here.

    – jasonwryan
    Aug 13 '18 at 4:34






  • 1





    What distro are you on?

    – Tim
    Aug 14 '18 at 10:12











  • Does you system run only in ram? (busybox tag). Is the filesystem persistent? If it is not persistent, does the script need to run more then once in the lifetime of the boot session?

    – Tim
    Aug 14 '18 at 10:29












  • @Tim, busybox. Filesystem is persistent.

    – GeneCode
    Aug 14 '18 at 23:28











  • If it's plain BusyBox, how is it you have udev? BusyBox uses mdev.

    – Tim
    Aug 15 '18 at 6:11















1















I setup a rule in udev to run a script in background to automount the USB drive. It managed to run the script fine, but the mounting is failing for some reason.



RULE:



ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/etc/mntUsbChk.sh &"


In this script:



#!/bin/sh

sleep 2

mkdir /mnt/usb

foundUsb=false

if [ -e /dev/sda1 ]; then
mount /dev/sda1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdb1 ]; then
mount /dev/sdb1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdc1 ]; then
mount /dev/sdc1 /mnt/usb
$foundUsb=true
fi

if [ $foundUsb -eq false ]; then
exit
fi

echo "USB MOUNTED"



[ 1610.868626] FAT-fs (sdb1): Volume was not properly unmounted. Some
data may be corrupt. Please run fsck.




EDIT: More details: The script was being executed for sure because the /mnt/usb folder was created after I plugged in the USB.



And also, if I manually type mount /dev/sda1 /mnt/usb after I plug in USB before I enable the rule, it was able to mount it just fine. I just don't get it why it cannot mount in the rules udev.










share|improve this question



















  • 2





    See my answer here.

    – jasonwryan
    Aug 13 '18 at 4:34






  • 1





    What distro are you on?

    – Tim
    Aug 14 '18 at 10:12











  • Does you system run only in ram? (busybox tag). Is the filesystem persistent? If it is not persistent, does the script need to run more then once in the lifetime of the boot session?

    – Tim
    Aug 14 '18 at 10:29












  • @Tim, busybox. Filesystem is persistent.

    – GeneCode
    Aug 14 '18 at 23:28











  • If it's plain BusyBox, how is it you have udev? BusyBox uses mdev.

    – Tim
    Aug 15 '18 at 6:11













1












1








1








I setup a rule in udev to run a script in background to automount the USB drive. It managed to run the script fine, but the mounting is failing for some reason.



RULE:



ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/etc/mntUsbChk.sh &"


In this script:



#!/bin/sh

sleep 2

mkdir /mnt/usb

foundUsb=false

if [ -e /dev/sda1 ]; then
mount /dev/sda1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdb1 ]; then
mount /dev/sdb1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdc1 ]; then
mount /dev/sdc1 /mnt/usb
$foundUsb=true
fi

if [ $foundUsb -eq false ]; then
exit
fi

echo "USB MOUNTED"



[ 1610.868626] FAT-fs (sdb1): Volume was not properly unmounted. Some
data may be corrupt. Please run fsck.




EDIT: More details: The script was being executed for sure because the /mnt/usb folder was created after I plugged in the USB.



And also, if I manually type mount /dev/sda1 /mnt/usb after I plug in USB before I enable the rule, it was able to mount it just fine. I just don't get it why it cannot mount in the rules udev.










share|improve this question
















I setup a rule in udev to run a script in background to automount the USB drive. It managed to run the script fine, but the mounting is failing for some reason.



RULE:



ACTION=="add", KERNEL=="sd[a-z]*", RUN+="/etc/mntUsbChk.sh &"


In this script:



#!/bin/sh

sleep 2

mkdir /mnt/usb

foundUsb=false

if [ -e /dev/sda1 ]; then
mount /dev/sda1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdb1 ]; then
mount /dev/sdb1 /mnt/usb
$foundUsb=true
fi

if [ -e /dev/sdc1 ]; then
mount /dev/sdc1 /mnt/usb
$foundUsb=true
fi

if [ $foundUsb -eq false ]; then
exit
fi

echo "USB MOUNTED"



[ 1610.868626] FAT-fs (sdb1): Volume was not properly unmounted. Some
data may be corrupt. Please run fsck.




EDIT: More details: The script was being executed for sure because the /mnt/usb folder was created after I plugged in the USB.



And also, if I manually type mount /dev/sda1 /mnt/usb after I plug in USB before I enable the rule, it was able to mount it just fine. I just don't get it why it cannot mount in the rules udev.







usb udev automounting busybox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 19 '18 at 19:16









slm

254k71537687




254k71537687










asked Aug 10 '18 at 0:59









GeneCodeGeneCode

659




659







  • 2





    See my answer here.

    – jasonwryan
    Aug 13 '18 at 4:34






  • 1





    What distro are you on?

    – Tim
    Aug 14 '18 at 10:12











  • Does you system run only in ram? (busybox tag). Is the filesystem persistent? If it is not persistent, does the script need to run more then once in the lifetime of the boot session?

    – Tim
    Aug 14 '18 at 10:29












  • @Tim, busybox. Filesystem is persistent.

    – GeneCode
    Aug 14 '18 at 23:28











  • If it's plain BusyBox, how is it you have udev? BusyBox uses mdev.

    – Tim
    Aug 15 '18 at 6:11












  • 2





    See my answer here.

    – jasonwryan
    Aug 13 '18 at 4:34






  • 1





    What distro are you on?

    – Tim
    Aug 14 '18 at 10:12











  • Does you system run only in ram? (busybox tag). Is the filesystem persistent? If it is not persistent, does the script need to run more then once in the lifetime of the boot session?

    – Tim
    Aug 14 '18 at 10:29












  • @Tim, busybox. Filesystem is persistent.

    – GeneCode
    Aug 14 '18 at 23:28











  • If it's plain BusyBox, how is it you have udev? BusyBox uses mdev.

    – Tim
    Aug 15 '18 at 6:11







2




2





See my answer here.

– jasonwryan
Aug 13 '18 at 4:34





See my answer here.

– jasonwryan
Aug 13 '18 at 4:34




1




1





What distro are you on?

– Tim
Aug 14 '18 at 10:12





What distro are you on?

– Tim
Aug 14 '18 at 10:12













Does you system run only in ram? (busybox tag). Is the filesystem persistent? If it is not persistent, does the script need to run more then once in the lifetime of the boot session?

– Tim
Aug 14 '18 at 10:29






Does you system run only in ram? (busybox tag). Is the filesystem persistent? If it is not persistent, does the script need to run more then once in the lifetime of the boot session?

– Tim
Aug 14 '18 at 10:29














@Tim, busybox. Filesystem is persistent.

– GeneCode
Aug 14 '18 at 23:28





@Tim, busybox. Filesystem is persistent.

– GeneCode
Aug 14 '18 at 23:28













If it's plain BusyBox, how is it you have udev? BusyBox uses mdev.

– Tim
Aug 15 '18 at 6:11





If it's plain BusyBox, how is it you have udev? BusyBox uses mdev.

– Tim
Aug 15 '18 at 6:11










3 Answers
3






active

oldest

votes


















2





+25









This script will fail is /mnt/usb already exists. Not sure if this is the source of the problem, but it is for sure an error of the script.




mount /mnt/usb




Use instead:



mount -p /mnt/usb


From man mount:




-p, --parents



no error if existing, make parent directories as needed







share|improve this answer























  • Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.

    – GeneCode
    Aug 14 '18 at 23:28


















1














You have no guarantee the USB drive will be sda1,sdb1,sdc1 on add. I think some better logic may be needed there, but completely unrelated to the question.



For the question, see the existing link below:
Why does Linux mark FAT as 'dirty' simply due to mounting it?



My understanding is just pulling a usb from a Windows machine w/o ejecting doesn't clear the dirty bit on the drive. Linux assumes the worst when the dirty bit is set. I'm assuming here you can automatically run fsck to clear the dirty bit on this error message if present, then proceed to mount again. Although I'm not sure if a call to umount or fsck would clear the dirty bit.



I hope this heads in the right direction.






share|improve this answer























  • Helps if I add the link reference: unix.stackexchange.com/questions/230181/…

    – MalformedPacket
    Aug 13 '18 at 5:35



















0














Although it is not very well documented, you are not supposed to run mount from a Udev rule.



Please see my answer to a similar question for alternatives.






share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461700%2fhow-to-mount-usb-drive-in-udev-rule%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2





    +25









    This script will fail is /mnt/usb already exists. Not sure if this is the source of the problem, but it is for sure an error of the script.




    mount /mnt/usb




    Use instead:



    mount -p /mnt/usb


    From man mount:




    -p, --parents



    no error if existing, make parent directories as needed







    share|improve this answer























    • Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.

      – GeneCode
      Aug 14 '18 at 23:28















    2





    +25









    This script will fail is /mnt/usb already exists. Not sure if this is the source of the problem, but it is for sure an error of the script.




    mount /mnt/usb




    Use instead:



    mount -p /mnt/usb


    From man mount:




    -p, --parents



    no error if existing, make parent directories as needed







    share|improve this answer























    • Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.

      – GeneCode
      Aug 14 '18 at 23:28













    2





    +25







    2





    +25



    2




    +25





    This script will fail is /mnt/usb already exists. Not sure if this is the source of the problem, but it is for sure an error of the script.




    mount /mnt/usb




    Use instead:



    mount -p /mnt/usb


    From man mount:




    -p, --parents



    no error if existing, make parent directories as needed







    share|improve this answer













    This script will fail is /mnt/usb already exists. Not sure if this is the source of the problem, but it is for sure an error of the script.




    mount /mnt/usb




    Use instead:



    mount -p /mnt/usb


    From man mount:




    -p, --parents



    no error if existing, make parent directories as needed








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 14 '18 at 10:35









    TimTim

    531211




    531211












    • Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.

      – GeneCode
      Aug 14 '18 at 23:28

















    • Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.

      – GeneCode
      Aug 14 '18 at 23:28
















    Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.

    – GeneCode
    Aug 14 '18 at 23:28





    Good to know about the -p flag. But I always delete the /mnt/usb first before i plug in my usb.

    – GeneCode
    Aug 14 '18 at 23:28













    1














    You have no guarantee the USB drive will be sda1,sdb1,sdc1 on add. I think some better logic may be needed there, but completely unrelated to the question.



    For the question, see the existing link below:
    Why does Linux mark FAT as 'dirty' simply due to mounting it?



    My understanding is just pulling a usb from a Windows machine w/o ejecting doesn't clear the dirty bit on the drive. Linux assumes the worst when the dirty bit is set. I'm assuming here you can automatically run fsck to clear the dirty bit on this error message if present, then proceed to mount again. Although I'm not sure if a call to umount or fsck would clear the dirty bit.



    I hope this heads in the right direction.






    share|improve this answer























    • Helps if I add the link reference: unix.stackexchange.com/questions/230181/…

      – MalformedPacket
      Aug 13 '18 at 5:35
















    1














    You have no guarantee the USB drive will be sda1,sdb1,sdc1 on add. I think some better logic may be needed there, but completely unrelated to the question.



    For the question, see the existing link below:
    Why does Linux mark FAT as 'dirty' simply due to mounting it?



    My understanding is just pulling a usb from a Windows machine w/o ejecting doesn't clear the dirty bit on the drive. Linux assumes the worst when the dirty bit is set. I'm assuming here you can automatically run fsck to clear the dirty bit on this error message if present, then proceed to mount again. Although I'm not sure if a call to umount or fsck would clear the dirty bit.



    I hope this heads in the right direction.






    share|improve this answer























    • Helps if I add the link reference: unix.stackexchange.com/questions/230181/…

      – MalformedPacket
      Aug 13 '18 at 5:35














    1












    1








    1







    You have no guarantee the USB drive will be sda1,sdb1,sdc1 on add. I think some better logic may be needed there, but completely unrelated to the question.



    For the question, see the existing link below:
    Why does Linux mark FAT as 'dirty' simply due to mounting it?



    My understanding is just pulling a usb from a Windows machine w/o ejecting doesn't clear the dirty bit on the drive. Linux assumes the worst when the dirty bit is set. I'm assuming here you can automatically run fsck to clear the dirty bit on this error message if present, then proceed to mount again. Although I'm not sure if a call to umount or fsck would clear the dirty bit.



    I hope this heads in the right direction.






    share|improve this answer













    You have no guarantee the USB drive will be sda1,sdb1,sdc1 on add. I think some better logic may be needed there, but completely unrelated to the question.



    For the question, see the existing link below:
    Why does Linux mark FAT as 'dirty' simply due to mounting it?



    My understanding is just pulling a usb from a Windows machine w/o ejecting doesn't clear the dirty bit on the drive. Linux assumes the worst when the dirty bit is set. I'm assuming here you can automatically run fsck to clear the dirty bit on this error message if present, then proceed to mount again. Although I'm not sure if a call to umount or fsck would clear the dirty bit.



    I hope this heads in the right direction.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 13 '18 at 5:34









    MalformedPacketMalformedPacket

    113




    113












    • Helps if I add the link reference: unix.stackexchange.com/questions/230181/…

      – MalformedPacket
      Aug 13 '18 at 5:35


















    • Helps if I add the link reference: unix.stackexchange.com/questions/230181/…

      – MalformedPacket
      Aug 13 '18 at 5:35

















    Helps if I add the link reference: unix.stackexchange.com/questions/230181/…

    – MalformedPacket
    Aug 13 '18 at 5:35






    Helps if I add the link reference: unix.stackexchange.com/questions/230181/…

    – MalformedPacket
    Aug 13 '18 at 5:35












    0














    Although it is not very well documented, you are not supposed to run mount from a Udev rule.



    Please see my answer to a similar question for alternatives.






    share|improve this answer



























      0














      Although it is not very well documented, you are not supposed to run mount from a Udev rule.



      Please see my answer to a similar question for alternatives.






      share|improve this answer

























        0












        0








        0







        Although it is not very well documented, you are not supposed to run mount from a Udev rule.



        Please see my answer to a similar question for alternatives.






        share|improve this answer













        Although it is not very well documented, you are not supposed to run mount from a Udev rule.



        Please see my answer to a similar question for alternatives.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 14 hours ago









        MetamorphicMetamorphic

        356110




        356110



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f461700%2fhow-to-mount-usb-drive-in-udev-rule%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.