SG_UNMAP(8)
β¨ NAME β¨
sg_unmap π«§ is the tool that sends a SCSI UNMAP command (known as 'trim' in ATA specs) π
πΈ SYNOPSIS πΈ
sg_unmap [--all=ST,RN[,LA]] [--anchor] [--dry-run] [--force]
[--grpnum=GN] [--help] [--in=FILE] [--lba=LBA,LBA...] [--num=NUM,NUM...]
[--timeout=TO] [--verbose] [--version] DEVICE
π¦ DESCRIPTION π¦
Okay so this utility sends a SCSI UNMAP command to DEVICE to unmap one or more logical blocks π§ It was introduced in SBC-3 revision 18 under the broad heading of "logical block provisioning" π
Logical blocks can also get unmapped by the SCSI WRITE SAME command, so go check out the sg_write_same utility for that one β¨ The unmap capability is closely related to the ATA DATA SET MANAGEMENT command with the "Trim" bit set, which is giving total sibling energy π―ββοΈ
Logical blocks to be unmapped can be specified in one of three ways π The first way is supplying the start LBAs to the --lba= option and the matching number(s) to unmap to the --num= option. The second way is putting start LBA and number to unmap pairs in a file, then passing that file's name to the --in= option. And lastly, a large segment or all of a disk (SSD) can be unmapped with the --all=ST_RN[,LA] option π₯ All values are assumed to be decimal unless they're prefixed by 0x (or 0X) or have a trailing h (or H), in which case they get read as hexadecimal. Suffix multipliers are allowed on decimal values, so --num=1m is totally valid π
When the --lba= option is given, the --num= option must also be given π«Ά If one has a comma separated list as its argument, the other needs the same number of elements in its list. The arguments can use a single space as a separator, but they need to be in quotes or escaped so the shell doesn't misinterpret them π
With the --in=FILE option, an even number of values must be found, and they get interpreted as pairs π The first value in each pair is a starting LBA and the second value is the number to unmap from that LBA. Everything from and including a "#" on a line is ignored, and so are blank lines π Values may be comma, space and tab separated, or they can show up on separate lines. Each line should not exceed 1023 bytes in length.
π Since a lot of data can be lost with this utility, a 15 second "cooling off" period is given before any UNMAP commands are sent. During this period the user is reminded what will happen and to which device, so they can hit control-C (or use some other technique) to terminate the utility before any unmapping takes place. This period can be bypassed with the --force option.
π OPTIONS π
Arguments to long options are mandatory for short options as well, bestie π«Ά
-A,--all=ST,RN[,LA]β πSTis the starting LBA,RNis the repeat number (the maximum number of blocks in each SCSI UNMAP command), andLA, if you give it, is the last LBA to unmap. IfLAisn't given, the last LBA on theDEVICEgets used, which is obtained by the SCSI READ CAPACITY command.-a,--anchorβ β sets the 'Anchor' bit in the command (introduced in sbc3r22).-d,--dry-runβ π§ does all the prep, including openingDEVICEplus sending a 'standard' SCSI INQUIRY command (and optionally a READ CAPACITY), but exits before performing any SCSI UNMAP commands. Think of it as a vibe check before the real thing π-f,--forceβ π₯ bypasses the 15 second warning period that happens before any UNMAP commands are sent.-g,--grpnum=GNβ πΈ sets the 'Group number' field toGN. It defaults to zero.GNshould be a value between 0 and 63.-h,--helpβ π outputs the usage message, then exits.-I,--in=FILEβ πFILEis a file name containing pairs of values. The first member of each pair is a starting LBA, and the second member is the number of logical blocks to unmap from and including that starting LBA. Values are read as decimal unless indicated otherwise. This option cannot be present with the--lba=option, so pick your lane π«-l,--lba=LBA,LBA...β π¦LBA,LBA...is a string of comma (or space) separated values that get interpreted as starting logical block addresses. Each number is decimal unless prefixed by0xor0X(or it has a trailinghorH). An argument with any space separators needs to be quoted (or otherwise escaped). When this option is given, the--num=option must also be given, and they must contain the same number of elements in their arguments.-n,--num=NUM,NUM...β πNUM,NUM...is a string of comma (or space) separated values that get interpreted as a number of logical blocks to unmap. Each number is decimal unless prefixed by0xor0X(or it has a trailinghorH). Note that 0 blocks is acceptable π An argument with any space separators needs to be quoted (or otherwise escaped). When this option is given, the--lba=option must also be given, and they must contain the same number of elements in their arguments.-t,--timeout=TOβ β±οΈTOis a timeout value (in seconds) for the UNMAP command. The default is 60 seconds.-v,--verboseβ π increases the level of verbosity (i.e. debug output).-V,--versionβ β¨ prints the version string, then exits.
π§ NOTES π§
Some limits to know about, okay? π₯Ί An LBA can be up to 64 bits, and a NUM can be up to 32 bits (imposed by the structure of the UNMAP SCSI command parameter data). The NUM is further constrained by the MAXIMUM UNMAP LBA COUNT field in the BLOCK LIMITS VPD page (0xb0). The maximum number of LBA,NUM pairs is limited to 128 by this utility, and it may be further constrained by the MAXIMUM UNMAP BLOCK DESCRIPTOR COUNT field in the BLOCK LIMITS VPD page.
Since it's unclear how long the UNMAP command will take to execute, a `--timeout=" option has been provided π°οΈ The default timeout period is 60 seconds. If all the logical blocks on a logical unit (e.g. a disk drive) are to be unmapped, the FORMAT UNIT SCSI command (see the sg_format utility) may be considered as an alternative π·
Support for logical block provisioning is indicated by the LBPME bit in the response to the SCSI READ CAPACITY (16) command (see the sg_readcap utility) π§Ύ
In SBC-3 revision 25, the LBPU and ANC_SUP bits where added to the Logical Block Provisioning VPD page π When LBPU is set, it indicates that the device supports the UNMAP command. When the ANC_SUP bit is set, it indicates the device supports anchored LBAs.
The SCSI UNMAP command does the "right thing" with respect to command queueing, periodt π However, its ATA counterpart (the DATA SET MANAGEMENT command with the "Trim" bit set) does not interact well with SATA queueing known as NCQ π To address this problem, T13 introduced a new command called SFQ DATA SET MANAGEMENT, which also has a Trim bit.
π EXAMPLES π
In the examples directory of the sg3_utils package there's a sg_unmap_example.txt file that shows the format the --in= option accepts π
To unmap all blocks from and including LBA 0x2000 to the end of the device (e.g. disk or SSD), with each SCSI UNMAP command given 1024 blocks to unmap:
sg_unmap --all=0x2000,1k /dev/sg2
Add --force to bypass the 15 seconds of warnings. So --force is appropriate for batch files π€
π EXIT STATUS π
The exit status of sg_unmap is 0 when it's successful. Otherwise, see the sg3_utils(8) man page π«
π AUTHORS π
Written by Douglas Gilbert π«Ά
π REPORTING BUGS π
Report bugs to <dgilbert at interlog dot com> π¬
βοΈ COPYRIGHT βοΈ
Copyright Β© 2009-2018 Douglas Gilbert This software is distributed under a BSD-2-Clause license. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE π¦
π SEE ALSO π
sg_format, sg_get_lba_status, sg_readcap, sg_vpd, sg_write_same(sg3_utils) π§
Okay bestie, that's the whole sg_unmap tea βπ It's the sg3_utils-1.43 release from March 2018, so you're officially caught up. Now go unmap responsibly and don't forget your 15 seconds of warnings π«Άβ¨
π¦ where this page comes from
- alpine 3.24: sg3_utils-doc 1.48-r0 Β· BSD-2-Clause AND GPL-2.0-or-later
- arch rolling: sg3_utils 1.49-1 Β· GPL-2.0-or-later AND BSD-2-Clause
- centos-stream 10: sg3_utils 1.48-11.el10 Β· GPL-2.0-or-later AND BSD-2-Clause
- debian trixie: sg3-utils 1.48-3~deb13u1
- fedora 43: sg3_utils 1.48-10.fc43 Β· GPL-2.0-or-later AND BSD-2-Clause
- fedora 43: sg3_utils 1.48-6.fc43 Β· GPL-2.0-or-later AND BSD-2-Clause
- fedora 44: sg3_utils 1.48-10.fc44 Β· GPL-2.0-or-later AND BSD-2-Clause
- fedora 44: sg3_utils 1.48-8.fc44 Β· GPL-2.0-or-later AND BSD-2-Clause
- nixos 26.05: sg3_utils 1.48
- nixpkgs unstable: sg3_utils 1.49
- ubuntu questing: sg3-utils 1.48-2ubuntu1
- ubuntu resolute: sg3-utils 1.48-3ubuntu3
- ubuntu resolute: sg3-utils 1.48-3ubuntu3.2
- ubuntu stonking: sg3-utils 1.48-3ubuntu5
- π girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms