Skip to content

Commit 7c40663

Browse files
acatangiualxiord
authored andcommitted
vmm: std::convert::From<DriveError> for VmmActionError
Decorate each type of DriveError with the correct ErrorKind. Signed-off-by: Adrian Catangiu <[email protected]>
1 parent 26a3d10 commit 7c40663

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

vmm/src/lib.rs

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ pub enum VmmActionError {
212212
VsockConfig(ErrorKind, VsockError),
213213
}
214214

215+
// It's convenient to turn DriveErrors into VmmActionErrors directly.
216+
impl std::convert::From<DriveError> for VmmActionError {
217+
fn from(e: DriveError) -> Self {
218+
let kind = match e {
219+
// User errors.
220+
DriveError::CannotOpenBlockDevice
221+
| DriveError::InvalidBlockDeviceID
222+
| DriveError::InvalidBlockDevicePath
223+
| DriveError::BlockDevicePathAlreadyExists
224+
| DriveError::BlockDeviceUpdateFailed
225+
| DriveError::OperationNotAllowedPreBoot
226+
| DriveError::UpdateNotAllowedPostBoot
227+
| DriveError::RootBlockDeviceAlreadyAdded => ErrorKind::User,
228+
};
229+
VmmActionError::DriveConfig(kind, e)
230+
}
231+
}
232+
215233
// It's convenient to turn StartMicrovmErrors into VmmActionErrors directly.
216234
impl std::convert::From<StartMicrovmError> for VmmActionError {
217235
fn from(e: StartMicrovmError) -> Self {
@@ -1681,29 +1699,23 @@ impl Vmm {
16811699
let block_device_index = self
16821700
.block_device_configs
16831701
.get_index_of_drive_id(&drive_id)
1684-
.ok_or(VmmActionError::DriveConfig(
1685-
ErrorKind::User,
1686-
DriveError::InvalidBlockDeviceID,
1687-
))?;
1702+
.ok_or(DriveError::InvalidBlockDeviceID)?;
16881703

16891704
let file_path = PathBuf::from(path_on_host);
16901705
// Try to open the file specified by path_on_host using the permissions of the block_device.
16911706
let disk_file = OpenOptions::new()
16921707
.read(true)
16931708
.write(!self.block_device_configs.config_list[block_device_index].is_read_only())
16941709
.open(&file_path)
1695-
.map_err(|_| {
1696-
VmmActionError::DriveConfig(ErrorKind::User, DriveError::CannotOpenBlockDevice)
1697-
})?;
1710+
.map_err(|_| DriveError::CannotOpenBlockDevice)?;
16981711

16991712
// Update the path of the block device with the specified path_on_host.
17001713
self.block_device_configs.config_list[block_device_index].path_on_host = file_path;
17011714

17021715
// When the microvm is running, we also need to update the drive handler and send a
17031716
// rescan command to the drive.
17041717
if self.is_instance_initialized() {
1705-
self.update_drive_handler(&drive_id, disk_file)
1706-
.map_err(|e| VmmActionError::DriveConfig(ErrorKind::User, e))?;
1718+
self.update_drive_handler(&drive_id, disk_file)?;
17071719
self.rescan_block_device(&drive_id)?;
17081720
}
17091721
Ok(VmmData::Empty)
@@ -1715,10 +1727,7 @@ impl Vmm {
17151727
) -> std::result::Result<VmmData, VmmActionError> {
17161728
// Rescan can only happen after the guest is booted.
17171729
if !self.is_instance_initialized() {
1718-
return Err(VmmActionError::DriveConfig(
1719-
ErrorKind::User,
1720-
DriveError::OperationNotAllowedPreBoot,
1721-
));
1730+
Err(DriveError::OperationNotAllowedPreBoot)?;
17221731
}
17231732

17241733
// Safe to unwrap() because mmio_device_manager is initialized in init_devices(), which is
@@ -1728,12 +1737,8 @@ impl Vmm {
17281737
Some(&address) => {
17291738
for drive_config in self.block_device_configs.config_list.iter() {
17301739
if drive_config.drive_id == *drive_id {
1731-
let metadata = metadata(&drive_config.path_on_host).map_err(|_| {
1732-
VmmActionError::DriveConfig(
1733-
ErrorKind::User,
1734-
DriveError::BlockDeviceUpdateFailed,
1735-
)
1736-
})?;
1740+
let metadata = metadata(&drive_config.path_on_host)
1741+
.map_err(|_| DriveError::BlockDeviceUpdateFailed)?;
17371742
let new_size = metadata.len();
17381743
if new_size % virtio::block::SECTOR_SIZE != 0 {
17391744
warn!(
@@ -1746,23 +1751,12 @@ impl Vmm {
17461751
return device_manager
17471752
.update_drive(address, new_size)
17481753
.map(|_| VmmData::Empty)
1749-
.map_err(|_| {
1750-
VmmActionError::DriveConfig(
1751-
ErrorKind::User,
1752-
DriveError::BlockDeviceUpdateFailed,
1753-
)
1754-
});
1754+
.map_err(|_| VmmActionError::from(DriveError::BlockDeviceUpdateFailed));
17551755
}
17561756
}
1757-
Err(VmmActionError::DriveConfig(
1758-
ErrorKind::User,
1759-
DriveError::BlockDeviceUpdateFailed,
1760-
))
1757+
Err(VmmActionError::from(DriveError::BlockDeviceUpdateFailed))
17611758
}
1762-
_ => Err(VmmActionError::DriveConfig(
1763-
ErrorKind::User,
1764-
DriveError::InvalidBlockDeviceID,
1765-
)),
1759+
_ => Err(VmmActionError::from(DriveError::InvalidBlockDeviceID)),
17661760
}
17671761
}
17681762

@@ -1773,16 +1767,13 @@ impl Vmm {
17731767
block_device_config: BlockDeviceConfig,
17741768
) -> std::result::Result<VmmData, VmmActionError> {
17751769
if self.is_instance_initialized() {
1776-
return Err(VmmActionError::DriveConfig(
1777-
ErrorKind::User,
1778-
DriveError::UpdateNotAllowedPostBoot,
1779-
));
1770+
Err(DriveError::UpdateNotAllowedPostBoot)?;
17801771
}
17811772

17821773
self.block_device_configs
17831774
.insert(block_device_config)
17841775
.map(|_| VmmData::Empty)
1785-
.map_err(|e| VmmActionError::DriveConfig(ErrorKind::User, e))
1776+
.map_err(VmmActionError::from)
17861777
}
17871778

17881779
fn init_logger(

0 commit comments

Comments
 (0)