[pve-devel] [PATCH storage 05/14] Diskmanage: introduce get_sysdir_size helper

Fabian Ebner f.ebner at proxmox.com
Tue Jan 26 12:45:21 CET 2021


to be used for partitions as well.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 PVE/Diskmanage.pm                    | 18 ++++++++++++------
 test/disk_tests/usages/sdd/sdd1/size |  1 +
 test/disk_tests/usages/sdd/sdd2/size |  1 +
 test/disk_tests/usages/sde/sde1/size |  1 +
 test/disk_tests/usages/sdf/sdf1/size |  1 +
 test/disk_tests/usages/sdm/sdm1/size |  1 +
 test/disk_tests/usages/sdm/sdm9/size |  1 +
 test/disklist_test.pm                | 12 ++++++++++++
 8 files changed, 30 insertions(+), 6 deletions(-)
 create mode 100644 test/disk_tests/usages/sdd/sdd1/size
 create mode 100644 test/disk_tests/usages/sdd/sdd2/size
 create mode 100644 test/disk_tests/usages/sde/sde1/size
 create mode 100644 test/disk_tests/usages/sdf/sdf1/size
 create mode 100644 test/disk_tests/usages/sdm/sdm1/size
 create mode 100644 test/disk_tests/usages/sdm/sdm9/size

diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm
index 5672d0f..6d96e77 100644
--- a/PVE/Diskmanage.pm
+++ b/PVE/Diskmanage.pm
@@ -369,6 +369,17 @@ sub get_udev_info {
     return $data;
 }
 
+sub get_sysdir_size {
+    my ($sysdir) = @_;
+
+    my $size = file_read_firstline("$sysdir/size");
+    return if !$size;
+
+    # linux always considers sectors to be 512 bytes,
+    # independently of real block size
+    return $size * 512;
+}
+
 sub get_sysdir_info {
     my ($sysdir) = @_;
 
@@ -376,12 +387,7 @@ sub get_sysdir_info {
 
     my $data = {};
 
-    my $size = file_read_firstline("$sysdir/size");
-    return undef if !$size;
-
-    # linux always considers sectors to be 512 bytes,
-    # independently of real block size
-    $data->{size} = $size * 512;
+    $data->{size} = get_sysdir_size($sysdir) or return;
 
     # dir/queue/rotational should be 1 for hdd, 0 for ssd
     $data->{rotational} = file_read_firstline("$sysdir/queue/rotational") // -1;
diff --git a/test/disk_tests/usages/sdd/sdd1/size b/test/disk_tests/usages/sdd/sdd1/size
new file mode 100644
index 0000000..83b33d2
--- /dev/null
+++ b/test/disk_tests/usages/sdd/sdd1/size
@@ -0,0 +1 @@
+1000
diff --git a/test/disk_tests/usages/sdd/sdd2/size b/test/disk_tests/usages/sdd/sdd2/size
new file mode 100644
index 0000000..8bd1af1
--- /dev/null
+++ b/test/disk_tests/usages/sdd/sdd2/size
@@ -0,0 +1 @@
+2000
diff --git a/test/disk_tests/usages/sde/sde1/size b/test/disk_tests/usages/sde/sde1/size
new file mode 100644
index 0000000..13de30f
--- /dev/null
+++ b/test/disk_tests/usages/sde/sde1/size
@@ -0,0 +1 @@
+3000
diff --git a/test/disk_tests/usages/sdf/sdf1/size b/test/disk_tests/usages/sdf/sdf1/size
new file mode 100644
index 0000000..13de30f
--- /dev/null
+++ b/test/disk_tests/usages/sdf/sdf1/size
@@ -0,0 +1 @@
+3000
diff --git a/test/disk_tests/usages/sdm/sdm1/size b/test/disk_tests/usages/sdm/sdm1/size
new file mode 100644
index 0000000..83b33d2
--- /dev/null
+++ b/test/disk_tests/usages/sdm/sdm1/size
@@ -0,0 +1 @@
+1000
diff --git a/test/disk_tests/usages/sdm/sdm9/size b/test/disk_tests/usages/sdm/sdm9/size
new file mode 100644
index 0000000..8bd1af1
--- /dev/null
+++ b/test/disk_tests/usages/sdm/sdm9/size
@@ -0,0 +1 @@
+2000
diff --git a/test/disklist_test.pm b/test/disklist_test.pm
index 9cb6763..bfce1ea 100644
--- a/test/disklist_test.pm
+++ b/test/disklist_test.pm
@@ -87,6 +87,16 @@ sub mocked_get_sysdir_info {
     return &$originalsub($param);
 }
 
+sub mocked_get_sysdir_size {
+    my ($param) = @_;
+
+    my $originalsub = $diskmanage_module->original('get_sysdir_size');
+
+    $param =~ s|/sys/block|disk_tests/$testcasedir|;
+
+    return &$originalsub($param);
+}
+
 sub mocked_is_iscsi {
     return 0;
 }
@@ -219,6 +229,8 @@ $diskmanage_module->mock('dir_glob_foreach' => \&mocked_dir_glob_foreach);
 print("\tMocked dir_glob_foreach\n");
 $diskmanage_module->mock('get_sysdir_info' => \&mocked_get_sysdir_info);
 print("\tMocked get_sysdir_info\n");
+$diskmanage_module->mock('get_sysdir_size' => \&mocked_get_sysdir_size);
+print("\tMocked get_sysdir_size\n");
 $diskmanage_module->mock('is_iscsi' => \&mocked_is_iscsi);
 print("\tMocked is_iscsi\n");
 $diskmanage_module->mock('assert_blockdev' => sub { return 1; });
-- 
2.20.1






More information about the pve-devel mailing list