Can select disk bus type when adding new disk to VM
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is failing
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	continuous-integration/drone/push Build is failing
				
			This commit is contained in:
		@@ -6,7 +6,7 @@ use crate::libvirt_rest_structures::LibVirtStructError;
 | 
			
		||||
use crate::libvirt_rest_structures::LibVirtStructError::StructureExtraction;
 | 
			
		||||
use crate::utils::file_size_utils::FileSize;
 | 
			
		||||
use crate::utils::files_utils;
 | 
			
		||||
use crate::utils::vm_file_disks_utils::{VMDiskFormat, VMFileDisk};
 | 
			
		||||
use crate::utils::vm_file_disks_utils::{VMDiskBus, VMDiskFormat, VMFileDisk};
 | 
			
		||||
use lazy_regex::regex;
 | 
			
		||||
use num::Integer;
 | 
			
		||||
 | 
			
		||||
@@ -313,7 +313,11 @@ impl VMInfo {
 | 
			
		||||
                        "vd{}",
 | 
			
		||||
                        ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"][disks.len()]
 | 
			
		||||
                    ),
 | 
			
		||||
                    bus: "virtio".to_string(),
 | 
			
		||||
                    bus: match disk.bus {
 | 
			
		||||
                        VMDiskBus::Virtio => "virtio",
 | 
			
		||||
                        VMDiskBus::Sata => "sata",
 | 
			
		||||
                    }
 | 
			
		||||
                    .to_string(),
 | 
			
		||||
                },
 | 
			
		||||
                readonly: None,
 | 
			
		||||
                boot: DiskBootXML {
 | 
			
		||||
@@ -479,7 +483,7 @@ impl VMInfo {
 | 
			
		||||
                .iter()
 | 
			
		||||
                .filter(|d| d.device == "disk")
 | 
			
		||||
                .map(|d| {
 | 
			
		||||
                    VMFileDisk::load_from_file(&d.source.file)
 | 
			
		||||
                    VMFileDisk::load_from_file(&d.source.file, &d.target.bus)
 | 
			
		||||
                        .expect("Failed to load file disk information!")
 | 
			
		||||
                })
 | 
			
		||||
                .collect(),
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,12 @@ enum VMDisksError {
 | 
			
		||||
    Config(&'static str),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub enum VMDiskBus {
 | 
			
		||||
    Virtio,
 | 
			
		||||
    Sata,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Disk allocation type
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize)]
 | 
			
		||||
#[serde(tag = "format")]
 | 
			
		||||
@@ -30,6 +36,8 @@ pub struct VMFileDisk {
 | 
			
		||||
    pub name: String,
 | 
			
		||||
    /// Disk size, in bytes
 | 
			
		||||
    pub size: FileSize,
 | 
			
		||||
    /// Disk bus
 | 
			
		||||
    pub bus: VMDiskBus,
 | 
			
		||||
    /// Disk format
 | 
			
		||||
    #[serde(flatten)]
 | 
			
		||||
    pub format: VMDiskFormat,
 | 
			
		||||
@@ -41,7 +49,7 @@ pub struct VMFileDisk {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl VMFileDisk {
 | 
			
		||||
    pub fn load_from_file(path: &str) -> anyhow::Result<Self> {
 | 
			
		||||
    pub fn load_from_file(path: &str, bus: &str) -> anyhow::Result<Self> {
 | 
			
		||||
        let file = Path::new(path);
 | 
			
		||||
 | 
			
		||||
        let info = DiskFileInfo::load_file(file)?;
 | 
			
		||||
@@ -61,6 +69,13 @@ impl VMFileDisk {
 | 
			
		||||
                DiskFileFormat::QCow2 { .. } => VMDiskFormat::QCow2,
 | 
			
		||||
                _ => anyhow::bail!("Unsupported image format: {:?}", info.format),
 | 
			
		||||
            },
 | 
			
		||||
 | 
			
		||||
            bus: match bus {
 | 
			
		||||
                "virtio" => VMDiskBus::Virtio,
 | 
			
		||||
                "sata" => VMDiskBus::Sata,
 | 
			
		||||
                _ => anyhow::bail!("Unsupported disk bus type: {bus}"),
 | 
			
		||||
            },
 | 
			
		||||
 | 
			
		||||
            delete: false,
 | 
			
		||||
            from_image: None,
 | 
			
		||||
        })
 | 
			
		||||
 
 | 
			
		||||
@@ -19,9 +19,13 @@ export type VMState =
 | 
			
		||||
 | 
			
		||||
export type VMFileDisk = BaseFileVMDisk & (RawVMDisk | QCow2Disk);
 | 
			
		||||
 | 
			
		||||
export type DiskBusType = "Virtio" | "Sata";
 | 
			
		||||
 | 
			
		||||
export interface BaseFileVMDisk {
 | 
			
		||||
  size: number;
 | 
			
		||||
  name: string;
 | 
			
		||||
  bus: DiskBusType;
 | 
			
		||||
 | 
			
		||||
  delete: boolean;
 | 
			
		||||
 | 
			
		||||
  // For new disk only
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										20
									
								
								virtweb_frontend/src/widgets/forms/DiskBusSelect.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								virtweb_frontend/src/widgets/forms/DiskBusSelect.tsx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
import { DiskBusType } from "../../api/VMApi";
 | 
			
		||||
import { SelectInput } from "./SelectInput";
 | 
			
		||||
 | 
			
		||||
export function DiskBusSelect(p: {
 | 
			
		||||
  editable: boolean;
 | 
			
		||||
  value: DiskBusType;
 | 
			
		||||
  onValueChange: (value: DiskBusType) => void;
 | 
			
		||||
}): React.ReactElement {
 | 
			
		||||
  return (
 | 
			
		||||
    <SelectInput
 | 
			
		||||
      {...p}
 | 
			
		||||
      label="Disk bus type"
 | 
			
		||||
      options={[
 | 
			
		||||
        { label: "virtio", value: "Virtio" },
 | 
			
		||||
        { label: "sata", value: "Sata" },
 | 
			
		||||
      ]}
 | 
			
		||||
      onValueChange={(v) => p.onValueChange(v as any)}
 | 
			
		||||
    />
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
@@ -23,6 +23,7 @@ import { SelectInput } from "./SelectInput";
 | 
			
		||||
import { TextInput } from "./TextInput";
 | 
			
		||||
import { DiskImageSelect } from "./DiskImageSelect";
 | 
			
		||||
import { DiskImage } from "../../api/DiskImageApi";
 | 
			
		||||
import { DiskBusSelect } from "./DiskBusSelect";
 | 
			
		||||
 | 
			
		||||
export function VMDisksList(p: {
 | 
			
		||||
  vm: VMInfo;
 | 
			
		||||
@@ -39,6 +40,7 @@ export function VMDisksList(p: {
 | 
			
		||||
    p.vm.file_disks.push({
 | 
			
		||||
      format: "QCow2",
 | 
			
		||||
      size: 10000 * 1000 * 1000,
 | 
			
		||||
      bus: "Virtio",
 | 
			
		||||
      delete: false,
 | 
			
		||||
      name: `disk${p.vm.file_disks.length}`,
 | 
			
		||||
      new: true,
 | 
			
		||||
@@ -220,6 +222,16 @@ function DiskInfo(p: {
 | 
			
		||||
        }}
 | 
			
		||||
      />
 | 
			
		||||
 | 
			
		||||
      {/* Bus selection */}
 | 
			
		||||
      <DiskBusSelect
 | 
			
		||||
        editable
 | 
			
		||||
        value={p.disk.bus}
 | 
			
		||||
        onValueChange={(v) => {
 | 
			
		||||
          p.disk.bus = v;
 | 
			
		||||
          p.onChange?.();
 | 
			
		||||
        }}
 | 
			
		||||
      />
 | 
			
		||||
 | 
			
		||||
      {p.disk.format === "Raw" && (
 | 
			
		||||
        <CheckboxInput
 | 
			
		||||
          editable
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user