Blame SOURCES/rear-rhbz1610638.patch

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