PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 14, 2022

[FIXED] How to check that an encrypted disk was previously open using cryptsetup?

 May 14, 2022     encryption, shell, ubuntu     No comments   

Issue

I am writing a shell script (meant to work with Ubuntu only) that assumes that a disk has been previously open (using the command below) to make operations on it (resize2fs, lvcreate, ...). However, this might not always be the case, and when the disk is closed, the user of the script has to run this line before running the script, asking for his/her passphrase:

sudo cryptsetup luksOpen /dev/sdaX sdaX_crypt

Ideally, the script should start with this command, simplifying the user sequence. However, if the disk was indeed already opened, the script will fail because an encrypted disk cannot be opened twice.

How can I check if the disk was previously open? Is checking that /dev/mapper/sdX_crypt exists a valid solution / enough? If not or not possible, is there a way to make the command run only if necessary?


Solution

Since I could not find a better solution, I went ahead and chose the "check if the device exists" one.

The encrypted disk embeds a specific Volume Group (called my-vg for the example), so my working solution is:

if [ ! -b /dev/my-vg ]; then
  sudo cryptsetup luksOpen /dev/sdaX sdaX_crypt
fi

I check that /dev/my-vg exists instead of /dev/mapper/sda_cryptX because every other command in my script uses the first one as an argument so I kept it for consistency, but I reckon that this solution below looks more encapsulated:

if [ ! -b /dev/mapper/sdaX_crypt ]; then
  sudo cryptsetup luksOpen /dev/sdaX sdaX_crypt
fi

Although the solution I described above works for me, is there a good reason I should switch to the latter one or it doesn't matter?



Answered By - astorije
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing