Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blkdiscard support for disk wipe #91

Merged
merged 2 commits into from
Jul 3, 2024
Merged

Conversation

mmlb
Copy link
Member

@mmlb mmlb commented Jul 1, 2024

Uses ironlib's blkdiscard support to wipe disks that support TRIM.

// Lets figure out if the drive supports TRIM
var trim bool
for _, cap := range drive.Capabilities {
if strings.HasPrefix(cap.Description, "Data Set Management TRIM supported") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work across different drives vendors/models?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep worked correctly across a bunch of different vendors/models so far:
I've run hdparm -I /dev/sd?|grep -i -E '(/dev/|model|trim)' across a bunch of different em plan types that result in:

/dev/sda:
   Model Number:       MZ7LH480HBHQ0D3                         
     *	Data Set Management TRIM supported (limit 8 blocks)
     *	Deterministic read ZEROs after TRIM
/dev/sdc:
   Model Number:       MTFDDAV240TDU                           
     *	Data Set Management TRIM supported (limit 8 blocks)
     *	Deterministic read ZEROs after TRIM
/dev/sda:
	Model Number:       INTEL SSDSC2KB480G8                     
	  *	Data Set Management TRIM supported (limit 4 blocks)
	  *	Deterministic read ZEROs after TRIM
/dev/sda:
	Model Number:       Micron_5200_MTFDDAK480TDN               
	  *	Data Set Management TRIM supported (limit 8 blocks)
	  *	Deterministic read ZEROs after TRIM
/dev/sda:
	Model Number:       SSDSCKKB240G8R                          
	  *	Data Set Management TRIM supported (limit 4 blocks)
	  *	Deterministic read ZEROs after TRIM
/dev/sdb:
	Model Number:       MZ7LH480HBHQ0D3                         
	  *	Data Set Management TRIM supported (limit 8 blocks)
	  *	Deterministic read ZEROs after TRIM

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm seeing some apparently-conflicting info on this -- on a Micron SSD in an n2.xlarge:

$ hdparm -I /dev/sda | grep -i trim
	   *	Data Set Management TRIM supported (limit 8 blocks)
	   *	Deterministic read ZEROs after TRIM
$ cat /sys/block/sda/queue/discard_zeroes_data 
0

I'm not sure offhand what exactly the difference is between what the kernel reports in discard_zeroes_data and what hdparm reports as "Deterministic read ZEROs after TRIM" -- anyone happen to know?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=48920ff2a5a940cd07d12cc79e4a2c75f1185aee

Will always return 0. Don't rely on any specific behavior for discards, and don't read this file.

Tracing through some kernel codepaths, it looks like maybe we should be trying to use the BLKZEROOUT ioctl instead...which is available via blkdiscard --zeroout, but it doesn't look like ironlib currently passes that flag?

Copy link
Member Author

@mmlb mmlb Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm seeing some apparently-conflicting info on this -- on a Micron SSD in an n2.xlarge:

$ hdparm -I /dev/sda | grep -i trim
	   *	Data Set Management TRIM supported (limit 8 blocks)
	   *	Deterministic read ZEROs after TRIM
$ cat /sys/block/sda/queue/discard_zeroes_data 
0

I'm not sure offhand what exactly the difference is between what the kernel reports in discard_zeroes_data and what hdparm reports as "Deterministic read ZEROs after TRIM" -- anyone happen to know?

Hah: git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit?id=48920ff2a5a940cd07d12cc79e4a2c75f1185aee

Will always return 0. Don't rely on any specific behavior for discards, and don't read this file.

Tracing through some kernel codepaths, it looks like maybe we should be trying to use the BLKZEROOUT ioctl instead...which is available via blkdiscard --zeroout, but it doesn't look like ironlib currently passes that flag?

Looks like discard_zeros_data was just internal use to me. That patch series also adds a bunch of code handling REQ_OP_WRITE_ZEROES and keeps REQ_OP_DISCARD around:

 	if (op == REQ_OP_DISCARD)
 		special_cmd_max_sectors = q->limits.max_discard_sectors;
+	else if (op == REQ_OP_WRITE_ZEROES)

From the blkdiscard manpage:

       -z, --zeroout
           Zero-fill rather than discard.

which is not what we want 😄 (tried running this and its definitely taking a long time to come finish and is in D+ state). fwiw hdparm gets the data from SG_IO ioctl:

ioctl(3, SG_IO, {interface_id='S', dxfer_direction=SG_DXFER_FROM_DEV, cmd_len=16, cmdp="\x85\x09\x0e\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\xe0\x2f\x00", mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=15000, flags=0, dxferp="\x01\x00\x00\x00\x00\x00\x14\x00\x08\x00\x00\x00\x00\x00\x02\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"..., status=0, masked_status=0, msg_status=0, sb_len_wr=0, sbp="", host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0                                                   

cmd/disk_wipe.go Outdated Show resolved Hide resolved
mmlb added 2 commits July 2, 2024 12:19
Expand drive wiping capabilities by using blkdiscard for devices that
support it.
Copy link
Member

@joelrebel joelrebel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving this since the intended efforts are covered @mmlb can create any follow up PRs if required.

@mmlb mmlb merged commit 90681f6 into metal-toolbox:main Jul 3, 2024
2 checks passed
@mmlb mmlb deleted the wipe-trim branch July 3, 2024 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants