[PATCH 1/1] Add DISK media support to TGTIMG

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Please find attached a patch that adds "DISK" media support to tgtimg.
This allows us to use TGTIMG to create all types of devices.

The manpage for tgtimg is updated with this supprot and an example.

The README.iser documentation has been changed to use tgtimg instead
of dd to create the mediafile used in the example.


regards
ronnie sahlberg

Attachment: 0002-Update-TGTIMG-to-use-it-to-create-DISK-images.patch.gz
Description: GNU Zip compressed data

From c235db24c4b5eacf269926ad6e34498f25103fb5 Mon Sep 17 00:00:00 2001
From: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
Date: Sun, 4 Apr 2010 08:07:33 +1000
Subject: [PATCH] Update TGTIMG to use it to create DISK images.
 This is a trivial reimplementation of dd if=/dev/zero bs=1M ...
 but offers us to use one command for any type of media.

Update the manpage for TGTIMG to describe how to use TGTIMG
to create DISK media.

Change the example in README.iser to use TGTIMG instead of dd to
create the mediafile.

Signed-off-by: Ronnie Sahlberg <ronniesahlberg@xxxxxxxxx>
---
 doc/README.iser             |    3 +-
 doc/htmlpages/tgtimg.8.html |   32 +++++++++++--------
 doc/manpages/tgtimg.8       |   29 ++++++++++++-----
 doc/tgtimg.8.xml            |   26 ++++++++++-----
 usr/tgtimg.c                |   71 +++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 129 insertions(+), 32 deletions(-)

diff --git a/doc/README.iser b/doc/README.iser
index 2361c4e..f96e7aa 100644
--- a/doc/README.iser
+++ b/doc/README.iser
@@ -189,7 +189,8 @@ Configure the running target with one or more devices, using the tgtadm
 program you just built (also as root).  Full information is in
 doc/README.iscsi.  Here is a quick-start guide:
 
-	dd if=/dev/zero bs=1k count=1 seek=1048575 of=/tmp/tid1lun1
+	./tgtimg --op new --device-type disk --type disk --size 1024 \
+		 --file /tmp/tid1lun1
 	./tgtadm --lld iscsi --mode target \
 		 --op new --tid 1 --targetname $(hostname)
 	./tgtadm --lld iscsi --mode target \
diff --git a/doc/htmlpages/tgtimg.8.html b/doc/htmlpages/tgtimg.8.html
index 202473f..f689149 100644
--- a/doc/htmlpages/tgtimg.8.html
+++ b/doc/htmlpages/tgtimg.8.html
@@ -1,22 +1,26 @@
-<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>tgtimg</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en"><a name="tgtimg.8"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>tgtimg &#8212; Linux SCSI Target Framework Image File Utility</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">tgtimg</code>  [-o --op &lt;operation&gt;] [-Y --device-type &lt;device-type&gt;] [-b --barcode &lt;barcode&gt;] [-s --size &lt;size&gt;] [-t --type &lt;media-type&gt;] [-f --file &lt;path&gt;]</p></div><div class="cmdsynopsis"><p><code class="command">tgtimg --help</code> </p></div></div><div class="refsect1" lang="en"><a name="id2476001"></a><h2>DESCRIPTION</h2><p>
+<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>tgtimg</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en"><a name="tgtimg.8"></a><div class="titlepage"></div><div class="refnamediv"><h2>Name</h2><p>tgtimg &#8212; Linux SCSI Target Framework Image File Utility</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p><code class="command">tgtimg</code>  [-o --op &lt;operation&gt;] [-Y --device-type &lt;device-type&gt;] [-b --barcode &lt;barcode&gt;] [-s --size &lt;size&gt;] [-t --type &lt;media-type&gt;] [-f --file &lt;path&gt;]</p></div><div class="cmdsynopsis"><p><code class="command">tgtimg --help</code> </p></div></div><div class="refsect1" lang="en"><a name="id2476008"></a><h2>DESCRIPTION</h2><p>
       Tgtimg is a utility to create and manage the image files used by the 
       TGTD device emulation.
     </p><p>
       This command is used to initialize device image files with the additional
       metadata, such as barcode, list of blocks, total size, etc that
       the TGTD emulation needs.
