1SG_DD(8)                           SG3_UTILS                          SG_DD(8)
2
3
4

NAME

6       sg_dd  -  copies  data  to  and from files and devices. Specialised for
7       devices that understand the SCSI command set.
8

SYNOPSIS

10       sg_dd [bs=BS] [count=COUNT] [ibs=BS] [if=IFILE] [iflag=FLAGS]  [obs=BS]
11       [of=OFILE] [oflag=FLAGS] [seek=SEEK] [skip=SKIP] [--help] [--version]
12
13       [blk_sgio=0|1]      [bpt=BPT]      [cdbsz=6|10|12|16]     [coe=0|1|2|3]
14       [coe_limit=CL]   [dio=0|1]   [odir=0|1]    [retries=RETR]    [sync=0|1]
15       [time=0|1] [verbose=VERB]
16

DESCRIPTION

18       Copy data to and from any files. Specialized for "files" that are Linux
19       SCSI generic (sg) devices, raw devices or other  devices  that  support
20       the  SG_IO  ioctl  (which are only found in the lk 2.6 series). Similar
21       syntax and semantics to dd(1) but does not perform any conversions.
22
23       The first group in the synopsis above are "standard" Unix  dd(1)  oper‐
24       ands.  The  second group are extra options added by this utility.  Both
25       groups are defined below.
26

OPTIONS

