WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [Solved] How to Fill with zeroes the end of a disk?  (Read 1811 times)

Offline Santos

  • Full Member
  • ***
  • Posts: 105
[Solved] How to Fill with zeroes the end of a disk?
« on: December 07, 2019, 02:50:51 PM »
Hi there!

I want to erase the end of a memory flash, by the end I mean the last blocks. Is there any way to choose one specific "block", like 100MB-250MB or just the last 10MB of the memory flash?

I've tried, but is going to work just for the first megabyte:

dd if=/dev/zero of=/dev/sdb bs=1048576 count=1

The option 'seek' works for just a file (or ignore such amount of input data, right?)

Thank you for your help.
« Last Edit: December 08, 2019, 07:00:40 PM by Rich »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11243
Re: How to Fill with zeroes the end of a disk?
« Reply #1 on: December 07, 2019, 04:45:06 PM »
Hi Santos
This should do what you want:
Code: [Select]
dd if=/dev/zero of=/dev/sdb ibs=1M obs=1M seek=200 count=10That should skip the first 200MB and then write 10MB of zeros.

Offline Santos

  • Full Member
  • ***
  • Posts: 105
Re: How to Fill with zeroes the end of a disk?
« Reply #2 on: December 08, 2019, 03:14:28 AM »
Thank you for answer.

So the memory flash in question it's a MMC 4GB (GPT, as per parted, 3975MB). I was messing with the B.E.E.R partition of it (last couple of KBs) and now I need to delete just that portion of memory.

Code: [Select]
dd if=/dev/zero of=/dev/sdb ibs=1M obs=1M seek=3970 count=5

So I typed the previous line in and it did not work, I got:

Code: [Select]
dd:   /dev/sdb: Invalid argument

The same error is always popping up whenever I use the option 'seek' and a disk instead of a file.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11243
Re: How to Fill with zeroes the end of a disk?
« Reply #3 on: December 08, 2019, 09:21:54 AM »
Hi Santos
I think that error is from seeking a sector number higher than what exists on the device.

A little searching turned up this:
https://unix.stackexchange.com/questions/275243/what-does-dd-if-dev-zero-of-dev-sda-do/275260#275260

So I tried it on a 4GB thumb drive:
Code: [Select]
tc@E310:~$ dd if=/dev/zero of=/dev/sdg bs=512 count=10240 seek=$(expr `blockdev --getsz /dev/sdg` - 10240)
10240+0 records in
10240+0 records out
5242880 bytes (5.2 MB, 5.0 MiB) copied, 0.0386705 s, 136 MB/s
tc@E310:~$ sync
tc@E310:~$
And it zeroed out the last 5MB of the drive. Wait for the  sync  command to complete before removing the device.

Some quick notes on what's going on:
Code: [Select]
tc@E310:~$ blockdev --getsz /dev/sdg
7856127
tc@E310:~$ echo $(expr `blockdev --getsz /dev/sdg` - 10240)
7845887
The  blockdev  command (provided by util-linux.tcz) retrieves the number of sectors on the device.
The  expr  command subtracts the number of sectors you want to write (count) from the number of sectors on the device.

If you don't want to install  util-linux.tcz  you can use  fdisk  to get the number of sectors on the device:
Code: [Select]
tc@E310:~$ fdisk -l /dev/sdg
Disk /dev/sdg: 3.8 GiB, 4022337024 bytes, 7856127 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start     End Sectors  Size Id Type
/dev/sdg1          38 7839719 7839682  3.8G  b W95 FAT32

And then plug in the number of sectors on the device by hand:
Code: [Select]
dd if=/dev/zero of=/dev/sdg bs=512 count=10240 seek=$(expr 7856127 - 10240)

Offline Santos

  • Full Member
  • ***
  • Posts: 105
Re: How to Fill with zeroes the end of a disk?
« Reply #4 on: December 08, 2019, 06:33:56 PM »
Thank you very much!

This question is now solved.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11243
Re: How to Fill with zeroes the end of a disk?
« Reply #5 on: December 08, 2019, 06:59:56 PM »
Hi Santos
You are welcome. I will mark your original post as solved.