| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh |
| 4 |
|
| 5 |
function setup_debug { |
| 6 |
if getargbool 0 rd.kiwi.debug; then |
| 7 |
local log=/run/initramfs/log |
| 8 |
mkdir -p ${log} |
| 9 |
exec >> ${log}/boot.kiwi |
| 10 |
exec 2>> ${log}/boot.kiwi |
| 11 |
set -x |
| 12 |
fi |
| 13 |
} |
| 14 |
|
| 15 |
function get_system_id { |
| 16 |
local disk_device=$1 |
| 17 |
blkid -s PTUUID -o value "${disk_device}" |
| 18 |
} |
| 19 |
|
| 20 |
function set_device_lock { |
| 21 |
# """ |
| 22 |
# Set device lock, preferrable via udevadm or via |
| 23 |
# flock as fallback if systemd/udev does not provide |
| 24 |
# the command |
| 25 |
# """ |
| 26 |
if udevadm lock --help &>/dev/null;then |
| 27 |
udevadm lock --device "$@" |
| 28 |
else |
| 29 |
flock -x "$@" |
| 30 |
fi |
| 31 |
} |
| 32 |
|
| 33 |
function disk_matches_system_identifier { |
| 34 |
local disk_device=$1 |
| 35 |
local system_identifier=/system_identifier |
| 36 |
local id_disk |
| 37 |
local id_image |
| 38 |
if [ -e "${system_identifier}" ];then |
| 39 |
id_disk=$(get_system_id "${disk_device}") |
| 40 |
read -r id_image < "${system_identifier}" |
| 41 |
if [ "${id_disk}" = "${id_image}" ]; then |
| 42 |
return 0 |
| 43 |
fi |
| 44 |
fi |
| 45 |
return 1 |
| 46 |
} |
| 47 |
|
| 48 |
function set_root_map { |
| 49 |
root_map=$1 |
| 50 |
export root_map |
| 51 |
} |
| 52 |
|
| 53 |
function get_root_map { |
| 54 |
echo "${root_map}" |
| 55 |
} |
| 56 |
|
| 57 |
function get_mapped_multipath_disk { |
| 58 |
declare DEVICE_TIMEOUT=${DEVICE_TIMEOUT} |
| 59 |
local disk_device=$1 |
| 60 |
local check=0 |
| 61 |
local limit=30 |
| 62 |
if [[ "${DEVICE_TIMEOUT}" =~ ^[0-9]+$ ]]; then |
| 63 |
limit=$(((DEVICE_TIMEOUT + 1)/ 2)) |
| 64 |
fi |
| 65 |
udev_pending &>/dev/null |
| 66 |
while true;do |
| 67 |
for wwn in $(multipath -l -v1 "${disk_device}");do |
| 68 |
if [ -e "/dev/mapper/${wwn}" ];then |
| 69 |
echo "/dev/mapper/${wwn}" |
| 70 |
return |
| 71 |
fi |
| 72 |
done |
| 73 |
if [ "${check}" -eq "${limit}" ]; then |
| 74 |
die "Multipath map for ${disk_device} did not show up" |
| 75 |
fi |
| 76 |
check=$((check + 1)) |
| 77 |
sleep 2 |
| 78 |
done |
| 79 |
} |
| 80 |
|
| 81 |
function lookup_disk_device_from_root { |
| 82 |
declare root=${root} |
| 83 |
declare kiwi_RaidDev=${kiwi_RaidDev} |
| 84 |
declare kiwi_oemmultipath_scan=${kiwi_oemmultipath_scan} |
| 85 |
local root_device=${root#block:} |
| 86 |
local disk_device |
| 87 |
local wwn |
| 88 |
kiwi_oemmultipath_scan=$(bool "${kiwi_oemmultipath_scan}") |
| 89 |
if [ -z "${root_device}" ];then |
| 90 |
die "No root device found" |
| 91 |
fi |
| 92 |
if [ -L "${root_device}" ];then |
| 93 |
root_device=/dev/$(basename "$(readlink "${root_device}")") |
| 94 |
fi |
| 95 |
for disk_device in $( |
| 96 |
lsblk -p -n -r -s -o NAME,TYPE "${root_device}" |\ |
| 97 |
grep -E "disk|raid" | cut -f1 -d ' ' |
| 98 |
); do |
| 99 |
# If multipath is requested, set the disk_device to the |
| 100 |
# multipath mapped device |
| 101 |
if [ "${kiwi_oemmultipath_scan}" = "true" ];then |
| 102 |
disk_device=$(get_mapped_multipath_disk "${disk_device}") |
| 103 |
fi |
| 104 |
# Check if root_device is managed by mdadm and that the md raid |
| 105 |
# is not created as part of the kiwi image building process. If |
| 106 |
# this is the case the md raid device comes from a fake raid |
| 107 |
# controller and we need to prefer the md device over the storage |
| 108 |
# disks |
| 109 |
if type mdadm &> /dev/null && [ -z "${kiwi_RaidDev}" ]; then |
| 110 |
if mdadm --detail -Y "${disk_device}" &>/dev/null; then |
| 111 |
echo "${disk_device}" |
| 112 |
return |
| 113 |
fi |
| 114 |
fi |
| 115 |
done |
| 116 |
echo "${disk_device}" |
| 117 |
} |
| 118 |
|
| 119 |
function shasum_tool { |
| 120 |
declare kiwi_shasum_suffix=${kiwi_shasum_suffix} |
| 121 |
if getargbool 0 rd.kiwi.oem.sha512; then |
| 122 |
echo sha512sum |
| 123 |
elif [ "${kiwi_shasum_suffix}" = ".sha512" ]; then |
| 124 |
echo sha512sum |
| 125 |
else |
| 126 |
echo sha256sum |
| 127 |
fi |
| 128 |
} |
| 129 |
|
| 130 |
function shasum_extension { |
| 131 |
declare kiwi_shasum_suffix=${kiwi_shasum_suffix} |
| 132 |
if getargbool 0 rd.kiwi.oem.sha512; then |
| 133 |
echo sha512 |
| 134 |
elif [ "${kiwi_shasum_suffix}" = ".sha512" ]; then |
| 135 |
echo sha512 |
| 136 |
else |
| 137 |
echo sha256 |
| 138 |
fi |
| 139 |
} |
| 140 |
|
| 141 |
function udev_pending { |
| 142 |
declare DEVICE_TIMEOUT=${DEVICE_TIMEOUT} |
| 143 |
local limit=30 |
| 144 |
if [[ "${DEVICE_TIMEOUT}" =~ ^[0-9]+$ ]]; then |
| 145 |
limit=$(((DEVICE_TIMEOUT + 1)/ 2)) |
| 146 |
fi |
| 147 |
udevadm settle --timeout=${limit} |
| 148 |
} |
| 149 |
|
| 150 |
function get_persistent_device_from_unix_node { |
| 151 |
local unix_device=$1 |
| 152 |
local schema=$2 |
| 153 |
local node |
| 154 |
local persistent_name |
| 155 |
node=$(basename "${unix_device}") |
| 156 |
udev_pending |
| 157 |
for persistent_name in /dev/disk/"${schema}"/*; do |
| 158 |
if [ "$(basename "$(readlink "${persistent_name}")")" = "${node}" ];then |
| 159 |
echo "${persistent_name}" |
| 160 |
return |
| 161 |
fi |
| 162 |
done |
| 163 |
warn "Could not find ${schema} representation of ${node}" |
| 164 |
warn "Using original device ${unix_device}" |
| 165 |
echo "${unix_device}" |
| 166 |
} |
| 167 |
|
| 168 |
function deactivate_all_device_maps { |
| 169 |
dmsetup remove_all |
| 170 |
} |
| 171 |
|
| 172 |
function import_file { |
| 173 |
# """ |
| 174 |
# import file with key=value format. the function |
| 175 |
# will export each entry of the file as variable into |
| 176 |
# the current shell environment |
| 177 |
# """ |
| 178 |
local source_format=/tmp/source_file_formatted |
| 179 |
# create clean input, no empty lines and comments |
| 180 |
grep -v '^$' "$1" | grep -v '^[ \t]*#' > ${source_format} |
| 181 |
# remove start/stop quoting from values |
| 182 |
sed -i -e s"#\(^[a-zA-Z0-9_]\+\)=[\"']\(.*\)[\"']#\1=\2#" ${source_format} |
| 183 |
# remove backslash quotes if any |
| 184 |
sed -i -e s"#\\\\\(.\)#\1#g" ${source_format} |
| 185 |
# quote simple quotation marks |
| 186 |
sed -i -e s"#'\+#'\\\\''#g" ${source_format} |
| 187 |
# add '...' quoting to values |
| 188 |
sed -i -e s"#\(^[a-zA-Z0-9_]\+\)=\(.*\)#\1='\2'#" ${source_format} |
| 189 |
source ${source_format} &>/dev/null |
| 190 |
while read -r line;do |
| 191 |
local key |
| 192 |
key=$(echo "${line}" | cut -d '=' -f1) |
| 193 |
eval "export ${key}" &>/dev/null |
| 194 |
done < ${source_format} |
| 195 |
} |
| 196 |
|
| 197 |
function binsize_to_bytesize { |
| 198 |
# """ |
| 199 |
# converts binary sizes (1024k, 2.4G) to bytes |
| 200 |
# uses awk to handle floating point numbers |
| 201 |
# """ |
| 202 |
local sz="$1" |
| 203 |
local bs=${sz:0:-1} |
| 204 |
case ${sz} in |
| 205 |
*K|*k) mult=1024 ;; |
| 206 |
*M) mult=$((1024*1024)) ;; |
| 207 |
*G) mult=$((1024*1024*1024)) ;; |
| 208 |
*T) mult=$((1024*1024*1024*1024)) ;; |
| 209 |
*) bs=${sz}; mult=1 ;; |
| 210 |
esac |
| 211 |
awk "BEGIN {print int(${bs}*${mult})}" |
| 212 |
} |
| 213 |
|
| 214 |
function bool { |
| 215 |
# """ |
| 216 |
# provides boolean string true|false for given value. |
| 217 |
# Only if value matches true return true, in any other |
| 218 |
# case return false |
| 219 |
# """ |
| 220 |
local value=$1 |
| 221 |
if [ -n "${value}" ] && [ "${value}" = "true" ] ;then |
| 222 |
echo "true" |
| 223 |
else |
| 224 |
echo "false" |
| 225 |
fi |
| 226 |
} |
| 227 |
|
| 228 |
function lib_initialize { |
| 229 |
# """ |
| 230 |
# Source profile variables into runtime environment |
| 231 |
# The method will exit from the initrd if the profile |
| 232 |
# file does not exist |
| 233 |
# """ |
| 234 |
local term |
| 235 |
local kiwi_oem_installdevice |
| 236 |
local profile=/.profile |
| 237 |
local profile_extra=/.profile.extra |
| 238 |
|
| 239 |
# Read and import mandatory image profile to env |
| 240 |
test -f ${profile} || \ |
| 241 |
report_and_quit "No profile setup found" |
| 242 |
import_file ${profile} |
| 243 |
|
| 244 |
# Optional env TERM setup |
| 245 |
term=$(getarg "rd.kiwi.term=") |
| 246 |
[ -n "${term}" ] && export TERM="${term}" |
| 247 |
|
| 248 |
# Source optional profile.extra to env |
| 249 |
test -f ${profile_extra} && source ${profile_extra} |
| 250 |
|
| 251 |
# Create dialog profile from current env |
| 252 |
# Used in the systemd dialog unit |
| 253 |
env >/dialog_profile |
| 254 |
|
| 255 |
# Handle kiwi specific env overwrites |
| 256 |
kiwi_oem_installdevice=$(getarg rd.kiwi.oem.installdevice=) |
| 257 |
if [ -n "${kiwi_oem_installdevice}" ];then |
| 258 |
# activate unattended mode. In case a user explicitly |
| 259 |
# provides the device name to deploy the image to via |
| 260 |
# the kernel commandline, no further questions if this |
| 261 |
# is wanted should appear |
| 262 |
message="rd.kiwi.oem.installdevice explicitly set via cmdline." |
| 263 |
message="${message} The following OEM device settings will be ignored:" |
| 264 |
message="${message} oem-unattended," |
| 265 |
message="${message} oem-unattended-id," |
| 266 |
message="${message} oem-device-filter" |
| 267 |
export kiwi_oemunattended="true" |
| 268 |
export kiwi_oemunattended_id="" |
| 269 |
export kiwi_oemdevicefilter="" |
| 270 |
info "${message}" >&2 |
| 271 |
fi |
| 272 |
} |