28       blk_sgio=0 | 1
29              when set to 0, block devices (e.g. /dev/sda)  are  treated  like
30              normal  files (i.e.  read(2) and write(2) are used for IO). When
31              set to 1, block devices are assumed to accept  the  SG_IO  ioctl
32              and  SCSI commands are issued for IO. This is only supported for
33              2.6 series kernels. Note that ATAPI devices (e.g.  cd/dvd  play‐
34              ers) use the SCSI command set but ATA disks do not (unless there
35              is a protocol conversion as often occurs in the USB mass storage
36              class).  If  the input or output device is a block device parti‐
37              tion (e.g. /dev/sda3) then setting this option causes the parti‐
38              tion  information to be ignored (since access is directly to the
39              underlying device). Default is 0. See the 'sgio' flag.
40
41       bpt=BPT
42              each IO transaction will be made using BPT blocks  (or  less  if
43              near  the  end of the copy). Default is 128 for block sizes less
44              that 2048 bytes, otherwise the default is 32. So for bs=512  the
45              reads  and  writes  will  each  convey 64 KiB of data by default
46              (less if near the end of the transfer or  memory  restrictions).
47              When  cd/dvd  drives  are  accessed, the block size is typically
48              2048 bytes and bpt defaults to 32 which  again  implies  64  KiB
49              transfers.  The  block  layer when the blk_sgio=1 option is used
50              has relatively low upper limits for transfer sizes (compared  to
51              sg  device nodes, see /sys/block/<dev_name>/queue/max_sectors_kb
52              ).
53
54       bs=BS  where BS must be the block  size  of  the  physical  device  (if
55              either  the  input  or  output  files are accessed via SCSI com‐
56              mands). Note that this differs from dd(1) which permits BS to be
57              an  integral  multiple.  Default is 512 which is usually correct
58              for disks but incorrect for cdroms  (which  normally  have  2048
59              byte blocks). For this utility the maximum size of each individ‐
60              ual IO operation is BS * BPT bytes.
61
62       cdbsz=6 | 10 | 12 | 16
63              size of SCSI READ and/or WRITE  commands  issued  on  sg  device
64              names (or block devices when 'iflag=sgio' and/or 'oflag=sgio' is
65              given).  Default is 10 byte SCSI command blocks (unless calcula‐
66              tions indicate that a 4 byte block number may be exceeded or BPT
67              is greater than 16 bits (65535), in which case it defaults to 16
68              byte SCSI commands).
69
70       coe=0 | 1 | 2 | 3
71              set  to  1 or more for continue on error. Only applies to errors
72              on sg devices or block devices with the 'sgio'  flag  set.  Thus
73              errors  on  other  files  will  stop  sg_dd.  Default is 0 which
74              implies stop on any error. See the 'coe' flag for more  informa‐
75              tion.
76
77       coe_limit=CL
78              where CL is the maximum number of consecutive bad blocks stepped
79              over (due to "coe>0") on reads before the copy terminates.  This
80              only  applies  when  IFILE  is accessed via the SG_IO ioctl. The
81              default is 0 which is interpreted as no limit.  This  option  is
82              meant  to  stop the copy soon after unrecorded media is detected
83              while still offering "continue on error" capability.
84
85       count=COUNT
86              copy COUNT blocks from IFILE to OFILE. Default  is  the  minimum
87              (of  IFILE  and  OFILE)  number of blocks that sg devices report
88              from SCSI READ CAPACITY commands or that block devices (or their
89              partitions)  report. Normal files are not probed for their size.
90              If skip=SKIP or skip=SEEK are given and  the  count  is  derived
91              (i.e.   not  explicitly  given) then the derived count is scaled
92              back so that the copy will not overrun the device. If  the  file
93              name is a block device partition and COUNT is not given then the
94              size of the partition rather than the size of the  whole  device
95              is  used.  If  COUNT  is not given and cannot be derived then an
96              error message is issued and no copy takes place.
97
98       dio=0 | 1
99              default is 0 which selects indirect (buffered) IO on sg devices.
100              Value  of  1  attempts  direct IO which, if not available, falls
101              back to indirect IO and notes this at completion. If  direct  IO
102              is  selected and /proc/scsi/sg/allow_dio has the value of 0 then
103              a warning is issued (and indirect IO is performed).
104
105       ibs=BS if given must be the same as BS given to 'bs=' option.
106
107       if=IFILE
108              read from IFILE instead of stdin. If IFILE is '-' then stdin  is
109              read.  Starts  reading  at the beginning of IFILE unless SKIP is
110              given.
111
112       iflag=FLAGS
113              where FLAGS is a comma separated list of one or more flags  out‐
114              lined  below.   These  flags  are  associated with IFILE and are
115              ignored when IFILE is stdin.
116
117       obs=BS if given must be the same as BS given to 'bs=' option.
118
119       odir=0 | 1
120              when set to one opens block devices  (e.g.  /dev/sda)  with  the
121              O_DIRECT  flag. User memory buffers are aligned to the page size
122              when set. The default is 0 (i.e. the O_DIRECT flag is not used).
123              Has  no  effect  on sg, normal or raw files. If blk_sgio is also
124              set then both are honoured: block devices are  opened  with  the
125              O_DIRECT flag and SCSI commands are issued via the SG_IO ioctl.
126
127       of=OFILE
128              write to OFILE instead of stdout. If OFILE is '-' then writes to
129              stdout.  If OFILE is /dev/null then no actual  writes  are  per‐
130              formed.   If  OFILE  is '.' (period) then it is treated the same
131              way as /dev/null (this is a shorthand notation). If OFILE exists
132              then  it is _not_ truncated; it is overwritten from the start of
133              OFILE unless 'oflag=append' or SEEK is given.
134
135       oflag=FLAGS
136              where FLAGS is a comma separated list of one or more flags  out‐
137              lined  below.   These  flags  are  associated with OFILE and are
138              ignored when OFILE is /dev/null, '.' (period), or stdout.
139
140       retries=RETR
141              sometimes retries at the host are useful, for example when there
142              is  a  transport error. When RETR is greater than zero then SCSI
143              READs and WRITEs are retried on error, RETR times. Default value
144              is zero.
145
146       seek=SEEK
147              start  writing  SEEK  bs-sized  blocks  from the start of OFILE.
148              Default is block 0 (i.e. start of file).
149
150       skip=SKIP
151              start reading SKIP bs-sized blocks  from  the  start  of  IFILE.
152              Default is block 0 (i.e. start of file).
153
154       sync=0 | 1
155              when  1,  does  SYNCHRONIZE CACHE command on OFILE at the end of
156              the transfer. Only active when OFILE is a sg device file name or
157              a block device and 'blk_sgio=1' is given.
158
159       time=0 | 1
160              when  1,  times  transfer  and does throughput calculation, out‐
161              putting the results (to stderr) at completion. When 0  (default)
162              doesn't perform timing.
163
164       verbose=VERB
165              as  VERB  increases  so  does the amount of debug output sent to
166              stderr.  Default value is zero which yields the  minimum  amount
167              of debug output.  A value of 1 reports extra information that is
168              not repetitive. A value 2 reports cdbs and  responses  for  SCSI
169              commands  that  are  not  repetitive  (i.e.  other that READ and
170              WRITE). Error processing is not considered repetitive. Values of
171              3  and 4 yield output for all SCSI commands (and Unix read() and
172              write() calls) so there can be  a  lot  of  output.   This  only
173              occurs  for scsi generic (sg) devices and block devices when the
174              'blk_sgio=1' option is set.
175
176       --help outputs usage message and exits.
177
178       --version
179              outputs version number information and exits.
180