-    </p></div><div class="refsect1" lang="en"><a name="id2476018"></a><h2>OPTIONS</h2><div class="variablelist"><pre class="screen">
+    </p></div><div class="refsect1" lang="en"><a name="id2476026"></a><h2>OPTIONS</h2><div class="variablelist"><pre class="screen">
 Supported device types are :
-    tape : to create media for a tape device
     cd   : to create media for a dvd device
+    disk : to create media for a disk device
+    tape : to create media for a tape device
       </pre><pre class="screen">
+Supported media types for cd devices are :
+    dvd+r : create a blank writeable DVD+R disk
+
+Supported media types for disk devices are :
+    disk  : create an empty disk
+
 Supported media types for tape devices are :
     data  : create a normal data tape
     clean : create a cleaning tape
     worm  : create a worm
-
-Supported media types for cd devices are :
-    dvd+r : create a blank writeable DVD+R disk
       </pre><dl><dt><span class="term"><code class="option">-h, --help</code></span></dt><dd><p>
 	    Show a help screen and exit.
           </p></dd><dt><span class="term"><code class="option">-o, --op {new|show}</code></span></dt><dd><p>
@@ -26,10 +30,8 @@ Supported media types for cd devices are :
 	    This specifies the type of image file.
           </p></dd><dt><span class="term"><code class="option">-t, --type &lt;media-type&gt;</code></span></dt><dd><p>
 	    When creating a new image, this specifies the type of media to
-	    emulate, normal data, cleaning image, or worm.
-          </p><p>
-	    Data is a normal tape to read/write to and probably what you 
-	    want to use.
+	    emulate. The available mediatypes depend on the type of the
+	    device.
           </p></dd><dt><span class="term"><code class="option">-b, --barcode &lt;barcode&gt;</code></span></dt><dd><p>
 	    When creating a new image, this argument specifies the barcode
 	    to use with this image file. Backup application software often
@@ -40,7 +42,11 @@ Supported media types for cd devices are :
 	    for the virtual tape image.
           </p></dd><dt><span class="term"><code class="option">-f, --file &lt;path&gt;</code></span></dt><dd><p>
 	    The filename of the image file.
-          </p></dd></dl></div></div><div class="refsect1" lang="en"><a name="id2476161"></a><h2>EXAMPLES</h2><p>
+          </p></dd></dl></div></div><div class="refsect1" lang="en"><a name="id2476166"></a><h2>EXAMPLES</h2><p>
+      To create a new 100MByte disk
+    </p><pre class="screen">
+      tgtimg --op new --device-type disk --type disk --size 100 --file /data/hd001.raw
+    </pre><p>
       To create a new tape image
     </p><pre class="screen">
       tgtimg --op new --device-type tape --barcode 12345 --size 100 --type data --file /data/tape001.img
@@ -52,9 +58,9 @@ Supported media types for cd devices are :
       To create a new blank DVD+R image
     </p><pre class="screen">
       tgtimg --op new --device-type cd --type dvd+r --file /data/dvd001.iso
-    </pre></div><div class="refsect1" lang="en"><a name="id2476201"></a><h2>SEE ALSO</h2><p>
+    </pre></div><div class="refsect1" lang="en"><a name="id2476217"></a><h2>SEE ALSO</h2><p>
       tgtd(8), tgtadm(8), tgt-admin(8), tgt-setup-lun(8).
       <a class="ulink" href="http://stgt.sourceforge.net/"; target="_top">http://stgt.sourceforge.net/</a>
