Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to find something that resembles the output of findmnt but with volumes instead mount-points (example):

TARGET                       TYPE        FSTYPE  
sda 
├─sda1                       Primary     ext4
│ └─/
├─sda2                       Primary     NTFS    
├─sda3                       Primary     ext4 
│ └─/boot
└─sda4                       Extended
  ├─sda5                     Logical     ext4
  │ └─/home
  └─sda6                     Logical     ext4
    └─/var

The show of TYPE and FSTYPE is optional, what interest me more is the tree layout.

The output of lsblk is somewhat incomplete since it doesn't show the differences between Primary, Extended and Logical partitions. (I know I'm being picky here):

lsblk -a
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 186.3G  0 disk 
├─sda1   8:1    0 106.6G  0 part 
├─sda2   8:2    0  52.8G  0 part /media/F66E431C6E42D551
├─sda3   8:3    0  1023M  0 part [SWAP]
├─sda4   8:4    0     1K  0 part 
├─sda5   8:5    0   953M  0 part /boot
├─sda6   8:6    0   487M  0 part [SWAP]
├─sda7   8:7    0    14G  0 part /
└─sda8   8:8    0  10.5G  0 part /home

I know that sda4 extended partition includes sda5, sda6, sda7, sda8 logical partitions; but if I was seeing someone else output I wouldn't know. If instead of part and disk it saids pri log and ext would be a good alternative, at least I know the relationship. But since all are parts... no good.

sudo fdisk -l output:

Disk /dev/sda: 200.0 GB, 200049647616 bytes
255 heads, 63 sectors/track, 24321 cylinders, total 390721968 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
Disk identifier: 0x1549f232

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63   223629311   111814624+   7  HPFS/NTFS/exFAT
/dev/sda2       225724416   336437306    55356445+   7  HPFS/NTFS/exFAT
/dev/sda3       223629312   225724415     1047552   82  Linux swap / Solaris
/dev/sda4       336439294   390721535    27141121    5  Extended
/dev/sda5       336439296   338391039      975872   83  Linux
/dev/sda6       338393088   339390463      498688   82  Linux swap / Solaris
/dev/sda7       339392512   368687103    14647296   83  Linux
/dev/sda8       368689152   390721535    11016192   83  Linux

Partition table entries are not in disk order
share|improve this question

3 Answers 3

up vote 3 down vote accepted

If all you want is an lsblk that shows you primary/logical partitions, you should be able to do this with a combination of fdisk and parsing. fdisk -l if run as root will list all partitions and will mark extended ones with Ext'd:

# fdisk -l | grep dev
Disk /dev/sda: 500.1 GB, 500107862016 bytes
/dev/sda1              63       80324       40131   de  Dell Utility
/dev/sda2   *       81920    30801919    15360000    7  HPFS/NTFS/exFAT
/dev/sda3        30801920   194643539    81920810    7  HPFS/NTFS/exFAT
/dev/sda4       194643601   976773119   391064759+   f  W95 Ext'd (LBA)
/dev/sda5       194643603   198836504     2096451    c  W95 FAT32 (LBA)
/dev/sda6       342951936   960387071   308717568   83  Linux
/dev/sda7       198840320   342949887    72054784   83  Linux
/dev/sda8       960389120   976773119     8192000   82  Linux swap / Solaris

You could then combine that with a little parsing to get the output you want:

$ lsblk -a | perl -lpe 'BEGIN{open(A,"sudo fdisk -l |");
                      while(<A>){next unless /Ext/; 
                                 $k{$1}++ if /^.*?(...\d)\s/; }} 
                      @a=split(/\s+/);
                      $a[0]=~s/\W+//;
                      s/$a[5]/Extended/ if defined($k{$a[0]});'
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0  39.2M  0 part 
├─sda2   8:2    0  14.7G  0 part 
├─sda3   8:3    0  78.1G  0 part /winblows
├─sda4   8:4    0     1K  0 Extended 
├─sda5   8:5    0     2G  0 part 
├─sda6   8:6    0 294.4G  0 part /home
├─sda7   8:7    0  68.7G  0 part /
└─sda8   8:8    0   7.8G  0 part [SWAP]

I think that's the best you can do since findmnt won't show extended partitions since they will never be mounted. Otherwise, you could parse it in the same way.

share|improve this answer
    
the Ext.d is intended in the grep? –  Braiam Sep 12 '13 at 2:27
    
@Braiam yes, I am assuming that all non extended are primary (which is true as far as I know) so I filter out the irrelevant lines. I then save the device name of anything whose line contains the string Ext'd (the . is there because otherwise bash complains about unmatched ') and look for those devices in the output of lsblk. –  terdon Sep 12 '13 at 2:31
    
If I do sudo fdisk -l | grep Ext.d it returns nothing, are you sure of this? While if I do sudo fdisk -l | grep Ext it returns the extended partition. Also, maybe you can escape the ' instead. –  Braiam Sep 12 '13 at 2:32
    
@Braiam weird, it works on my Debian fdisk (util-linux 2.20.1). Unfortunately, I can't escape the ' unless I put the whole thing in a script because the command line perl script is already delineated by ' and I can't use " because I need them in the script too. Perhaps its an alias thing, try using /bin/grep instead. Can you confirm that your fdisk shows the Ext'd string? –  terdon Sep 12 '13 at 2:47
    
@Braiam never mind, I included the pattern match in the Perl code, it should work fine now. –  terdon Sep 12 '13 at 2:54

blkid

You can use the command blkid to show something along those lines:

$ blkid
/dev/sda1: LABEL="SYSTEM_DRV" UUID="XXXXXX" TYPE="ntfs" 
/dev/sda2: LABEL="Windows7_OS" UUID="XXXXX" TYPE="ntfs" 
/dev/sda3: LABEL="Lenovo_Recovery" UUID="XXXX" TYPE="ntfs" 
/dev/sda5: UUID="XXXX" TYPE="ext4" 
/dev/sda6: UUID="XXXX" TYPE="LVM2_member" 
/dev/mapper/vg_grinchy-lv_root: UUID="XXXX" TYPE="ext4" 
/dev/mapper/vg_grinchy-lv_swap: UUID="XXXX" TYPE="swap" 
/dev/mapper/vg_grinchy-lv_home: UUID="XXXX" TYPE="ext4" 

I've removed the UUIDs from above and replaced them with X's. The command blkid also can take arguments if you want different output.

For example:

$ blkid -o list
device                         fs_type      label         mount point                        UUID
----------------------------------------------------------------------------------------------------------------------------------
/dev/sda1                      ntfs         SYSTEM_DRV    (not mounted)                      XXXX
/dev/sda2                      ntfs         Windows7_OS   (not mounted)                      XXXX

lsblk

An alternative tool to blkid is lsblk. You could use the following options to list all the block devices:

$ lsblk -a
NAME                         MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0                          7:0    0         0 loop 
loop1                          7:1    0         0 loop 
loop2                          7:2    0         0 loop 
loop3                          7:3    0         0 loop 
loop4                          7:4    0         0 loop 
loop5                          7:5    0         0 loop 
loop6                          7:6    0         0 loop 
loop7                          7:7    0         0 loop 
sda                            8:0    0 465.8G  0 disk 
├─sda1                         8:1    0   500M  0 part /boot
└─sda2                         8:2    0 465.3G  0 part 
  ├─vg_totoro-lv_root (dm-0) 253:0    0 431.5G  0 lvm  /
  ├─vg_totoro-lv_swap (dm-1) 253:1    0  13.8G  0 lvm  [SWAP]
  └─vg_totoro-lv_home (dm-2) 253:2    0    20G  0 lvm  /home
sr0                           11:0    1  1024M  0 rom  

Check out it's usage, it takes additional options.

share|improve this answer
    
Sadly it doesn't shows me the extended volumes nor the organization. –  Braiam Sep 11 '13 at 21:20
    
@Braiam - I see what you're looking for, you want to see the partitions in the extended partitions too. Hmm... –  slm Sep 11 '13 at 21:23
    
@Braiam - does that help? –  slm Sep 11 '13 at 21:30
    
I looked at that early, didn't got the expected results. It shows me the partitions but I have no way to differentiate logical volumes from primary... mm... more thickening... –  Braiam Sep 11 '13 at 21:34

You can list types of partitions with parted:

$ sudo parted /dev/sda print
Model: ATA ST3320613AS (scsi)
Dysk /dev/sda: 320GB
Rozmiar sektora (logiczny/fizyczny): 512B/512B
Tablica partycji: msdos

Numer  Początek  Koniec  Rozmiar  Typ       System plików   Flaga
 1     1049kB    318GB   318GB    primary   ext4            ładowalna
 2     318GB     320GB   2145MB   extended
 5     318GB     319GB   1074MB   logical   linux-swap(v1)
 6     319GB     320GB   1071MB   logical   ext2

There is no tree but it may help, since as you stated it would be a good to at least know the relationship. If you want to use GUI you can try with GParted:

enter image description here

share|improve this answer
    
I looked at that early, didn't got the expected results. It shows me the partitions but I have no way to differentiate logical volumes from primary... mm... more thickening... –  Braiam Sep 11 '13 at 21:34
    
@Braiam, I updated my answer. Unfortunately, I didn't found any command line way to do tree listing. –  Nykakin Sep 11 '13 at 23:56
    
Seems that it will be the only solution... btw, fix that NTFS partition :P –  Braiam Sep 11 '13 at 23:57
    
It's just a screen from the web, as you see it's different from command line output :) –  Nykakin Sep 11 '13 at 23:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.