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