1use std::io; 2use std::path::{Path, PathBuf}; 3use std::process::Command; 4 5pub fn write(dir: &Path, pubkey: &str, uid: u32) -> io::Result<PathBuf> { 6 let src = dir.join("seed"); 7 std::fs::create_dir_all(&src)?; 8 std::fs::write( 9 src.join("meta-data"), 10 "instance-id: cast-bake\nlocal-hostname: cast\n", 11 )?; 12 std::fs::write(src.join("user-data"), user_data(pubkey.trim(), uid))?; 13 let iso = dir.join("seed.iso"); 14 let ok = Command::new("xorriso") 15 .args([ 16 "-as", "mkisofs", "-quiet", "-volid", "cidata", "-joliet", "-rock", "-output", 17 ]) 18 .arg(&iso) 19 .arg(src.join("user-data")) 20 .arg(src.join("meta-data")) 21 .status()? 22 .success(); 23 if ok { 24 Ok(iso) 25 } else { 26 Err(io::Error::other( 27 "xorriso failed to build the cloud-init seed", 28 )) 29 } 30} 31 32fn user_data(pubkey: &str, uid: u32) -> String { 33 format!( 34 r#"#cloud-config 35users: 36 - name: cast 37 uid: {uid} 38 shell: /bin/bash 39 sudo: "ALL=(ALL) NOPASSWD:ALL" 40 lock_passwd: true 41 ssh_authorized_keys: 42 - {pubkey} 43ssh_pwauth: false 44runcmd: 45 - [sh, -c, "mkdir -p /mnt/stage && mount -t 9p -o trans=virtio,version=9p2000.L stage /mnt/stage && bash /mnt/stage/provision.sh; poweroff"] 46"# 47 ) 48}