LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-06-2021, 11:07 AM   #1
Sad_Bed5609
LQ Newbie
 
Registered: Apr 2021
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 04-06-2021, 11:11 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,223

Rep: Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446
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?
 
Old 04-06-2021, 11:21 AM   #3
Sad_Bed5609
LQ Newbie
 
Registered: Apr 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-06-2021, 12:08 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,223

Rep: Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446
how do you know it did not work?
 
Old 04-06-2021, 12:26 PM   #5
Emerson
LQ Sage
 
Registered: Nov 2004
Location: Saint Amant, Acadiana
Distribution: Gentoo ~arch
Posts: 7,045

Rep: Reputation: Disabled
I'd suggest eliminating sudo and running it as root.
 
Old 04-06-2021, 01:01 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,185

Rep: Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946
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?

Last edited by michaelk; 04-06-2021 at 01:04 PM.
 
Old 04-06-2021, 01:26 PM   #7
Sad_Bed5609
LQ Newbie
 
Registered: Apr 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
@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?

Last edited by Sad_Bed5609; 04-06-2021 at 02:03 PM.
 
Old 04-06-2021, 02:12 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,185

Rep: Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946
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 ))
 
Old 04-06-2021, 02:20 PM   #9
Sad_Bed5609
LQ Newbie
 
Registered: Apr 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
@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 :-/
 
Old 04-07-2021, 01:43 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,223

Rep: Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446
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.
 
1 members found this post helpful.
Old 04-07-2021, 03:55 AM   #11
Sad_Bed5609
LQ Newbie
 
Registered: Apr 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
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?

Last edited by Sad_Bed5609; 04-07-2021 at 04:01 AM.
 
Old 04-07-2021, 07:03 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,223

Rep: Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446
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.
 
1 members found this post helpful.
Old 04-07-2021, 08:46 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 21,185

Rep: Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946Reputation: 3946
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.
 
1 members found this post helpful.
Old 04-07-2021, 09:01 AM   #14
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,223

Rep: Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446Reputation: 5446
Quote:
Originally Posted by michaelk View Post
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"
 
Old 04-07-2021, 09:54 AM   #15
Sad_Bed5609
LQ Newbie
 
Registered: Apr 2021
Posts: 6

Original Poster
Rep: Reputation: Disabled
@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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gpg: do_plaintext(): wrote 1210414045 bytes but expected 822504068 bytes MensaWater Linux - Software 6 02-27-2018 08:48 AM
Get first day of last month and last day of last month in bash xowl Linux - Software 18 02-09-2017 09:49 AM
Is it okay if my sector size is "512 bytes / 4096 bytes" Altiris Slackware 5 07-31-2015 03:19 AM
[SOLVED] Overwriting free space or overwriting single files restored by photorec fcrok Linux - Security 22 09-15-2012 12:53 PM
TX bytes vs. httpd bytes ovrload Linux - Networking 3 10-12-2005 04:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration