Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through block devices using python

I have around 10 EBS volumes attached to a single instance. Below is e.g., of lsblk for some of them. Here we can't simply mount xvdf or xvdp to some location but actual point is xvdf1, xvdf2, xvdp which are to be mounted. I want to have a script that would allow me to iterate through all the points under xvdf, xvdp etc. using python. I m newbie to python.

[root@ip-172-31-1-65 ec2-user]# lsblk
NAME    MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvdf    202:80   0   35G  0 disk 
├─xvdf1 202:81   0  350M  0 part 
└─xvdf2 202:82   0 34.7G  0 part
xvdp   202:0    0    8G  0 disk 
└─xvdp1 202:1    0    8G  0 part
like image 555
Aniruddha Joshi Avatar asked Sep 12 '25 23:09

Aniruddha Joshi


1 Answers

If you have a relatively new lsblk, you can easily import its json output into a python dictionary, which then open all possibilities for iterations.

# lsblk --version
lsblk from util-linux 2.28.2

For example, you could run the following command to gather all block devices and their children with their name and mount point. Use --help to get a list of all supported columns.

# lsblk --json -o NAME,MOUNTPOINT
{
"blockdevices": [
    {"name": "vda", "mountpoint": null,
        "children": [
            {"name": "vda1", "mountpoint": null,
            "children": [
                {"name": "pv-root", "mountpoint": "/"},
                {"name": "pv-var", "mountpoint": "/var"},
                {"name": "pv-swap", "mountpoint": "[SWAP]"},
            ]
            },
        ]
    }
]
}

So you just have to pipe that output into a file and use python's json parser. Or run the command straight within your script as the example below shows:

#!/usr/bin/python3.7

import json
import subprocess

process = subprocess.run("/usr/bin/lsblk --json -o NAME,MOUNTPOINT".split(), 
                            capture_output=True, text=True)

# blockdevices is a dictionary with all the info from lsblk.
# Manipulate it as you wish.
blockdevices = json.loads(process.stdout)


print(json.dumps(blockdevices, indent=4))
like image 196
brickonthewall Avatar answered Sep 15 '25 13:09

brickonthewall