This commit is contained in:
		@@ -20,7 +20,7 @@ enum DisksError {
 | 
			
		||||
 | 
			
		||||
/// Type of disk allocation
 | 
			
		||||
#[derive(Copy, Clone, Debug, serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub enum DiskAllocType {
 | 
			
		||||
pub enum VMDiskAllocType {
 | 
			
		||||
    Fixed,
 | 
			
		||||
    Sparse,
 | 
			
		||||
}
 | 
			
		||||
@@ -28,28 +28,28 @@ pub enum DiskAllocType {
 | 
			
		||||
/// Disk allocation type
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize)]
 | 
			
		||||
#[serde(tag = "format")]
 | 
			
		||||
pub enum DiskFormat {
 | 
			
		||||
pub enum VMDiskFormat {
 | 
			
		||||
    Raw {
 | 
			
		||||
        /// Type of disk allocation
 | 
			
		||||
        alloc_type: DiskAllocType,
 | 
			
		||||
        alloc_type: VMDiskAllocType,
 | 
			
		||||
    },
 | 
			
		||||
    QCow2,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Serialize, serde::Deserialize)]
 | 
			
		||||
pub struct FileDisk {
 | 
			
		||||
pub struct VMFileDisk {
 | 
			
		||||
    /// Disk name
 | 
			
		||||
    pub name: String,
 | 
			
		||||
    /// Disk size, in bytes
 | 
			
		||||
    pub size: usize,
 | 
			
		||||
    /// Disk format
 | 
			
		||||
    #[serde(flatten)]
 | 
			
		||||
    pub format: DiskFormat,
 | 
			
		||||
    pub format: VMDiskFormat,
 | 
			
		||||
    /// Set this variable to true to delete the disk
 | 
			
		||||
    pub delete: bool,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FileDisk {
 | 
			
		||||
impl VMFileDisk {
 | 
			
		||||
    pub fn load_from_file(path: &str) -> anyhow::Result<Self> {
 | 
			
		||||
        let file = Path::new(path);
 | 
			
		||||
 | 
			
		||||
@@ -66,13 +66,13 @@ impl FileDisk {
 | 
			
		||||
            },
 | 
			
		||||
 | 
			
		||||
            format: match info.format {
 | 
			
		||||
                DiskFileFormat::Raw { is_sparse } => DiskFormat::Raw {
 | 
			
		||||
                DiskFileFormat::Raw { is_sparse } => VMDiskFormat::Raw {
 | 
			
		||||
                    alloc_type: match is_sparse {
 | 
			
		||||
                        true => DiskAllocType::Sparse,
 | 
			
		||||
                        false => DiskAllocType::Fixed,
 | 
			
		||||
                        true => VMDiskAllocType::Sparse,
 | 
			
		||||
                        false => VMDiskAllocType::Fixed,
 | 
			
		||||
                    },
 | 
			
		||||
                },
 | 
			
		||||
                DiskFileFormat::QCow2 { .. } => DiskFormat::QCow2,
 | 
			
		||||
                DiskFileFormat::QCow2 { .. } => VMDiskFormat::QCow2,
 | 
			
		||||
                _ => anyhow::bail!("Unsupported image format: {:?}", info.format),
 | 
			
		||||
            },
 | 
			
		||||
            delete: false,
 | 
			
		||||
@@ -102,8 +102,8 @@ impl FileDisk {
 | 
			
		||||
    pub fn disk_path(&self, id: XMLUuid) -> PathBuf {
 | 
			
		||||
        let domain_dir = AppConfig::get().vm_storage_path(id);
 | 
			
		||||
        let file_name = match self.format {
 | 
			
		||||
            DiskFormat::Raw { .. } => self.name.to_string(),
 | 
			
		||||
            DiskFormat::QCow2 => format!("{}.qcow2", self.name),
 | 
			
		||||
            VMDiskFormat::Raw { .. } => self.name.to_string(),
 | 
			
		||||
            VMDiskFormat::QCow2 => format!("{}.qcow2", self.name),
 | 
			
		||||
        };
 | 
			
		||||
        domain_dir.join(&file_name)
 | 
			
		||||
    }
 | 
			
		||||
@@ -134,15 +134,15 @@ impl FileDisk {
 | 
			
		||||
 | 
			
		||||
        // Prepare command to create file
 | 
			
		||||
        let res = match self.format {
 | 
			
		||||
            DiskFormat::Raw { alloc_type } => {
 | 
			
		||||
            VMDiskFormat::Raw { alloc_type } => {
 | 
			
		||||
                let mut cmd = Command::new("/usr/bin/dd");
 | 
			
		||||
                cmd.arg("if=/dev/zero")
 | 
			
		||||
                    .arg(format!("of={}", file.to_string_lossy()))
 | 
			
		||||
                    .arg("bs=1M");
 | 
			
		||||
 | 
			
		||||
                match alloc_type {
 | 
			
		||||
                    DiskAllocType::Fixed => cmd.arg(format!("count={}", self.size_mb())),
 | 
			
		||||
                    DiskAllocType::Sparse => {
 | 
			
		||||
                    VMDiskAllocType::Fixed => cmd.arg(format!("count={}", self.size_mb())),
 | 
			
		||||
                    VMDiskAllocType::Sparse => {
 | 
			
		||||
                        cmd.arg(format!("seek={}", self.size_mb())).arg("count=0")
 | 
			
		||||
                    }
 | 
			
		||||
                };
 | 
			
		||||
@@ -150,7 +150,7 @@ impl FileDisk {
 | 
			
		||||
                cmd.output()?
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            DiskFormat::QCow2 => {
 | 
			
		||||
            VMDiskFormat::QCow2 => {
 | 
			
		||||
                let mut cmd = Command::new(constants::QEMU_IMAGE_PROGRAM);
 | 
			
		||||
                cmd.arg("create")
 | 
			
		||||
                    .arg("-f")
 | 
			
		||||
@@ -182,6 +182,7 @@ impl FileDisk {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, serde::Serialize)]
 | 
			
		||||
#[serde(tag = "format")]
 | 
			
		||||
pub enum DiskFileFormat {
 | 
			
		||||
    Raw { is_sparse: bool },
 | 
			
		||||
    QCow2 { virtual_size: usize },
 | 
			
		||||
@@ -193,6 +194,7 @@ pub enum DiskFileFormat {
 | 
			
		||||
#[derive(serde::Serialize)]
 | 
			
		||||
pub struct DiskFileInfo {
 | 
			
		||||
    file_size: usize,
 | 
			
		||||
    #[serde(flatten)]
 | 
			
		||||
    format: DiskFileFormat,
 | 
			
		||||
    file_name: String,
 | 
			
		||||
    name: String,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user