Blame SOURCES/s390x-support-virtio-dasd-0.9.1.patch

302623
From c4fabc7c801491019263523df3c9078cca99bb6e Mon Sep 17 00:00:00 2001
302623
From: Nikita Dubrovskii <nikita@linux.ibm.com>
302623
Date: Wed, 2 Jun 2021 14:27:42 +0200
302623
Subject: [PATCH] s390: add support for virtio dasds
302623
302623
This is a fix for https://bugzilla.redhat.com/show_bug.cgi?id=1960485
302623
302623
Signed-off-by: Nikita Dubrovskii <nikita@linux.ibm.com>
302623
---
302623
 src/blockdev.rs   | 30 ++++++++++++++++++++++++++++--
302623
 src/cmdline.rs    |  2 +-
302623
 src/install.rs    |  6 +++---
302623
 src/s390x/dasd.rs | 15 +++++++++++----
302623
 4 files changed, 43 insertions(+), 10 deletions(-)
302623
302623
diff --git a/src/blockdev.rs b/src/blockdev.rs
302623
index 90c6bee..df8e10a 100644
302623
--- a/src/blockdev.rs
302623
+++ b/src/blockdev.rs
302623
@@ -970,10 +970,36 @@ pub fn detect_formatted_sector_size(buf: &[u8]) -> Option<NonZeroU32> {
302623
 }
302623
 
302623
 /// Checks if underlying device is IBM DASD disk
