Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{env, path::PathBuf};

use serde::{Deserialize, Serialize};

use crate::{BootOption::CloudImage, os::Os};

pub mod access;
pub mod app;
pub mod cloudinit;
Expand Down Expand Up @@ -66,9 +68,15 @@ impl TryFrom<&str> for Arch {
}
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BootOption {
#[default]
CloudImage,
CloudImage(Os),
LocalFile,
DirectLinuxBoot((PathBuf, Option<PathBuf>, Option<String>)),
}

impl Default for BootOption {
fn default() -> Self {
CloudImage(Os::default())
}
}
14 changes: 13 additions & 1 deletion src/qemu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
};

use crate::{
Arch, KVM_ENABLED,
Arch, BootOption, KVM_ENABLED,
event::Event,
vm::{VM, VmId},
};
Expand Down Expand Up @@ -109,6 +109,18 @@ impl Qemu {
command.arg("-drive").arg(drive.to_qemu_arg());
}

if let BootOption::DirectLinuxBoot((kernel, initrd, append)) = &vm.boot_option {
command.arg("-kernel").arg(kernel);

if let Some(initrd) = initrd {
command.arg("-initrd").arg(initrd);
}

if let Some(append) = append {
command.arg("-append").arg(append);
}
}

command.stdout(Stdio::null());
command.stderr(Stdio::piped());

Expand Down
24 changes: 14 additions & 10 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ use crate::{

pub type VmId = uuid::Uuid;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Firmware {
Bios,
Uefi,
None,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VM {
pub id: VmId,
pub name: String,
pub boot_option: BootOption,
pub os: Option<Os>,
pub arch: Arch,
pub vcpu: u16,
pub memory: u32,
Expand Down Expand Up @@ -196,7 +202,8 @@ impl VM {
path.pop();
}

if data.enable_uefi
if let BootOption::LocalFile | BootOption::CloudImage(_) = data.boot_option
&& data.firmware == Firmware::Uefi
&& let Ok((code, vars)) = firmware::get_uefi_file_path(data.arch)
{
path.push("uefi_vars.fd");
Expand Down Expand Up @@ -273,7 +280,6 @@ impl VM {
id,
name: data.name,
boot_option: data.boot_option,
os: data.os,
arch: data.arch,
vcpu: data.vcpu,
memory: data.memory,
Expand Down Expand Up @@ -434,9 +440,8 @@ impl VM {
}

pub fn start(&mut self, sender: Sender<Event>) -> Result<()> {
if self.boot_option == BootOption::CloudImage {
let distro = self.os.unwrap();
if !distro.is_available(self.arch) {
if let BootOption::CloudImage(os) = self.boot_option {
if !os.is_available(self.arch) {
thread::spawn({
let vm = self.clone();
let sender = sender.clone();
Expand All @@ -447,7 +452,7 @@ impl VM {
vm.downloading
.store(true, std::sync::atomic::Ordering::Relaxed);
}
if let Err(error) = distro.download(vm.arch, sender.clone(), vm.id) {
if let Err(error) = os.download(vm.arch, sender.clone(), vm.id) {
vm.downloading
.store(false, std::sync::atomic::Ordering::Relaxed);
let _ = sender
Expand All @@ -461,7 +466,7 @@ impl VM {
return Ok(());
} else {
if !self.get_boot_file().exists()
&& let Err(e) = fs::copy(distro.get_file_path(self.arch), self.get_boot_file())
&& let Err(e) = fs::copy(os.get_file_path(self.arch), self.get_boot_file())
{
let _ = sender.send(Event::Notification(Notification::error(e)));
}
Expand Down Expand Up @@ -650,8 +655,7 @@ impl VM {
]),
];

if self.boot_option == BootOption::CloudImage {
let os = self.os.unwrap();
if let BootOption::CloudImage(os) = self.boot_option {
items.push(ListItem::from(vec![
Line::from(vec![
Span::from("OS").bold().fg(Color::Yellow),
Expand Down
5 changes: 2 additions & 3 deletions src/vmbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::{
fs::Filesystem,
graphics::Graphics,
network::Network,
os::Os::{self},
storage::{Disk, Interface},
vm::Firmware,
vmbuilder::{advanced::Advanced, quick::Quick},
};

Expand Down Expand Up @@ -60,11 +60,10 @@ pub struct VMBuildData {
pub name: String,
pub cloudinit: Option<PathBuf>,
pub boot_file: Option<(PathBuf, Interface)>,
pub os: Option<Os>,
pub vcpu: u16,
pub memory: u32,
pub networks: Vec<Network>,
pub enable_uefi: bool,
pub firmware: Firmware,
pub disks: Vec<Disk>,
pub fs: Vec<Filesystem>,
pub remote_access: Option<RemoteAccess>,
Expand Down
29 changes: 15 additions & 14 deletions src/vmbuilder/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ use ratatui::{
};

use crate::{
Arch,
event::Event,
graphics, network,
os::Os::ArchLinux,
vmbuilder::{VMBuildData, access, fs, hardware, overview, port, storage},
};

Expand Down Expand Up @@ -77,11 +75,10 @@ impl Advanced {
name: self.overview.name(),
cloudinit: self.overview.cloudinit(),
boot_file: self.overview.boot_file(),
os: self.overview.os(),
vcpu: self.hardware.vcpu(),
memory: self.hardware.memory(),
arch: self.hardware.arch(),
enable_uefi: self.hardware.enable_uefi(),
firmware: self.hardware.firmware(),
networks: self.network.networks().take(),
disks: self.storage.disks(),
fs: self.fs.filesystems(),
Expand Down Expand Up @@ -128,6 +125,7 @@ impl Advanced {
Section::Overview => {
if self.validate_overview_section() {
self.focused_section = Section::Hardware;
self.hardware.refresh(self.overview.boot_option());
}
}
Section::Hardware => {
Expand Down Expand Up @@ -163,9 +161,6 @@ impl Advanced {
KeyCode::BackTab => match self.focused_section {
Section::Overview => {
if self.validate_overview_section() {
if self.overview.os() == Some(ArchLinux) {
self.hardware.set_arch(Arch::X86_64);
}
self.focused_section = Section::Summary;
}
}
Expand All @@ -174,34 +169,40 @@ impl Advanced {
self.focused_section = Section::Overview;
}
}
Section::Storage => self.focused_section = Section::Hardware,
Section::Storage => {
self.focused_section = Section::Hardware;
self.hardware.refresh(self.overview.boot_option());
}
Section::Filesystem => {
self.focused_section = Section::Storage;
}
Section::Network => {
self.port_fowrwaring.refresh();
self.focused_section = Section::Filesystem
self.focused_section = Section::Filesystem;
}
Section::PortForwarding => {
self.focused_section = Section::Network;
}
Section::PortForwarding => self.focused_section = Section::Network,
Section::RemoteAccess => {
if self.validate_remote_access() {
self.focused_section = Section::PortForwarding
self.focused_section = Section::PortForwarding;
}
}
Section::Graphics => {
if self.validate_graphics_section() {
self.focused_section = Section::RemoteAccess;
}
}
Section::Summary => self.focused_section = Section::Graphics,
Section::Summary => {
self.focused_section = Section::Graphics;
}
},
_ => match &self.focused_section {
Section::Overview => {
self.overview.handle_key_events(key_event);
}
Section::Hardware => {
self.hardware
.handle_key_events(key_event, self.overview.os());
self.hardware.handle_key_events(key_event);
}
Section::Storage => {
self.storage
Expand Down
Loading
Loading