A simple, zero-configuration script to quickly boot FreeBSD ISO images using QEMU

Add support for creating drive images with specified size in QEMU options

+45
+45
main.ts
··· 12 12 memory: string; 13 13 drive?: string; 14 14 diskFormat: string; 15 + size?: string; 15 16 } 16 17 17 18 async function downloadIso(url: string, outputPath?: string): Promise<string> { ··· 118 119 return input; 119 120 } 120 121 122 + async function createDriveImageIfNeeded( 123 + { 124 + drive: path, 125 + diskFormat: format, 126 + size, 127 + }: Options, 128 + ): Promise<void> { 129 + if (await Deno.stat(path!).catch(() => false)) { 130 + console.log( 131 + chalk.yellowBright( 132 + `Drive image ${path} already exists, skipping creation.`, 133 + ), 134 + ); 135 + return; 136 + } 137 + 138 + const cmd = new Deno.Command("qemu-img", { 139 + args: ["create", "-f", format, path!, size!], 140 + stdin: "inherit", 141 + stdout: "inherit", 142 + stderr: "inherit", 143 + }); 144 + 145 + const status = await cmd.spawn().status; 146 + if (!status.success) { 147 + console.error(chalk.redBright("Failed to create drive image.")); 148 + Deno.exit(status.code); 149 + } 150 + 151 + console.log(chalk.greenBright(`Created drive image at ${path}`)); 152 + } 153 + 121 154 if (import.meta.main) { 122 155 await new Command() 123 156 .name("freebsd-up") ··· 144 177 default: "raw", 145 178 }, 146 179 ) 180 + .option( 181 + "-s, --size <size:string>", 182 + "Size of the disk image to create if it doesn't exist", 183 + { 184 + default: "20G", 185 + }, 186 + ) 147 187 .example( 148 188 "Default usage", 149 189 "freebsd-up", ··· 171 211 isoPath = await downloadIso(resolvedInput, options.output); 172 212 } 173 213 214 + if (options.drive) { 215 + await createDriveImageIfNeeded(options); 216 + } 217 + 174 218 await runQemu(isoPath, { 175 219 cpu: options.cpu, 176 220 memory: options.memory, 177 221 cpus: options.cpus, 178 222 drive: options.drive, 179 223 diskFormat: options.diskFormat, 224 + size: options.size, 180 225 }); 181 226 }) 182 227 .parse(Deno.args);