Skip to content

Instantly share code, notes, and snippets.

@norpol
Last active January 31, 2017 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norpol/fbe3a87dd9fa0e836606cdcda7648a82 to your computer and use it in GitHub Desktop.
Save norpol/fbe3a87dd9fa0e836606cdcda7648a82 to your computer and use it in GitHub Desktop.
findmount luks block device
#!/bin/sh
# figure which luks device is used for a given mountpoint (e.g. /)
# usage: findmount_luks_block_device.sh /
# See findmount_luks_block_device_LICENCE.MD (MIT)
set -ue
mountpoint="${1}"
lsblk_output="$(lsblk -l -o NAME,KNAME,PKNAME,TYPE,UUID,MOUNTPOINT)"
# each matching line provided above the command
# exit after print in each awk, because mdadm softraid lvm block device will appear multiple times (md-0)
# and we only want to return a single line each
# NAME KNAME PKNAME TYPE UUID MOUNTPOINT
# lvm-root dm-1 dm-0 lvm f4584e908-88ef-402a-b26c-6aa0934a9bc5 /
pkname="$(echo -n "${lsblk_output}" | awk -v mount="${mountpoint}" '$6 == mount {print $3; exit}')"
[ -n "${pkname}" ]
# sda3_crypt dm-0 sda3 crypt b2ddf1b2-8e15-4b56-a7f1-85918952db08
kname="$(echo -n "${lsblk_output}" | awk -v dev="${pkname}" '$2 == dev {print $3; exit}')"
[ -n "${kname}" ]
# sda3 sda3 sda part 31955fb1-5972-4d44-b80a-75efb77ef8d9
uuid="$(echo -n "${lsblk_output}" | awk -v dev="${kname}" '$2 == dev {print $5; exit}')"
[ -n "${uuid}" ]
echo "/dev/disk/by-uuid/${uuid}"

MIT License

Copyright (c) 2016 https://gist.github.com/norpol/fbe3a87dd9fa0e836606cdcda7648a82

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment