Skip to content

Instantly share code, notes, and snippets.

@rmrfslashbin
Last active February 26, 2021 13:28
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 rmrfslashbin/5dae3facab6f7a44b4cdb720c34482fd to your computer and use it in GitHub Desktop.
Save rmrfslashbin/5dae3facab6f7a44b4cdb720c34482fd to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
##
# This script walks block storage devices and locates
# EC2 instance stores. It then lables the disks as
# LMV2 physical volumes (PV), creates an LVM2 volume
# group (VG), then an LVM2 logical volume (LV) and
# finally formats the new LV as XFS.
##
import subprocess
from pathlib import Path
# Consts
INST_STOR='Amazon EC2 NVMe Instance Storage'
VG='vg_inst_store'
LV='lv_inst_store'
# Walk block devices to find instance stores
root = Path('/sys/block')
nvme_inst = []
# For each block device...
for x in root.iterdir():
mod = Path(x, 'device', 'model')
# get the model
if mod.is_file():
with open(mod, 'r') as fh:
dev_mod = fh.read()
# Compare to the magic
if dev_mod.strip() == INST_STOR:
# if it's an instance store...
nvme_inst.append(x.name)
# PV Create
# for each instance store nvme disk...
for nvme in nvme_inst:
dev = f"/dev/{nvme}"
subprocess.run(['pvcreate', dev], check=True)
print(f"PV created: {nvme}")
# VG Create
subprocess.run(['vgcreate', VG] + [f"/dev/{y}" for y in nvme_inst], check=True)
print(f"Created {VG}")
# LV Create
subprocess.run(['lvcreate', '-l', '100%FREE', '-n', LV, VG], check=True)
print(f"Created {LV}")
# mkfs.xfs
lv_disk = f"/dev/{VG}/{LV}"
subprocess.run(['mkfs.xfs', lv_disk], check=True)
print(f"XFS formatted {lv_disk}")
print(f"You may now 'mount {lv_disk} /mnt/my_mount_point'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment