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

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