FLAGS

182       Here is a list of flags and their meanings:
183
184       append causes the O_APPEND flag to be added to the open of  OFILE.  For
185              regular  files this will lead to data appended to the end of any
186              existing data.  Cannot  be  used  together  with  the  seek=SEEK
187              option  as they conflict.  The default action of this utility is
188              to overwrite any existing data from the beginning  of  the  file
189              or, if SEEK is given, starting at block SEEK. Note that attempt‐
190              ing to 'append' to a device file (e.g.  a disk) will usually  be
191              ignored or may cause an error to be reported.
192
193       coe    continue  on error. Only active for sg devices and block devices
194              that have the 'sgio' flag set. 'iflag=coe oflag=coe' and 'coe=1'
195              are  equivalent.  Use  this flag twice (e.g. 'iflag=coe,coe') to
196              have the same action as the 'coe=2'. A medium, hardware or blank
197              check  error  while reading will re-read blocks prior to the bad
198              block, then try to recover the bad  block,  supplying  zeros  if
199              that fails, and finally reread the blocks after the bad block. A
200              medium, hardware or blank check error while writing is noted and
201              ignored.  The  recovery  of  the bad block when reading uses the
202              SCSI READ LONG command if 'coe' given twice or more  (also  with
203              the  command  line  option 'coe=2'). Further, the READ LONG will
204              set its CORRCT bit if 'coe' given thrice. SCSI disks  may  auto‐
205              matically try and remap faulty sectors (see the AWRE and ARRE in
206              the read write error recovery mode page (the sdparm  utility  to
207              access  and possibly change these attributes)). Errors occurring
208              on other files types will stop sg_dd.  Error messages  are  sent
209              to  stderr.  This flag is similar to 'conv=noerror' in the dd(1)
210              utility. See note about READ LONG below.
211
212       direct causes the O_DIRECT flag to be added to the open of IFILE and/or
213              OFILE.  This  flag  requires  some memory alignment on IO. Hence
214              user memory buffers are aligned to the page size. Has no  effect
215              on  sg, normal or raw files. If 'iflag=sgio' and/or 'oflag=sgio'
216              is also set then both are honoured:  block  devices  are  opened
217              with  the  O_DIRECT  flag  and  SCSI commands are issued via the
218              SG_IO ioctl.
219
220       dpo    set the DPO bit (disable page out) in SCSI READ and  WRITE  com‐
221              mands.  Not supported for 6 byte cdb variants of READ and WRITE.
222              Indicates that data is unlikely to be required to stay in device
223              (e.g.  disk)  cache.   May speed media copy and/or cause a media
224              copy to have less impact on other device users.
225
226       dsync  causes the O_SYNC flag to be added to the open of  IFILE  and/or
227              OFILE.  The  'd'  is  prepended  to  lower  confusion  with  the
228              'sync=0|1' option which has another action (i.e. a  synchronisa‐
229              tion to media at the end of the transfer).
230
231       excl   causes  the  O_EXCL flag to be added to the open of IFILE and/or
232              OFILE.
233
234       fua    causes the FUA (force unit access) bit to be set  in  SCSI  READ
235              and/or  WRITE  commands. This only has an effect with sg devices
236              or block devices that have the 'sgio' flag set. The 6 byte vari‐
237              ants  of the SCSI READ and WRITE commands do not support the FUA
238              bit.
239
240       sgio   causes block devices to be accessed via the SG_IO  ioctl  rather
241              than  standard  UNIX read() and write() commands. When the SG_IO
242              ioctl is used the SCSI READ and WRITE commands are used directly
243              to  move  data. sg devices always use the SG_IO ioctl. This flag
244              offers finer grain control compared to the  otherwise  identical
245              'blk_sgio=1' option.
246

