Blame SOURCES/rear-rhbz1610638.patch

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