summaryrefslogtreecommitdiffstats
path: root/modules.d/dnbd3-rootfs/hooks/copy-dracut-systemd-files-into-newroot.sh
blob: 0cae2b35d7ab88271511a8eab714c4adcae1c590 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
type emergency_shell >/dev/null 2>&1 || source /lib/dracut-lib.sh

# Needed to be able to go back to dracut at system shutdown.
# only do this if there is enough space in /run since systemd decided to limit
# some tmpfs to 10% of the memory size breaking the system with low memory...
# See: https://github.com/systemd/systemd/commit/b67ec8e5b2e1a74d7e9a3a2b3ac60b7b2e39d4ea
_run_size_kb="$(slx-tools fs_path_space /run | cut -d' ' -f2)"
_initrd_block_size="$( stat -f / -c%S)"
_initrd_total_blocks="$(stat -f / -c%b)"
_initrd_free_blocks="$(stat -f / -c%a)"
_initrd_used_blocks="$(( _initrd_total_blocks - _initrd_free_blocks ))"
_initrd_size_kb="$(( ( _initrd_used_blocks * _initrd_block_size ) / 1024 ))"
if [ "$(( _run_size_kb - _initrd_size_kb ))" -ge "200000" ]; then
	# more than 200MB space, should be safe to copy initramfs over
	temporary_directory_path="$(mktemp --directory)"
	mount --options bind / "$temporary_directory_path"
	cp --recursive --no-target-directory "$temporary_directory_path" /run/initramfs
	umount "$temporary_directory_path"
	rm --dir "$temporary_directory_path"
fi

# Dracut may not be installed on the new root. Thus copy all services over.
dracut_mount_unit_path="$(systemctl show -p FragmentPath dracut-mount.service \
	| cut -c 14-)"
systemd_system_unit_path="${dracut_mount_unit_path%/*}"
new_systemd_system_unit_path="${NEWROOT}/lib/systemd/system"

mkdir --parents "$new_systemd_system_unit_path/initrd.target.wants"
for file in \
    dracut-cmdline.service \
    dracut-initqueue.service \
    dracut-mount.service \
    dracut-pre-mount.service \
    dracut-pre-pivot.service \
    dracut-pre-trigger.service \
    dracut-pre-udev.service
do
    cp "${systemd_system_unit_path}/${file}" \
        "${new_systemd_system_unit_path}/${file}"
    # "ln" returns an error if the link already exists.
    source_path="../${file}"
    target_path="${new_systemd_system_unit_path}/initrd.target.wants/${file}"
    ln --symbolic "$source_path" "$target_path" &>/dev/null || \
    warn "Failed to link \"$source_path\" to \"$target_path\"."
done