[systemd-devel] having trouble hooking into early boot process with local-fs.target

Bond Masuda bond.masuda at jlbond.com
Tue Aug 23 09:42:14 UTC 2016


Hello systemd-devel:

My question to the list might be more "user" oriented, but I did not 
find another list that seemed more appropriate. If my question is best 
directed elsewhere, please let me know. 1st time joining this list, so I 
don't know all the rules yet.

I'm trying to accomplish something using EL7 platform that is a bit 
unorthodox: I want to copy the contents of /etc from a USB flash drive 
early in the boot process onto a ramdisk and mount it over /etc. Under 
normal conditions, I want my system to boot normally. But, in other 
cases, when a USB flash drive is inserted and available during boot, 
there will be a etc.tar.gz file on the USB flash drive that contains the 
content for /etc and I want this content extracted and mounted over /etc 
as early as possible.

Before I explain below what I've tried to do to accomplish my objective, 
I want to mention that my problem is that the results are not 
consistent. The difference is that sometimes it sees the USB flash 
drive, and sometimes it does not. When it does, everything works as i 
want.So, I suspect a race condition between steps of the boot up 
sequence since systemd runs things in parallel.

Here's what I am trying to do:

1) based on my understanding of the boot up process in systemd, I 
thought the appropriate place to hook into is at the local-fs.target. 
so, I've created a service that I added to the "wants" directory for 
local-fs.target:

/lib/systemd/system/local-fs.target.wants/usb-load-etc.service -> 
../usb-load-etc.service

2) the contents of this usb-load-etc.service looks like this:

[Unit]
Description=load /etc from usb drive
DefaultDependencies=no
Conflicts=shutdown.target
Before=shutdown.target emergency.service emergency.target 
systemd-tmpfiles-setup.service local-fs.target 
systemd-random-seed-load.service
After=systemd-remount-fs.service systemd-modules-load.service

[Service]
ExecStart=/lib/systemd/usb-load-etc
Type=oneshot
TimeoutSec=0
RemainAfterExit=yes

3) the script "usb-load-etc" uses 'lsblk' to look for a usb disk, and 
then looks for partitions contained on those usb disks. it then 
sequentially mounts each partition to look for the file etc.tar.gz. if 
it doesn't find it, it umount the partition and tries the next. if it 
does find the file etc.tar.gz, it creates a ramdisk /dev/ram0, mkfs 
/dev/ram0, mounts it on /mnt and extracts the etc.tar.gz on to it. then 
it umount /mnt and mounts /dev/ram0 over /etc, and umount the usb flash 
drive partition.

As I've already mentioned, this does work sometimes. As I understand it, 
local-fs.target is wanted by sysinit.target, which also wants 
systemd-modules-load.service. So, I thought maybe my problem is that the 
usb_storage driver was not loading fast enough sometimes, and so I tried 
adding systemd-modules-load.service to "After=" in my 
usb-load-etc.service. Unfortunately, that did not help.

I know that the script usb-load-etc sometimes sees the USB flash drive 
and sometimes does not because I run 'lsblk' and i can see in the logs 
that when it works, 'lsblk' shows the USB flash drive listed in the 
output, but when it doesn't work, 'lsblk' does not show any USB flash 
drive listed. When the system is fully booted (whether using the /etc 
from the USB flash drive OR NOT), 'lsblk' shows the USB flash drive upon 
login. So, in both cases, the USB flash drives eventually shows up, but 
does not consistently show up when the 'usb-load-etc' script is run 
during boot.

I would appreciate any advice or suggestions to help me accomplish my 
objective here.

Thanks,
Bond




More information about the systemd-devel mailing list