LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Overwriting the last bytes of a partition (https://www.linuxquestions.org/questions/linux-general-1/overwriting-the-last-bytes-of-a-partition-4175693213/)

Sad_Bed5609 04-06-2021 11:07 AM

Overwriting the last bytes of a partition
 
Greetings, I am looking for a way to delete the last 131072 bytes of any partition. So far I have been trying to create a dd command without success, any ideas or suggestions? Thanks.

pan64 04-06-2021 11:11 AM

Hi, and welcome here, at LQ.
I would like to know what dd commands did you create and execute and how do you know if they were successful (or not).
Is this your homework?

Sad_Bed5609 04-06-2021 11:21 AM

Hi, thanks for replying, the closest I have to a working one (I think) is this [sudo dd if=/dev/urandom of=/dev/sdX bs=512 count=256 seek=$(( $(sudo blockdev --getsize64 /dev/sdX) - 256 ))]. It seems that the "seek" argument is not accepted due to its dynamic content, "skip" didn't work either.

pan64 04-06-2021 12:08 PM

how do you know it did not work?

Emerson 04-06-2021 12:26 PM

I'd suggest eliminating sudo and running it as root.

michaelk 04-06-2021 01:01 PM

Code:

blockdev --getsize64 /dev/sdX
At face value that would be the size of the entire drive and not a particular partition.

Code:

sudo blockdev --getsize64 /dev/sdX) - 256
Your substracting 256 bytes from the end but wanting to over write 131072 bytes. That could possibly lead to a disaster.

How are you going to find the end of a particular partition?

Sad_Bed5609 04-06-2021 01:26 PM

@pan64
The "seek" parameter reports the error when it contains a variable, "skip" doesn't but it doesn't work either.

I verify it using veracrypt, it is an encryption software that (according to its documentation) creates a header at the beginning and a backup header at the end, each of 131072 bytes. The purpose is both to erase both master passwords and to completely hide the use of veracrypt.
I easily remove the main one with [sudo dd if=/dev/urandom of=/dev/sdX bs=512 count=256] but despite my attempts I can't remove the backup one. In my different attempts or does nothing, allowing me to restore the main header successfully and without damaging the content, or simply breaks everything.

@Emerson
Using root didn't change anything by trying it, I also have a good relationship with sudo, but thanks.

@Michaelk
Oops thanks, I tried other blockdev parameters and left the wrong one saved, the others used blocks instead of bytes, I should have noted which ones I used. That did not cause any catastrophic results, nor any others that I can appreciate.
Anyway using [sudo dd if=/dev/urandom of=/dev/sdX bs=512 count=256 seek=$(( $((sudo blockdev --getsize64 /dev/sdf2) - 131072 ))] didn't work either.

I tried blockdev separately and it works as expected (on /dev/sdf2 in my case), I was hoping to dynamically mark the end of several partitions (and preferably disks), should I use another tool?

michaelk 04-06-2021 02:12 PM

Code:

seek=$(( $((sudo blockdev --getsize64 /dev/sdf2) - 131072 ))
Your syntax is not correct. I would probably use a bash script to find the end of the encrypted volume and calculate the offset separately from the dd command.

seek=$(( $(sudo blockdev --getsize64 /dev/sdf2) - 131072 ))

Sad_Bed5609 04-06-2021 02:20 PM

@Michaelk
*faceplam* Another error I added when posting this, the original was fine.
I'm pretty much a noob, I was hoping to get it the easy way. But I guess, whether it works or not, it's time to learn about bash :-/

pan64 04-07-2021 01:43 AM

in that case would be nice to post what did you try exactly, we cannot handle the differences and the "original".
And also please use code tags.
Code:

sudo dd if=/dev/urandom of=/dev/sdf2 bs=512 count=256 seek=$(( $(sudo blockdev --getsize64 /dev/sdf2) - 131072 ))
theoretically it should work, the getsize command will be executed/evaluated first, the dd command should see something like seek=<number>
But only if there was no any syntactical issue with it. To make it more readable you can split it into several lines and add some debug prints like:
Code:

P_SIZE=$(sudo blockdev --getsize64 /dev/sdf2)
echo partition size = $P_SIZE
SEEK=$(( P_SIZE - 131072 ))
echo seeking to $SEEK
sudo dd if=/dev/urandom of=/dev/sdf2 bs=512 count=256 seek=$SEEK

also you can use shellcheck to analyze your script.

Sad_Bed5609 04-07-2021 03:55 AM

I previously tried the first one you just posted, without the small syntax error. My apologies for creating confusion and not using the tags.

Unfortunately neither worked (the format in which the second one is written is interesting to me and will be useful in the future, also shellcheck, thanks!) and it still says that seek= is an invalid argument. I noticed that it is also invalid if instead of using a dynamic content I directly use the number (total bytes minus the desired ones). Strangely only by providing a number two digits below the correct one it is accepted, which obviously corrupted the content.

I have rechecked everything again thoroughly, blockdev gives the same results as gnome disks. In dd everything works in bytes, count= is well calculated, deleting bs= or replacing it with obs= (although both default to 512) doesn't change anything even if seek= has no dynamic content. Everything looks like it should be fine, could it be a bug in dd? Is there any other suitable tool for this?

pan64 04-07-2021 07:03 AM

there is no dynamic content at all. You see something like that, but the shell itself (bash) will evaluate the text [and execute that blockdev getsize] you entered and dd will only see the result.
dd has no any idea about everything else, but the arguments passed.
and I think there is no such bug in dd.

michaelk 04-07-2021 08:46 AM

I sometimes miss the obvious...

seek defaults to blocks but we are calculating bytes. To use bytes you need to add
oflag=seek_bytes option to your dd line.

pan64 04-07-2021 09:01 AM

Quote:

Originally Posted by michaelk (Post 6238172)
I sometimes miss the obvious...

seek defaults to blocks but we are calculating bytes. To use bytes you need to add
oflag=seek_bytes option to your dd line.

that's why would have been better to post the full output/error message instead of saying "it did not work"

Sad_Bed5609 04-07-2021 09:54 AM

@Michaelk
Thank you so much!! It's embarrassing, I really messed up. I have done some tests and now it works perfectly :-)

@pan64
Well, it really only said that it was an invalid argument in a non-English language. Now it is clear that the number entered was greater than the total blocks.
Thanks for trying and especially for the other useful data.


All times are GMT -5. The time now is 04:26 AM.