-    </p></div><div class="refsect1" lang="en"><a name="id2476215"></a><h2>REPORTING BUGS</h2><p>
+    </p></div><div class="refsect1" lang="en"><a name="id2476232"></a><h2>REPORTING BUGS</h2><p>
       Report bugs to &lt;stgt@xxxxxxxxxxxxxxx&gt;
     </p></div></div></body></html>
diff --git a/doc/manpages/tgtimg.8 b/doc/manpages/tgtimg.8
index 920562f..23af44c 100644
--- a/doc/manpages/tgtimg.8
+++ b/doc/manpages/tgtimg.8
@@ -1,11 +1,11 @@
 .\"     Title: tgtimg
 .\"    Author: 
 .\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
-.\"      Date: 04/02/2010
+.\"      Date: 04/04/2010
 .\"    Manual: 
 .\"    Source: 
 .\"
-.TH "TGTIMG" "8" "04/02/2010" "" ""
+.TH "TGTIMG" "8" "04/04/2010" "" ""
 .\" disable hyphenation
 .nh
 .\" disable justification (adjust text to left margin only)
@@ -42,28 +42,30 @@ This specifies the type of image file\.
 .RS 4
 .nf
 Supported device types are :
-    tape : to create media for a tape device
     cd   : to create media for a dvd device
+    disk : to create media for a disk device
+    tape : to create media for a tape device
       
 .fi
 .RE
 .PP
 \fB\-t, \-\-type <media\-type>\fR
 .RS 4
-When creating a new image, this specifies the type of media to emulate, normal data, cleaning image, or worm\.
-.sp
-Data is a normal tape to read/write to and probably what you want to use\.
+When creating a new image, this specifies the type of media to emulate\. The available mediatypes depend on the type of the device\.
 .RE
 .sp
 .RS 4
 .nf
+Supported media types for cd devices are :
+    dvd+r : create a blank writeable DVD+R disk
+
+Supported media types for disk devices are :
+    disk  : create an empty disk
+
 Supported media types for tape devices are :
     data  : create a normal data tape
     clean : create a cleaning tape
     worm  : create a worm
-
-Supported media types for cd devices are :
-    dvd+r : create a blank writeable DVD+R disk
       
 .fi
 .RE
@@ -84,6 +86,15 @@ The filename of the image file\.
 .RE
 .SH "EXAMPLES"
 .PP
+To create a new 100MByte disk
+.sp
+.RS 4
+.nf
+      tgtimg \-\-op new \-\-device\-type disk \-\-type disk \-\-size 100 \-\-file /data/hd001\.raw
+    
+.fi
+.RE
+.PP
 To create a new tape image
 .sp
 .RS 4
diff --git a/doc/tgtimg.8.xml b/doc/tgtimg.8.xml
index a17e923..5a787af 100644
--- a/doc/tgtimg.8.xml
+++ b/doc/tgtimg.8.xml
@@ -71,30 +71,31 @@
       </varlistentry>
       <screen format="linespecific">
 Supported device types are :
-    tape : to create media for a tape device
     cd   : to create media for a dvd device
+    disk : to create media for a disk device
+    tape : to create media for a tape device
       </screen>
 
       <varlistentry><term><option>-t, --type &lt;media-type&gt;</option></term>
         <listitem>
           <para>
 	    When creating a new image, this specifies the type of media to
-	    emulate, normal data, cleaning image, or worm.
-          </para>
-          <para>
-	    Data is a normal tape to read/write to and probably what you 
-	    want to use.
+	    emulate. The available mediatypes depend on the type of the
+	    device.
           </para>
         </listitem>
       </varlistentry>
       <screen format="linespecific">
+Supported media types for cd devices are :
+    dvd+r : create a blank writeable DVD+R disk
+
+Supported media types for disk devices are :
+    disk  : create an empty disk
+
 Supported media types for tape devices are :
     data  : create a normal data tape
     clean : create a cleaning tape
     worm  : create a worm
-
-Supported media types for cd devices are :
-    dvd+r : create a blank writeable DVD+R disk
       </screen>
 
       <varlistentry><term><option>-b, --barcode &lt;barcode&gt;</option></term>
