3

I'm trying to make it so that when I insert a dvd into the cdrom It exports it into an iso using dd. Here's the script for exporting to an iso

#!/bin/bash
timestamp=$(date +"%Y%m%d_%H%M%S")
filename="/home/tom/sambashare/${timestamp}.iso"
dd if=/dev/sr0 of=${filename} status=progress
md5sum ${filename} > ${filename}.md5

The script works just fine when launched from the terminal. Hovewer when I try to launch it from udev (using an udev rule) it will create a file and add some kind of data to it till about 139mb where it stops and gets stuck. An interesting thing is that the computer doesn't make any noises like it does when it's reading the dvd when launched from terminal

Here is the udev rule:

ACTION=="change", KERNEL=="sr0", SUBSYSTEM=="block", ENV{ID_CDROM_MEDIA}=="1", RUN+="/home/tom/mybashscript.sh"

I tried adding a sudo into the udev rule which didn't work as well. Using cat instead of dd shows the same behaviour.

tpko
  • 39
  • 1
    Why are you using dd instead of cat? Does it work as expected if you use cat instead? (I doubt it, to be honest, but dd is weird) – terdon Mar 12 '24 at 16:53
  • 1
    start a scriot from udev that starts a script that starts your scriot detached (ie space + & at the end) and it should continue. – Rinzwind Mar 12 '24 at 17:02
  • 1
    Sounds like an answer, @Rinzwind. Do you really need three steps? udev.script -> script1 -> script2 &? – terdon Mar 12 '24 at 17:11
  • 1
    if udev detaches by itself I do not know why it stops at 139Mb. and if it does not I would think you need an extra step :P Looking at it: udev.script -> script1 & can be good enough? – Rinzwind Mar 12 '24 at 18:14
  • 1
    You, can't do it from UDEV rules in a single shot like that ... The device triggering that rule i.e. sr0 isn't made available to your script until that rule finishes evaluation and completes execution which is not going to happen given your script operates on that same device ... Please, see for example Different behaviour of bash script in udev ... Also, "somewhat" related https://askubuntu.com/q/1503723/968501 on tagging systemd instead and use a systemd system service. – Raffa Mar 12 '24 at 18:16
  • 1
    @Rinzwind That should work in theory and had worked in the past "sending the script to the background and allowing that particular rule to finish evaluating so the device is made available to the script" ... Now UDEV rules actions are sand-boxed and denied access to mounting disks, accessing network and other restrictions ... So, I don't know if that approach will still work on recent Ubuntu releases. – Raffa Mar 12 '24 at 18:37
  • @terdon Yes I indeed did try with cat. I'm adding that to the question. I liked dd for seeing the progress when i execute it from terminal. – tpko Mar 13 '24 at 17:16
  • Hm maybe you can fire it up with a socalled systemd Path_Unit and the DirectoryNotEmpty=/media/your-username/ that triggers another systemd unit that starts your script. I made something similiar for backup my $HOME when my backup harddrive connects. – nobody Mar 14 '24 at 16:46

0 Answers0