| 1 |
#!/bin/bash |
| 2 |
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh |
| 3 |
type setup_debug >/dev/null 2>&1 || . /lib/kiwi-lib.sh |
| 4 |
type run_dialog >/dev/null 2>&1 || . /lib/kiwi-dialog-lib.sh |
| 5 |
type get_block_device_kbsize >/dev/null 2>&1 || . /lib/kiwi-partitions-lib.sh |
| 6 |
type fetch_file >/dev/null 2>&1 || . /lib/kiwi-net-lib.sh |
| 7 |
|
| 8 |
#====================================== |
| 9 |
# Functions |
| 10 |
#-------------------------------------- |
| 11 |
function scan_multipath_devices { |
| 12 |
# """ |
| 13 |
# starts multipath daemon from multipath module |
| 14 |
# """ |
| 15 |
systemctl start multipathd |
| 16 |
} |
| 17 |
|
| 18 |
function find_disk_entry { |
| 19 |
# """ |
| 20 |
# lookup disk entry by name and echo the |
| 21 |
# associated entry parameter (size) when found |
| 22 |
# """ |
| 23 |
local list_items=$1 |
| 24 |
local search=$2 |
| 25 |
local count=0 |
| 26 |
local found= |
| 27 |
for entry in ${list_items};do |
| 28 |
if [ -n "${found}" ];then |
| 29 |
echo "${entry}" |
| 30 |
return |
| 31 |
fi |
| 32 |
if [ $((count % 2)) -eq 0 ];then |
| 33 |
if [ "${entry}" = "${search}" ];then |
| 34 |
found=1 |
| 35 |
fi |
| 36 |
fi |
| 37 |
count=$((count + 1)) |
| 38 |
done |
| 39 |
} |
| 40 |
|
| 41 |
function sort_disk_entries { |
| 42 |
# """ |
| 43 |
# sort the disk entry names |
| 44 |
# """ |
| 45 |
local list_items=$1 |
| 46 |
local count=0 |
| 47 |
local device_index=0 |
| 48 |
local device_array |
| 49 |
local list_items_sorted |
| 50 |
for entry in ${list_items};do |
| 51 |
if [ $((count % 2)) -eq 0 ];then |
| 52 |
device_array[${device_index}]=${entry} |
| 53 |
device_index=$((device_index + 1)) |
| 54 |
fi |
| 55 |
count=$((count + 1)) |
| 56 |
done |
| 57 |
printf '%s\0' "${device_array[@]}" | sort -z > list_items_sorted.txt |
| 58 |
readarray -td '' list_items_sorted < list_items_sorted.txt |
| 59 |
echo "${list_items_sorted[*]}" |
| 60 |
} |
| 61 |
|
| 62 |
function get_disk_list { |
| 63 |
declare kiwi_oemdevicefilter=${kiwi_oemdevicefilter} |
| 64 |
declare kiwi_oemmultipath_scan=${kiwi_oemmultipath_scan} |
| 65 |
declare kiwi_devicepersistency=${kiwi_devicepersistency} |
| 66 |
declare kiwi_install_volid=${kiwi_install_volid} |
| 67 |
declare kiwi_oemunattended=${kiwi_oemunattended} |
| 68 |
declare kiwi_oemunattended_id=${kiwi_oemunattended_id} |
| 69 |
declare kiwi_oemramdisksize=${kiwi_oemramdisksize} |
| 70 |
local disk_id="by-id" |
| 71 |
local disk_size |
| 72 |
local disk_device |
| 73 |
local disk_device_by_id |
| 74 |
local disk_trans |
| 75 |
local disk_meta |
| 76 |
local list_items |
| 77 |
local list_items_sorted |
| 78 |
local max_disk |
| 79 |
local kiwi_oem_maxdisk |
| 80 |
local blk_opts="-p -n -r --sort NAME -o NAME,SIZE,TYPE,TRAN" |
| 81 |
local message |
| 82 |
local blk_opts_plus_label="${blk_opts},LABEL" |
| 83 |
local kiwi_install_disk_part |
| 84 |
|
| 85 |
if [ -n "${kiwi_devicepersistency}" ];then |
| 86 |
disk_id=${kiwi_devicepersistency} |
| 87 |
fi |
| 88 |
max_disk=0 |
| 89 |
kiwi_install_devicepersistency=$(getarg rd.kiwi.install.devicepersistency=) |
| 90 |
kiwi_oemmultipath_scan=$(bool "${kiwi_oemmultipath_scan}") |
| 91 |
kiwi_oem_maxdisk=$(getarg rd.kiwi.oem.maxdisk=) |
| 92 |
kiwi_oem_installdevice=$(getarg rd.kiwi.oem.installdevice=) |
| 93 |
|
| 94 |
# Check for smallest disk parameter |
| 95 |
# - Configure lsblk options based on the selection mode |
| 96 |
if getargbool 0 rd.kiwi.oem.smallest_disk; then |
| 97 |
# Sort by SIZE (ascending), include TRAN to identify USBs |
| 98 |
blk_opts="-p -n -r --sort SIZE -o NAME,SIZE,TYPE,TRAN" |
| 99 |
blk_opts_plus_label="${blk_opts},LABEL" |
| 100 |
fi |
| 101 |
|
| 102 |
if [ -n "${kiwi_install_devicepersistency}" ];then |
| 103 |
disk_id=${kiwi_install_devicepersistency} |
| 104 |
fi |
| 105 |
if [ -n "${kiwi_oem_maxdisk}" ]; then |
| 106 |
max_disk=$(binsize_to_bytesize "${kiwi_oem_maxdisk}") || max_disk=0 |
| 107 |
fi |
| 108 |
if getargbool 0 rd.kiwi.ramdisk; then |
| 109 |
# target should be a ramdisk on request. Thus actively |
| 110 |
# load the ramdisk block driver and support custom sizes |
| 111 |
local rd_size |
| 112 |
local custom_rd_size |
| 113 |
local modfile=/etc/modprobe.d/99-brd.conf |
| 114 |
custom_rd_size=$(getarg ramdisk_size=) |
| 115 |
if [ -n "${custom_rd_size}" ];then |
| 116 |
rd_size="${custom_rd_size}" |
| 117 |
elif [ -n "${kiwi_oemramdisksize}" ];then |
| 118 |
rd_size="${kiwi_oemramdisksize}" |
| 119 |
fi |
| 120 |
mkdir -p /etc/modprobe.d |
| 121 |
if [ -n "${rd_size}" ];then |
| 122 |
echo "options brd rd_size=${rd_size}" > ${modfile} |
| 123 |
fi |
| 124 |
modprobe brd |
| 125 |
udev_pending |
| 126 |
# target should be a ramdisk on request. Thus instruct |
| 127 |
# lsblk to list only ramdisk devices (Major=1) |
| 128 |
blk_opts="-I 1 ${blk_opts}" |
| 129 |
elif [ "${kiwi_oemmultipath_scan}" = "true" ];then |
| 130 |
scan_multipath_devices |
| 131 |
fi |
| 132 |
kiwi_install_disk_part=$( |
| 133 |
eval lsblk "${blk_opts_plus_label}" | \ |
| 134 |
tr -s ' ' ":" | \ |
| 135 |
grep ":${kiwi_install_volid}$" | \ |
| 136 |
cut -f1 -d: |
| 137 |
) |
| 138 |
for disk_meta in $( |
| 139 |
eval lsblk "${blk_opts}" | grep -E "disk|raid" | tr ' ' ":" |
| 140 |
);do |
| 141 |
disk_device="$(echo "${disk_meta}" | cut -f1 -d:)" |
| 142 |
if [[ "${kiwi_install_disk_part}" == "${disk_device}"* ]]; then |
| 143 |
# ignore install source device |
| 144 |
continue |
| 145 |
fi |
| 146 |
disk_mpath_child=$( |
| 147 |
eval lsblk "${blk_opts}" "${disk_device}" | grep mpath | \ |
| 148 |
tr ' ' ":" |
| 149 |
) |
| 150 |
if [[ -n "${disk_mpath_child}" ]];then |
| 151 |
# use mpath device instead of parent device for multipath |
| 152 |
disk_device="$(echo "${disk_mpath_child}" | cut -f1 -d:)" |
| 153 |
fi |
| 154 |
|
| 155 |
# Filter USBs if smallest disk is requested |
| 156 |
disk_trans=$(echo "${disk_meta}" | cut -f4 -d:) |
| 157 |
if getargbool 0 rd.kiwi.oem.smallest_disk; then |
| 158 |
if [[ "${disk_trans}" == "usb" ]]; then |
| 159 |
continue |
| 160 |
fi |
| 161 |
fi |
| 162 |
|
| 163 |
disk_size=$(echo "${disk_meta}" | cut -f2 -d:) |
| 164 |
if [ "${max_disk}" -gt 0 ]; then |
| 165 |
local disk_size_bytes |
| 166 |
disk_size_bytes=$(binsize_to_bytesize "${disk_size}") || \ |
| 167 |
disk_size_bytes=0 |
| 168 |
if [ "${disk_size_bytes}" -gt "${max_disk}" ]; then |
| 169 |
message="${disk_device} filtered out by" |
| 170 |
message="${message} rd.kiwi.oem.maxdisk=${kiwi_oem_maxdisk}" |
| 171 |
message="${message} (disk size is: ${disk_size})" |
| 172 |
info "${message}" >&2 |
| 173 |
continue |
| 174 |
fi |
| 175 |
fi |
| 176 |
disk_device_by_id=$( |
| 177 |
get_persistent_device_from_unix_node "${disk_device}" "${disk_id}" |
| 178 |
) |
| 179 |
if [ -n "${disk_device_by_id}" ];then |
| 180 |
disk_device=${disk_device_by_id} |
| 181 |
fi |
| 182 |
# check for static filter rules |
| 183 |
if [[ ${disk_device} =~ ^/dev/fd ]];then |
| 184 |
# ignore floppy disk devices |
| 185 |
continue |
| 186 |
fi |
| 187 |
# check for custom filter rule |
| 188 |
if [ -n "${kiwi_oemdevicefilter}" ];then |
| 189 |
if [[ ${disk_device} =~ ${kiwi_oemdevicefilter} ]];then |
| 190 |
message="${disk_device} filtered out by rule:" |
| 191 |
message="${message} ${kiwi_oemdevicefilter}" |
| 192 |
info "${message}" >&2 |
| 193 |
continue |
| 194 |
fi |
| 195 |
fi |
| 196 |
if [[ "${list_items}" == *"${disk_device}"* ]];then |
| 197 |
# device already in the list, can happen in multipath systems |
| 198 |
continue |
| 199 |
fi |
| 200 |
list_items="${list_items} ${disk_device} ${disk_size}" |
| 201 |
done |
| 202 |
if [ -n "${kiwi_oem_installdevice}" ];then |
| 203 |
# install device overwritten by cmdline. |
| 204 |
local device=${kiwi_oem_installdevice} |
| 205 |
local device_meta |
| 206 |
local device_size |
| 207 |
if [ ! -e "${device}" ];then |
| 208 |
local no_dev="Given device ${device} does not exist" |
| 209 |
report_and_quit "${no_dev}" |
| 210 |
fi |
| 211 |
if [ ! -b "${device}" ];then |
| 212 |
local no_block_dev="Given device ${device} is not a block special" |
| 213 |
report_and_quit "${no_block_dev}" |
| 214 |
fi |
| 215 |
device_meta=$( |
| 216 |
eval lsblk "${blk_opts}" "${device}" |\ |
| 217 |
grep -E "disk|raid|mpath" | tr ' ' ":" |
| 218 |
) |
| 219 |
device_size=$(echo "${device_meta}" | cut -f2 -d:) |
| 220 |
list_items="${device} ${device_size}" |
| 221 |
fi |
| 222 |
if [ -z "${list_items}" ];then |
| 223 |
local no_device_text="No device(s) for installation found" |
| 224 |
report_and_quit "${no_device_text}" |
| 225 |
fi |
| 226 |
|
| 227 |
# Conditional Sorting |
| 228 |
if getargbool 0 rd.kiwi.oem.smallest_disk; then |
| 229 |
# If looking for smallest disk, preserve the lsblk size-sort order |
| 230 |
echo "${list_items}" |
| 231 |
else |
| 232 |
# Default behavior: |
| 233 |
# apply final alphabetical sorting for the used disk_device names |
| 234 |
list_items_sorted=$(sort_disk_entries "${list_items}") |
| 235 |
for entry in ${list_items_sorted[*]}; do |
| 236 |
echo -n "${entry} $(find_disk_entry "${list_items}" "${entry}") " |
| 237 |
done |
| 238 |
echo |
| 239 |
fi |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
function validate_disk_selection { |
| 244 |
local selected=$1 |
| 245 |
local disk_list |
| 246 |
local entry |
| 247 |
local count=0 |
| 248 |
disk_list=$(get_disk_list) |
| 249 |
for entry in ${disk_list};do |
| 250 |
if [ $((count % 2)) -eq 0 ];then |
| 251 |
if [ ! "${entry}" = "${selected}" ];then |
| 252 |
# check system_identifier for the not selected disk |
| 253 |
# if one of them matches the system_identifier because |
| 254 |
# that would produce a global system inconsistency |
| 255 |
if disk_matches_system_identifier "${entry}";then |
| 256 |
report_and_quit "Disk ${entry} has the same partition UUID" |
| 257 |
fi |
| 258 |
fi |
| 259 |
fi |
| 260 |
count=$((count + 1)) |
| 261 |
done |
| 262 |
} |
| 263 |
|
| 264 |
function get_selected_disk { |
| 265 |
declare kiwi_oemunattended=${kiwi_oemunattended} |
| 266 |
declare kiwi_oemunattended_id=${kiwi_oemunattended_id} |
| 267 |
local disk_list |
| 268 |
local device_array |
| 269 |
kiwi_oemunattended=$(bool "${kiwi_oemunattended}") |
| 270 |
disk_list=$(get_disk_list) |
| 271 |
if [ -n "${disk_list}" ];then |
| 272 |
local count=0 |
| 273 |
local device_index=0 |
| 274 |
for entry in ${disk_list};do |
| 275 |
if [ $((count % 2)) -eq 0 ];then |
| 276 |
device_array[${device_index}]=${entry} |
| 277 |
device_index=$((device_index + 1)) |
| 278 |
fi |
| 279 |
count=$((count + 1)) |
| 280 |
done |
| 281 |
if [ "${device_index}" -eq 1 ];then |
| 282 |
# one single disk device found, use it |
| 283 |
echo "${device_array[0]}" |
| 284 |
elif [ "${kiwi_oemunattended}" = "true" ];then |
| 285 |
if [ -z "${kiwi_oemunattended_id}" ];then |
| 286 |
# unattended mode requested but no target specifier, |
| 287 |
# thus use first device from list |
| 288 |
echo "${device_array[0]}" |
| 289 |
else |
| 290 |
# unattended mode requested with target specifier |
| 291 |
# use this device if present |
| 292 |
local device |
| 293 |
for device in "${device_array[@]}"; do |
| 294 |
if [[ ${device} =~ ${kiwi_oemunattended_id} ]];then |
| 295 |
echo "${device}" |
| 296 |
return |
| 297 |
fi |
| 298 |
done |
| 299 |
fi |
| 300 |
else |
| 301 |
# manually select from storage list |
| 302 |
if ! run_dialog \ |
| 303 |
--menu "\"Select Installation Disk\"" 20 75 15 "${disk_list}" |
| 304 |
then |
| 305 |
report_and_quit "System installation canceled" |
| 306 |
fi |
| 307 |
get_dialog_result |
| 308 |
fi |
| 309 |
fi |
| 310 |
} |
| 311 |
|
| 312 |
function export_image_metadata { |
| 313 |
local image_source_files=$1 |
| 314 |
export checksum |
| 315 |
export blocks |
| 316 |
export blocksize |
| 317 |
export zblocks |
| 318 |
export zblocksize |
| 319 |
local meta_file |
| 320 |
meta_file="$(echo "${image_source_files}" | cut -f2 -d\|)" |
| 321 |
if ! read -r checksum blocks blocksize zblocks zblocksize < "${meta_file}" |
| 322 |
then |
| 323 |
report_and_quit "Reading ${meta_file} failed" |
| 324 |
fi |
| 325 |
echo "Image checksum: ${checksum}" |
| 326 |
echo "Image blocks: ${blocks} / blocksize: ${blocksize}" |
| 327 |
if [ -n "${zblocks}" ];then |
| 328 |
echo "Image compressed blocks: ${zblocks} / blocksize: ${zblocksize}" |
| 329 |
fi |
| 330 |
} |
| 331 |
|
| 332 |
function check_image_fits_target { |
| 333 |
local image_target=$1 |
| 334 |
local need_mbytes |
| 335 |
local have_mbytes |
| 336 |
need_mbytes=$((blocks * blocksize / 1048576)) |
| 337 |
have_mbytes=$(($(get_block_device_kbsize "${image_target}") / 1024)) |
| 338 |
echo "Have size: ${image_target} -> ${have_mbytes} MB" |
| 339 |
echo "Need size: ${image_source} -> ${need_mbytes} MB" |
| 340 |
if [ ${need_mbytes} -gt ${have_mbytes} ];then |
| 341 |
report_and_quit "Not enough space available for this image" |
| 342 |
fi |
| 343 |
} |
| 344 |
|
| 345 |
function check_image_matches_target_blocksize { |
| 346 |
declare kiwi_target_blocksize=${kiwi_target_blocksize} |
| 347 |
local image_blocksize=${kiwi_target_blocksize} |
| 348 |
local target_node |
| 349 |
local target_blocksize |
| 350 |
local message |
| 351 |
target_node="${1}" |
| 352 |
if [ -L "${target_node}" ];then |
| 353 |
target_node="$(readlink "${target_node}")" |
| 354 |
fi |
| 355 |
target_node=$(basename "${target_node}") |
| 356 |
target_blocksize="/sys/block/${target_node}/queue/physical_block_size" |
| 357 |
mountpoint -q /sys || mount -t sysfs sysfs /sys |
| 358 |
if [ -n "${image_blocksize}" ] && [ -e "${target_blocksize}" ];then |
| 359 |
read -r target_blocksize < "${target_blocksize}" |
| 360 |
if [ ! "${target_blocksize}" = "${image_blocksize}" ];then |
| 361 |
message="Target blocksize(${target_blocksize}b) doesn't match" |
| 362 |
message="${message} image blocksize(${image_blocksize}b)" |
| 363 |
report_and_quit "${message}" |
| 364 |
fi |
| 365 |
fi |
| 366 |
} |
| 367 |
|
| 368 |
function dump_image { |
| 369 |
declare kiwi_oemsilentinstall=${kiwi_oemsilentinstall} |
| 370 |
declare kiwi_oemunattended=${kiwi_oemunattended} |
| 371 |
local image_source_files=$1 |
| 372 |
local image_target=$2 |
| 373 |
local image_from_remote=$3 |
| 374 |
local image_source |
| 375 |
local image_basename |
| 376 |
kiwi_oemsilentinstall=$(bool "${kiwi_oemsilentinstall}") |
| 377 |
kiwi_oemunattended=$(bool "${kiwi_oemunattended}") |
| 378 |
image_source="$(echo "${image_source_files}" | cut -f1 -d\|)" |
| 379 |
image_basename=$(basename "${image_source}") |
| 380 |
local progress=/dev/install_progress |
| 381 |
local load_text="Loading ${image_basename}..." |
| 382 |
local title_text="Installation..." |
| 383 |
local dump |
| 384 |
local parttable |
| 385 |
local count_32k |
| 386 |
local image_size |
| 387 |
local count=0 |
| 388 |
local block_size=32k |
| 389 |
|
| 390 |
# can we dump this |
| 391 |
check_image_fits_target "${image_target}" |
| 392 |
check_image_matches_target_blocksize "${image_target}" |
| 393 |
|
| 394 |
# select dump method |
| 395 |
if [ -n "${image_from_remote}" ];then |
| 396 |
dump=dump_remote_image |
| 397 |
else |
| 398 |
dump=dump_local_image |
| 399 |
fi |
| 400 |
|
| 401 |
# preserve pre-dump partition table |
| 402 |
if getargbool 0 rd.kiwi.install.retain_past; then |
| 403 |
fetch_local_partition_table "${image_target}" /tmp/parttable_pre |
| 404 |
fi |
| 405 |
|
| 406 |
# setup blocks and blocksize to retain last |
| 407 |
if getargbool 0 rd.kiwi.install.retain_last || \ |
| 408 |
getargbool 0 rd.kiwi.install.retain_past |
| 409 |
then |
| 410 |
if [ -n "${image_from_remote}" ];then |
| 411 |
image_size=$((blocks * blocksize)) |
| 412 |
parttable=$( |
| 413 |
fetch_remote_partition_table "${image_source}" "${image_size}" |
| 414 |
) |
| 415 |
else |
| 416 |
parttable=$( |
| 417 |
fetch_local_partition_table "${image_source}" |
| 418 |
) |
| 419 |
fi |
| 420 |
if compatible_to_retain "${parttable}" "${image_target}"; then |
| 421 |
if getargbool 0 rd.kiwi.install.retain_last; then |
| 422 |
count=$(get_disk_offset_retain_last_partition "${parttable}") |
| 423 |
else |
| 424 |
# retain_past always dumps the complete image |
| 425 |
# the compatible_to_retain check for this image blob |
| 426 |
# has been done and the deployment can happen for |
| 427 |
# all blocks |
| 428 |
count=0 |
| 429 |
fi |
| 430 |
fi |
| 431 |
if [ "${count}" -gt 0 ];then |
| 432 |
block_size=$(get_sector_size_from_table_dump "${parttable}") |
| 433 |
count_32k=$( |
| 434 |
optimize_count_for_32k_blocksize "${block_size}" "${count}" |
| 435 |
) |
| 436 |
if [ ! "${count}" = "${count_32k}" ];then |
| 437 |
count="${count_32k}" |
| 438 |
block_size=32k |
| 439 |
fi |
| 440 |
fi |
| 441 |
fi |
| 442 |
|
| 443 |
# last chance to stop us |
| 444 |
if [ "${kiwi_oemunattended}" = "false" ];then |
| 445 |
local ack_dump_text="Destroying ALL data on ${image_target}, continue ?" |
| 446 |
if ! run_dialog --yesno "\"${ack_dump_text}\"" 7 80; then |
| 447 |
local install_cancel_text="System installation canceled" |
| 448 |
report_and_quit "${install_cancel_text}" |
| 449 |
fi |
| 450 |
fi |
| 451 |
|
| 452 |
# deploy |
| 453 |
echo "${load_text} [${image_target}]..." |
| 454 |
if with_progress && [ "${kiwi_oemsilentinstall}" = "false" ]; then |
| 455 |
# dump with dialog based progress information |
| 456 |
setup_progress_fifo ${progress} |
| 457 |
eval \ |
| 458 |
"${dump}" \ |
| 459 |
"${image_source}" \ |
| 460 |
"${image_target}" \ |
| 461 |
"${count}" \ |
| 462 |
"${block_size}" \ |
| 463 |
"${progress}" \ |
| 464 |
& |
| 465 |
run_progress_dialog "${load_text}" "${title_text}" |
| 466 |
else |
| 467 |
# dump with silently blocked console |
| 468 |
if ! eval \ |
| 469 |
"${dump}" \ |
| 470 |
"${image_source}" \ |
| 471 |
"${image_target}" \ |
| 472 |
"${count}" \ |
| 473 |
"${block_size}" |
| 474 |
then |
| 475 |
report_and_quit "Failed to install image" |
| 476 |
fi |
| 477 |
fi |
| 478 |
|
| 479 |
# recreate last partition from pre-dump table |
| 480 |
if getargbool 0 rd.kiwi.install.retain_past && \ |
| 481 |
[ ! -e /tmp/retain_not_applicable ] |
| 482 |
then |
| 483 |
recreate_last_partition "${image_target}" |
| 484 |
fi |
| 485 |
} |
| 486 |
|
| 487 |
function with_progress { |
| 488 |
if getargbool 0 rd.debug || ! command -v pv &>/dev/null; then |
| 489 |
return 1 |
| 490 |
fi |
| 491 |
return 0 |
| 492 |
} |
| 493 |
|
| 494 |
function fetch_local_partition_table { |
| 495 |
local image_source=$1 |
| 496 |
local parttable=$2 |
| 497 |
if [ -z "${parttable}" ];then |
| 498 |
parttable=/tmp/parttable |
| 499 |
fi |
| 500 |
sfdisk -d "${image_source}" > "${parttable}" 2>/dev/null |
| 501 |
echo "${parttable}" |
| 502 |
} |
| 503 |
|
| 504 |
function recreate_last_partition { |
| 505 |
# recreate last partition as it existed prior dump |
| 506 |
local image_target=$1 |
| 507 |
local table_type |
| 508 |
table_type=$(get_partition_table_type "${image_target}") |
| 509 |
if [ "${table_type}" = "gpt" ];then |
| 510 |
relocate_gpt_at_end_of_disk "${image_target}" |
| 511 |
fi |
| 512 |
# parttable after image dump, the OS |
| 513 |
sfdisk -d "${image_target}" > /tmp/parttable_1 2>/dev/null |
| 514 |
# last partition of table as it existed prior dump |
| 515 |
tail -n 1 /tmp/parttable_pre > /tmp/parttable_2 |
| 516 |
# combine table snippets. Please note there can now be a gap |
| 517 |
# between the end of the OS table and the start of the last |
| 518 |
# partition of the table prior dump. Please also note, if the |
| 519 |
# table prior dump had more partitions than only the last |
| 520 |
# one to retain, this table concat will not retain those. |
| 521 |
# and the OS dump might have overwritten them. A more |
| 522 |
# sophisticated concat procedure would be needed to support |
| 523 |
# this case here in this code and also in the |
| 524 |
# compatible_to_retain() method |
| 525 |
cat /tmp/parttable_1 /tmp/parttable_2 > /tmp/parttable |
| 526 |
set_device_lock "${image_target}" \ |
| 527 |
sfdisk -f "${image_target}" < /tmp/parttable |
| 528 |
} |
| 529 |
|
| 530 |
function fetch_remote_partition_table { |
| 531 |
local image_source=$1 |
| 532 |
local image_size=$2 |
| 533 |
local parttable=/tmp/parttable |
| 534 |
dd if=/dev/zero of=/tmp/table bs=1 count=0 seek="${image_size}" &>/dev/null |
| 535 |
fetch_file "${image_source}" 2>/dev/null | dd of=/tmp/table bs=512 count=1 conv=notrunc &>/dev/null |
| 536 |
sfdisk -d /tmp/table > "${parttable}" 2>/dev/null |
| 537 |
echo "${parttable}" |
| 538 |
} |
| 539 |
|
| 540 |
function get_sector_size_from_table_dump { |
| 541 |
local parttable=$1 |
| 542 |
sector_size=$(grep sector-size: "${parttable}" | cut -f2 -d:) |
| 543 |
echo "${sector_size}" |
| 544 |
} |
| 545 |
|
| 546 |
function compatible_to_retain { |
| 547 |
local parttable=$1 |
| 548 |
local image_target=$2 |
| 549 |
local source_start |
| 550 |
local target_start |
| 551 |
source_start=$( |
| 552 |
tail -n 1 "${parttable}" | cut -f2 -d= | cut -f1 -d, |
| 553 |
) |
| 554 |
target_start=$( |
| 555 |
sfdisk -d "${image_target}" 2>/dev/null |\ |
| 556 |
tail -n 1 | cut -f2 -d= | cut -f1 -d, |
| 557 |
) |
| 558 |
if [ -z "${source_start}" ] || [ -z "${target_start}" ];then |
| 559 |
# no partition information for either source or target |
| 560 |
# broken or net new deployment on empty disk |
| 561 |
touch /tmp/retain_not_applicable |
| 562 |
return 1 |
| 563 |
fi |
| 564 |
if getargbool 0 rd.kiwi.install.retain_last; then |
| 565 |
if [ ! "${source_start}" = "${target_start}" ];then |
| 566 |
report_and_quit "Cannot retain partition, start address mismatch" |
| 567 |
fi |
| 568 |
elif getargbool 0 rd.kiwi.install.retain_past; then |
| 569 |
if [ "${source_start}" -gt "${target_start}" ];then |
| 570 |
report_and_quit "Cannot retain partition, image overlaps" |
| 571 |
fi |
| 572 |
fi |
| 573 |
return 0 |
| 574 |
} |
| 575 |
|
| 576 |
function get_disk_offset_retain_last_partition { |
| 577 |
local parttable=$1 |
| 578 |
local next_to_last |
| 579 |
local start |
| 580 |
local size |
| 581 |
local offset |
| 582 |
next_to_last=$(tail -n 2 "${parttable}" | head -n 1) |
| 583 |
if [ -n "${next_to_last}" ];then |
| 584 |
start=$(echo "${next_to_last}" | cut -f2 -d= | cut -f1 -d,) |
| 585 |
size=$(echo "${next_to_last}" | cut -f3 -d= | cut -f1 -d,) |
| 586 |
offset=$((start + size)) |
| 587 |
echo "${offset}" |
| 588 |
else |
| 589 |
echo 0 |
| 590 |
fi |
| 591 |
} |
| 592 |
|
| 593 |
function optimize_count_for_32k_blocksize { |
| 594 |
local block_size=$1 |
| 595 |
local count=$2 |
| 596 |
local dump_bytes |
| 597 |
dump_bytes=$((block_size * count)) |
| 598 |
if [ $((dump_bytes % 32768)) -eq 0 ];then |
| 599 |
# dump_bytes is a multiple of 32k, use it for better I/O performance |
| 600 |
count=$((dump_bytes / 32768)) |
| 601 |
fi |
| 602 |
echo "${count}" |
| 603 |
} |
| 604 |
|
| 605 |
function dump_local_image { |
| 606 |
local image_source=$1 |
| 607 |
local image_target=$2 |
| 608 |
local count=$3 |
| 609 |
local block_size=$4 |
| 610 |
local progress=$5 |
| 611 |
if [ "${count}" -gt 0 ];then |
| 612 |
count="count=${count}" |
| 613 |
else |
| 614 |
unset count |
| 615 |
fi |
| 616 |
# shellcheck disable=SC2086 |
| 617 |
if [ -e "${progress}" ];then |
| 618 |
( |
| 619 |
pv -n "${image_source}" |\ |
| 620 |
dd bs="${block_size}" ${count} of="${image_target}" &>/dev/null |
| 621 |
) 2>"${progress}" |
| 622 |
else |
| 623 |
dd \ |
| 624 |
if="${image_source}" bs="${block_size}" ${count} \ |
| 625 |
of="${image_target}" &>/dev/null |
| 626 |
fi |
| 627 |
} |
| 628 |
|
| 629 |
function dump_remote_image { |
| 630 |
local image_source=$1 |
| 631 |
local image_target=$2 |
| 632 |
local count=$3 |
| 633 |
local block_size=$4 |
| 634 |
local progress=$5 |
| 635 |
local image_size |
| 636 |
image_size=$((blocks * blocksize)) |
| 637 |
if [ "${count}" -gt 0 ];then |
| 638 |
count="count=${count}" |
| 639 |
else |
| 640 |
unset count |
| 641 |
fi |
| 642 |
# shellcheck disable=SC2086 |
| 643 |
if [ -e "${progress}" ];then |
| 644 |
( |
| 645 |
fetch_file "${image_source}" "${image_size}" |\ |
| 646 |
dd bs="${block_size}" ${count} of="${image_target}" &>/dev/null |
| 647 |
) 2>"${progress}" |
| 648 |
else |
| 649 |
fetch_file "${image_source}" |\ |
| 650 |
dd bs="${block_size}" ${count} of="${image_target}" &>/dev/null |
| 651 |
fi |
| 652 |
} |
| 653 |
|
| 654 |
function check_image_integrity { |
| 655 |
declare kiwi_oemskipverify=${kiwi_oemskipverify} |
| 656 |
declare kiwi_oemsilentverify=${kiwi_oemsilentverify} |
| 657 |
local image_target=$1 |
| 658 |
local progress=/dev/install_verify_progress |
| 659 |
local verify_text="Verifying ${image_target}" |
| 660 |
local title_text="Installation..." |
| 661 |
local verify_result |
| 662 |
local shasum |
| 663 |
local sha_extension |
| 664 |
kiwi_oemskipverify=$(bool "${kiwi_oemskipverify}") |
| 665 |
kiwi_oemsilentverify=$(bool "${kiwi_oemsilentverify}") |
| 666 |
shasum=$(shasum_tool) |
| 667 |
sha_extension=$(shasum_extension) |
| 668 |
verify_result=/dumped_image."${sha_extension}" |
| 669 |
if [ "${kiwi_oemskipverify}" = "true" ];then |
| 670 |
# no verification wanted |
| 671 |
return |
| 672 |
fi |
| 673 |
if getargbool 0 rd.kiwi.install.retain_last || \ |
| 674 |
getargbool 0 rd.kiwi.install.retain_past |
| 675 |
then |
| 676 |
if [ ! -e /tmp/retain_not_applicable ];then |
| 677 |
# no verification possible as only a portion of |
| 678 |
# the image got deployed intentionally (retain_last) |
| 679 |
# or parts of the former partition table got |
| 680 |
# restored (retain_past) |
| 681 |
return |
| 682 |
fi |
| 683 |
fi |
| 684 |
if with_progress && [ "${kiwi_oemsilentverify}" = "false" ]; then |
| 685 |
# verify with dialog based progress information |
| 686 |
setup_progress_fifo ${progress} |
| 687 |
( |
| 688 |
pv --size $((blocks * blocksize)) --stop-at-size \ |
| 689 |
-n "${image_target}" | "${shasum}" - > "${verify_result}" |
| 690 |
) 2>${progress} & |
| 691 |
run_progress_dialog "${verify_text}" "${title_text}" |
| 692 |
else |
| 693 |
# verify with silently blocked console |
| 694 |
head --bytes=$((blocks * blocksize)) "${image_target}" |\ |
| 695 |
"${shasum}" - > "${verify_result}" |
| 696 |
fi |
| 697 |
local checksum_dumped_image |
| 698 |
local checksum_fileref |
| 699 |
read -r checksum_dumped_image checksum_fileref < "${verify_result}" |
| 700 |
echo "Dumped Image checksum: ${checksum_dumped_image}/${checksum_fileref}" |
| 701 |
if [ "${checksum}" != "${checksum_dumped_image}" ];then |
| 702 |
report_and_quit "Image checksum test failed" |
| 703 |
fi |
| 704 |
} |
| 705 |
|
| 706 |
function get_local_image_source_files { |
| 707 |
declare root=${root} |
| 708 |
local iso_device="${root#install:}" |
| 709 |
local iso_mount_point=/run/install |
| 710 |
local image_mount_point=/run/image |
| 711 |
local sha_extension |
| 712 |
local image_source |
| 713 |
local image_sha |
| 714 |
sha_extension=$(shasum_extension) |
| 715 |
mkdir -m 0755 -p "${iso_mount_point}" |
| 716 |
if ! mount -n "${iso_device}" "${iso_mount_point}"; then |
| 717 |
report_and_quit "Failed to mount install ISO device" |
| 718 |
fi |
| 719 |
mkdir -m 0755 -p "${image_mount_point}" |
| 720 |
if ! mount -n "${iso_mount_point}"/*.squashfs ${image_mount_point};then |
| 721 |
report_and_quit "Failed to mount install image squashfs filesystem" |
| 722 |
fi |
| 723 |
image_source="$(echo "${image_mount_point}"/*.raw)" |
| 724 |
image_sha="$(echo "${image_mount_point}"/*."${sha_extension}")" |
| 725 |
echo "${image_source}|${image_sha}" |
| 726 |
} |
| 727 |
|
| 728 |
function get_remote_image_source_files { |
| 729 |
local image_uri |
| 730 |
local sha_extension |
| 731 |
local image_sha |
| 732 |
local install_dir=/run/install |
| 733 |
local metadata_dir="${install_dir}/boot/remote/loader" |
| 734 |
|
| 735 |
sha_extension=$(shasum_extension) |
| 736 |
|
| 737 |
local image_sha="${install_dir}/image.${sha_extension}" |
| 738 |
|
| 739 |
mkdir -p "${metadata_dir}" |
| 740 |
|
| 741 |
image_uri=$(getarg rd.kiwi.install.image=) |
| 742 |
# make sure the protocol type is tftp for metadata files. There is no need for |
| 743 |
# complex protocol types on small files and for standard PXE boot operations |
| 744 |
# only tftp can be guaranteed |
| 745 |
image_sha_uri=$( |
| 746 |
echo "${image_uri}" | awk '{ gsub("\\.xz",".'"${sha_extension}"'", $1); gsub("dolly:","tftp:", $1); print $1 }' |
| 747 |
) |
| 748 |
image_initrd_uri=$( |
| 749 |
echo "${image_uri}" | awk '{ gsub("\\.xz",".initrd", $1); gsub("dolly:","tftp:", $1); print $1 }' |
| 750 |
) |
| 751 |
image_kernel_uri=$( |
| 752 |
echo "${image_uri}" | awk '{ gsub("\\.xz",".kernel", $1); gsub("dolly:","tftp:", $1); print $1 }' |
| 753 |
) |
| 754 |
image_config_uri=$( |
| 755 |
echo "${image_uri}" | \ |
| 756 |
awk '{ gsub("\\.xz",".config.bootoptions", $1); gsub("dolly:","tftp:", $1); print $1 }' |
| 757 |
) |
| 758 |
|
| 759 |
# if we can not access image_sha_uri, maybe network setup |
| 760 |
# by dracut did fail, so collect some additional info |
| 761 |
if ! fetch_file "${image_sha_uri}" > "${image_sha}";then |
| 762 |
{ |
| 763 |
echo "--- ip a ---"; ip a |
| 764 |
echo "--- ip r ---"; ip r |
| 765 |
} >> /tmp/fetch.info 2>&1 |
| 766 |
show_log_and_quit \ |
| 767 |
"Failed to fetch ${image_sha_uri}" /tmp/fetch.info |
| 768 |
fi |
| 769 |
|
| 770 |
if ! getargbool 0 rd.kiwi.ramdisk; then |
| 771 |
if ! fetch_file "${image_kernel_uri}" > "${metadata_dir}/linux";then |
| 772 |
show_log_and_quit \ |
| 773 |
"Failed to fetch ${image_kernel_uri}" /tmp/fetch.info |
| 774 |
fi |
| 775 |
|
| 776 |
if ! fetch_file "${image_initrd_uri}" > "${install_dir}/initrd.system_image" |
| 777 |
then |
| 778 |
show_log_and_quit \ |
| 779 |
"Failed to fetch ${image_initrd_uri}" /tmp/fetch.info |
| 780 |
fi |
| 781 |
fi |
| 782 |
|
| 783 |
if ! fetch_file "${image_config_uri}" > "/config.bootoptions" |
| 784 |
then |
| 785 |
show_log_and_quit \ |
| 786 |
"Failed to fetch ${image_config_uri}" /tmp/fetch.info |
| 787 |
fi |
| 788 |
|
| 789 |
echo "${image_uri}|${image_sha}" |
| 790 |
} |
| 791 |
|
| 792 |
#====================================== |
| 793 |
# Perform image dump/install operations |
| 794 |
#-------------------------------------- |
| 795 |
setup_debug |
| 796 |
|
| 797 |
lib_initialize |
| 798 |
|
| 799 |
udev_pending |
| 800 |
|
| 801 |
image_target=$(get_selected_disk) |
| 802 |
|
| 803 |
if getargbool 0 rd.kiwi.oem.disk.consistency; then |
| 804 |
validate_disk_selection "${image_target}" |
| 805 |
fi |
| 806 |
|
| 807 |
if getargbool 0 rd.kiwi.install.pxe; then |
| 808 |
image_source_files=$(get_remote_image_source_files) |
| 809 |
else |
| 810 |
image_source_files=$(get_local_image_source_files) |
| 811 |
fi |
| 812 |
|
| 813 |
export_image_metadata "${image_source_files}" |
| 814 |
|
| 815 |
if getargbool 0 rd.kiwi.install.pxe; then |
| 816 |
dump_image "${image_source_files}" "${image_target}" "remote_image" |
| 817 |
else |
| 818 |
dump_image "${image_source_files}" "${image_target}" |
| 819 |
fi |
| 820 |
|
| 821 |
check_image_integrity "${image_target}" |