Debian Codegraph @

dracut/modules.d/55kiwi-overlay/kiwi-overlay-root.sh

raw
1 #!/bin/bash
2 # root=overlay:* was converted into a root=block:/dev/... node
3 type getOverlayBaseDirectory >/dev/null 2>&1 || . /lib/kiwi-filesystem-lib.sh
4
5 #======================================
6 # functions
7 #--------------------------------------
8 function setupDebugMode {
9 if getargbool 0 rd.kiwi.debug; then
10 local log=/run/initramfs/log
11 mkdir -p ${log}
12 exec > ${log}/boot.kiwi
13 exec 2>> ${log}/boot.kiwi
14 set -x
15 fi
16 }
17
18 function loadKernelModules {
19 modprobe squashfs
20 }
21
22 function initGlobalDevices {
23 local nbd_host
24 local nbd_export
25 if [ -z "$1" ]; then
26 die "No root device for operation given"
27 fi
28 write_partition=$(getarg rd.root.overlay.write=)
29 if [[ "${root_cmdline}" =~ "overlay:nbd=" ]];then
30 nbd_host=$(echo "${root_cmdline}" | cut -f2 -d= | cut -f1 -d:)
31 nbd_export=$(echo "${root_cmdline}" | cut -f3 -d:)
32 read_only_partition="/dev/nbd0"
33 if ! nbd-client \
34 "${nbd_host}" "${read_only_partition}" -name "${nbd_export}"
35 then
36 die "Failed to setup nbd client for ${root_cmdline}"
37 fi
38 elif [[ "${root_cmdline}" =~ "overlay:aoe=" ]];then
39 read_only_partition="/dev/etherd/$(echo "${root_cmdline}"|cut -f2 -d=)"
40 else
41 read_only_partition="$1"
42 fi
43 }
44
45 function mountReadOnlyRootImage {
46 local overlay_base
47 local unit_name
48 overlay_base=$(getOverlayBaseDirectory)
49 local root_mount_point="${overlay_base}/rootfsbase"
50 mkdir -m 0755 -p "${root_mount_point}"
51 unit_name=$(echo "${root_mount_point}" | cut -c 2- | tr / -)
52 cat >/run/systemd/system/"${unit_name}".mount <<-EOF
53 [Unit]
54 Before=initrd-root-fs.target
55 After=run-overlay.mount
56 DefaultDependencies=no
57 [Mount]
58 Where=$root_mount_point
59 What=$read_only_partition
60 Type=auto
61 DirectoryMode=0755
62 [Install]
63 WantedBy=multi-user.target
64 EOF
65 if ! systemctl start "${unit_name}".mount;then
66 die "Failed to mount overlay(ro) root filesystem"
67 fi
68 echo "${root_mount_point}"
69 }
70
71 function prepareTemporaryOverlay {
72 local overlay_base
73 overlay_base=$(getOverlayBaseDirectory)
74 mkdir -m 0755 -p "${overlay_base}/overlayfs/rw"
75 mkdir -m 0755 -p "${overlay_base}/overlayfs/work"
76 }
77
78 function preparePersistentOverlay {
79 local overlay_base
80 local unit_name
81 overlay_base=$(getOverlayBaseDirectory)
82 local overlay_mount_point="${overlay_base}/overlayfs"
83 mkdir -m 0755 -p "${overlay_mount_point}"
84 unit_name=$(echo "${overlay_mount_point}" | cut -c 2- | tr / -)
85 cat >/run/systemd/system/"${unit_name}".mount <<-EOF
86 [Unit]
87 Before=initrd-root-fs.target
88 After=run-overlay.mount
89 DefaultDependencies=no
90 [Mount]
91 Where=$overlay_mount_point
92 What=$write_partition
93 Type=auto
94 DirectoryMode=0755
95 [Install]
96 WantedBy=multi-user.target
97 EOF
98 if ! systemctl start "${unit_name}".mount; then
99 die "Failed to mount overlay(rw) filesystem"
100 fi
101 mkdir -m 0755 -p "${overlay_mount_point}/rw"
102 mkdir -m 0755 -p "${overlay_mount_point}/work"
103 }
104
105 #======================================
106 # perform root access preparation
107 #--------------------------------------
108 PATH=/usr/sbin:/usr/bin:/sbin:/bin
109
110 declare root=${root}
111 declare root_cmdline
112
113 root_cmdline=$(getarg root=)
114
115 # only run if overlay is requested
116 if [[ ! "${root_cmdline}" =~ "overlay:" ]];then
117 return 0
118 fi
119
120 # init debug log file if wanted
121 setupDebugMode
122
123 # device nodes and types
124 initGlobalDevices "${root#block:}"
125
126 # load required kernel modules
127 loadKernelModules
128
129 # mount readonly root filesystem
130 mountReadOnlyRootImage
131
132 if [ -z "${write_partition}" ] || getargbool 0 rd.root.overlay.temporary; then
133 prepareTemporaryOverlay
134 else
135 preparePersistentOverlay
136 fi
137
138 need_shutdown
139
140 return 0