@@ -130,6 +131,13 @@ Supported media types for cd devices are :
 
   <refsect1><title>EXAMPLES</title>
     <para>
+      To create a new 100MByte disk
+    </para>
+    <screen format="linespecific">
+      tgtimg --op new --device-type disk --type disk --size 100 --file /data/hd001.raw
+    </screen>
+
+    <para>
       To create a new tape image
     </para>
     <screen format="linespecific">
diff --git a/usr/tgtimg.c b/usr/tgtimg.c
index c67c7f2..4349fff 100644
--- a/usr/tgtimg.c
+++ b/usr/tgtimg.c
@@ -88,6 +88,7 @@ Linux SCSI Target Framework Image File Utility, version %s\n\
 			[type] is media type \n\
 				(data, clean or WORM) for tape devices\n\
 				(dvd+r) for cd devices\n\
+				(disk) for disk devices\n\
 			[path] is a newly created file\n\
   --op show --device-type tape --file=[path]\n\
 			dump the tape image file contents.\n\
@@ -105,6 +106,8 @@ static int str_to_device_type(char *str)
 		return TYPE_TAPE;
 	else if (!strcmp(str, "cd"))
 		return TYPE_MMC;
+	else if (!strcmp(str, "disk"))
+		return TYPE_DISK;
 	else {
 		eprintf("unknown target type: %s\n", str);
 		exit(EINVAL);
@@ -421,6 +424,71 @@ static int mmc_ops(int op, char *path, char *media_type)
 	return 0;
 }
 
+static int sbc_new(int op, char *path, char *capacity, char *media_type)
+{
+	int fd;
+
+	if (!strncasecmp("disk", media_type, 4)) {
+		uint32_t pos, size;
+		char *buf;
+
+		sscanf(capacity, "%d", &size);
+		if (size == 0) {
+			printf("Capacity must be > 0\n");
+			exit(3);
+		}
+
+		buf = malloc(1024*1024);
+		if (buf == NULL) {
+			printf("Failed to malloc buffer\n");
+			exit(4);
+		}
+		fd = creat(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
+		if (fd < 0) {
+			perror("Failed creating file");
+			exit(2);
+		}
+
+		for (pos = 0; pos < size; pos++)
+			pwrite(fd, buf, 1024*1024, pos*1024*1024LL);
+
+		free(buf);
+		close(fd);
+
+		printf("Created blank DISK image file : %s\n", path);
+		syslog(LOG_DAEMON|LOG_INFO, "DISK %s being created", path);
+	} else {
+		eprintf("unknown media type when creating disk\n");
+		usage(1);
+	}
+
+	return 0;
+}
+
+static int sbc_ops(int op, char *path, char *capacity, char *media_type)
+{
+	if (op == OP_NEW) {
+		if (!media_type) {
+			eprintf("Missing media type: DISK\n");
+			usage(1);
+		}
+		if (strncasecmp("disk", media_type, 4)) {
+			eprintf("Media type must be DISK for disk devices\n");
+			usage(1);
+		}
+		if (!capacity) {
+			eprintf("Missing the capacity param\n");
+			usage(1);
+		}
+		return sbc_new(op, path, capacity, media_type);
+	} else {
+		eprintf("unknown the operation type\n");
+		usage(1);
+	}
+
+	return 0;
+}
+
 int main(int argc, char **argv)
 {
 	int ch, longindex;
@@ -483,6 +551,9 @@ int main(int argc, char **argv)
 	case TYPE_MMC:
 		mmc_ops(op, path, media_type);
 		break;
+	case TYPE_DISK:
+		sbc_ops(op, path, media_capacity, media_type);
+		break;
 	default:
 		eprintf("unsupported the device type operation\n");
 		usage(1);
-- 
1.5.4.3


[Index of Archives]     [Linux SCSI]     [Linux RAID]     [Linux Clusters]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]

  Powered by Linux