Skip to content

Commit f398a3b

Browse files
foxengwkozaczuk
authored andcommitted
loader: add support for booting from virtio-fs
This makes the necessary and pretty straight-forward additions to the laoder to support using virtio-fs as the root filesystem. It also makes minimal changes to scripts/build to add support there as well. Note that, to obtain a directory with contents specified by the manifest files, usable as the virtio-fs host directory, one can use the existing 'export' and 'export_dir' (previously undocumented) options to scripts/build. Ref #1062. Signed-off-by: Fotis Xenakis <[email protected]> Message-Id: <AM0PR03MB62921196B1CC7E09D5EFA29DA62C0@AM0PR03MB6292.eurprd03.prod.outlook.com>
1 parent c04f226 commit f398a3b

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

fs/vfs/main.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2404,6 +2404,30 @@ extern "C" int mount_zfs_rootfs(bool pivot_root, bool extra_zfs_pools)
24042404
return 0;
24052405
}
24062406

2407+
extern "C" int mount_virtiofs_rootfs(bool pivot_root)
2408+
{
2409+
constexpr char* mp = "/virtiofs";
2410+
2411+
if (mkdir(mp, 0755) < 0) {
2412+
int ret = errno;
2413+
kprintf("failed to create %s, error = %s\n", mp, strerror(errno));
2414+
return ret;
2415+
}
2416+
2417+
int ret = sys_mount("/dev/virtiofs0", mp, "virtiofs", MNT_RDONLY, nullptr);
2418+
if (ret) {
2419+
kprintf("failed to mount %s, error = %s\n", mp, strerror(ret));
2420+
rmdir(mp);
2421+
return ret;
2422+
}
2423+
2424+
if (pivot_root) {
2425+
pivot_rootfs(mp);
2426+
}
2427+
2428+
return 0;
2429+
}
2430+
24072431
extern "C" void unmount_rootfs(void)
24082432
{
24092433
int ret;

loader.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ extern "C" {
9191
int mount_zfs_rootfs(bool, bool);
9292
int mount_rofs_rootfs(bool);
9393
void rofs_disable_cache();
94+
int mount_virtiofs_rootfs(bool);
9495
}
9596

9697
void premain()
@@ -165,7 +166,7 @@ static void usage()
165166
std::cout << " --leak start leak detector after boot\n";
166167
std::cout << " --nomount don't mount the root file system\n";
167168
std::cout << " --nopivot do not pivot the root from bootfs to the root fs\n";
168-
std::cout << " --rootfs=arg root filesystem to use (zfs, rofs or ramfs)\n";
169+
std::cout << " --rootfs=arg root filesystem to use (zfs, rofs, ramfs or virtiofs)\n";
169170
std::cout << " --assign-net assign virtio network to the application\n";
170171
std::cout << " --maxnic=arg maximum NIC number\n";
171172
std::cout << " --norandom don't initialize any random device\n";
@@ -435,6 +436,13 @@ void* do_main_thread(void *_main_args)
435436
// TODO: Avoid the hack of using pivot_rootfs() just for mounting
436437
// the fstab entries.
437438
pivot_rootfs("/");
439+
} else if (opt_rootfs.compare("virtiofs") == 0) {
440+
auto error = mount_virtiofs_rootfs(opt_pivot);
441+
if (error) {
442+
debug("Could not mount virtiofs root filesystem.\n");
443+
}
444+
445+
boot_time.event("Virtio-fs mounted");
438446
} else {
439447
// Fallback to original behavior for compatibility: try rofs -> zfs
440448
if (mount_rofs_rootfs(opt_pivot) == 0) {

scripts/build

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ usage() {
2424
--help|-h Print this help message
2525
arch=x64|aarch64 Specify the build architecture; default is x64
2626
mode=release|debug Specify the build mode; default is release
27-
export=none|selected|all If 'selected' or 'all' export the app files to build/export
28-
fs=zfs|rofs|ramfs Specify the filesystem of the image partition
27+
export=none|selected|all If 'selected' or 'all' export the app files to <export_dir>
28+
export_dir=<dir> The directory to export the files to; default is build/export
29+
fs=zfs|rofs|ramfs|virtiofs Specify the filesystem of the image partition
2930
fs_size=N Specify the size of the image in bytes
3031
fs_size_mb=N Specify the size of the image in MiB
3132
app_local_exec_tls_size=N Specify the size of app local TLS in bytes; the default is 64
@@ -182,12 +183,17 @@ manifest=bootfs.manifest.skel
182183
fs_type=${vars[fs]-zfs}
183184
usrskel_arg=
184185
case $fs_type in
185-
zfs);; # Nothing to change here. This is our default behavior
186-
rofs) manifest=bootfs_empty.manifest.skel
186+
zfs)
187+
;; # Nothing to change here. This is our default behavior
188+
rofs|virtiofs)
189+
# Both are read-only (in OSv) and require nothing extra on bootfs to work
190+
manifest=bootfs_empty.manifest.skel
187191
usrskel_arg="--usrskel usr_rofs.manifest.skel";;
188-
ramfs) manifest=$OUT/usr.manifest
192+
ramfs)
193+
manifest=$OUT/usr.manifest
189194
usrskel_arg="--usrskel usr_ramfs.manifest.skel";;
190-
*) echo "Unknown filesystem \"$fs_type\"" >&2
195+
*)
196+
echo "Unknown filesystem \"$fs_type\"" >&2
191197
exit 2
192198
esac
193199

@@ -312,7 +318,9 @@ rofs)
312318
partition_size=`stat --printf %s rofs.img`
313319
image_size=$((partition_offset + partition_size))
314320
create_rofs_disk ;;
315-
ramfs)
321+
ramfs|virtiofs)
322+
# No need to create extra fs like above: ramfs is already created (as the
323+
# bootfs) and virtio-fs is specified with virtiofsd at run time
316324
qemu-img convert -f raw -O qcow2 loader.img usr.img ;;
317325
esac
318326
# Prepend the root fs type option to the command line (preserved by run.py)

0 commit comments

Comments
 (0)