LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 06-19-2023, 07:48 PM   #1
someone_
LQ Newbie
 
Registered: Jun 2023
Posts: 2

Rep: Reputation: 0
Post Encrypting whole device with LUKS


I have a block device called /dev/sdb, i want to know if it is possible to encrypt it completely, including the partition table.
I did
Code:
$ cryptsetup luksFormat -y -v /dev/sdb
$ cryptsetup open /dev/sdb usb
then i created a new partition table, and partition with fdisk on /dev/mapper/usb
Code:
Disk /dev/mapper/usb: 184 MiB, 192937984 bytes, 376832 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: 0xca187b28

Device                Boot Start    End Sectors  Size Id Type
/dev/mapper/usb-part1       2048 376831  374784  183M 83 Linux
But the device fdisk is referring to does not exist
I was able to use the created partition by doing
Code:
$ losetup /dev/loop0 /dev/sdb -o $(( 2048 * 512 ))
and then using /dev/loop0 as the partition device, i want to know if there is a better way of doing this, or to make the kernel automatically create the new partition device.
 
Old 06-20-2023, 07:47 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,163

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Have you rebooted ?. What happened/changed ?.

partprobe is a tool to force the kernel to re-read partition tables if you can't reboot.
 
1 members found this post helpful.
Old 06-20-2023, 09:23 AM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,786

Rep: Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215
Quote:
Originally Posted by someone_ View Post
I was able to use the created partition by doing
Code:
$ losetup /dev/loop0 /dev/sdb -o $(( 2048 * 512 ))
and then using /dev/loop0 as the partition device, i want to know if there is a better way of doing this, or to make the kernel automatically create the new partition device.
That is bypassing the encryption and directly accessing the underlying device. Anything you write to that device will be unencrypted. You don't need to run cryptsetup at all to access it.

You need to run "partprobe /dev/mapper/usb" or "kpartx -a /dev/mapper/usb" to make the kernel recognize the partitions within that encrypted device.

Once you have done that, you will find that any attempt to luksClose that device will fail with "Device usb is still in use." You must first run "kpartx -d /dev/mapper/usb" to un-map the partitions (after unmounting them, of course).

Getting the partition(s) on that encrypted device recognized automatically will require some scripting, and will depend on whether the device is present at boot time or inserted later.
 
1 members found this post helpful.
Old 06-20-2023, 11:31 AM   #4
someone_
LQ Newbie
 
Registered: Jun 2023
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rknichols View Post
That is bypassing the encryption and directly accessing the underlying device. Anything you write to that device will be unencrypted. You don't need to run cryptsetup at all to access it.
Yes i'm sorry i was pretty tired when i wrote this yesterday, what i actually did was "losetup /dev/loop0 /dev/mapper/usb -o $(( 2048 * 512 ))"

Quote:
Originally Posted by rknichols View Post
You need to run "partprobe /dev/mapper/usb" or "kpartx -a /dev/mapper/usb" to make the kernel recognize the partitions within that encrypted device.
Quote:
Originally Posted by syg00 View Post
partprobe is a tool to force the kernel to re-read partition tables if you can't reboot.
Partprobe and xpartx where, indeed, the commands i was missing, thank you

If it is some device inserted later i have no way other than to create a shell script that would do all of this automatically right?

And one last question, is there any specific reason why i did not find anything about encrypting the whole device and not just a partition? is there any reason not to do it?
 
Old 06-20-2023, 06:40 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,786

Rep: Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215Reputation: 2215
Quote:
Originally Posted by someone_ View Post
And one last question, is there any specific reason why i did not find anything about encrypting the whole device and not just a partition? is there any reason not to do it?
Just the issues that you've run into. Various parts of the system tacitly assume that block devices other than floppy disks will be partitioned at the top level. If you don't partition at all, or partition in some other way, you can expect that it might not be straightforward to make it all work.
 
  


Reply



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
Encrypting the whole system after the installation and using an external device netpumber Linux - Security 9 12-12-2016 01:41 PM
LXer: Basic Guide To Encrypting Linux Partitions With LUKS LXer Syndicated Linux News 0 10-22-2016 04:42 PM
Help with encrypting entire hard drive with LUKS in Debian 8.2.0 "Jessie". Maxsvenman Linux - Newbie 5 12-28-2015 04:41 PM
Software RAID and LUKS encrypting alpha_lt Linux - Software 2 08-29-2010 07:14 AM
A pondering about Encrypting the Keycard for a LUKS/LVM partition. lumak Slackware 3 08-15-2010 02:15 PM

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

All times are GMT -5. The time now is 07:30 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