{
    "mode": "man",
    "parameter": "sg_write_long",
    "section": "8",
    "url": "https://www.chedong.com/phpMan.php/man/sg_write_long/8/json",
    "generated": "2026-10-10T10:16:16Z",
    "synopsis": "sgwritelong  [--16]  [--cordis]  [--help]  [--in=IF]  [--lba=LBA]  [--pblock]  [--verbose]\n[--version] [--wruncor] [--xferlen=BTL] DEVICE",
    "sections": {
        "NAME": {
            "content": "sgwritelong - send SCSI WRITE LONG command\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "sgwritelong  [--16]  [--cordis]  [--help]  [--in=IF]  [--lba=LBA]  [--pblock]  [--verbose]\n[--version] [--wruncor] [--xferlen=BTL] DEVICE\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Send the SCSI WRITE LONG (10 or 16 byte) command to DEVICE. The buffer to be written  to  the\nDEVICE  is  filled with 0xff bytes or read from the IF file. This buffer includes the logical\ndata (e.g. 512 bytes) and the ECC bytes.\n\nThis utility can be used to generate a MEDIUM ERROR at a specific logical block address. This\ncan be useful for testing error handling. Prior to such a test, the sgdd  utility  could  be\nused  to copy the original contents of the logical block address to some safe location. After\nthe test the sgdd utility could be used to write back the original contents of  the  logical\nblock  address.  An  alternate  strategy  would be to read the \"long\" contents of the logical\nblock address with sgreadlong utility prior to testing and restore it with this utility af‐\nter testing.\n\nTake care: If recoverable errors are being injected (e.g. only one or a few bits  changed  so\nthat  the ECC is able to correct the data) then care should be taken with the settings in the\n\"read write error recovery\" mode page.  Specifically if the ARRE (for reads) and/or AWRE (for\nwrites) are set then recovered errors will cause the lba to be reassigned (and the old  loca‐\ntion to be added to the grown defect list (PLIST)). This is not easily reversed and uses (one\nof  the  finite  number  of)  the spare sectors set aside for this purpose. If in doubt it is\nprobably safest to clear the ARRE and AWRE bits. These bits can be checked and modified  with\nthe  sdparm  utility.   For example: \"sdparm -c AWRE,ARRE /dev/sda\" will clear the bits until\nthe disk is power cycled.\n\nIn SBC-4 revision 7 all uses of SCSI WRITE LONG (10 and 16 byte) commands were made  obsolete\napart  from  the  case in which the WRUNCOR bit is set.  The SCSI READ LONG (10 and 16 byte)\ncommands were made obsolete in the same revision.\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "Arguments to long options are mandatory for short options as well.\n",
            "subsections": [
                {
                    "name": "-S --16",
                    "content": "send a SCSI WRITE LONG (16) command to DEVICE. The default action (in the  absence  of\nthis option) is to send a SCSI WRITE LONG (10) command.\n",
                    "flag": "-S",
                    "long": "--16"
                },
                {
                    "name": "-c --cor_dis",
                    "content": "sets  the  correction disabled (i.e 'CORDIS') bit. This inhibits various other mecha‐\nnisms such as automatic block reallocation, error recovery and  various  informational\nexception conditions being triggered.  This bit is relatively new in SBC-3 .\n",
                    "flag": "-c",
                    "long": "--cor_dis"
                },
                {
                    "name": "-h --help",
                    "content": "output the usage message then exit.\n",
                    "flag": "-h",
                    "long": "--help"
                },
                {
                    "name": "-i --in",
                    "content": "read  data  (binary) from file named IF and use it for the SCSI WRITE LONG command. If\nIF is \"-\" then stdin is read. If this option is not given then 0xff bytes are used  as\nfill.\n",
                    "flag": "-i",
                    "long": "--in"
                },
                {
                    "name": "-l --lba",
                    "content": "where  LBA is the logical block address of the sector to overwrite.  Defaults to lba 0\nwhich is a dangerous block to overwrite on a disk that is in use.  Assumed  to  be  in\ndecimal unless prefixed with '0x' or has a trailing 'h'. If LBA is larger than can fit\nin 32 bits then the --16 option should be used.\n",
                    "flag": "-l",
                    "long": "--lba"
                },
                {
                    "name": "-p --pblock",
                    "content": "sets  the  physical  block  (i.e 'PBLOCK') bit. This instructs DEVICE to use the given\ndata (unless --wruncor is also given) to write to the  physical  block  specified  by\nLBA.  The  default  action is to write to the logical block corresponding to the given\nlba.  This bit is relatively new in SBC-3 .\n",
                    "flag": "-p",
                    "long": "--pblock"
                },
                {
                    "name": "-v --verbose",
                    "content": "increase the degree of verbosity (debug messages).\n",
                    "flag": "-v",
                    "long": "--verbose"
                },
                {
                    "name": "-V --version",
                    "content": "output version string then exit.\n",
                    "flag": "-V",
                    "long": "--version"
                },
                {
                    "name": "-w --wr_uncor",
                    "content": "sets the \"write uncorrected\" (i.e 'WRUNCOR') bit. This instructs the DEVICE  to  flag\nthe  given  lba  (or the physical block that contains it if --pblock is also given) as\nhaving an unrecoverable error associated with it. Note: no data is transferred to  DE‐\nVICE,  other  than  the command (i.e. the cdb). In the absence of this option, the de‐\nfault action is to use the provided data or 0xff bytes (--xferlen=BTL in length)  and\nwrite it to DEVICE.  This bit is relatively new in SBC-3 .\n",
                    "flag": "-w",
                    "long": "--wr_uncor"
                },
                {
                    "name": "-x --xfer_len",
                    "content": "where BTL is the byte transfer length (default to 520). If the given value (or the de‐\nfault)  does  not match the \"long\" block size of the device, nothing is written to DE‐\nVICE and the appropriate xferlen value may be deduced from the error  response  which\nis printed (to stderr).\n",
                    "flag": "-x",
                    "long": "--xfer_len"
                }
            ]
        },
        "NOTES": {
            "content": "Various numeric arguments (e.g. LBA) may include multiplicative suffixes or be given in hexa‐\ndecimal. See the \"NUMERIC ARGUMENTS\" section in the sg3utils(8) man page.\n\nThe  10  byte  SCSI WRITE LONG command limits the logical block address to a 32 bit quantity.\nFor larger LBAs use the --16 option for the SCSI WRITE LONG (16) command.\n",
            "subsections": []
        },
        "EXAMPLES": {
            "content": "This section outlines setting up a block with corrupted data, checking the  error  condition,\nthen restoring useful contents to that sector.\n\nFirst, if the data in a sector is important, save it with the sgreadlong utility:\n\nsgreadlong --lba=0x1234 --out=0x12341.img -x BTL /dev/sda\n\nThis  utility  may  need  to  be executed several time in order to determine what the correct\nvalue for BTL is.  Next use this utility to \"corrupt\" that sector. That might be done with:\n\nsgwritelong --lba=0x1234 -x BTL /dev/sda\n\nThis will write a sector (and ECC data) of 0xff bytes. Some disks may reject this  (at  least\none of the author's does). Another approach is to copy the 0x12341.img file (to 0x12342.img\nin this example) and change some values with a hex editor. Then write the changed image with:\n\nsgwritelong --lba=0x1234 --in=0x12342.img -x BTL /dev/sda\n\nYet another approach is to use the --wruncor option, if supported:\n\nsgwritelong --lba=0x1234 --wruncor /dev/sda\n\nNext we use the sgdd utility to check that the sector is corrupted. Here is an example:\n\nsgdd if=/dev/sda blksgio=1 skip=0x1234 of=. bs=512 count=1 verbose=4\n\nNotice  that  the  \"blksgio=1\" option is given. This is to make sure that the sector is read\n(and no others) and the error is fully reported.  The \"blksgio=1\" option  causes  the  SGIO\nioctl to be used by sgdd rather than the block subsystem.\n\nFinally  we  should  restore  sector  0x1234 to a non-corrupted state. A sector full of zeros\ncould be written with:\n\nsgdd if=/dev/zero of=/dev/sda blksgio=1 seek=0x1234 bs=512 count=1\n\nThis will result in a sector (block) with 512 bytes of 0x0 without a MEDIUM ERROR  since  the\nECC  and associated data will be regenerated and thus well formed. The 'blksgio=1' option is\neven more important in this case as it may stop the block subsystem doing a read before write\n(since the read will most likely fail).  Another approach is to write back the original  con‐\ntents:\n\nsgwritelong --lba=0x1234 --in=0x12341.img -x BTL /dev/sda\n",
            "subsections": []
        },
        "EXIT STATUS": {
            "content": "The  exit  status of sgwritelong is 0 when it is successful. Otherwise see the sg3utils(8)\nman page.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Written by Saeed Bishara. Further work by Douglas Gilbert.\n",
            "subsections": []
        },
        "REPORTING BUGS": {
            "content": "Report bugs to <dgilbert at interlog dot com>.\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright © 2004-2016 Douglas Gilbert\nThis software is distributed under the GPL version 2. There is NO warranty; not even for MER‐\nCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "",
            "subsections": [
                {
                    "name": "sg_read_long, sg_dd (both in sg3_utils), sdparm(sdparm)",
                    "content": "sg3utils-1.42                              January 2016                            SGWRITELONG(8)"
                }
            ]
        }
    },
    "summary": "sgwritelong - send SCSI WRITE LONG command",
    "flags": [
        {
            "flag": "-S",
            "long": "--16",
            "arg": null,
            "description": "send a SCSI WRITE LONG (16) command to DEVICE. The default action (in the absence of this option) is to send a SCSI WRITE LONG (10) command."
        },
        {
            "flag": "-c",
            "long": "--cor_dis",
            "arg": null,
            "description": "sets the correction disabled (i.e 'CORDIS') bit. This inhibits various other mecha‐ nisms such as automatic block reallocation, error recovery and various informational exception conditions being triggered. This bit is relatively new in SBC-3 ."
        },
        {
            "flag": "-h",
            "long": "--help",
            "arg": null,
            "description": "output the usage message then exit."
        },
        {
            "flag": "-i",
            "long": "--in",
            "arg": null,
            "description": "read data (binary) from file named IF and use it for the SCSI WRITE LONG command. If IF is \"-\" then stdin is read. If this option is not given then 0xff bytes are used as fill."
        },
        {
            "flag": "-l",
            "long": "--lba",
            "arg": null,
            "description": "where LBA is the logical block address of the sector to overwrite. Defaults to lba 0 which is a dangerous block to overwrite on a disk that is in use. Assumed to be in decimal unless prefixed with '0x' or has a trailing 'h'. If LBA is larger than can fit in 32 bits then the --16 option should be used."
        },
        {
            "flag": "-p",
            "long": "--pblock",
            "arg": null,
            "description": "sets the physical block (i.e 'PBLOCK') bit. This instructs DEVICE to use the given data (unless --wruncor is also given) to write to the physical block specified by LBA. The default action is to write to the logical block corresponding to the given lba. This bit is relatively new in SBC-3 ."
        },
        {
            "flag": "-v",
            "long": "--verbose",
            "arg": null,
            "description": "increase the degree of verbosity (debug messages)."
        },
        {
            "flag": "-V",
            "long": "--version",
            "arg": null,
            "description": "output version string then exit."
        },
        {
            "flag": "-w",
            "long": "--wr_uncor",
            "arg": null,
            "description": "sets the \"write uncorrected\" (i.e 'WRUNCOR') bit. This instructs the DEVICE to flag the given lba (or the physical block that contains it if --pblock is also given) as having an unrecoverable error associated with it. Note: no data is transferred to DE‐ VICE, other than the command (i.e. the cdb). In the absence of this option, the de‐ fault action is to use the provided data or 0xff bytes (--xferlen=BTL in length) and write it to DEVICE. This bit is relatively new in SBC-3 ."
        },
        {
            "flag": "-x",
            "long": "--xfer_len",
            "arg": null,
            "description": "where BTL is the byte transfer length (default to 520). If the given value (or the de‐ fault) does not match the \"long\" block size of the device, nothing is written to DE‐ VICE and the appropriate xferlen value may be deduced from the error response which is printed (to stderr)."
        }
    ],
    "examples": [
        "This section outlines setting up a block with corrupted data, checking the  error  condition,",
        "then restoring useful contents to that sector.",
        "First, if the data in a sector is important, save it with the sgreadlong utility:",
        "sgreadlong --lba=0x1234 --out=0x12341.img -x BTL /dev/sda",
        "This  utility  may  need  to  be executed several time in order to determine what the correct",
        "value for BTL is.  Next use this utility to \"corrupt\" that sector. That might be done with:",
        "sgwritelong --lba=0x1234 -x BTL /dev/sda",
        "This will write a sector (and ECC data) of 0xff bytes. Some disks may reject this  (at  least",
        "one of the author's does). Another approach is to copy the 0x12341.img file (to 0x12342.img",
        "in this example) and change some values with a hex editor. Then write the changed image with:",
        "sgwritelong --lba=0x1234 --in=0x12342.img -x BTL /dev/sda",
        "Yet another approach is to use the --wruncor option, if supported:",
        "sgwritelong --lba=0x1234 --wruncor /dev/sda",
        "Next we use the sgdd utility to check that the sector is corrupted. Here is an example:",
        "sgdd if=/dev/sda blksgio=1 skip=0x1234 of=. bs=512 count=1 verbose=4",
        "Notice  that  the  \"blksgio=1\" option is given. This is to make sure that the sector is read",
        "(and no others) and the error is fully reported.  The \"blksgio=1\" option  causes  the  SGIO",
        "ioctl to be used by sgdd rather than the block subsystem.",
        "Finally  we  should  restore  sector  0x1234 to a non-corrupted state. A sector full of zeros",
        "could be written with:",
        "sgdd if=/dev/zero of=/dev/sda blksgio=1 seek=0x1234 bs=512 count=1",
        "This will result in a sector (block) with 512 bytes of 0x0 without a MEDIUM ERROR  since  the",
        "ECC  and associated data will be regenerated and thus well formed. The 'blksgio=1' option is",
        "even more important in this case as it may stop the block subsystem doing a read before write",
        "(since the read will most likely fail).  Another approach is to write back the original  con‐",
        "tents:",
        "sgwritelong --lba=0x1234 --in=0x12341.img -x BTL /dev/sda"
    ],
    "see_also": []
}