Debian Codegraph @

dracut/modules.d/55kiwi-repart/kiwi-repart-disk.sh

raw
1 #!/bin/bash
2 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
3 type lookup_disk_device_from_root >/dev/null 2>&1 || . /lib/kiwi-lib.sh
4 type create_partitions >/dev/null 2>&1 || . /lib/kiwi-partitions-lib.sh
5 type resize_filesystem >/dev/null 2>&1 || . /lib/kiwi-filesystem-lib.sh
6 type activate_volume_group >/dev/null 2>&1 || . /lib/kiwi-lvm-lib.sh
7 type activate_mdraid >/dev/null 2>&1 || . /lib/kiwi-mdraid-lib.sh
8 type luks_system >/dev/null 2>&1 || . /lib/kiwi-luks-lib.sh
9
10 #======================================
11 # Functions
12 #--------------------------------------
13 function initialize {
14 declare root=${root}
15 local profile=/.profile
16 local partition_ids=/config.partids
17
18 if test ! -f ${profile}; then
19 warn "No profile setup found"
20 warn "Settings from the kiwi description will be ignored"
21 fi
22
23 test -f ${profile} && import_file ${profile}
24
25 # Optional env TERM setup
26 term=$(getarg "rd.kiwi.term=")
27 [ -n "${term}" ] && export TERM="${term}"
28
29 # Create dialog profile from current env
30 # Used in the systemd dialog unit
31 env >/dialog_profile
32
33 if ! test -f ${partition_ids}; then
34 warn "No partition id setup found, rebuilding..."
35 {
36 disk=$(lookup_disk_device_from_root)
37 export disk
38
39 echo "kiwi_RootPart=\"$(get_last_partition_id "${disk}")\""
40 } > ${partition_ids}
41
42 import_file ${partition_ids}
43 else
44 import_file ${partition_ids}
45
46 disk=$(lookup_disk_device_from_root)
47 export disk
48 fi
49
50 last_device=$(get_last_partition_device "${disk}")
51 export last_device
52
53 disk_free_mbytes=$((
54 $(get_free_disk_bytes "${disk}") / 1048576
55 ))
56 export disk_free_mbytes
57
58 disk_root_mbytes=$((
59 $(get_block_device_kbsize "${last_device}") / 1024
60 ))
61 export disk_root_mbytes
62 }
63
64 function get_last_partition_id_from_config_file {
65 # """
66 # Read the partition id with the biggest value from
67 # config.partids. This method will be replaced by
68 # get_last_partition_id() in the future. As of today
69 # there is still code in 59kiwi-lib which reads
70 # the imported variables from config.partids. This
71 # code needs to be refactored first before we can
72 # get rid of config.partids
73 # """
74 local partition_ids=/config.partids
75 local partid=1
76 local partid_cur
77 while read -r partname; do
78 partid_cur=$(echo "${partname}" | cut -f2 -d= | tr -d \")
79 if [ "${partid_cur}" -gt "${partid}" ];then
80 partid="${partid_cur}"
81 fi
82 done < "${partition_ids}"
83 echo "${partid}"
84 }
85
86 function deactivate_device_mappings {
87 if lvm_system;then
88 deactivate_volume_group
89 fi
90 if mdraid_system;then
91 deactivate_mdraid
92 fi
93 if luks_system "${disk}";then
94 deactivate_luks
95 fi
96 }
97
98 function finalize_disk_repart {
99 local kiwi_ResizePart
100 kiwi_ResizePart=$(get_last_partition_id_from_config_file)
101 finalize_partition_table "${disk}"
102 set_root_map \
103 "$(get_partition_node_name "${disk}" "${kiwi_ResizePart}")"
104 }
105
106 function get_target_rootpart_size {
107 declare kiwi_oemrootMB=${kiwi_oemrootMB}
108 local oemrootMB
109 oemrootMB=$(getarg rd.kiwi.install.install.systemsize=)
110 if [ -n "${oemrootMB}" ]; then
111 if [ "${oemrootMB}" = "all" ];then
112 kiwi_oemrootMB=""
113 else
114 kiwi_oemrootMB="${oemrootMB}"
115 fi
116 fi
117 echo "${kiwi_oemrootMB}"
118 }
119
120 function repart_standard_disk {
121 # """
122 # repartition disk with read/write root filesystem
123 # Image partition table layout is:
124 # =====================================
125 # pX: [ boot ]
126 # pX+1: ( root ) [+luks +raid]
127 # -------------------------------------
128 # """
129 local kiwi_oemrootMB
130 local kiwi_ResizePart
131 local command_query
132 local root_part_size
133 local part_name
134 kiwi_oemrootMB=$(get_target_rootpart_size)
135 kiwi_ResizePart=$(get_last_partition_id_from_config_file)
136 part_name=$(get_partition_name "${disk}" "${kiwi_ResizePart}")
137 if [ -z "${kiwi_oemrootMB}" ];then
138 local disk_have_root_system_mbytes=$((
139 disk_root_mbytes + disk_free_mbytes
140 ))
141 local min_additional_mbytes=10
142 else
143 local disk_have_root_system_mbytes=${kiwi_oemrootMB}
144 local min_additional_mbytes=$((
145 kiwi_oemrootMB - disk_root_mbytes
146 ))
147 fi
148 if [ "${min_additional_mbytes}" -lt 10 ];then
149 min_additional_mbytes=10
150 fi
151 # check if we can repart this disk
152 if ! check_repart_possible \
153 ${disk_root_mbytes} ${disk_free_mbytes} ${min_additional_mbytes}
154 then
155 return 1
156 fi
157 # deactivate all active device mappings
158 deactivate_device_mappings
159 # repart root partition
160 root_part_size=+${disk_have_root_system_mbytes}M
161 if [ -z "${kiwi_oemrootMB}" ];then
162 # no new parts and no rootsize limit, use rest disk space
163 root_part_size=.
164 fi
165 command_query="
166 d ${kiwi_ResizePart}
167 n ${part_name} ${kiwi_ResizePart} . ${root_part_size}
168 "
169 if mdraid_system; then
170 command_query="
171 d ${kiwi_ResizePart}
172 n ${part_name} ${kiwi_ResizePart} . ${root_part_size}
173 t ${kiwi_ResizePart} fd
174 "
175 fi
176 if ! create_partitions "${disk}" "${command_query}";then
177 die "Failed to create partition table"
178 fi
179 if mdraid_system; then
180 update_devicesize_mdraid
181 fi
182 # finalize table changes
183 finalize_disk_repart
184 }
185
186 function repart_lvm_disk {
187 # """
188 # repartition disk if LVM partition plus boot partition
189 # is used. Initial partition table layout is:
190 # =====================================
191 # pX: ( boot )
192 # pX+1: ( LVM ) [+luks +raid]
193 # -------------------------------------
194 # """
195 local kiwi_oemrootMB
196 local kiwi_ResizePart
197 local command_query
198 local lvm_part_size
199 local part_name
200 kiwi_oemrootMB=$(get_target_rootpart_size)
201 kiwi_ResizePart=$(get_last_partition_id_from_config_file)
202 part_name=$(get_partition_name "${disk}" "${kiwi_ResizePart}")
203 if [ -z "${kiwi_oemrootMB}" ];then
204 local disk_have_root_system_mbytes=$((
205 disk_root_mbytes + disk_free_mbytes
206 ))
207 local min_additional_mbytes=10
208 else
209 local disk_have_root_system_mbytes=$((
210 kiwi_oemrootMB
211 ))
212 local min_additional_mbytes=$((
213 kiwi_oemrootMB - disk_root_mbytes
214 ))
215 fi
216 if [ "${min_additional_mbytes}" -lt 10 ];then
217 min_additional_mbytes=10
218 fi
219 # check if we can repart this disk
220 if ! check_repart_possible \
221 ${disk_root_mbytes} ${disk_free_mbytes} ${min_additional_mbytes}
222 then
223 return 1
224 fi
225 # deactivate all active device mappings
226 deactivate_device_mappings
227 # create lvm.conf appropriate for resize
228 setup_lvm_config
229 # repart lvm partition
230 lvm_part_size=+${disk_have_root_system_mbytes}M
231 if [ -z "${kiwi_oemrootMB}" ];then
232 # no rootsize limit, use rest disk space
233 lvm_part_size=.
234 fi
235 command_query="
236 d ${kiwi_ResizePart}
237 n ${part_name} ${kiwi_ResizePart} . ${lvm_part_size}
238 t ${kiwi_ResizePart} 8e
239 "
240 if ! create_partitions "${disk}" "${command_query}";then
241 die "Failed to create partition table"
242 fi
243 # finalize table changes
244 finalize_disk_repart
245 }
246
247 function check_repart_possible {
248 local disk_root_mbytes=$1
249 local disk_free_mbytes=$2
250 local min_additional_mbytes=$3
251 local kiwi_oemrootMB
252 kiwi_oemrootMB=$(get_target_rootpart_size)
253 if [ -n "${kiwi_oemrootMB}" ];then
254 if [ "${kiwi_oemrootMB}" -lt "${disk_root_mbytes}" ];then
255 # specified oem-systemsize is smaller than root partition
256 warn "Requested OEM systemsize is smaller than root partition:"
257 warn "Disk won't be re-partitioned !"
258 echo
259 warn "Current Root partition: ${disk_root_mbytes} MB"
260 warn "==> Requested size: ${kiwi_oemrootMB} MB"
261 return 1
262 fi
263 fi
264 if [ "${min_additional_mbytes}" -gt "${disk_free_mbytes}" ];then
265 if [ "${disk_free_mbytes}" -gt 2 ];then
266 # Requested size for root exceeds free space on disk
267 local requested_size
268 if [ -n "${kiwi_oemrootMB}" ];then
269 requested_size="root:($((kiwi_oemrootMB - disk_root_mbytes)) MB)"
270 else
271 requested_size="root:(keep)"
272 fi
273 warn "Requested OEM systemsize exceeds free space on the disk:"
274 warn "Disk won't be re-partitioned !"
275 echo
276 warn "Requested size(s): ${requested_size}"
277 warn "==> Free Space on disk: ${disk_free_mbytes} MB"
278 else
279 # The free space on disk calculated to a very small number.
280 # This usually indicates that the disk geometry was not
281 # intentionally changed and the rest free space is a
282 # rounding number on the partition alignment. In this case
283 # no warning message is shown because it's the typical
284 # situation on reboot of a machine without disk geometry
285 # changes.
286 :
287 fi
288 return 1
289 fi
290 return 0
291 }
292
293 #======================================
294 # Perform repart/resize operations
295 #--------------------------------------
296 PATH=/usr/sbin:/usr/bin:/sbin:/bin
297
298 setup_debug
299
300 # abort for iso/live mode
301 case "${root}" in
302 install:* | live:*)
303 return
304 ;;
305 esac
306
307 # initialize for disk repartition
308 initialize
309
310 # reencrypt luks device
311 if luks_system "${disk}";then
312 if getargbool 0 rd.kiwi.oem.luks.reencrypt; then
313 reencrypt_luks "${disk}"
314 fi
315 fi
316
317 # wait for the root device to appear
318 wait_for_storage_device "${last_device}"
319
320 # check if repart/resize is wanted
321 if ! resize_wanted "${last_device}" "${disk}"; then
322 return
323 fi
324
325 # prepare disk for repartition
326 if [ "$(get_partition_table_type "${disk}")" = 'gpt' ];then
327 relocate_gpt_at_end_of_disk "${disk}"
328 fi
329
330 # wait for the root device to appear
331 wait_for_storage_device "${last_device}"
332
333 # resize disk partition table
334 if lvm_system;then
335 repart_lvm_disk || return
336 else
337 repart_standard_disk || return
338 fi
339
340 # resize luks if present
341 if luks_system "${disk}";then
342 activate_luks "$(get_root_map)"
343 resize_luks
344 fi
345
346 # resize raid if present
347 if mdraid_system;then
348 activate_mdraid
349 resize_mdraid
350 fi
351
352 # resize volumes and filesystems
353 if lvm_system; then
354 resize_pyhiscal_volumes
355 activate_volume_group
356 resize_lvm_volumes_and_filesystems
357 else
358 resize_filesystem "$(get_root_map)"
359 fi
360
361 # wait for the root device to appear
362 wait_for_storage_device "${last_device}"