From eabcb26fa4a91d410a6f75a9915a9ebb9f702c6b Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Fri, 6 Oct 2017 09:55:40 +0200
Subject: [PATCH] dasdfmt: Implement '-Y/--yast_mode'

Implement an option '-Y' to suppress most output.

Signed-off-by: Hannes Reinecke <hare@suse.com>
---
 dasdfmt/dasdfmt.8 |  7 ++++++-
 dasdfmt/dasdfmt.c | 27 ++++++++++++++++++++-------
 dasdfmt/dasdfmt.h |  1 +
 3 files changed, 27 insertions(+), 8 deletions(-)

Index: b/dasdfmt/dasdfmt.8
===================================================================
--- a/dasdfmt/dasdfmt.8	2019-11-07 11:20:11.967697033 +0100
+++ b/dasdfmt/dasdfmt.8	2019-11-07 11:20:11.967697033 +0100
@@ -7,7 +7,7 @@
 dasdfmt \- formatting of DASD (ECKD) disk drives.
 
 .SH SYNOPSIS
-\fBdasdfmt\fR [-h] [-t] [-v] [-y] [-p] [-Q] [-P] [-m \fIstep\fR]
+\fBdasdfmt\fR [-h] [-t] [-v] [-y] [-p] [-Q] [-P] [-Y] [-m \fIstep\fR]
 .br
         [-r \fIcylinder\fR] [-b \fIblksize\fR] [-l \fIvolser\fR] [-d \fIlayout\fR]
 .br
@@ -113,6 +113,11 @@ The value will be at least as big as the -r or --requestsize value.
 .br
 
 .TP
+\fB-Y\fR or \fB--yast_mode\fR
+YaST mode; suppress most output.
+.br
+
+.TP
 \fB-M\fR \fImode\fR or \fB--mode\fR=\fImode\fR
 Specify the \fImode\fR to be used to format the device. Valid modes are:
 .RS
Index: b/dasdfmt/dasdfmt.c
===================================================================
--- a/dasdfmt/dasdfmt.c	2019-11-07 11:20:11.967697033 +0100
+++ b/dasdfmt/dasdfmt.c	2019-11-08 09:36:08.495150185 +0100
@@ -129,6 +129,10 @@ static struct util_opt opt_vec[] = {
 		.option = { "percentage", no_argument, NULL, 'Q' },
 		.desc = "Show progress in percent",
 	},
+	{
+		.option = { "yast_mode", no_argument, NULL, 'Y' },
+		.desc = "YaST mode",
+	},
 	UTIL_OPT_SECTION("MISC"),
 	{
 		.option = { "check_host_count", no_argument, NULL, 'C' },
@@ -351,7 +355,8 @@ static void evaluate_format_error(dasdfmt_info_t *info, format_check_t *cdata,
 	unsigned int kl = 0;
 	int blksize = cdata->expect.blksize;
 
-	if (info->print_progressbar || info->print_hashmarks)
+	if ((info->print_progressbar || info->print_hashmarks) &&
+	    !info->yast_mode)
 		printf("\n");
 
 	/*
@@ -758,9 +763,9 @@ static void check_hashmarks(dasdfmt_info_t *info)
 			       "using the default.\n");
 			info->hashstep = 10;
 		}
-
-		printf("Printing hashmark every %d cylinders.\n",
-		       info->hashstep);
+		if (!info->yast_mode)
+			printf("Printing hashmark every %d cylinders.\n",
+			       info->hashstep);
 	}
 }
 
@@ -1445,16 +1450,18 @@ static void do_format_dasd(dasdfmt_info_t *info, char *devname,
 			break;
 		}
 
-		printf("Finished formatting the device.\n");
+		if (!info->yast_mode)
+			printf("Finished formatting the device.\n");
 
 		if (!(info->writenolabel || mode == EXPAND))
 			dasdfmt_write_labels(info, vlabel, cylinders, heads);
 
-		printf("Rereading the partition table... ");
+		if (!info->yast_mode)
+			printf("Rereading the partition table... ");
 		if (reread_partition_table()) {
 			ERRMSG("%s: error during rereading the partition "
 			       "table: %s.\n", prog_name, strerror(errno));
-		} else {
+		} else if (!info->yast_mode) {
 			printf("ok\n");
 		}
 	}
@@ -1525,6 +1532,41 @@
 
 }
 
+/*
+ * Print cylinder size of a device, or UINT_MAX if the information cannot be
+ * obtained.
+ *
+ * This is a support function for the YaST mode to output first cylinder
+ * information for all devices so YaST can use this information to draw a
+ * sensible progress bar.
+ */
+static void yast_print_cylinfo(const char *dev_filename)
+{
+	unsigned int cylinders = (unsigned int)-1;
+	int fd;
+	dasd_information2_t dasd_info;
+	struct dasd_eckd_characteristics *characteristics;
+
+	fd = open(dev_filename, O_RDONLY);
+	if (fd == -1)
+		goto out;
+	if (ioctl(fd, BIODASDINFO2, &dasd_info))
+		goto out;
+
+	characteristics = (struct dasd_eckd_characteristics *)
+				&dasd_info.characteristics;
+	if (characteristics->no_cyl == LV_COMPAT_CYL &&
+	    characteristics->long_no_cyl)
+		cylinders = characteristics->long_no_cyl;
+	else
+		cylinders = characteristics->no_cyl;
+
+out:
+	if (fd != -1)
+		close(fd);
+	printf("%u\n", cylinders);
+}
+
 int main(int argc, char *argv[])
 {
 	dasdfmt_info_t info = {
@@ -1665,6 +1707,10 @@ int main(int argc, char *argv[])
 					    "more information.\n",
 					    prog_name, optarg);
 			break;
+		case 'Y':
+			/* YaST mode */
+			info.yast_mode = 1;
+			break;
 		case 'P':
 			max_parallel = atoi(optarg);
 			break;
@@ -1720,6 +1766,12 @@
 		ERRMSG_EXIT(EXIT_MISUSE, "%s: No device specified!\n",
 			    prog_name);
 
+	if (info.yast_mode) {
+		for (i = 0; i < numdev; i++)
+			yast_print_cylinfo(dev_filename[i]);
+		fflush(stdout);
+	}
+
 	for (i = 0; i < numdev; i++) {
 		int chpid;
 		int tmp;
Index: b/dasdfmt/dasdfmt.h
===================================================================
--- a/dasdfmt/dasdfmt.h	2019-11-07 11:20:11.967697033 +0100
+++ b/dasdfmt/dasdfmt.h	2019-11-07 11:20:11.967697033 +0100
@@ -308,6 +308,7 @@ typedef struct dasdfmt_info {
 	int   layout_specified;
 	int   check;
 	int   procnum;
+	int   yast_mode;
 } dasdfmt_info_t;
 
 
