Skip to content

Commit 25d23f7

Browse files
Add rustdos and intra-doc links to media.rs (#144)
* Add rustdocs to basic structs * add rustdoc for get_mount_devices * Add rustdocs for parse_ovf_env and read_ovf_env_to_string * Add some more rustdocs and tests * Fixes * Add module level rustdoc * Update libazureinit/src/media.rs * Add more links Co-authored-by: Dongsu Park <dpark@linux.microsoft.com>
1 parent 0267ea8 commit 25d23f7

File tree

1 file changed

+152
-3
lines changed

1 file changed

+152
-3
lines changed

libazureinit/src/media.rs

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
//! This module provides functionality for handling media devices, including mounting,
5+
//! unmounting, and reading [`OVF`] (Open Virtualization Format) environment data. It defines
6+
//! the [`Media`] struct with state management for [`Mounted`] and [`Unmounted`] states, as well
7+
//! as utility functions for parsing [`OVF`] environment data and retrieving mounted devices
8+
//! with CDROM-type filesystems.
9+
//!
10+
//! # Overview
11+
//!
12+
//! The `media` module is designed to manage media devices in a cloud environment. It
13+
//! includes functionality to mount and unmount media devices, read [`OVF`] environment data,
14+
//! and parse the data into structured formats. This is particularly useful for provisioning
15+
//! virtual machines with specific configurations.
16+
//!
17+
//! # Key Components
18+
//!
19+
//! - [`Media`]: A struct representing a media device, with state management for [`Mounted`] and [`Unmounted`] states.
20+
//! - [`Mounted`] and [`Unmounted`]: Zero-sized structs used to indicate the state of a [`Media`] instance.
21+
//! - [`parse_ovf_env`]: A function to parse [`OVF`] environment data from a string.
22+
//! - [`mount_parse_ovf_env`]: A function to mount a media device, read its [`OVF`] environment data, and return the parsed data.
23+
//! - [`get_mount_device`]: A function to retrieve a list of mounted devices with CDROM-type filesystems.
24+
//!
25+
//! [`Media`]: struct.Media.html
26+
//! [`Mounted`]: struct.Mounted.html
27+
//! [`Unmounted`]: struct.Unmounted.html
28+
//! [`parse_ovf_env`]: fn.parse_ovf_env.html
29+
//! [`mount_parse_ovf_env`]: fn.mount_parse_ovf_env.html
30+
//! [`get_mount_device`]: fn.get_mount_device.html
31+
//! [`OVF`]: https://www.dmtf.org/standards/ovf
32+
433
use std::fs;
534
use std::fs::create_dir_all;
635
use std::fs::File;
@@ -19,6 +48,11 @@ use tracing::instrument;
1948
use crate::error::Error;
2049
use fstab::FsTab;
2150

51+
/// Represents a media device.
52+
///
53+
/// # Type Parameters
54+
///
55+
/// * `State` - The state of the media, either `Mounted` or `Unmounted`.
2256
#[derive(Debug, Default, Deserialize, PartialEq, Clone)]
2357
pub struct Environment {
2458
#[serde(rename = "ProvisioningSection")]
@@ -27,6 +61,7 @@ pub struct Environment {
2761
pub platform_settings_section: PlatformSettingsSection,
2862
}
2963

64+
/// Provisioning section of the environment configuration.
3065
#[derive(Debug, Default, Deserialize, PartialEq, Clone)]
3166
pub struct ProvisioningSection {
3267
#[serde(rename = "Version")]
@@ -35,6 +70,7 @@ pub struct ProvisioningSection {
3570
pub linux_prov_conf_set: LinuxProvisioningConfigurationSet,
3671
}
3772

73+
/// Linux provisioning configuration set.
3874
#[derive(Debug, Default, Deserialize, PartialEq, Clone)]
3975
pub struct LinuxProvisioningConfigurationSet {
4076
#[serde(rename = "UserName")]
@@ -45,6 +81,7 @@ pub struct LinuxProvisioningConfigurationSet {
4581
pub hostname: String,
4682
}
4783

84+
/// Platform settings section of the environment configuration.
4885
#[derive(Debug, Default, Deserialize, PartialEq, Clone)]
4986
pub struct PlatformSettingsSection {
5087
#[serde(rename = "Version")]
@@ -53,6 +90,7 @@ pub struct PlatformSettingsSection {
5390
pub platform_settings: PlatformSettings,
5491
}
5592

93+
/// Platform settings details.
5694
#[derive(Debug, Default, Deserialize, PartialEq, Clone)]
5795
pub struct PlatformSettings {
5896
#[serde(default = "default_preprov", rename = "PreprovisionedVm")]
@@ -61,25 +99,52 @@ pub struct PlatformSettings {
6199
pub preprovisioned_vm_type: String,
62100
}
63101

102+
/// Returns an empty string as the default password.
103+
///
104+
/// # Returns
105+
///
106+
/// A `String` containing an empty password.
64107
fn default_password() -> String {
65108
"".to_owned()
66109
}
67110

111+
/// Returns `false` as the default value for preprovisioned VM.
112+
///
113+
/// # Returns
114+
///
115+
/// A `bool` indicating that the VM is not preprovisioned.
68116
fn default_preprov() -> bool {
69117
false
70118
}
71119

120+
/// Returns "None" as the default type for preprovisioned VM.
121+
///
122+
/// # Returns
123+
///
124+
/// A `String` containing "None" as the default preprovisioned VM type.
72125
fn default_preprov_type() -> String {
73126
"None".to_owned()
74127
}
75128

129+
/// Path to the default mount device.
76130
pub const PATH_MOUNT_DEVICE: &str = "/dev/sr0";
131+
/// Path to the default mount point.
77132
pub const PATH_MOUNT_POINT: &str = "/run/azure-init/media/";
78133

134+
/// Valid filesystems for CDROM devices.
79135
const CDROM_VALID_FS: &[&str] = &["iso9660", "udf"];
136+
/// Path to the mount table file.
80137
const MTAB_PATH: &str = "/etc/mtab";
81138

82-
// Get a mounted device with any filesystem for CDROM
139+
/// Retrieves a list of mounted devices with CDROM-type filesystems.
140+
///
141+
/// # Arguments
142+
///
143+
/// * `path` - Optional path to the mount table file.
144+
///
145+
/// # Returns
146+
///
147+
/// A `Result` containing a vector of device paths as strings, or an `Error`.
83148
#[instrument]
84149
pub fn get_mount_device(path: Option<&Path>) -> Result<Vec<String>, Error> {
85150
let fstab = FsTab::new(path.unwrap_or_else(|| Path::new(MTAB_PATH)));
@@ -100,12 +165,19 @@ pub fn get_mount_device(path: Option<&Path>) -> Result<Vec<String>, Error> {
100165
Ok(cdrom_devices)
101166
}
102167

103-
// Some zero-sized structs that just provide states for our state machine
168+
/// Represents the state of a mounted media.
104169
#[derive(Debug)]
105170
pub struct Mounted;
171+
172+
/// Represents the state of an unmounted media.
106173
#[derive(Debug)]
107174
pub struct Unmounted;
108175

176+
/// Represents a media device.
177+
///
178+
/// # Type Parameters
179+
///
180+
/// * `State` - The state of the media, either `Mounted` or `Unmounted`.
109181
#[derive(Debug)]
110182
pub struct Media<State = Unmounted> {
111183
device_path: PathBuf,
@@ -114,6 +186,16 @@ pub struct Media<State = Unmounted> {
114186
}
115187

116188
impl Media<Unmounted> {
189+
/// Creates a new `Media` instance.
190+
///
191+
/// # Arguments
192+
///
193+
/// * `device_path` - The path to the media device.
194+
/// * `mount_path` - The path where the media will be mounted.
195+
///
196+
/// # Returns
197+
///
198+
/// A new `Media` instance in the `Unmounted` state.
117199
pub fn new(device_path: PathBuf, mount_path: PathBuf) -> Media<Unmounted> {
118200
Media {
119201
device_path,
@@ -122,6 +204,11 @@ impl Media<Unmounted> {
122204
}
123205
}
124206

207+
/// Mounts the media device.
208+
///
209+
/// # Returns
210+
///
211+
/// A `Result` containing the `Media` instance in the `Mounted` state, or an `Error`.
125212
#[instrument]
126213
pub fn mount(self) -> Result<Media<Mounted>, Error> {
127214
create_dir_all(&self.mount_path)?;
@@ -155,6 +242,11 @@ impl Media<Unmounted> {
155242
}
156243

157244
impl Media<Mounted> {
245+
/// Unmounts the media device.
246+
///
247+
/// # Returns
248+
///
249+
/// A `Result` indicating success or failure.
158250
#[instrument]
159251
pub fn unmount(self) -> Result<(), Error> {
160252
let umount_status =
@@ -178,6 +270,11 @@ impl Media<Mounted> {
178270
}
179271
}
180272

273+
/// Reads the OVF environment data to a string.
274+
///
275+
/// # Returns
276+
///
277+
/// A `Result` containing the OVF environment data as a string, or an `Error`.
181278
#[instrument]
182279
pub fn read_ovf_env_to_string(&self) -> Result<String, Error> {
183280
let mut file_path = self.mount_path.clone();
@@ -191,6 +288,50 @@ impl Media<Mounted> {
191288
}
192289
}
193290

291+
/// Parses the OVF environment data.
292+
///
293+
/// # Arguments
294+
///
295+
/// * `ovf_body` - A string slice containing the OVF environment data.
296+
///
297+
/// # Returns
298+
///
299+
/// A `Result` containing the parsed `Environment` struct, or an `Error`.
300+
///
301+
/// # Example
302+
///
303+
/// ```
304+
/// use libazureinit::media::parse_ovf_env;
305+
///
306+
/// // Example dummy OVF environment data
307+
/// let ovf_body = r#"
308+
/// <Environment xmlns="http://schemas.dmtf.org/ovf/environment/1">
309+
/// <ProvisioningSection>
310+
/// <Version>1.0</Version>
311+
/// <LinuxProvisioningConfigurationSet>
312+
/// <UserName>myusername</UserName>
313+
/// <UserPassword></UserPassword>
314+
/// <DisableSshPasswordAuthentication>false</DisableSshPasswordAuthentication>
315+
/// <HostName>myhostname</HostName>
316+
/// </LinuxProvisioningConfigurationSet>
317+
/// </ProvisioningSection>
318+
/// <PlatformSettingsSection>
319+
/// <Version>1.0</Version>
320+
/// <PlatformSettings>
321+
/// <PreprovisionedVm>false</PreprovisionedVm>
322+
/// <PreprovisionedVmType>None</PreprovisionedVmType>
323+
/// </PlatformSettings>
324+
/// </PlatformSettingsSection>
325+
/// </Environment>
326+
/// "#;
327+
///
328+
/// let environment = parse_ovf_env(ovf_body).unwrap();
329+
/// assert_eq!(environment.provisioning_section.linux_prov_conf_set.username, "myusername");
330+
/// assert_eq!(environment.provisioning_section.linux_prov_conf_set.password, "");
331+
/// assert_eq!(environment.provisioning_section.linux_prov_conf_set.hostname, "myhostname");
332+
/// assert_eq!(environment.platform_settings_section.platform_settings.preprovisioned_vm, false);
333+
/// assert_eq!(environment.platform_settings_section.platform_settings.preprovisioned_vm_type, "None");
334+
/// ```
194335
#[instrument(skip_all)]
195336
pub fn parse_ovf_env(ovf_body: &str) -> Result<Environment, Error> {
196337
let environment: Environment = from_str(ovf_body)?;
@@ -207,7 +348,15 @@ pub fn parse_ovf_env(ovf_body: &str) -> Result<Environment, Error> {
207348
}
208349
}
209350

210-
// Mount the given device, get OVF environment data, return it.
351+
/// Mounts the given device, gets OVF environment data, and returns it.
352+
///
353+
/// # Arguments
354+
///
355+
/// * `dev` - A string containing the device path.
356+
///
357+
/// # Returns
358+
///
359+
/// A `Result` containing the parsed `Environment` struct, or an `Error`.
211360
#[instrument(skip_all)]
212361
pub fn mount_parse_ovf_env(dev: String) -> Result<Environment, Error> {
213362
let mount_media =

0 commit comments

Comments
 (0)