302623
-pub fn is_dasd(device: &str) -> Result<bool> {
302623
+pub fn is_dasd(device: &str, fd: Option<&mut File>) -> Result<bool> {
302623
     let target =
302623
         canonicalize(device).with_context(|| format!("getting absolute path to {}", device))?;
302623
-    Ok(target.to_string_lossy().starts_with("/dev/dasd"))
302623
+    if target.to_string_lossy().starts_with("/dev/dasd") {
302623
+        return Ok(true);
302623
+    }
302623
+    let read_magic = |device: &str, disk: &mut File| -> Result<[u8; 4]> {
302623
+        let offset = disk
302623
+            .seek(SeekFrom::Current(0))
302623
+            .with_context(|| format!("saving offset {}", device))?;
302623
+        disk.seek(SeekFrom::Start(8194))
302623
+            .with_context(|| format!("seeking {}", device))?;
302623
+        let mut lbl = [0u8; 4];
302623
+        disk.read_exact(&mut lbl)
302623
+            .with_context(|| format!("reading label {}", device))?;
302623
+        disk.seek(SeekFrom::Start(offset))
302623
+            .with_context(|| format!("restoring offset {}", device))?;
302623
+        Ok(lbl)
302623
+    };
302623
+    if target.to_string_lossy().starts_with("/dev/vd") {
302623
+        let cdl_magic = [0xd3, 0xf1, 0xe5, 0xd6];
302623
+        let lbl = if let Some(t) = fd {
302623
+            read_magic(device, t)?
302623
+        } else {
302623
+            let mut disk = File::open(device).with_context(|| format!("opening {}", device))?;
302623
+            read_magic(device, &mut disk)?
302623
+        };
302623
+        return Ok(cdl_magic == lbl);
302623
+    }
302623
+    Ok(false)
302623
 }
302623
 
302623
 // create unsafe ioctl wrappers
302623
diff --git a/src/cmdline.rs b/src/cmdline.rs
302623
index db3d1d0..53b8179 100644
302623
--- a/src/cmdline.rs
302623
+++ b/src/cmdline.rs
302623
@@ -853,7 +853,7 @@ fn parse_install(matches: &ArgMatches) -> Result<Config> {
302623
     // it changes to the recommended 4096
302623
     // https://bugzilla.redhat.com/show_bug.cgi?id=1905159
302623
     #[allow(clippy::match_bool, clippy::match_single_binding)]
302623
-    let sector_size = match is_dasd(&device)
302623
+    let sector_size = match is_dasd(&device, None)
302623
         .with_context(|| format!("checking whether {} is an IBM DASD disk", device))?
302623
     {
302623
         #[cfg(target_arch = "s390x")]
302623
diff --git a/src/install.rs b/src/install.rs
302623
index baa3880..20d1f41 100644
302623
--- a/src/install.rs
302623
+++ b/src/install.rs
302623
@@ -47,7 +47,7 @@ pub fn install(config: &InstallConfig) -> Result<()> {
302623
 
302623
     #[cfg(target_arch = "s390x")]
302623
     {
302623
-        if is_dasd(&config.device)? {
302623
+        if is_dasd(&config.device, None)? {
302623
             if !config.save_partitions.is_empty() {
302623
                 // The user requested partition saving, but SavedPartitions
302623
                 // doesn't understand DASD VTOCs and won't find any partitions
302623
@@ -156,7 +156,7 @@ fn write_disk(
302623
 
302623
     // copy the image
302623
     #[allow(clippy::match_bool, clippy::match_single_binding)]
302623
-    let image_copy = match is_dasd(&config.device)? {
302623
+    let image_copy = match is_dasd(&config.device, Some(dest))? {
302623
         #[cfg(target_arch = "s390x")]
302623
         true => s390x::image_copy_s390x,
302623
         _ => image_copy_default,
302623
@@ -527,7 +527,7 @@ fn reset_partition_table(
302623
 ) -> Result<()> {
302623
     eprintln!("Resetting partition table");
302623
 
302623
-    if is_dasd(&config.device)? {
302623
+    if is_dasd(&config.device, Some(dest))? {
302623
         // Don't write out a GPT, since the backup GPT may overwrite
302623
         // something we're not allowed to touch.  Just clear the first MiB
302623
         // of disk.
302623
diff --git a/src/s390x/dasd.rs b/src/s390x/dasd.rs
302623
index 7145071..b7dea78 100644
302623
--- a/src/s390x/dasd.rs
302623
+++ b/src/s390x/dasd.rs
302623
@@ -35,13 +35,15 @@ pub(crate) struct Range {
302623
     pub length: u64,
302623
 }
302623
 
302623
-/// There are 2 types of DASD devices:
302623
+/// There are 3 types of DASD devices:
302623
 ///   - ECKD (Extended Count Key Data) - is regular DASD of type 3390
302623
 ///   - FBA (Fixed Block Access) - is used for emulated device that represents a real SCSI device
302623
+///   - Virt - ECKD on LPAR/zKVM as virtio-device
302623
 /// Only ECKD disks require `dasdfmt, fdasd` linux tools to be configured.
302623
 enum DasdType {
302623
     Eckd,
302623
     Fba,
302623
+    Virt,
302623
 }
302623
 
302623
 fn get_dasd_type<P: AsRef<Path>>(device: P) -> Result<DasdType> {
302623
@@ -53,6 +55,9 @@ fn get_dasd_type<P: AsRef<Path>>(device: P) -> Result<DasdType> {
302623
         .with_context(|| format!("getting name of {}", device.display()))?
302623
         .to_string_lossy()
302623
         .to_string();
302623
+    if device.starts_with("vd") {
302623
+        return Ok(DasdType::Virt);
302623
+    }
302623
     let devtype_path = format!("/sys/class/block/{}/device/devtype", device);
302623
     let devtype_str = std::fs::read_to_string(&devtype_path)
302623
         .with_context(|| format!("reading {}", devtype_path))?;
302623
@@ -66,7 +71,7 @@ fn get_dasd_type<P: AsRef<Path>>(device: P) -> Result<DasdType> {
302623
 pub fn prepare_dasd(dasd: &str) -> Result<()> {
302623
     match get_dasd_type(dasd)? {
302623
         DasdType::Eckd => eckd_prepare(dasd),
302623
-        DasdType::Fba => Ok(()),
302623
+        DasdType::Fba | DasdType::Virt => Ok(()),
302623
     }
302623
 }
302623
 
302623
@@ -75,7 +80,7 @@ pub fn prepare_dasd(dasd: &str) -> Result<()> {
302623
 pub fn dasd_try_get_sector_size(dasd: &str) -> Result<Option<NonZeroU32>> {
302623
     match get_dasd_type(dasd)? {
302623
         DasdType::Eckd => eckd_try_get_sector_size(dasd),
302623
-        DasdType::Fba => Ok(None),
302623
+        DasdType::Fba | DasdType::Virt => Ok(None),
302623
     }
302623
 }
302623
 
302623
@@ -87,8 +92,10 @@ pub fn image_copy_s390x(
302623
     _saved: Option<&SavedPartitions>,
302623
 ) -> Result<()> {
302623
     let ranges = match get_dasd_type(dest_path)? {
302623
-        DasdType::Eckd => eckd_make_partitions(&dest_path.to_string_lossy(), dest_file, first_mb)?,
302623
         DasdType::Fba => fba_make_partitions(&dest_path.to_string_lossy(), dest_file, first_mb)?,
302623
+        DasdType::Eckd | DasdType::Virt => {
302623
+            eckd_make_partitions(&dest_path.to_string_lossy(), dest_file, first_mb)?
302623
+        }
302623
     };
302623
 
302623
     // copy each partition
302623
-- 
302623
2.31.1
302623