MAAS : How can I import a storage layout from one machine and export it to some other nodes?

I’d like to customize the layout of disk partitions for multiple machines on MAAS. The way that I know with UI is to 1) change "Storage layout" to "No storage (blank) layout", and 2) Click "Add partition…" to add custom partitions.

Is there a way to import a storage layout from one machine and export it to some other nodes, ideally by CLI (or API)?

UPDATE 5/2 14:00

I’ve read the How to manage partitions, and I’m now able to create custom partitions by the following commands. I’ve got closer to what I want to achieve, but this is not perfect yet.

maas admin partitions create <node_id> <device_id> size=512000000
maas admin partition format <node_id> <device_id> <partition_id> fstype=fat32
maas admin partition mount <node_id> <device_id> <partition_id> mount_point=/boot/efi
maas admin partitions create <node_id> <device_id> size=100000000000
maas admin partition format <node_id> <device_id> <partition_id> fstype=ext4
maas admin partition mount <node_id> <device_id> <partition_id> mount_point=/
maas admin partitions create dkrymn 56 size=<remaining_size>

I appreciate any feedback. Thank you!

UPDATE 5/2 15:15

I have ended up writing a bash script that satisfies my needs. I’ve written it as an answer down below.

Asked By: kjtanaka

||

I (myself) have ended up creating a bash script as shown below, which is good enough for satisfying my needs at this moment.

If anyone knows a better/smarter way, please comment. Thank you!

#!/bin/bash
#
# maas_partitions_type_a.sh - deletes existing partitions and 
# create partitons for type A machines.
#

if [ $# -ne 1 ]; then
    echo "./maas_partitions_type_a.sh <hostname>"
    exit 1
fi

MAAS_API_URL=https://my_maas.example.com:5240/MAAS/api/2.0
maas login admin $MAAS_API_URL $(cat ~/.config/maas/api-key)

: Get HOSTNAME, SYSTEM_ID, and DEVICE_ID
HOSTNAME=$1
SYSTEM_ID=$(maas admin machines read hostname=$HOSTNAME | jq '.[].system_id')
SYSTEM_ID=${SYSTEM_ID//'"'}
DEVICE_ID=$(maas admin block-devices read $SYSTEM_ID | jq '.[].id')

: Clean up existing partitions
# (Extra CAUTION: This will wipe out all partitions)
maas admin partitions read $SYSTEM_ID $DEVICE_ID | jq '.[].id' | while read part_id; do
    maas admin partition delete $SYSTEM_ID $DEVICE_ID $part_id
done

: 512MB for /boot/efi with fat32
PART_ID=$(maas admin partitions create $SYSTEM_ID $DEVICE_ID size=512000000| jq '.id')
maas admin partition format $SYSTEM_ID $DEVICE_ID $PART_ID fstype=fat32
maas admin partition mount $SYSTEM_ID $DEVICE_ID $PART_ID mount_point=/boot/efi

: 300GB for / with ext4
PART_ID=$(maas admin partitions create $SYSTEM_ID $DEVICE_ID size=300000000000| jq '.id')
maas admin partition format $SYSTEM_ID $DEVICE_ID $PART_ID fstype=ext4
maas admin partition mount $SYSTEM_ID $DEVICE_ID $PART_ID mount_point=/

: Remaining
maas admin partitions create $SYSTEM_ID $DEVICE_ID size=659680000000

Don’t be TOO BRAVE, but you can simply run this script in a for loop like this:

for node in node{01..32}; do
  ./maas_partitions_type_a.sh $node
done
Answered By: kjtanaka
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.