RETIRED OPTIONS

248       Here are some retired options that are still present:
249
250       append=0 | 1
251              when set, equivalent to 'oflag=append'. When clear the action is
252              to overwrite the existing file  (if  it  exists);  this  is  the
253              default.  See the 'append' flag.
254
255       fua=0 | 1 | 2 | 3
256              force  unit  access  bit.  When  3, fua is set on both IFILE and
257              OFILE; when 2, fua is set on IFILE;,  when  1,  fua  is  set  on
258              OFILE;  when  0 (default), fua is cleared on both. See the 'fua'
259              flag.
260

NOTES

262       Block devices (e.g. /dev/sda and /dev/hda) can be given for IFILE.   If
263       neither  '-iflag=direct',  'iflag=sgio'  nor 'blk_sgio=1' is given then
264       normal block IO involving buffering and caching is performed.  If  only
265       '-iflag=direct'  is  given  then  the buffering and caching is bypassed
266       (this  is  applicable  to  both  SCSI  devices  and  ATA  disks).    If
267       'iflag=sgio'  or  'blk_sgio=1' is given then the SG_IO ioctl is used on
268       the given file causing SCSI commands to be sent to the device and  that
269       also bypasses most of the actions performed by the block layer (this is
270       only applicable to SCSI devices, not ATA disks). The same  applies  for
271       block devices given for OFILE.
272
273       COUNT,  SKIP,  SEEK, BPT and BS may include one of these multiplicative
274       suffixes: c C *1; w W *2; b B *512; k K KiB *1,024; KB *1,000; m M  MiB
275       *1,048,576;  MB  *1,000,000  .  This pattern continues for "G", "T" and
276       "P". The latter two suffixes can only be used for COUNT, SKIP and SEEK.
277       Also  a suffix of the form "x<n>" multiplies the leading number by <n>.
278       These multiplicative suffixes are  compatible  with  GNU's  dd  command
279       (since 2002) which claims compliance with SI and with IEC 60027-2.
280
281       Alternatively  numerical values can be given in hexadecimal preceded by
282       either "0x" or "0X" (or with a trailing "h" or "H"). When  hex  numbers
283       are given, multipliers cannot be used.
284
285       The  COUNT,  SKIP  and SEEK arguments can take 64 bit values (i.e. very
286       big numbers). Other values are limited to what can fit in a  signed  32
287       bit number.
288
289       Data  usually  gets  to  the user space in a 2 stage process: first the
290       SCSI adapter DMAs into kernel buffers and then  the  sg  driver  copies
291       this  data  into  user memory (write operations reverse this sequence).
292       This is called "indirect IO" and there is  a  'dio'  option  to  select
293       "direct  IO"  which  will  DMA  directly  into user memory. Due to some
294       issues "direct IO" is disabled in the sg driver and needs a  configura‐
295       tion  change  to  activate  it.  This  is typically done with 'echo 1 >
296       /proc/scsi/sg/allow_dio'.
297
298       All informative, warning and error output is sent  to  stderr  so  that
299       dd's output file can be stdout and remain unpolluted. If no options are
300       given, then the usage message is output and nothing else happens.
301
302       Even if READ LONG succeeds on a "bad" block when 'coe=2'  (or  'coe=3')
303       is given, the recovered data may not be useful. There are no guarantees
304       that the user data will appear "as is" in the first 512 bytes.
305
306       A raw device must be bound to a block device prior to using sg_dd.  See
307       raw(8)  for more information about binding raw devices. To be safe, the
308       sg device mapping to SCSI block devices should  be  checked  with  'cat
309       /proc/scsi/scsi', or sg_map before use.
310
311       Disk  partition information can often be found with fdisk(8) [the "-ul"
312       argument is useful in this respect].
313
314       For sg devices (and block devices when blk_sgio=1 is given) this  util‐
315       ity issues SCSI READ and WRITE (SBC) commands which are appropriate for
316       disks and reading from CD/DVD drives. Those commands are not  formatted
317       correctly for tape devices so sg_dd should not be used on tape devices.
318       If the largest block address of the requested transfer exceeds a 32 bit
319       block number (i.e 0xffff) then a warning is issued and the sg device is
320       accessed via SCSI READ(16) and WRITE(16) commands.
321
322       The  attributes  of  a  block  device  (partition)  are  ignored   when
323       'blk_sgio=1'  is used. Hence the whole device is read (rather than just
324       the second partition) by this invocation:
325
326          sg_dd if=/dev/sdb2 blk_sgio=1 of=t bs=512
327

