1

I have made a script that loads a VHD file, fixes it with ntfsfix so it can be mount rw and then mounts it. This script works absolute wonders when run via the terminal using sudo, but when running it via crontab, ~/.profile or via startup applications, it does not work. The script in question is

#!/bin/sh
rmmod nbd
modprobe nbd max_part=16
qemu-nbd -c /dev/nbd0 '/media/tygo/1.8TB Drive/MEGA.vhdx' 
ntfsfix /dev/nbd0p2
mount -o rw /dev/nbd0p2 /home/tygo/MEGA

2 Answers 2

1

To run your script using crontab, make sure you're specifying full paths:

#!/bin/sh
/usr/sbin/rmmod nbd
/usr/sbin/modprobe nbd max_part=16
/usr/bin/qemu-nbd -c /dev/nbd0 '/media/tygo/1.8TB Drive/MEGA.vhdx' 
/usr/bin/ntfsfix /dev/nbd0p2
/usr/bin/mount -o rw /dev/nbd0p2 /home/tygo/MEGA

You could find paths using whereis command: whereis mount.

Then run crontab with sudo: sudo crontab -e and add full path to your script:

@reboot /home/user/scripts/vhdscript.sh

Use your own time instead of @reboot

To see if it is executed, you could add some additional information to your script. For example, add to the end of your script:

echo "VHD file loading have been completed" | logger

Then, using terminal, monitor your script execution by your message appearing:

journalctl -f | grep VHD
1

I eventually managed to fix it by sleeping a second after loading the VHD with qemu and running it with cronjob with sudo every half minute using the following two lines:

* * * * * sudo /etc/megaMount.sh > /var/log/megaMount.log
* * * * * (sleep 30; sudo /etc/megaMount.sh > /var/log/megaMount.log)

The edited script is as follows, the first line checks if it's mounted already:

if ! grep -qs '/home/tygo/MEGA' /proc/mounts; 
then 
        sleep 1
        rmmod nbd
        modprobe nbd max_part=16
        qemu-nbd -c /dev/nbd0 '/media/tygo/1.8TB Drive/MEGA.vhdx' 
        sleep 1
        ntfsfix /dev/nbd0p2
        mount -o rw /dev/nbd0p2 /home/tygo/MEGA
fi

EDIT: Forgot to mention that the jobs are added using sudo crontab -e, not exactly sure why I then also need to run the script using sudo, but it doesn't work without it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .