All of lore.kernel.org
 help / color / mirror / Atom feed
* [dm-crypt] How to find out about mapped device name?
@ 2013-02-04 23:32 David Li
  2013-02-04 23:50 ` .. ink ..
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: David Li @ 2013-02-04 23:32 UTC (permalink / raw)
  To: dm-crypt

[-- Attachment #1: Type: text/plain, Size: 203 bytes --]

Hi Experts,

I have already called luksFormat on a raw block dev and luksOpen to set up
mapped device in /dev/mapper. Is there a command to find out about the
mapping between the raw dev and mapped dev?

[-- Attachment #2: Type: text/html, Size: 302 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [dm-crypt] How to find out about mapped device name?
  2013-02-04 23:32 [dm-crypt] How to find out about mapped device name? David Li
@ 2013-02-04 23:50 ` .. ink ..
  2013-02-04 23:57   ` David Li
  2013-02-05  0:30 ` Robert Nichols
  2013-02-05  0:42 ` Arno Wagner
  2 siblings, 1 reply; 8+ messages in thread
From: .. ink .. @ 2013-02-04 23:50 UTC (permalink / raw)
  To: David Li; +Cc: dm-crypt

[-- Attachment #1: Type: text/plain, Size: 804 bytes --]

On Mon, Feb 4, 2013 at 6:32 PM, David Li <w.david.li@gmail.com> wrote:

> Hi Experts,
>
> I have already called luksFormat on a raw block dev and luksOpen to set up
> mapped device in /dev/mapper. Is there a command to find out about the
> mapping between the raw dev and mapped dev?
>
>
>
"cryptsetup status /dev/mapper/mapped_device" will give you the info at the
"device" field.

"readlink /dev/mapper/mapped_device" will give you something like
"../dm-1"..That address says the dm device is "/dev/dm-1"

take the "dm-1" component,add around it to gave a path
"/sys/block/dm-1/slaves/" and then go to the folder and you will see an
entry that will look like "sdc5".This entry will tell you the "raw device"
is "/dev/sdc5.

The first way require root's privileges.
The second way is hacky but does not

[-- Attachment #2: Type: text/html, Size: 1533 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [dm-crypt] How to find out about mapped device name?
  2013-02-04 23:50 ` .. ink ..
@ 2013-02-04 23:57   ` David Li
  2013-02-05  0:08     ` .. ink ..
  0 siblings, 1 reply; 8+ messages in thread
From: David Li @ 2013-02-04 23:57 UTC (permalink / raw)
  To: .. ink ..; +Cc: dm-crypt

[-- Attachment #1: Type: text/plain, Size: 1086 bytes --]

Thanks for the info!

What if I was only given the raw dev name, .e.g, /dev/sdd? Is there an easy
way to find out its mapped encrypted device name in /dev/mapper?


On Mon, Feb 4, 2013 at 3:50 PM, .. ink .. <mhogomchungu@gmail.com> wrote:

>
> On Mon, Feb 4, 2013 at 6:32 PM, David Li <w.david.li@gmail.com> wrote:
>
>> Hi Experts,
>>
>> I have already called luksFormat on a raw block dev and luksOpen to set
>> up mapped device in /dev/mapper. Is there a command to find out about the
>> mapping between the raw dev and mapped dev?
>>
>>
>>
> "cryptsetup status /dev/mapper/mapped_device" will give you the info at
> the "device" field.
>
> "readlink /dev/mapper/mapped_device" will give you something like
> "../dm-1"..That address says the dm device is "/dev/dm-1"
>
> take the "dm-1" component,add around it to gave a path
> "/sys/block/dm-1/slaves/" and then go to the folder and you will see an
> entry that will look like "sdc5".This entry will tell you the "raw device"
> is "/dev/sdc5.
>
> The first way require root's privileges.
> The second way is hacky but does not
>
>
>

[-- Attachment #2: Type: text/html, Size: 2181 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [dm-crypt] How to find out about mapped device name?
  2013-02-04 23:57   ` David Li
@ 2013-02-05  0:08     ` .. ink ..
  0 siblings, 0 replies; 8+ messages in thread
From: .. ink .. @ 2013-02-05  0:08 UTC (permalink / raw)
  To: David Li; +Cc: dm-crypt

[-- Attachment #1: Type: text/plain, Size: 1049 bytes --]

On Mon, Feb 4, 2013 at 6:57 PM, David Li <w.david.li@gmail.com> wrote:

>
> Thanks for the info!
>
> What if I was only given the raw dev name, .e.g, /dev/sdd? Is there an
> easy way to find out its mapped encrypted device name in /dev/mapper?
>
>
The easiest way is to use a mapper path that gives a direct relationship to
the underlying device.For example,if you want to open a device at
"/dev/sdc5",use a mapper path of something like "dev_sdc5.This way,if given
a device of "/dev.sda6",you know the mapper path is "/dev/mapper"sda_6".

Most tools that open volumes have some sort of a consistency on what mapper
paths they create and its based on the user ID and device path

If you dont know the relationship btw a device and its mapper,then the only
thing you can do i can think of is to go through all entries in
"/dev/mapper" and look for their underying devices and compare it with the
device you are interested in.

I believe "LVM" volumes also add entries in "/dev/mapper" so the folder may
have other entries apart from cryptsetup ones.

[-- Attachment #2: Type: text/html, Size: 1712 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [dm-crypt] How to find out about mapped device name?
  2013-02-04 23:32 [dm-crypt] How to find out about mapped device name? David Li
  2013-02-04 23:50 ` .. ink ..
@ 2013-02-05  0:30 ` Robert Nichols
  2013-02-05  2:24   ` Milan Broz
  2013-02-05  0:42 ` Arno Wagner
  2 siblings, 1 reply; 8+ messages in thread
From: Robert Nichols @ 2013-02-05  0:30 UTC (permalink / raw)
  To: dm-crypt

On 02/04/2013 05:32 PM, David Li wrote:
> Hi Experts,
>
> I have already called luksFormat on a raw block dev and luksOpen to set up
> mapped device in /dev/mapper. Is there a command to find out about the mapping
> between the raw dev and mapped dev?

"dmsetup deps [name]" will give you the lists of (major, minor) device numbers
underlying the names in /dev/mapper. Include the "name" argument (without the
leading "/dev/mapper/") to get the device number(s) for a single name.

Root privileges are required.

-- 
Bob Nichols     "NOSPAM" is really part of my email address.
                 Do NOT delete it.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [dm-crypt] How to find out about mapped device name?
  2013-02-04 23:32 [dm-crypt] How to find out about mapped device name? David Li
  2013-02-04 23:50 ` .. ink ..
  2013-02-05  0:30 ` Robert Nichols
@ 2013-02-05  0:42 ` Arno Wagner
  2 siblings, 0 replies; 8+ messages in thread
From: Arno Wagner @ 2013-02-05  0:42 UTC (permalink / raw)
  To: dm-crypt

[-- Attachment #1: Type: text/plain, Size: 937 bytes --]

Attached is a Python-script I wrote some time ago, that 
traverses /dev/mapper/* and finds all the relevant information, 
including mount-point (or absence of).

Arno

On Mon, Feb 04, 2013 at 03:32:21PM -0800, David Li wrote:
> Hi Experts,
> 
> I have already called luksFormat on a raw block dev and luksOpen to set up
> mapped device in /dev/mapper. Is there a command to find out about the
> mapping between the raw dev and mapped dev?

> _______________________________________________
> dm-crypt mailing list
> dm-crypt@saout.de
> http://www.saout.de/mailman/listinfo/dm-crypt


-- 
Arno Wagner,     Dr. sc. techn., Dipl. Inform.,    Email: arno@wagner.name
GnuPG: ID: CB5D9718  FP: 12D6 C03B 1B30 33BB 13CF  B774 E35C 5FA1 CB5D 9718
----
One of the painful things about our time is that those who feel certainty
are stupid, and those with any imagination and understanding are filled
with doubt and indecision. -- Bertrand Russell

[-- Attachment #2: cstat --]
[-- Type: text/plain, Size: 7143 bytes --]

#!/usr/bin/python

# cryptstat.py

# Displays all matching devices in /dev/ for a cryptsetup target

# Calls "cryptsetup status <target>" to get the initial info,
# then determines major/minor of the reported device and does
# a traversal of /dev/ to find all matches.
# Reports an augemented result of "cryptsetup status", also
# including mount points.

# Arguments are any number of cryptsetup targets. Does a validity
# check and refuses targets not found in /dev/mapper/
# Without argument, displays info on all targets found in /dev/mapper/

# (c) Arno Wagner 2010, distributed under the modified BSD license.
# Version 0.03 
# Version history
# 0.03 Adjustments for cryptsrtup 1.2.0
# 0.02 fixed error on unmounted but mapped devices

import sys, os, stat, subprocess, re

dev_path = '/dev' 
mapper_path = '/dev/mapper'
cryptsetup_path = '/usr/sbin/cryptsetup'

# IEC function
def str_iec(v):

  n = float(v)
  if n < 0 :
    raise ValueError, 'Argument of str_iec() must not be negative'

  prefix = '  '     # Actually the postfix of the output, but the
                    # prefix of the unit, if one is attached.

  if n < 1024:
    pass
  elif n < 1024 * 1024:
    n = n / 1024
    prefix = 'ki'
  elif n < 1024 * 1024 * 1024:
    n = n / ( 1024 * 1024 )
    prefix = 'Mi'
  elif n < 1024 * 1024 * 1024 * 1024:
    n = n / ( 1024 * 1024 * 1024 )
    prefix = 'Gi'
  elif n < 1024 * 1024 * 1024 * 1024 * 1024:
    n = n / ( 1024 * 1024 * 1024 * 1024 )
    prefix = 'Ti'
  elif n < 1024 * 1024 * 1024 * 1024 * 1024 * 1024:
    n = n / ( 1024 * 1024 * 1024 * 1024 * 1024 )
    prefix = 'Ei'
  else:
    raise ValueError, 'Argument of str_iec() larger than (2^50)-1'

  s = "%(n)5.1f " % {'n': n}
  s = s + prefix
  return s

# Utility functions

def normalize_path(path):
    """Replaces any number of occurences of '/' with a single '/'"""
    # Note: INefficient, but should not matter
    while path.find('//') > -1:
        path = path.replace('//','/')
    return(path)    

def dir_walk(dir):
    """Returns a list of all filenames with full paths
       (including dirs, specials, etc.) below the argument.
       "." and ".." are omitted. Does not follow symblinks
       to avoid loops, but there is no real loop prevention."""
    names = []
    dir = normalize_path(dir)
    # process current directory 
    current = os.listdir(dir)
    for d in current:
        d = dir + '/' + d
        # Test whether d is a directory. If so, recurse.
        s = os.lstat(d)
        if stat.S_ISDIR( s[stat.ST_MODE] ):
            names.extend(dir_walk(d))  
        else:
            names.append(d)
    return(names)        

def dir_walk_block(dir):
    """Returns the names of all block devices in the subtree at dir"""
    names = dir_walk(dir)
    block_dev = []
    for n in names:
        if stat.S_ISBLK(os.lstat(n)[stat.ST_MODE]):
            block_dev.append(n)
    return(block_dev)

def special_dev_major_minor(n):
    """Returns (major,minor) for a device special file name"""
    s = os.lstat(n)
    if not stat.S_ISBLK(s[stat.ST_MODE]) and \
       not stat.S_ISCHR(s[stat.ST_MODE]) :
       return None
    else:
       maj = os.major(s.st_rdev)
       min = os.minor(s.st_rdev)
       return (maj, min)

def build_block_major_minor_tables(dir):
    """Returns 
       1) a table of filenames that are all block special files
       2) a dict mapping each name to (major,minor)
       3) a dict mapping each (major,minor) pair to a table of filenames"""
    names = []
    name2pair = {}
    pair2name = {}

    names = dir_walk_block(dir)
    for n in names:
        maj, min = special_dev_major_minor(n)
        name2pair[n] = (maj, min)
        if (maj, min) not in pair2name:
            pair2name[(maj, min)] = [n]
        else:
            pair2name[(maj, min)].append(n) 
        
    return (names, name2pair, pair2name)


# Start of main action

# determine list of targets to be processed
targets = []
# first get all in mapper_path except 'control'
mapper_targets = []
mapper_entries = os.listdir(mapper_path)
tmp = []
for e in mapper_entries:
    if e != 'control':
        tmp.append(e)
mapper_entries = tmp

# if argument list empty, copy mapper_entries
if len(sys.argv) == 1:
    targets = mapper_entries
    targets.sort()
else:
    targets = sys.argv[1:]

# Make sure all targets are in mapper_entries 
tbl = {}
for e in mapper_entries:
    tbl[e] = None
for e in targets:
    if e not in tbl:
        print '\nError: Command argument "'+e+'" is not present in '+\
              'mapper dir '+mapper_path+'!\nAborting\n'
        sys.exit(1)    

# print targets

# Create mappings (major,minor) <-> name
names, name2pair, pair2names = build_block_major_minor_tables(dev_path)

print

for t in targets:
    # do call to "cryptsetup status"
    cmd = cryptsetup_path + ' status ' + t
#    print 'calling "' + cmd + '"'
    p = subprocess.Popen(cmd, shell=True, bufsize=0,
             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
             stderr=subprocess.PIPE, close_fds=True)
    (out, err) = p.communicate()
    out_lines = out.splitlines()
    if (p.returncode != 0): raise IOError(p.returncode, err)  
#    print('command output = [' + out + ']')
    # check output line 7 for '  device:  <device name>'
    if not re.match('\ \ device\:\ \ \S+', out_lines[4]):
        print '\nError: Line 7 of cryptsetup output has unexpected form'
        print 'Expected: "  device:  <device name>'
        print 'found: [' + out_lines[4]+ ']'
        print 'complete cryptsetup output:\m'
        print out
        print '\nAborting!\n'
        sys.exit(1)
    device = out_lines[4][11:]
#    print 'device found: [' + device + ']'
    (maj, min) = name2pair[device]
#    print '(maj, min) = ', (maj, min)
    devices = pair2names[(maj, min)]
    dev_str = ''
    for d in devices: dev_str = dev_str + d + '  '
#    print 'devices: ', dev_str
    out_lines[4] = '  devices: '+str((maj, min))+'  '+dev_str
    for l in out_lines: 
      if re.match('\s+size:\s+(\d+)\ssectors.*', l):
        m = re.match('\s+size:\s+(\d+)\ssectors.*', l)
        s = int(m.group(1))
        l = l.rstrip('\n')
        print l + '   ( '+ str_iec(s * 512) + ' Bytes )'
      else:
        print l
    # determine mounts and print them
    mounts = '  mounted:'
    # ';echo' in next command is so that we have at least one line
    # of output to avoid an error from subprocess
    cmd = 'mount | grep '+mapper_path+'/'+t+'; echo'
#    print 'calling "' + cmd + '"'
    p = subprocess.Popen(cmd, shell=True, bufsize=0,
             stdin=subprocess.PIPE, stdout=subprocess.PIPE,
             stderr=subprocess.PIPE, close_fds=True)
    (out, err) = p.communicate()
    out_lines = out.splitlines()
#    print('command output = [' + out + ']')
    if (p.returncode != 0): raise IOError(p.returncode, err)  
    if len(out_lines) == 1:
        mounts = mounts + ' not mounted'
    else:
        for l in out_lines:       
#           print 'l = ', l
           match = re.search(' on (\S+) type', l)
           if match != None:
               mounts = mounts + ' ' + match.group(1)
    print mounts
    print













^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [dm-crypt] How to find out about mapped device name?
  2013-02-05  0:30 ` Robert Nichols
@ 2013-02-05  2:24   ` Milan Broz
  2013-02-05 17:57     ` David Li
  0 siblings, 1 reply; 8+ messages in thread
From: Milan Broz @ 2013-02-05  2:24 UTC (permalink / raw)
  To: dm-crypt

On 02/05/2013 01:30 AM, Robert Nichols wrote:
> On 02/04/2013 05:32 PM, David Li wrote:
>> Hi Experts,
>>
>> I have already called luksFormat on a raw block dev and luksOpen to set up
>> mapped device in /dev/mapper. Is there a command to find out about the mapping
>> between the raw dev and mapped dev?
> 
> "dmsetup deps [name]" will give you the lists of (major, minor) device numbers
> underlying the names in /dev/mapper. Include the "name" argument (without the
> leading "/dev/mapper/") to get the device number(s) for a single name.
> 
> Root privileges are required.

try lsblk (no root required, just it need updated util-linux with this util).

Milan

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [dm-crypt] How to find out about mapped device name?
  2013-02-05  2:24   ` Milan Broz
@ 2013-02-05 17:57     ` David Li
  0 siblings, 0 replies; 8+ messages in thread
From: David Li @ 2013-02-05 17:57 UTC (permalink / raw)
  To: Milan Broz; +Cc: dm-crypt

[-- Attachment #1: Type: text/plain, Size: 1065 bytes --]

Thanks all for the help!
It turns out lsblk mostly fits my requirement. It can also show the
filesystem mounted on the device which is nice.




On Mon, Feb 4, 2013 at 6:24 PM, Milan Broz <gmazyland@gmail.com> wrote:

> On 02/05/2013 01:30 AM, Robert Nichols wrote:
> > On 02/04/2013 05:32 PM, David Li wrote:
> >> Hi Experts,
> >>
> >> I have already called luksFormat on a raw block dev and luksOpen to set
> up
> >> mapped device in /dev/mapper. Is there a command to find out about the
> mapping
> >> between the raw dev and mapped dev?
> >
> > "dmsetup deps [name]" will give you the lists of (major, minor) device
> numbers
> > underlying the names in /dev/mapper. Include the "name" argument
> (without the
> > leading "/dev/mapper/") to get the device number(s) for a single name.
> >
> > Root privileges are required.
>
> try lsblk (no root required, just it need updated util-linux with this
> util).
>
> Milan
> _______________________________________________
> dm-crypt mailing list
> dm-crypt@saout.de
> http://www.saout.de/mailman/listinfo/dm-crypt
>

[-- Attachment #2: Type: text/html, Size: 1802 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-02-05 17:58 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-04 23:32 [dm-crypt] How to find out about mapped device name? David Li
2013-02-04 23:50 ` .. ink ..
2013-02-04 23:57   ` David Li
2013-02-05  0:08     ` .. ink ..
2013-02-05  0:30 ` Robert Nichols
2013-02-05  2:24   ` Milan Broz
2013-02-05 17:57     ` David Li
2013-02-05  0:42 ` Arno Wagner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.