Skip to content

Commit c110ec7

Browse files
committed
Add krun_add_disk2 API
Adds an enum that describes the supported disk image variants. Adds the `krun_add_disk2` API that requires the user to specify the format of the disk image they're providing. The following formats are supported: - KRUN_DISK_FORMAT_RAW - KRUN_DISK_FORMAT_QCOW2 Signed-off-by: Jake Correnti <[email protected]>
1 parent aca81b2 commit c110ec7

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

include/libkrun.h

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,25 @@ int32_t krun_set_root_disk(uint32_t ctx_id, const char *disk_path);
9595
int32_t krun_set_data_disk(uint32_t ctx_id, const char *disk_path);
9696

9797
/**
98-
* Adds a disk image to be used as a general partition for the microVM.
98+
* Adds a disk image to be used as a general partition for the microVM. The only supported image
99+
* format is "raw".
99100
*
100101
* This API is mutually exclusive with the deprecated krun_set_root_disk and
101102
* krun_set_data_disk methods and must not be used together.
102103
*
104+
* SECURITY NOTE:
105+
* Libkrun is not responsible for probing the disk image provided, as probing a disk image should
106+
* be avoided. However, if one must probe the disk image, it falls onto the user. The disk image
107+
* must only be probed once, and the probe of the image must be done before the first boot of the
108+
* microVM.
109+
*
110+
* The disk image is assumed to be a Raw disk image, and trust is put into the user to
111+
* correctly and securely probe the disk image, if at all.
112+
*
103113
* Arguments:
104114
* "ctx_id" - the configuration context ID.
105115
* "block_id" - a null-terminated string representing the partition.
106-
* "disk_path" - a null-terminated string representing the path leading to the disk image that
107-
* contains the root file-system.
116+
* "disk_path" - a null-terminated string representing the path leading to the disk image.
108117
* "read_only" - whether the mount should be read-only. Required if the caller does not have
109118
* write permissions (for disk images in /usr/share).
110119
*
@@ -113,6 +122,49 @@ int32_t krun_set_data_disk(uint32_t ctx_id, const char *disk_path);
113122
*/
114123
int32_t krun_add_disk(uint32_t ctx_id, const char *block_id, const char *disk_path, bool read_only);
115124

125+
/* Supported disk image formats */
126+
#define KRUN_DISK_FORMAT_RAW 0
127+
#define KRUN_DISK_FORMAT_QCOW2 1
128+
/**
129+
* Adds a disk image to be used as a general partition for the microVM. The supported
130+
* image formats are: "raw" and "qcow2".
131+
*
132+
* This API is mutually exclusive with the deprecated krun_set_root_disk and
133+
* krun_set_data_disk methods and must not be used together.
134+
*
135+
* SECURITY NOTE:
136+
* Libkrun is not responsible for probing the disk image provided, as probing a disk image should
137+
* be avoided. However, if one must probe the disk image, it falls onto the user. The disk image
138+
* must only be probed once, and the probe of the image must be done before the first boot of the
139+
* microVM.
140+
*
141+
* The disk image may be in the Raw or Qcow2 format, but trust is put into the user to
142+
* correctly and securely probe the disk image, if at all.
143+
*
144+
* It is possible for a Raw disk image to be probed, and have the guest write a Qcow2
145+
* image header into the Raw file's first sector. If the disk image is then re-probed, and the
146+
* user tells libkrun it is a Qcow2, then libkrun will boot the disk image as a Qcow2. Qcow2
147+
* files can, and will, open other files. The guest will most likely have access to the data in those
148+
* files, essentially giving the guest access to any file on the host system the disk emulation
149+
* has access to.
150+
*
151+
* Arguments:
152+
* "ctx_id" - the configuration context ID.
153+
* "block_id" - a null-terminated string representing the partition.
154+
* "disk_path" - a null-terminated string representing the path leading to the disk image.
155+
* "disk_format" - the disk image format (i.e. KRUN_DISK_FORMAT_{RAW, QCOW2})
156+
* "read_only" - whether the mount should be read-only. Required if the caller does not have
157+
* write permissions (for disk images in /usr/share).
158+
*
159+
* Returns:
160+
* Zero on success or a negative error number on failure.
161+
*/
162+
int32_t krun_add_disk2(uint32_t ctx_id,
163+
const char *block_id,
164+
const char *disk_path,
165+
uint32_t disk_format,
166+
bool read_only);
167+
116168
/**
117169
* NO LONGER SUPPORTED. DO NOT USE.
118170
*

src/libkrun/src/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,53 @@ pub unsafe extern "C" fn krun_add_disk(
534534
KRUN_SUCCESS
535535
}
536536

537+
#[allow(clippy::missing_safety_doc)]
538+
#[no_mangle]
539+
#[cfg(feature = "blk")]
540+
pub unsafe extern "C" fn krun_add_disk2(
541+
ctx_id: u32,
542+
c_block_id: *const c_char,
543+
c_disk_path: *const c_char,
544+
disk_format: u32,
545+
read_only: bool,
546+
) -> i32 {
547+
let disk_path = match CStr::from_ptr(c_disk_path).to_str() {
548+
Ok(disk) => disk,
549+
Err(_) => return -libc::EINVAL,
550+
};
551+
552+
let block_id = match CStr::from_ptr(c_block_id).to_str() {
553+
Ok(block_id) => block_id,
554+
Err(_) => return -libc::EINVAL,
555+
};
556+
557+
let format = match disk_format {
558+
0 => ImageType::Raw,
559+
1 => ImageType::Qcow2,
560+
_ => {
561+
// Do not continue if the user cannot specify a valid disk format
562+
return -libc::EINVAL;
563+
}
564+
};
565+
566+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
567+
Entry::Occupied(mut ctx_cfg) => {
568+
let cfg = ctx_cfg.get_mut();
569+
let block_device_config = BlockDeviceConfig {
570+
block_id: block_id.to_string(),
571+
cache_type: CacheType::Writeback,
572+
disk_image_path: disk_path.to_string(),
573+
disk_image_format: format,
574+
is_disk_read_only: read_only,
575+
};
576+
cfg.add_block_cfg(block_device_config);
577+
}
578+
Entry::Vacant(_) => return -libc::ENOENT,
579+
}
580+
581+
KRUN_SUCCESS
582+
}
583+
537584
#[allow(clippy::missing_safety_doc)]
538585
#[no_mangle]
539586
#[cfg(feature = "blk")]

0 commit comments

Comments
 (0)