Blame SOURCES/rear-rhbz1610638.patch

e8a0f5
diff --git a/usr/share/rear/conf/GNU/Linux.conf b/usr/share/rear/conf/GNU/Linux.conf
e8a0f5
index 5b9343b9..f0c44381 100644
e8a0f5
--- a/usr/share/rear/conf/GNU/Linux.conf
e8a0f5
+++ b/usr/share/rear/conf/GNU/Linux.conf
e8a0f5
@@ -184,6 +184,8 @@ env
e8a0f5
 w
e8a0f5
 dosfslabel
e8a0f5
 sysctl
e8a0f5
+blockdev
e8a0f5
+lsblk
e8a0f5
 )
e8a0f5
 
e8a0f5
 # the lib* serves to cover both 32bit and 64bit libraries!
e8a0f5
diff --git a/usr/share/rear/lib/layout-functions.sh b/usr/share/rear/lib/layout-functions.sh
e8a0f5
index eb45115b..af1069ea 100644
e8a0f5
--- a/usr/share/rear/lib/layout-functions.sh
e8a0f5
+++ b/usr/share/rear/lib/layout-functions.sh
e8a0f5
@@ -365,7 +365,9 @@ get_partition_start() {
e8a0f5
     local disk_name=$1
e8a0f5
     local start_block start
e8a0f5
 
e8a0f5
-    local block_size=$(get_block_size ${disk_name%/*})
e8a0f5
+    # When reading /sys/block/.../start or "dmsetup table", output is always in
e8a0f5
+    # 512 bytes blocks
e8a0f5
+    local block_size=512
e8a0f5
 
e8a0f5
     if [[ -r /sys/block/$disk_name/start ]] ; then
e8a0f5
         start_block=$(< $path/start)
e8a0f5
@@ -548,11 +550,32 @@ get_device_mapping() {
e8a0f5
 }
e8a0f5
 
e8a0f5
 # Get the size in bytes of a disk/partition.
e8a0f5
+# For disks, use "sda" as argument.
e8a0f5
 # For partitions, use "sda/sda1" as argument.
e8a0f5
 get_disk_size() {
e8a0f5
     local disk_name=$1
e8a0f5
+    # When a partition is specified (e.g. sda/sda1)
e8a0f5
+    # then it has to read /sys/block/sda/sda1/size in the old code below.
e8a0f5
+    # In contrast the get_block_size() function below is different
e8a0f5
+    # because it is non-sense asking for block size of a partition,
e8a0f5
+    # so that the get_block_size() function below is stripping everything
e8a0f5
+    # in front of the blockdev basename (e.g. /some/path/sda -> sda)
e8a0f5
+    # cf. https://github.com/rear/rear/pull/1885#discussion_r207900308
e8a0f5
+
e8a0f5
+    # Preferably use blockdev, see https://github.com/rear/rear/issues/1884
e8a0f5
+    if has_binary blockdev; then
e8a0f5
+        # ${disk_name##*/} translates 'sda/sda1' into 'sda1' and 'sda' into 'sda'
e8a0f5
+        blockdev --getsize64 /dev/${disk_name##*/} && return
e8a0f5
+        # If blockdev fails do not error out but fall through to the old code below
e8a0f5
+        # because blockdev fails e.g. for a CDROM device when no DVD or ISO is attached to
e8a0f5
+        # cf. https://github.com/rear/rear/pull/1885#issuecomment-410676283
e8a0f5
+        # and https://github.com/rear/rear/pull/1885#issuecomment-410697398
e8a0f5
+    fi
e8a0f5
 
e8a0f5
-    local block_size=$(get_block_size ${disk_name%/*})
e8a0f5
+    # Linux always considers sectors to be 512 bytes long. See the note in the
e8a0f5
+    # kernel source, specifically, include/linux/types.h regarding the sector_t
e8a0f5
+    # type for details.
e8a0f5
+    local block_size=512
e8a0f5
 
e8a0f5
     retry_command test -r /sys/block/$disk_name/size || Error "Could not determine size of disk $disk_name"
e8a0f5
 
e8a0f5
@@ -565,9 +588,20 @@ get_disk_size() {
e8a0f5
 
e8a0f5
 # Get the block size of a disk.
e8a0f5
 get_block_size() {
e8a0f5
+    local disk_name="${1##*/}" # /some/path/sda -> sda
e8a0f5
+
e8a0f5
+    # Preferably use blockdev, see https://github.com/rear/rear/issues/1884
e8a0f5
+    if has_binary blockdev; then
e8a0f5
+        blockdev --getss /dev/$disk_name && return
e8a0f5
+        # If blockdev fails do not error out but fall through to the old code below
e8a0f5
+        # because blockdev fails e.g. for a CDROM device when no DVD or ISO is attached to
e8a0f5
+        # cf. https://github.com/rear/rear/pull/1885#issuecomment-410676283
e8a0f5
+        # and https://github.com/rear/rear/pull/1885#issuecomment-410697398
e8a0f5
+    fi
e8a0f5
+
e8a0f5
     # Only newer kernels have an interface to get the block size
e8a0f5
-    if [ -r /sys/block/$1/queue/logical_block_size ] ; then
e8a0f5
-        echo $( < /sys/block/$1/queue/logical_block_size)
e8a0f5
+    if [ -r /sys/block/$disk_name/queue/logical_block_size ] ; then
e8a0f5
+        echo $( < /sys/block/$disk_name/queue/logical_block_size)
e8a0f5
     else
e8a0f5
         echo "512"
e8a0f5
     fi