Debian Codegraph @

dracut/modules.d/59kiwi-lib/kiwi-partitions-lib.sh

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2
3 type getarg >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || . /lib/dracut-lib.shscip-shell source . . `/lib/dracut-lib.sh`.
4 type udev_pending >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || . /lib/kiwi-lib.shscip-shell source . . `/lib/kiwi-lib.sh`.
5
6 #======================================
7 # Library Methods
8 #--------------------------------------
9 function create_partitionsscip-shell shell shell-project 0.0.0 create_partitions(). {
10 local disk_device=$1
11 local partition_setup=$2
12 local pt_table_type
13 pt_table_type=$(get_partition_table_type "${disk_device}")
14 case ${pt_table_type} in
15 "dos")
16 create_msdos_partitionsscip-shell shell shell-project 0.0.0 create_msdos_partitions(). "${disk_device}" "${partition_setup}"
17 ;;
18 "gpt")
19 create_gpt_partitionsscip-shell shell shell-project 0.0.0 create_gpt_partitions(). "${disk_device}" "${partition_setup}"
20 ;;
21 "dasd")
22 create_dasd_partitionsscip-shell shell shell-project 0.0.0 create_dasd_partitions(). "${disk_device}" "${partition_setup}"
23 ;;
24 esac
25 # notify the kernel about partition table changes
26 if [scip-shell system . . `[`. "${pt_table_type}" = "dasd" ];then
27 # partx doesn't work for DASD disk, so on s390 partprobe is used
28 partprobe "${disk_device}"
29 else
30 set_device_lock "${disk_device}" \
31 partx -u "${disk_device}"
32 fi
33 # the changes cause udev to remove and recreate links in /dev,
34 # wait until that has settled
35 udev_pending
36 }
37
38 function create_msdos_partitionsscip-shell shell shell-project 0.0.0 create_msdos_partitions(). {
39 # """
40 # create partitions using sfdisk (msdos table)
41 # """
42 local disk_device=$1
43 local partition_setup=$2
44 local partid
45 local part_size_end
46 local cmd_list
47 local start_sector_from
48 local cmd
49
50 # put partition setup in a command list(cmd_list)
51 for cmd in ${partition_setup};do
52 # default values in sfdisk are used if - is provided
53 cmd=$(echoscip-shell system . . echo. "${cmd}" | trscip-shell system . . tr. . -)
54 cmd_list[$index]=${cmd}
55 index=$((index + 1))
56 done
57
58 # operate on index based cmd_list
59 index=0
60 for cmd in ${cmd_list[*]};do
61 case ${cmd} in
62 "d")
63 # delete a partition...
64 partid=${cmd_list[$index + 1]}
65 start_sector_from[$partid]=$(
66 _get_msdos_partition_start_sector "${disk_device}" "${partid}"
67 )
68 set_device_lock "${disk_device}" \
69 sfdisk --force --delete "${disk_device}" "${partid}"
70 ;;
71 "n")
72 # create a partition...
73 partid=${cmd_list[$index + 2]}
74 part_size_end=${cmd_list[$index + 4]}
75 echoscip-shell system . . echo. "${start_sector_from[$partid]},${part_size_end}" > /tmp/sfdisk.inscip-shell filesystem . . `/tmp/sfdisk.in`.
76 set_device_lock "${disk_device}" \
77 sfdisk --force -N "${partid}" "${disk_device}" < /tmp/sfdisk.inscip-shell filesystem . . `/tmp/sfdisk.in`.
78 ;;
79 "t")
80 # change a partition type...
81 part_type=${cmd_list[$index + 2]}
82 partid=${cmd_list[$index + 1]}
83 set_device_lock "${disk_device}" \
84 sfdisk --force --change-id "${disk_device}" "${partid}" "${part_type}"
85 ;;
86 esac
87 index=$((index + 1))
88 done
89 }
90
91 function create_gpt_partitionsscip-shell shell shell-project 0.0.0 create_gpt_partitions(). {
92 # """
93 # create partitions using sgdisk (gpt table)
94 # """
95 local disk_device=$1
96 local partition_setup=$2
97 local partid
98 local part_name
99 local part_size_start
100 local part_size_end
101 local cmd_list
102 local cmd
103
104 # put partition setup in a command list(cmd_list)
105 for cmd in ${partition_setup};do
106 # default values in sgdisk are used if 0 is provided
107 cmd=$(echoscip-shell system . . echo. "${cmd}" | trscip-shell system . . tr. . 0)
108 cmd_list[$index]=${cmd}
109 index=$((index + 1))
110 done
111
112 # operate on index based cmd_list
113 index=0
114 for cmd in ${cmd_list[*]};do
115 case ${cmd} in
116 "d")
117 # delete a partition...
118 partid=${cmd_list[$index + 1]}
119 set_device_lock "${disk_device}" \
120 sgdisk --delete "${partid}" "${disk_device}"
121 ;;
122 "n")
123 # create a partition...
124 part_name=$(echoscip-shell system . . echo. "${cmd_list[$index + 1]}" | trscip-shell system . . tr. : .)
125 partid=${cmd_list[$index + 2]}
126 part_size_start=${cmd_list[$index + 3]}
127 part_size_end=${cmd_list[$index + 4]}
128 set_device_lock "${disk_device}" \
129 sgdisk --new "${partid}:${part_size_start}:${part_size_end}" "${disk_device}"
130 set_device_lock "${disk_device}" \
131 sgdisk --change-name "${partid}:${part_name}" "${disk_device}"
132 ;;
133 "t")
134 # change a partition type...
135 part_type=${cmd_list[$index + 2]}
136 partid=${cmd_list[$index + 1]}
137 set_device_lock "${disk_device}" \
138 sgdisk --typecode "${partid}:$(_to_guid "${part_type}")" "${disk_device}"
139 ;;
140 esac
141 index=$((index + 1))
142 done
143 }
144
145 function create_dasd_partitionsscip-shell shell shell-project 0.0.0 create_dasd_partitions(). {
146 # """
147 # create partitions using fdasd (s390)
148 # """
149 local disk_device=$1
150 local partition_setup=$2
151 local partition_setup_file=/run/fdasd.cmdsscip-shell filesystem . . `/run/fdasd.cmds`.
152 local ignore_cmd=0
153 local ignore_cmd_once=0
154 local cmd
155
156 # We resize first partition to enfore rewrite of the current
157 # partition table updated to the actual disk geometry. This is
158 # to circumvent the fdasd limitation of not being capable to
159 # expand the partition table up to the disk size bsc#1209247
160 parted --script --machine "${disk_device}" resizepart 1
161
162 # Trigger recreation of the VTOC to work around
163 # https://github.com/ibm-s390-linux/s390-tools/issues/166
164 echoscip-shell system . . echo. "u" >> ${partition_setup_file}
165 echoscip-shell system . . echo. "y" >> ${partition_setup_file}
166
167 for cmd in ${partition_setup};do
168 if [scip-shell system . . `[`. "${ignore_cmd}" = 1 ] && echoscip-shell system . . echo. "${cmd}" | grepscip-shell system . . grep. -qE '[dntwq]';then
169 ignore_cmd=0
170 elif [scip-shell system . . `[`. "${ignore_cmd}" = 1 ];then
171 continue
172 fi
173 if [scip-shell system . . `[`. "${ignore_cmd_once}" = "1" ];then
174 ignore_cmd_once=0
175 continue
176 fi
177 if [scip-shell system . . `[`. "${cmd}" = "a" ];then
178 ignore_cmd=1
179 continue
180 fi
181 if [[ "${cmd}" =~ ^p: ]];then
182 ignore_cmd_once=1
183 continue
184 fi
185 if [scip-shell system . . `[`. "${cmd}" = "83" ] || [scip-shell system . . `[`. "${cmd}" = "8e" ];then
186 cmd=1
187 fi
188 if [scip-shell system . . `[`. "${cmd}" = "82" ];then
189 cmd=2
190 fi
191 if [scip-shell system . . `[`. "${cmd}" = "." ];then
192 echoscip-shell system . . echo. >> ${partition_setup_file}
193 continue
194 fi
195 echoscip-shell system . . echo. "$cmd" >> ${partition_setup_file}
196 done
197 echoscip-shell system . . echo. "w" >> ${partition_setup_file}
198 echoscip-shell system . . echo. "q" >> ${partition_setup_file}
199 set_device_lock "${disk_device}" \
200 fdasd "${disk_device}" < ${partition_setup_file} 1>&2
201 }
202
203 function get_partition_node_namescip-shell shell shell-project 0.0.0 get_partition_node_name(). {
204 local disk=$1
205 local partid=$2
206 local index=1
207 local part
208 udev_pending
209 # backwards compat for lsblk before 2.38:
210 # if START column not supported, fall back to default sort
211 for partnode in $(
212 { lsblkscip-shell system . . lsblk. -p -l -o NAME,TYPE,START -x START "${disk}" 2>/dev/nullscip-shell filesystem . . `/dev/null`. ||\
213 lsblk -p -l -o NAME,TYPE "${disk}"; } |\
214 grep -E "part|md$" | cut -f1 -d ' '
215 );do
216 if [scip-shell system . . `[`. "${index}" = "${partid}" ];then
217 echoscip-shell system . . echo. "${partnode}"
218 return 0
219 fi
220 index=$((index + 1))
221 done
222 return 1
223 }
224
225 function get_partition_namescip-shell shell shell-project 0.0.0 get_partition_name(). {
226 local disk=$1
227 local part_id=$2
228 local part_name
229 local table_type
230 table_type=$(get_partition_table_type "${disk}")
231 if [scip-shell system . . `[`. "${table_type}" = "gpt" ];then
232 part_name=$(sfdisk --part-label "${disk}" "${part_id}")
233 else
234 part_name=p:unsupported
235 fi
236 echoscip-shell system . . echo. "${part_name}"
237 }
238
239 function get_last_partition_devicescip-shell shell shell-project 0.0.0 get_last_partition_device(). {
240 # """
241 # Get unix node of last partition in table
242 # """
243 local disk=$1
244 lsblkscip-shell system . . lsblk. -p -n -r -o NAME,TYPE "${disk}" |\
245 grep -E "part|md$" | tailscip-shell system . . tail. -n 1 | cutscip-shell system . . cut. -f1 -d ' '
246 }
247
248 function get_last_partition_idscip-shell shell shell-project 0.0.0 get_last_partition_id(). {
249 # """
250 # Get index of last partition from the current table
251 # """
252 local disk=$1
253 local index=0
254 for partnode in $(lsblkscip-shell system . . lsblk. -p -l -o NAME,TYPE "${disk}" |\
255 grep -E "part|md$" | cut -f1 -d ' '
256 );do
257 index=$((index + 1))
258 done
259 echoscip-shell system . . echo. "${index}"
260 }
261
262 function wait_for_storage_devicescip-shell shell shell-project 0.0.0 wait_for_storage_device(). {
263 # """
264 # function to check access on a storage device which could be
265 # a whole disk or a partition. The function will wait until
266 # the size of the storage device could be obtained and is
267 # greater than zero or the timeout is reached. Default timeout
268 # is set to 60 seconds, however it can be set to different
269 # value by setting the DEVICE_TIMEOUT variable on the kernel
270 # command line.
271 # """
272 declare DEVICE_TIMEOUT=${DEVICE_TIMEOUT}
273 local device=$1
274 local check=0
275 local limit=30
276 local storage_size=0
277 if [[ "${DEVICE_TIMEOUT}" =~ ^[0-9]+$ ]]; then
278 limit=$(((DEVICE_TIMEOUT + 1)/ 2))
279 fi
280 udev_pending
281 while truescip-shell system . . true.;do
282 storage_size=$(get_block_device_kbsize "${device}")
283 if [scip-shell system . . `[`. "${storage_size}" -gt 0 ]; then
284 sleepscip-shell system . . sleep. 1; return 0
285 fi
286 if [scip-shell system . . `[`. "${check}" -eq "${limit}" ]; then
287 die "Storage device ${device} did not appear"
288 fi
289 info "Waiting for storage device ${device} to settle..."
290 check=$((check + 1))
291 sleepscip-shell system . . sleep. 2
292 done
293 }
294
295 function get_block_device_kbsizescip-shell shell shell-project 0.0.0 get_block_device_kbsize(). {
296 local device=$1
297 if [scip-shell system . . `[`. ! -e "${device}" ];then
298 echoscip-shell system . . echo. 0; return 1
299 fi
300 echoscip-shell system . . echo. $(($(blockdevscip-shell system . . blockdev. --getsize64 "${device}") / 1024))
301 }
302
303 function get_free_disk_bytesscip-shell shell shell-project 0.0.0 get_free_disk_bytes(). {
304 local disk=$1
305 local pt_table_type
306 local disk_bytes
307 local max_dos_disk
308 pt_table_type=$(get_partition_table_type "${disk}")
309 disk_bytes=$(blockdevscip-shell system . . blockdev. --getsize64 "${disk}")
310 # max msdos table geometry is at 2TB - 512
311 max_dos_disk=$((4294967295 * 512))
312 if \
313 [ "${pt_table_type}" = "dos" ] && \
314 [ "${disk_bytes}" -gt "${max_dos_disk}" ]
315 then
316 warn "msdos table allows a max disk size of 2TB"
317 warn "disk expansion will be truncated to 2TB"
318 disk_bytes=${max_dos_disk}
319 fi
320 local rest_bytes
321 rest_bytes=${disk_bytes}
322 local part_bytes=0
323 local part_count=0
324 local part_uuids
325 udev_pending
326 for part in $(
327 lsblkscip-shell system . . lsblk. -p -l -o NAME,TYPE "${disk}" | grepscip-shell system . . grep. -E "part|md$" | cutscip-shell system . . cut. -f1 -d ' '
328 );do
329 current_part_uuid=$(get_partition_uuid "${part}")
330 for part_uuid in ${part_uuids[*]};do
331 if [scip-shell system . . `[`. "${current_part_uuid}" = "${part_uuid}" ];then
332 # this partition uuid was already handled. The device
333 # node is pointing to the same physical device and
334 # should only be taken into account once
335 unset part
336 break
337 fi
338 done
339 if [scip-shell system . . `[`. -n "${part}" ]; then
340 part_bytes=$((part_bytes + $(blockdevscip-shell system . . blockdev. --getsize64 "${part}")))
341 part_uuids[${part_count}]=${current_part_uuid}
342 part_count=$((part_count + 1))
343 fi
344 done
345 rest_bytes=$((rest_bytes - part_bytes))
346 echoscip-shell system . . echo. ${rest_bytes}
347 }
348
349 function get_partition_table_typescip-shell shell shell-project 0.0.0 get_partition_table_type(). {
350 local disk_device="$1"
351 local table_type
352 # blkid doesn't work for dasd partitions, so on s390 fdasd is used
353 # checking if it can read the partition table of the device and if it
354 # can set dasd as table_type. In any other case use blkid
355 # to detect the partition type (bsc#1209247)
356 if [[ "$(uname -m)" =~ s390 ]] && fdasd -p "${disk_device}" &>/dev/nullscip-shell filesystem . . `/dev/null`.;then
357 table_type="dasd"
358 echoscip-shell system . . echo. "${table_type}"
359 else
360 blkidscip-shell system . . blkid. -s PTTYPE -o value "$disk_device"
361 fi
362 }
363
364 function get_partition_uuidscip-shell shell shell-project 0.0.0 get_partition_uuid(). {
365 blkidscip-shell system . . blkid. -s PARTUUID -o value "$1"
366 }
367
368 function relocate_gpt_at_end_of_diskscip-shell shell shell-project 0.0.0 relocate_gpt_at_end_of_disk(). {
369 if ! sfdisk --relocate gpt-bak-std "$1";then
370 die "Failed to write backup GPT at end of disk"
371 fi
372 }
373
374 function disk_has_unallocated_spacescip-shell shell shell-project 0.0.0 disk_has_unallocated_space(). {
375 local disk_device=$1
376 local free_mbytes
377 local pt_table_type
378 local flag="GPT PMBR size mismatch"
379 pt_table_type=$(get_partition_table_typescip-shell shell shell-project 0.0.0 get_partition_table_type(). "${disk_device}")
380 if [scip-shell system . . `[`. "${pt_table_type}" = "gpt" ];then
381 # GPT disks store a backup table at the end of the disk
382 # if the disk geometry changes the backup table is no
383 # longer at the end and this condition can be easily
384 # checked and used to detect that there is space
385 # unallocated due to a geometry change of the underlying
386 # block device layer
387 if sfdisk --list-free "${disk_device}" 2>&1 | grepscip-shell system . . grep. -q "${flag}"; then
388 return
389 fi
390 fi
391 # lookup if there is free space on disk compared to
392 # the current geometry values in the partition table
393 free_mbytes=$(
394 sfdisk --list-free "${disk_device}" |\
395 grep -i Unpartitioned | cut -f2 -d, | grep -o -E '[0-9]+'
396 )
397 if [scip-shell system . . `[`. "${free_mbytes}" != "0" ];then
398 truescip-shell system . . true.
399 else
400 falsescip-shell system . . false.
401 fi
402 }
403
404 function activate_boot_partitionscip-shell shell shell-project 0.0.0 activate_boot_partition(). {
405 local disk_device=$1
406 local boot_partition_id=$2
407 local pt_table_type
408 pt_table_type=$(get_partition_table_typescip-shell shell shell-project 0.0.0 get_partition_table_type(). "${disk_device}")
409 if [[ "$(uname -m)" =~ i.86|x86_64 ]];then
410 if [scip-shell system . . `[`. "${pt_table_type}" = "dos" ];then
411 set_device_lock "${disk_device}" \
412 sfdisk --activate "${disk_device}" "${boot_partition_id}"
413 fi
414 fi
415 }
416
417 function create_hybrid_gptscip-shell shell shell-project 0.0.0 create_hybrid_gpt(). {
418 local disk_device=$1
419 local partition_count
420 udev_pending
421 partition_count=$(lsblkscip-shell system . . lsblk. -r -o NAME,TYPE "${disk_device}" | grepscip-shell system . . grep. -c part)
422 if [scip-shell system . . `[`. "${partition_count}" -gt 3 ]; then
423 # The max number of partitions to embed is 3
424 # see man sgdisk for details
425 partition_count=3
426 fi
427 if ! set_device_lock "${disk_device}" \
428 sgdisk -h "$(seqscip-shell system . . seq. -s : 1 "${partition_count}")" "${disk_device}"
429 then
430 die "Failed to create hybrid GPT/MBR !"
431 fi
432 }
433
434 function finalize_partition_tablescip-shell shell shell-project 0.0.0 finalize_partition_table(). {
435 # """
436 # finalize partition table with flags which
437 # could have got lost during repartition
438 # """
439 declare kiwi_BootPart=${kiwi_BootPart}
440 declare kiwi_gpt_hybrid_mbr=${kiwi_gpt_hybrid_mbr}
441 local disk_device=$1
442 if [scip-shell system . . `[`. -n "${kiwi_BootPart}" ];then
443 activate_boot_partitionscip-shell shell shell-project 0.0.0 activate_boot_partition(). "${disk_device}" "${kiwi_BootPart}"
444 fi
445 if [scip-shell system . . `[`. "${kiwi_gpt_hybrid_mbr}" = "true" ];then
446 create_hybrid_gptscip-shell shell shell-project 0.0.0 create_hybrid_gpt(). "${disk_device}"
447 fi
448 }
449
450 function resize_wantedscip-shell shell shell-project 0.0.0 resize_wanted(). {
451 # """
452 # check if oem-resize-once was requested in the image
453 # description. If not we always try to repart/resize
454 # the image according to the configured constraints.
455 #
456 # If oem-resize-once is set to true we check if the
457 # system has been already resized compared to the
458 # original image PARTUUID and repart/resize the system
459 # only if the PARTUUID is still the original value.
460 # After resize a new PARTUUID will be written by the
461 # partitioner and that will result in the repart/resize
462 # operation to happen only once in the livetime of
463 # the image
464 #
465 # If the resize is wanted the method also checks for
466 # a real change in geometry on the block device layer
467 # and returns accordingly. Please note geometry change
468 # can currently only be detected on GPT disks. In any
469 # other case it is assumed the geometry has changed
470 # such that a resize can at least be tried
471 # """
472 declare kiwi_oemresizeonce=${kiwi_oemresizeonce}
473 declare kiwi_rootpartuuid=${kiwi_rootpartuuid}
474 local current_rootpart_uuid
475 local root_device=$1
476 local disk_device=$2
477 kiwi_oemresizeonce=$(bool "${kiwi_oemresizeonce}")
478 if getargbool 0 rd.kiwi.oem.force_resize; then
479 info "System resize forced via rd.kiwi.oem.force_resize"
480 info "Activating resize operation"
481 return 0
482 fi
483 if [scip-shell system . . `[`. "${kiwi_oemresizeonce}" = "true" ];then
484 current_rootpart_uuid=$(get_partition_uuidscip-shell shell shell-project 0.0.0 get_partition_uuid(). "${root_device}")
485 if [scip-shell system . . `[`. "${current_rootpart_uuid}" == "${kiwi_rootpartuuid}" ];then
486 info "System was not yet resized"
487 else
488 info "System was already resized and oem-resize-once is requested"
489 info "Skipping resize operation"
490 return 1
491 fi
492 else
493 info "System resize is active on every reboot"
494 fi
495 if disk_has_unallocated_spacescip-shell shell shell-project 0.0.0 disk_has_unallocated_space(). "${disk_device}";then
496 info "Activating resize operation"
497 return 0
498 else
499 info "Disk geometry did not change"
500 info "Skipping resize operation"
501 return 1
502 fi
503 }
504
505 #======================================
506 # Methods considered private
507 #--------------------------------------
508 function _get_msdos_partition_start_sectorscip-shell shell shell-project 0.0.0 _get_msdos_partition_start_sector(). {
509 # """
510 # read start sector from given partition
511 # """
512 local disk_device=$1
513 local partid=$2
514 sfdisk --dump "${disk_device}" |\
515 grep "${partid} :" | cutscip-shell system . . cut. -f1 -d, | cutscip-shell system . . cut. -f2 -d= | trscip-shell system . . tr. -d " "
516 }
517
518 function _to_guidscip-shell shell shell-project 0.0.0 _to_guid(). {
519 # """
520 # convert two digit partition id to guid id
521 # """
522 local partid=$1
523 if [scip-shell system . . `[`. "${partid}" = "83" ];then
524 # Linux filesystem
525 echoscip-shell system . . echo. 8300
526 elif [scip-shell system . . `[`. "${partid}" = "8e" ];then
527 # Linux LVM
528 echoscip-shell system . . echo. 8e00
529 elif [scip-shell system . . `[`. "${partid}" = "fd" ];then
530 # Linux RAID
531 echoscip-shell system . . echo. fd00
532 elif [scip-shell system . . `[`. "${partid}" = "82" ];then
533 # Linux swap
534 echoscip-shell system . . echo. 8200
535 else
536 # Assume Linux filesystem
537 echoscip-shell system . . echo. 8300
538 fi
539 }