Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

OK, so I have a simple problem: I have a live CD, and I want the CD to eject when the computer is rebooted or halted.

Executing /usr/bin/eject -m makes the CD eject just fine... trouble is, as you might expect, this then crashes the OS because it now can't read any files. So the reboot never happens, because /sbin/reboot is inaccessible. (I tried executing reboot --help > /dev/null right before the reboot, but there are still other files which still can't be read...)

In summary, it appears that I need to make the eject be the very last thing that happens. So it seems that systemd is the puppy I need to play with... But I have wasted literally hours of my life poking and prodding it, and no matter what I do, it never, ever, under any circumstances, actually ejects the CD. And I have no idea why. I've tried a dozen different ways of invoking eject, but nothing ever happens.

Can anybody tell me the simplest way to run eject? (I imagine just after umount.target would be sensible...)

share|improve this question
    
+1 for Exceptional Question. –  Md. Mahbubur R. Aaman Nov 23 '12 at 5:06
    
I take it you have another OS on the computer or why else would you like to eject the CD on reboot? If this is the reason, you could probably do better if you set up a boot menu where you could select whether to boot from the CD or from the disk. –  Emanuel Berg Nov 23 '12 at 5:28
    
After you umount the CD, aren't you still left with the contents of your initramfs? If there's an eject there, you would be fine? –  Ulrich Schwarz Nov 23 '12 at 9:00
    
Hold on. That's odd. Linux Mint, and I think Ubuntu do this by default. –  Linuxios Nov 26 '12 at 23:02
add comment

3 Answers 3

up vote 3 down vote accepted

Eventually my colleague found the following article:

http://lists.freedesktop.org/archives/systemd-devel/2012-September/006573.html

In summary, I created a file named /lib/systemd/system/eject.service which contains

[Unit]
Description=Eject the DVD
Before=final.target
After=shutdown.target
DefaultDependencies=no

[Service]
Type=oneshot
ExecStart=/usr/bin/eject -m
StandardInput=tty-force
StandardOutput=inherit
StandardError=inherit

[Install]
WantedBy=shutdown.target

I also had to run systemctl enable eject.service to make this actually do anything. But now systemd ejects the CD right before shutdown, exactly as I wanted.

...unfortunately the OS still segfaults when it can't find the rest of the files with the CD ejected. I tried adjusting the above file so we have After=unmount.target, which you would think runs the script after the filesystems have been unmounted. But no, still it segfaults.

It looks like I'm going to have to figure out how to copy the live CD image into RAM on boot - but that is an entire other question...

share|improve this answer
add comment

The followings may help you :

  • Add your eject command at /etc/init.d/halt.local

  • You could also look at the /etc/init.d/skeleton template and customize this to perhaps add some output eg system shutting down, press any key to eject cdrom etc.

  • Check out the scripts in /etc/init.d that are linked to in the runlevel directories /etc/rc0.d (halt).

share|improve this answer
add comment

Check out the scripts in /etc/init.d that are linked to in the runlevel directories /etc/rc0.d (halt) and /etc/rc6.d (reboot). For example, I put the eject command in /etc/init.d/reboot and it worked fine.

But, I'm not on a Live CD so I can't test what would work.

If you think about it, something like this, it is hard to imagine, putting in a script or even a compiled binary. Even a binary requires some allocation/deallocation overhead, and as soon as you eject the CD, you are done. Rather, it should be like a flag and a pointer to a machine instruction, stored in some super-persistent memory...

Maybe it's easier to just run the command and deliberately crash the OS, but to do so when almost all stuff is offline, so it won't matter that you crash your remaining system.

(Edit) Another thought: Could you somehow partition your filesystem, creating a partition for just the eject command and reboot, unmount all the others, then run the command? (But, probably then fsck would have to be told not to complain every time, that the eject-partition was not unmounted correctly.) Or, could you put it on a USB stick? (This is a very interesting problem; if you solve it, do tell us!)

(Edit 2) What about merging eject and reboot, so you get one binary, it is loaded into memory, eject ejects the CD, but doesn't have to load reboot from any filesystem, it is already in main memory?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.