Would be something like like;
DISKS="data1 data2 data3"
PROJECT=arg[0]
#Note my addition of the project here, better to build support now than later.
lxc project switch ${PROJECT}
PROFILE_NAME="${VM}-${PROJECT}-volumes"
lxc profile create "${PROFILE_NAME}"
foreach($DISKS){
DISK_NAME="${PROJECT}-${VM}-${DISK}"
lxc storage volume create "${DISK_NAME}" ...
lxc profile device add ...
}
lxc launch ubuntu: c1 -p default -p "${PROFILE_NAME}"
Though with your follow up request (below) im not sure its any better than using instance config.
The follow up question (if i’ve understood it correctly) implies you want to delete the volume when the instance is deleted. Attaching a disk device to a profile would imply you want more than one instance to attach the volumes (but if more than one instance is using the profile with the volumes you wont be able to delete the profile & volumes without deleting the other instances first).
I dont think LXD has an ephemeral
key on storage (it might, i’m not a dev, there may be hacky workaround here) so I dont think you can implement this without writing code;
# pseudo code
client = new LXDClient()
volumeNames = [
"data1",
"data2",
"data3"
]
client.events.listen(function(event){
//TODO Make sure the operation has finished with success before trying this.
if(event.metadata.action === "instance-delete"){
//TODO The below will fail if > 1 instance uses the profile
client.profile.delete(`${event.VM}-${event.PROJECT}-volumes`)
foreach(volumeNames as volume){
client.volumes.delete(`${event.VM}-${event.PROJECT}-${volume}`)
}
}
});
There may be naming inconsistencies between the suggested first script & follow script, this is on you to address as this was supposed to serve as a “jumping off” point.