In my company we use a USB bootable live stick with Ubuntu (erm... Xubuntu) 18.04 to do job interviews. It has a bunch of tools (three or four IDEs, a couple of database GUI viewers...) and a toy project to ask questions about.
That USB stick has a Casper-RW overlay partition for persistent storage (created with mkusb). Said partition is mostly used to store Docker images and to keep some start-up values that can change a bit depending on which machine the stick is plugged into.
That partition is mounted "manually" in /media/overlay
by adding the following line mkdir --parents /media/overlay/ && mount -L casper-rw /media/overlay/
to the file rc.local
(thank you @sudodus for answering my previous question).
Now, that casper-rw
persistence layer is working great... Maybe too great. We would like to erase the /home/<user>
directory where the .bash_history
and other changes made by the interviewee have been stored when we shut down the test environment (test environment... meaning: the laptop where the interviewee has been doing stuff) so it's ready for the next candidate.
Thanks to these answers (Unix&Linux, AskUbuntu) I created a systemd service that... kindaaaa does what I want, it's just it seems to do it when the system boots, not when it's going down. Is not a huge deal, is just... it bugs me a little :-D
This is the systemd service I created in /etc/systemd/system/clear-stick.service
:
[Unit]
Description=Cleans-up interview sticker
RequiresMountsFor=/media/overlay/
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target umount.target casper.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/clear_interview_stick.sh
ExecStop=/usr/local/bin/clear_interview_stick.sh
RemainAfterExit=true
[Install]
WantedBy=multi-user.target
I suspect it's only running the ExecStart
line on boot, rather than the ExecStop
line on shutdown. If I make changes to the /home/
directory, power off the computer and mount the stick somewhere else, the changes are still there. Plus, if I look at the logs in journalctl
I only see references to Cleans-up interview sticker surrounded by what seem start sequences of other services.
I'm not 100% sure whether this happens because the .service
definition is wrong (very likely) or because I don't fully understand how the casper-rw
overlay works and even though the .service
is run on shutdown, it can't clear the /media/overlay/home/interviewee
directory?.
As I mentioned before, it's not a huge, huge deal, since it seems to be erasing the /home/
directory on boot, but I'd rather do it """right""" and erase it on shutdown.
And just in case it's relevant, this is the content of /usr/local/bin/clear_interview_stick.sh
:
#!/bin/bash
had_to_mount="false";
if [[ ! -d "/media/overlay/upper/" ]]; then
mount -L casper-rw /media/overlay/
had_to_mount="true";
fi
if [[ -d "/media/overlay/upper/home/interviewee/" ]]; then
echo "Removing interviewee /home";
rm -rf "/media/overlay/upper/home/interviewee/";
else
echo "interviewee /home/ not found"
fi
if [[ -f /root/.bash_history ]]; then
rm /root/.bash_history;
fi
if [[ "${had_to_mount}" == "true" ]]; then
umount /media/overlay;
fi
Before=shutdown.target reboot.target halt.target umount.target casper.service
. Could you try if usingAfter=final.target
instead makes a difference? That one is called BEFORE unmounting takes place but before shutdown cs. – Rinzwind Aug 02 '19 at 12:51mount
to thebash
script. – Savir Aug 02 '19 at 12:54After=network.target
(and noBefore
) But if it works, you sould make the answer and I'll gladly choose it. I mean... you have reputation to spare, but what's right is right :D – Savir Aug 02 '19 at 13:07After=network.target
, removing all theBefore
(s) and makingExecStart=/bin/true
. That didn't seem to work. Maybe I shouldn't have changed theExecStart
?? (that's what I get for getting creative!!) I'm gonna try substituting theAfter=network...
byBefore=final.target
. I'll keep you posted (and again: that you so much for all these hints!!) – Savir Aug 02 '19 at 14:06