1SG_DD(8) SG3_UTILS SG_DD(8)
2
3
4
6 sg_dd - copy data to and from files and devices, especially SCSI
7 devices
8
10 sg_dd [bs=BS] [conv=CONV] [count=COUNT] [ibs=BS] [if=IFILE]
11 [iflag=FLAGS] [obs=BS] [of=OFILE] [oflag=FLAGS] [seek=SEEK] [skip=SKIP]
12 [--help] [--verbose] [--version]
13
14 [blk_sgio={0|1}] [bpt=BPT] [cdbsz={6|10|12|16}] [coe={0|1|2|3}]
15 [coe_limit=CL] [dio={0|1}] [odir={0|1}] [of2=OFILE2] [retries=RETR]
16 [sync={0|1}] [time={0|1}] [verbose=VERB] [--dry-run] [-V] [--verify]
17
19 Copy data to and from any files. Specialized for "files" that are Linux
20 SCSI generic (sg) devices, raw devices or other devices that support
21 the SG_IO ioctl (which are only found in the lk 2.6 series). Similar
22 syntax and semantics to dd(1) command.
23
24 The first group in the synopsis above are "standard" Unix dd(1) oper‐
25 ands. The second group are extra options added by this utility. Both
26 groups are defined below.
27
28 When the --verify option is given, then the read side is the same but
29 the on the write side, the WRITE SCSI command is replaced by the VERIFY
30 SCSI command. If any VERIFY commands yields a sense key of MISCOMPARE
31 then the verify operation will stop. The --verify option can only be
32 used when OFILE is either a sg device or a block device with oflag=sgio
33 also given. When the --verify option is used, this utility works in a
34 similar fashion to the Unix cmp(1) command.
35
36 This utility is only supported on Linux whereas most other utilities in
37 the sg3_utils package have been ported to other operating systems. A
38 utility called "ddpt" has similar syntax and functionality to sg_dd.
39 ddpt drops some Linux specific features while adding some other generic
40 features. This allows ddpt to be ported to other operating systems.
41
43 blk_sgio={0|1}
44 when set to 0, block devices (e.g. /dev/sda) are treated like
45 normal files (i.e. read(2) and write(2) are used for IO). When
46 set to 1, block devices are assumed to accept the SG_IO ioctl
47 and SCSI commands are issued for IO. This is only supported for
48 2.6 series kernels. Note that ATAPI devices (e.g. cd/dvd play‐
49 ers) use the SCSI command set but ATA disks do not (unless there
50 is a protocol conversion as often occurs in the USB mass storage
51 class). If the input or output device is a block device parti‐
52 tion (e.g. /dev/sda3) then setting this option causes the parti‐
53 tion information to be ignored (since access is directly to the
54 underlying device). Default is 0. See the 'sgio' flag.
55
56 bpt=BPT
57 each IO transaction will be made using BPT blocks (or less if
58 near the end of the copy). Default is 128 for logical block
59 sizes less that 2048 bytes, otherwise the default is 32. So for
60 bs=512 the reads and writes will each convey 64 KiB of data by
61 default (less if near the end of the transfer or memory restric‐
62 tions). When cd/dvd drives are accessed, the logical block size
63 is typically 2048 bytes and bpt defaults to 32 which again
64 implies 64 KiB transfers. The block layer when the blk_sgio=1
65 option is used has relatively low upper limits for transfer
66 sizes (compared to sg device nodes, see
67 /sys/block/<dev_name>/queue/max_sectors_kb ).
68
69 bs=BS where BS must be the logical block size of the physical device
70 (if either the input or output files are accessed via SCSI com‐
71 mands). Note that this differs from dd(1) which permits BS to be
72 an integral multiple. Default is 512 which is usually correct
73 for disks but incorrect for cdroms (which normally have 2048
74 byte blocks). For this utility the maximum size of each individ‐
75 ual IO operation is BS * BPT bytes.
76
77 cdbsz={6|10|12|16}
78 size of SCSI READ and/or WRITE commands issued on sg device
79 names (or block devices when 'iflag=sgio' and/or 'oflag=sgio' is
80 given). Default is 10 byte SCSI command blocks (unless calcula‐
81 tions indicate that a 4 byte block number may be exceeded or BPT
82 is greater than 16 bits (65535), in which case it defaults to 16
83 byte SCSI commands).
84
85 coe={0|1|2|3}
86 set to 1 or more for continue on error. Only applies to errors
87 on sg devices or block devices with the 'sgio' flag set. Thus
88 errors on other files will stop sg_dd. Default is 0 which
89 implies stop on any error. See the 'coe' flag for more informa‐
90 tion.
91
92 coe_limit=CL
93 where CL is the maximum number of consecutive bad blocks stepped
94 over (due to "coe>0") on reads before the copy terminates. This
95 only applies when IFILE is accessed via the SG_IO ioctl. The
96 default is 0 which is interpreted as no limit. This option is
97 meant to stop the copy soon after unrecorded media is detected
98 while still offering "continue on error" capability.
99
100 conv=sparse
101 see the CONVERSIONS section below.
102
103 count=COUNT
104 copy COUNT blocks from IFILE to OFILE. Default is the minimum
105 (of IFILE and OFILE) number of blocks that sg devices report
106 from SCSI READ CAPACITY commands or that block devices (or their
107 partitions) report. Normal files are not probed for their size.
108 If skip=SKIP or seek=SEEK are given and the count is derived
109 (i.e. not explicitly given) then the derived count is scaled
110 back so that the copy will not overrun the device. If the file
111 name is a block device partition and COUNT is not given then the
112 size of the partition rather than the size of the whole device
113 is used. If COUNT is not given (or count=-1) and cannot be
114 derived then an error message is issued and no copy takes place.
115
116 dio={0|1}
117 default is 0 which selects indirect (buffered) IO on sg devices.
118 Value of 1 attempts direct IO which, if not available, falls
119 back to indirect IO and notes this at completion. If direct IO
120 is selected and /proc/scsi/sg/allow_dio has the value of 0 then
121 a warning is issued (and indirect IO is performed). For finer
122 grain control use 'iflag=dio' or 'oflag=dio'.
123
124 ibs=BS if given must be the same as BS given to 'bs=' option.
125
126 if=IFILE
127 read from IFILE instead of stdin. If IFILE is '-' then stdin is
128 read. Starts reading at the beginning of IFILE unless SKIP is
129 given.
130
131 iflag=FLAGS
132 where FLAGS is a comma separated list of one or more flags out‐
133 lined below. These flags are associated with IFILE and are
134 ignored when IFILE is stdin.
135
136 obs=BS if given must be the same as BS given to 'bs=' option.
137
138 odir={0|1}
139 when set to one opens block devices (e.g. /dev/sda) with the
140 O_DIRECT flag. User memory buffers are aligned to the page size
141 when set. The default is 0 (i.e. the O_DIRECT flag is not used).
142 Has no effect on sg, normal or raw files. If blk_sgio is also
143 set then both are honoured: block devices are opened with the
144 O_DIRECT flag and SCSI commands are issued via the SG_IO ioctl.
145
146 of=OFILE
147 write to OFILE instead of stdout. If OFILE is '-' then writes to
148 stdout. If OFILE is /dev/null then no actual writes are per‐
149 formed. If OFILE is '.' (period) then it is treated the same
150 way as /dev/null (this is a shorthand notation). If OFILE exists
151 then it is _not_ truncated; it is overwritten from the start of
152 OFILE unless 'oflag=append' or SEEK is given.
153
154 of2=OFILE2
155 write output to OFILE2. The default action is not to do this
156 additional write (i.e. when this option is not given). OFILE2 is
157 assumed to be a normal file or a fifo (i.e. a named pipe).
158 OFILE2 is opened for writing, created if necessary, and closed
159 at the end of the transfer. If OFILE2 is a fifo (named pipe)
160 then some other command should be consuming that data (e.g.
161 'md5sum OFILE2'), otherwise this utility will block.
162
163 oflag=FLAGS
164 where FLAGS is a comma separated list of one or more flags out‐
165 lined below. These flags are associated with OFILE and are
166 ignored when OFILE is /dev/null, '.' (period), or stdout.
167
168 retries=RETR
169 sometimes retries at the host are useful, for example when there
170 is a transport error. When RETR is greater than zero then SCSI
171 READs and WRITEs are retried on error, RETR times. Default value
172 is zero.
173
174 seek=SEEK
175 start writing SEEK bs-sized blocks from the start of OFILE.
176 Default is block 0 (i.e. start of file).
177
178 skip=SKIP
179 start reading SKIP bs-sized blocks from the start of IFILE.
180 Default is block 0 (i.e. start of file).
181
182 sync={0|1}
183 when 1, does SYNCHRONIZE CACHE command on OFILE at the end of
184 the transfer. Only active when OFILE is a sg device file name or
185 a block device and 'blk_sgio=1' is given.
186
187 time={0|1}
188 when 1, times transfer and does throughput calculation, out‐
189 putting the results (to stderr) at completion. When 0 (default)
190 doesn't perform timing.
191
192 verbose=VERB
193 as VERB increases so does the amount of debug output sent to
194 stderr. Default value is zero which yields the minimum amount
195 of debug output. A value of 1 reports extra information that is
196 not repetitive. A value 2 reports cdbs and responses for SCSI
197 commands that are not repetitive (i.e. other that READ and
198 WRITE). Error processing is not considered repetitive. Values of
199 3 and 4 yield output for all SCSI commands (and Unix read() and
200 write() calls) so there can be a lot of output. This only
201 occurs for scsi generic (sg) devices and block devices when the
202 'blk_sgio=1' option is set.
203
204 -d, --dry-run
205 does all the command line parsing and preparation but bypasses
206 the actual copy or read. That preparation may include opening
207 IFILE or OFILE to determine their lengths. This option may be
208 useful for testing the syntax of complex command line invoca‐
209 tions in advance of executing them.
210
211 -h, --help
212 outputs usage message and exits.
213
214 -v, --verbose
215 when used once, this is equivalent to verbose=1. When used twice
216 (e.g. "-vv") this is equivalent to verbose=2, etc.
217
218 -x, --verify
219 do a verify operation (like Unix command cmp(1)) rather than a
220 copy. Cannot be used with "oflag=sparse". of=OFILE must be given
221 and OFILE must be an sg device or a block device with
222 "oflag=sgio" also given. Uses the SCSI VERIFY command with the
223 BYTCHK field set to 1. The VERIFY command is used instead of
224 WRITE when this option is given. There is no VERIFY(6) command.
225
226 -V, --version
227 outputs version number information and exits.
228
230 One or more conversions can be given to the "conv=" option. If more
231 than one is given, they should be comma separated. sg_dd does not per‐
232 form the traditional dd conversions (e.g. ASCII to EBCDIC). Recently
233 added conversions overlap somewhat with the flags so some conversions
234 are now supported by sg_dd.
235
236 noerror
237 this conversion is very close to "iflag=coe" and is treated as
238 such. See the "coe" flag. Note that an error on OFILE will stop
239 the copy.
240
241 notrunc
242 this conversion is accepted for compatibility with dd and
243 ignored since the default action of this utility is not to trun‐
244 cate OFILE.
245
246 null has no affect, just a placeholder.
247
248 sparse FreeBSD supports "conv=sparse" so the same syntax is supported
249 in sg_dd. See "sparse" in the FLAGS sections for more informa‐
250 tion.
251
252 sync is ignored by sg_dd. With dd it means supply zero fill (rather
253 than skip) and is typically used like this "conv=noerror,sync"
254 to have the same functionality as sg_dd's "iflag=coe".
255
257 Here is a list of flags and their meanings:
258
259 append causes the O_APPEND flag to be added to the open of OFILE. For
260 regular files this will lead to data appended to the end of any
261 existing data. Cannot be used together with the seek=SEEK
262 option as they conflict. The default action of this utility is
263 to overwrite any existing data from the beginning of the file
264 or, if SEEK is given, starting at block SEEK. Note that attempt‐
265 ing to 'append' to a device file (e.g. a disk) will usually be
266 ignored or may cause an error to be reported.
267
268 coe continue on error. Only active for sg devices and block devices
269 that have the 'sgio' flag set. 'iflag=coe oflag=coe' and 'coe=1'
270 are equivalent. Use this flag twice (e.g. 'iflag=coe,coe') to
271 have the same action as the 'coe=2'. A medium, hardware or blank
272 check error while reading will re-read blocks prior to the bad
273 block, then try to recover the bad block, supplying zeros if
274 that fails, and finally reread the blocks after the bad block. A
275 medium, hardware or blank check error while writing is noted and
276 ignored. The recovery of the bad block when reading uses the
277 SCSI READ LONG command if 'coe' given twice or more (also with
278 the command line option 'coe=2'). Further, the READ LONG will
279 set its CORRCT bit if 'coe' given thrice. SCSI disks may auto‐
280 matically try and remap faulty sectors (see the AWRE and ARRE in
281 the read write error recovery mode page (the sdparm utility can
282 access and possibly change these attributes)). Errors occurring
283 on other files types will stop sg_dd. Error messages are sent
284 to stderr. This flag is similar
285 o 'conv=noerror,sync' in the dd(1) utility. See note about READ
286 LONG below.
287
288 dio request the sg device node associated with this flag does direct
289 IO. If direct IO is not available, falls back to indirect IO
290 and notes this at completion. If direct IO is selected and
291 /proc/scsi/sg/allow_dio has the value of 0 then a warning is
292 issued (and indirect IO is performed).
293
294 direct causes the O_DIRECT flag to be added to the open of IFILE and/or
295 OFILE. This flag requires some memory alignment on IO. Hence
296 user memory buffers are aligned to the page size. Has no effect
297 on sg, normal or raw files. If 'iflag=sgio' and/or 'oflag=sgio'
298 is also set then both are honoured: block devices are opened
299 with the O_DIRECT flag and SCSI commands are issued via the
300 SG_IO ioctl.
301
302 dpo set the DPO bit (disable page out) in SCSI READ and WRITE com‐
303 mands. Not supported for 6 byte cdb variants of READ and WRITE.
304 Indicates that data is unlikely to be required to stay in device
305 (e.g. disk) cache. May speed media copy and/or cause a media
306 copy to have less impact on other device users.
307
308 dsync causes the O_SYNC flag to be added to the open of IFILE and/or
309 OFILE. The 'd' is prepended to lower confusion with the
310 'sync=0|1' option which has another action (i.e. a synchronisa‐
311 tion to media at the end of the transfer).
312
313 excl causes the O_EXCL flag to be added to the open of IFILE and/or
314 OFILE.
315
316 flock after opening the associated file (i.e. IFILE and/or OFILE) an
317 attempt is made to get an advisory exclusive lock with the
318 flock() system call. The flock arguments are "FLOCK_EX |
319 FLOCK_NB" which will cause the lock to be taken if available
320 else a "temporarily unavailable" error is generated. An exit
321 status of 90 is produced in the latter case and no copy is done.
322
323 fua causes the FUA (force unit access) bit to be set in SCSI READ
324 and/or WRITE commands. This only has an effect with sg devices
325 or block devices that have the 'sgio' flag set. The 6 byte vari‐
326 ants of the SCSI READ and WRITE commands do not support the FUA
327 bit.
328
329 nocache
330 use posix_fadvise() to advise corresponding file there is no
331 need to fill the file buffer with recently read or written
332 blocks.
333
334 null has no affect, just a placeholder.
335
336 sgio causes block devices to be accessed via the SG_IO ioctl rather
337 than standard UNIX read() and write() commands. When the SG_IO
338 ioctl is used the SCSI READ and WRITE commands are used directly
339 to move data. sg devices always use the SG_IO ioctl. This flag
340 offers finer grain control compared to the otherwise identical
341 'blk_sgio=1' option.
342
343 sparse after each BS * BPT byte segment is read from the input, it is
344 checked for being all zeros. If so, nothing is written to the
345 output file unless this is the last segment of the transfer.
346 This flag is only active with the oflag option. It cannot be
347 used when the output is not seekable (e.g. stdout). It is
348 ignored if the output file is /dev/null . Note that this util‐
349 ity does not remove the OFILE prior to starting to write to it.
350 Hence it may be advantageous to manually remove the OFILE if it
351 is large prior to using oflag=sparse. The last segment is always
352 written so regular files will show the same length and so pro‐
353 grams like md5sum and sha1sum will generate the same value
354 regardless of whether oflag=sparse is given or not. This option
355 may be used when the OFILE is a raw device but is probably only
356 useful if the device is known to contain zeros (e.g. a SCSI disk
357 after a FORMAT command).
358
360 Here are some retired options that are still present:
361
362 append=0 | 1
363 when set, equivalent to 'oflag=append'. When clear the action is
364 to overwrite the existing file (if it exists); this is the
365 default. See the 'append' flag.
366
367 fua=0 | 1 | 2 | 3
368 force unit access bit. When 3, fua is set on both IFILE and
369 OFILE; when 2, fua is set on IFILE;, when 1, fua is set on
370 OFILE; when 0 (default), fua is cleared on both. See the 'fua'
371 flag.
372
374 Block devices (e.g. /dev/sda and /dev/hda) can be given for IFILE. If
375 neither '-iflag=direct', 'iflag=sgio' nor 'blk_sgio=1' is given then
376 normal block IO involving buffering and caching is performed. If only
377 '-iflag=direct' is given then the buffering and caching is bypassed
378 (this is applicable to both SCSI devices and ATA disks). If
379 'iflag=sgio' or 'blk_sgio=1' is given then the SG_IO ioctl is used on
380 the given file causing SCSI commands to be sent to the device and that
381 also bypasses most of the actions performed by the block layer (this is
382 only applicable to SCSI devices, not ATA disks). The same applies for
383 block devices given for OFILE.
384
385 Various numeric arguments (e.g. SKIP) may include multiplicative suf‐
386 fixes or be given in hexadecimal. See the "NUMERIC ARGUMENTS" section
387 in the sg3_utils(8) man page.
388
389 The COUNT, SKIP and SEEK arguments can take 64 bit values (i.e. very
390 big numbers). Other values are limited to what can fit in a signed 32
391 bit number.
392
393 Data usually gets to the user space in a 2 stage process: first the
394 SCSI adapter DMAs into kernel buffers and then the sg driver copies
395 this data into user memory (write operations reverse this sequence).
396 This is called "indirect IO" and there is a 'dio' option to select
397 "direct IO" which will DMA directly into user memory. Due to some
398 issues "direct IO" is disabled in the sg driver and needs a configura‐
399 tion change to activate it. This is typically done with 'echo 1 >
400 /proc/scsi/sg/allow_dio'.
401
402 All informative, warning and error output is sent to stderr so that
403 dd's output file can be stdout and remain unpolluted. If no options are
404 given, then the usage message is output and nothing else happens.
405
406 Even if READ LONG succeeds on a "bad" block when 'coe=2' (or 'coe=3')
407 is given, the recovered data may not be useful. There are no guarantees
408 that the user data will appear "as is" in the first 512 bytes.
409
410 A raw device must be bound to a block device prior to using sg_dd. See
411 raw(8) for more information about binding raw devices. To be safe, the
412 sg device mapping to SCSI block devices should be checked with 'cat
413 /proc/scsi/scsi', or sg_map before use.
414
415 Disk partition information can often be found with fdisk(8) [the "-ul"
416 argument is useful in this respect].
417
418 For sg devices (and block devices when blk_sgio=1 is given) this util‐
419 ity issues SCSI READ and WRITE (SBC) commands which are appropriate for
420 disks and reading from CD/DVD/HD-DVD/BD drives. Those commands are not
421 formatted correctly for tape devices so sg_dd should not be used on
422 tape devices. If the largest block address of the requested transfer
423 exceeds a 32 bit block number (i.e 0xffff) then a warning is issued and
424 the sg device is accessed via SCSI READ(16) and WRITE(16) commands.
425
426 The attributes of a block device (partition) are ignored when
427 'blk_sgio=1' is used. Hence the whole device is read (rather than just
428 the second partition) by this invocation:
429
430 sg_dd if=/dev/sdb2 blk_sgio=1 of=t bs=512
431
433 Looks quite similar in usage to dd:
434
435 sg_dd if=/dev/sg0 of=t bs=512 count=1MB
436
437 This will copy 1 million 512 byte blocks from the device associated
438 with /dev/sg0 (which should have 512 byte blocks) to a file called t.
439 Assuming /dev/sda and /dev/sg0 are the same device then the above is
440 equivalent to:
441
442 dd if=/dev/sda iflag=direct of=t bs=512 count=1000000
443
444 although dd's speed may improve if bs was larger and count was suitably
445 reduced. The use of the 'iflag=direct' option bypasses the buffering
446 and caching that is usually done on a block device.
447
448 Using a raw device to do something similar on a ATA disk:
449
450 raw /dev/raw/raw1 /dev/hda
451 sg_dd if=/dev/raw/raw1 of=t bs=512 count=1MB
452
453 To copy a SCSI disk partition to an ATA disk partition:
454
455 raw /dev/raw/raw2 /dev/hda3
456 sg_dd if=/dev/sg0 skip=10123456 of=/dev/raw/raw2 bs=512
457
458 This assumes a valid partition is found on the SCSI disk at the given
459 skip block address (past the 5 GB point of that disk) and that the par‐
460 tition goes to the end of the SCSI disk. An explicit count is probably
461 a safer option. The partition is copied to /dev/hda3 which is an offset
462 into the ATA disk /dev/hda . The exact number of blocks read from
463 /dev/sg0 are written to /dev/hda (i.e. no padding).
464
465 To time a streaming read of the first 1 GB (2 ** 30 bytes) on a disk
466 this utility could be used:
467
468 sg_dd if=/dev/sg0 of=/dev/null bs=512 count=2m time=1
469
470 On completion this will output a line like: "time to transfer data was
471 18.779506 secs, 57.18 MB/sec". The "MB/sec" in this case is 1,000,000
472 bytes per second.
473
474 The 'of2=' option can be used to copy data and take a md5sum of it
475 without needing to re-read the data:
476
477 mkfifo fif
478 md5sum fif &
479 sg_dd if=/dev/sg3 iflag=coe of=sg3.img oflag=sparse of2=fif bs=512
480
481 This will image /dev/sg3 (e.g. an unmounted disk) and place the con‐
482 tents in the (sparse) file sg3.img . Without re-reading the data it
483 will also perform a md5sum calculation on the image.
484
486 The signal handling has been borrowed from dd: SIGINT, SIGQUIT and SIG‐
487 PIPE output the number of remaining blocks to be transferred and the
488 records in + out counts; then they have their default action. SIGUSR1
489 causes the same information to be output yet the copy continues. All
490 output caused by signals is sent to stderr.
491
493 The exit status of sg_dd is 0 when it is successful. Otherwise see the
494 sg3_utils(8) man page. Since this utility works at a higher level than
495 individual commands, and there are 'coe' and 'retries' flags, individ‐
496 ual SCSI command failures do not necessary cause the process to exit.
497
498 An additional exit status of 90 is generated if the flock flag is given
499 and some other process holds the advisory exclusive lock.
500
502 Written by Douglas Gilbert and Peter Allworth.
503
505 Report bugs to <dgilbert at interlog dot com>.
506
508 Copyright © 2000-2020 Douglas Gilbert
509 This software is distributed under the GPL version 2. There is NO war‐
510 ranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PUR‐
511 POSE.
512
514 cmp(1)
515
516 There is a web page discussing sg_dd at
517 http://sg.danny.cz/sg/sg_dd.html
518
519 A POSIX threads version of this utility called sgp_dd is in the
520 sg3_utils package. Another version from that package is called sgm_dd
521 and it uses memory mapped IO to speed transfers from sg devices.
522
523 The lmbench package contains lmdd which is also interesting. For moving
524 data to and from tapes see dt which is found at http://www.scsi‐
525 faq.org/RMiller_Tools/index.html
526
527 To change mode parameters that effect a SCSI device's caching and error
528 recovery see sdparm(sdparm)
529
530 To verify the data on the media or to verify it against some other copy
531 of the data see sg_verify(sg3_utils)
532
533 See also raw(8), dd(1), ddrescue(GNU), ddpt
534
535
536
537sg3_utils-1.45 January 2020 SG_DD(8)