EXAMPLES

329       Looks quite similar in usage to dd:
330
331          sg_dd if=/dev/sg0 of=t bs=512 count=1MB
332
333       This will copy 1 million 512 byte blocks  from  the  device  associated
334       with  /dev/sg0  (which should have 512 byte blocks) to a file called t.
335       Assuming /dev/sda and /dev/sg0 are the same device then  the  above  is
336       equivalent to:
337
338          dd if=/dev/sda iflag=direct of=t bs=512 count=1000000
339
340       although dd's speed may improve if bs was larger and count was suitably
341       reduced. The use of the 'iflag=direct' option  bypasses  the  buffering
342       and caching that is usually done on a block device.
343
344       Using a raw device to do something similar on a ATA disk:
345
346          raw /dev/raw/raw1 /dev/hda
347          sg_dd if=/dev/raw/raw1 of=t bs=512 count=1MB
348
349       To copy a SCSI disk partition to an ATA disk partition:
350
351          raw /dev/raw/raw2 /dev/hda3
352          sg_dd if=/dev/sg0 skip=10123456 of=/dev/raw/raw2 bs=512
353
354       This  assumes  a valid partition is found on the SCSI disk at the given
355       skip block address (past the 5 GB point of that disk) and that the par‐
356       tition  goes to the end of the SCSI disk. An explicit count is probably
357       a safer option. The partition is copied to /dev/hda3 which is an offset
358       into  the  ATA  disk  /dev/hda  .  The exact number of blocks read from
359       /dev/sg0 are written to /dev/hda (i.e. no padding).
360
361       To time a streaming read of the first 1 GB (2 ** 30 bytes)  on  a  disk
362       this utility could be used:
363
364          sg_dd if=/dev/sg0 of=/dev/null bs=512 count=2m time=1
365
366       On  completion this will output a line like: "time to transfer data was
367       18.779506 secs, 57.18 MB/sec". The "MB/sec" in this case  is  1,000,000
368       bytes per second.
369

SIGNALS

371       The signal handling has been borrowed from dd: SIGINT, SIGQUIT and SIG‐
372       PIPE output the number of remaining blocks to be  transferred  and  the
373       records  in + out counts; then they have their default action.  SIGUSR1
374       causes the same information to be output yet the copy  continues.   All
375       output caused by signals is sent to stderr.
376

EXIT STATUS

378       The  exit status of sg_dd is 0 when it is successful. Otherwise see the
379       sg3_utils(8) man page. Since this utility works at a higher level  than
380       individual  commands, and there are 'coe' and 'retries' flags, individ‐
381       ual SCSI command failures do not necessary cause the process to exit.
382

AUTHORS

384       Written by Doug Gilbert and Peter Allworth.
385

REPORTING BUGS

387       Report bugs to <dgilbert at interlog dot com>.
388
390       Copyright © 2000-2007 Douglas Gilbert
391       This software is distributed under the GPL version 2. There is NO  war‐
392       ranty;  not  even  for MERCHANTABILITY or FITNESS FOR A PARTICULAR PUR‐
393       POSE.
394

SEE ALSO

396       There     is     a     web      page      discussing      sg_dd      at
397       http://www.torque.net/sg/sg_dd.html
398
399       A  POSIX  threads  version  of  this  utility  called  sgp_dd is in the
400       sg3_utils package. Another version from that package is  called  sgm_dd
401       and it uses memory mapped IO to speed transfers from sg devices.
402
403       The lmbench package contains lmdd which is also interesting. For moving
404       data to and from tapes  see  dt  which  is  found  at  http://www.scsi
405       faq.org/RMiller_Tools/index.html
406
407       To change mode parameters that effect a SCSI device's caching and error
408       recovery see sdparm(sdparm)
409
410       See also raw(8), dd(1), ddrescue(GNU)
411
412
413
414sg3_utils-1.23                   January 2007                         SG_DD(8)
Impressum