Clone partitions using head instead of dd
authorJacek Kryszyn <j.kryszyn@samsung.com>
Wed, 3 Apr 2024 14:16:11 +0000 (16:16 +0200)
committerChanwoo Choi <cw00.choi@samsung.com>
Wed, 24 Apr 2024 01:50:02 +0000 (10:50 +0900)
Instead of cloning partitions (whole disks) using dd,
head -c $part_size is used to copy only necessary bytes.
This can be much faster in case of partitions placed
on much bigger disks.

Change-Id: Ie15b9f543bac8ef351ef0a4ed4e9adfd148cf704

scripts/upgrade-support/upgrade-common.inc

index d542590..e53ab46 100644 (file)
@@ -235,6 +235,33 @@ check_used_block_device() {
        check_if_super
 }
 
+calculate_size () {
+       local part=$1
+       local info=""
+       local block_size=0
+       local block_count=0
+       local size=0
+
+       untrap
+
+       if info="$(dumpe2fs -h "$part" 2>/dev/null)"; then
+               retrap
+               block_size="$(echo "$info" | grep "Block size" | awk '{print $3}')"
+               block_count="$(echo "$info" | grep "Block count" | awk '{print $3}')"
+               echo "$((block_size*block_count))"
+               return 0
+       fi
+
+       if size="$(blockdev --getsize64 "$part" 2>/dev/null)"; then
+               retrap
+               echo "$size"
+               return 0
+       fi
+
+       retrap
+       return 1
+}
+
 background_copy() {
        log "[Info] Background copying A|B partitions for update..."
        start_duration=$(date +%s)
@@ -295,7 +322,8 @@ background_copy() {
                                fi
                        fi
                else
-                       /bin/dd if="$CURRENT_PARTITION" of="$NEXT_PARTITION" bs=4096
+                       PART_SIZE="$(calculate_size "$CURRENT_PARTITION")"
+                       head -c "$PART_SIZE" "$CURRENT_PARTITION" > "$NEXT_PARTITION"
                fi
                log "[Info] Finished background copy $partition_name from $CURRENT_PARTITION to $NEXT_PARTITION"
 
@@ -310,8 +338,8 @@ background_copy() {
                                        NEXT_SHA1=$(head -c "$ROOTFS_CURR_SIZE" "$NEXT_PARTITION" | sha1sum | awk '{print $1}')
                                fi
                        else
-                               CUR_SHA1=$(sha1sum "$CURRENT_PARTITION" | awk '{print $1}')
-                               NEXT_SHA1=$(sha1sum "$NEXT_PARTITION" | awk '{print $1}')
+                               CUR_SHA1=$(head -c "$PART_SIZE" "$CURRENT_PARTITION" | sha1sum | awk '{print $1}')
+                               NEXT_SHA1=$(head -c "$PART_SIZE" "$NEXT_PARTITION" | sha1sum | awk '{print $1}')
                        fi
 
                        if [ "$CUR_SHA1" == "$NEXT_SHA1" ]; then