Skip to content

Commit

Permalink
vm(qemu): support virtfs/9p file sharing
Browse files Browse the repository at this point in the history
Resolves #2184
  • Loading branch information
osy committed Aug 3, 2022
1 parent 4355723 commit aa70cd3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 11 deletions.
19 changes: 19 additions & 0 deletions Configuration/UTMQemuConfiguration+Arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,25 @@ extension UTMQemuConfiguration {
f("virtserialport,chardev=charchannel1,id=channel1,name=org.spice-space.webdav.0")
f("-chardev")
f("spiceport,name=org.spice-space.webdav.0,id=charchannel1")
} else if sharing.directoryShareMode == .virtfs, let url = sharing.directoryShareUrl {
f("-fsdev")
"local"
"id=virtfs0"
"path="
url
"security_model=mapped-xattr"
if sharing.isDirectoryShareReadOnly {
"readonly=on"
}
f()
f("-device")
if system.architecture == .s390x {
"virtio-9p-ccw"
} else {
"virtio-9p-pci"
}
"fsdev=virtfs0"
"mount_tag=share"
}
}
}
Expand Down
1 change: 0 additions & 1 deletion Managers/UTMQemuVirtualMachine+SPICE.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ NS_ASSUME_NONNULL_BEGIN

@interface UTMQemuVirtualMachine (SPICE)

@property (nonatomic, readonly) BOOL hasShareDirectoryEnabled;
@property (nonatomic, readonly) BOOL hasUsbRedirection;

- (BOOL)changeSharedDirectory:(NSURL *)url error:(NSError * _Nullable *)error;
Expand Down
14 changes: 6 additions & 8 deletions Managers/UTMQemuVirtualMachine+SPICE.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ - (UTMSpiceIO *)spiceIoWithError:(NSError * _Nullable __autoreleasing *)error {

#pragma mark - Shared Directory

- (BOOL)hasShareDirectoryEnabled {
return self.config.qemuHasWebdavSharing;
}

- (BOOL)saveSharedDirectory:(NSURL *)url error:(NSError * _Nullable __autoreleasing *)error {
[url startAccessingSecurityScopedResource];
NSData *bookmark = [url bookmarkDataWithOptions:kUTMBookmarkCreationOptions
Expand All @@ -82,11 +78,13 @@ - (BOOL)changeSharedDirectory:(NSURL *)url error:(NSError * _Nullable __autorele
// if we haven't started the VM yet, save the URL for when the VM starts
return [self saveSharedDirectory:url error:error];
}
UTMSpiceIO *spiceIO = [self spiceIoWithError:error];
if (!spiceIO) {
return NO;
if (self.config.qemuHasWebdavSharing) {
UTMSpiceIO *spiceIO = [self spiceIoWithError:error];
if (!spiceIO) {
return NO;
}
[spiceIO changeSharedDirectory:url];
}
[spiceIO changeSharedDirectory:url];
return [self saveSharedDirectory:url error:error];
}

Expand Down
2 changes: 2 additions & 0 deletions Managers/UTMQemuVirtualMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ - (void)_vmStartWithCompletion:(void (^)(NSError * _Nullable))completion {
[self.logging logToFile:self.config.qemuDebugLogURL];
}

[self prepareConfigurationForStart];

if (self.isRunningAsSnapshot) {
self.config.qemuIsDisposable = self.isRunningAsSnapshot;
} else {
Expand Down
12 changes: 12 additions & 0 deletions Managers/UTMVirtualMachineExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ public extension UTMQemuVirtualMachine {
}
}
}

/// Sets up values in VM configuration corrosponding to per-device data like sharing path
@objc func prepareConfigurationForStart() {
if config.qemuConfig!.sharing.directoryShareMode != .none {
var stale: Bool = false
if let bookmark = viewState.sharedDirectory, let url = try? URL(resolvingBookmarkData: bookmark, options: kUTMBookmarkResolutionOptions, bookmarkDataIsStale: &stale) {
config.qemuConfig!.sharing.directoryShareUrl = url
} else {
logger.error("Cannot resolve bookmark for share path '\(viewState.sharedDirectoryPath ?? "nil")'")
}
}
}
}

extension UTMDrive: Identifiable {
Expand Down
2 changes: 1 addition & 1 deletion Platform/Shared/VMRemovableDrivesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct VMRemovableDrivesView: View {


Group {
if vm.hasShareDirectoryEnabled {
if vm.config.qemuConfig!.sharing.directoryShareMode != .none {
HStack {
title
Spacer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class VMDisplayQemuWindowController: VMDisplayWindowController {
}
#endif
drivesToolbarItem.isEnabled = vmQemuConfig.drives.count > 0
sharedFolderToolbarItem.isEnabled = qemuVM.hasShareDirectoryEnabled
sharedFolderToolbarItem.isEnabled = vmQemuConfig.sharing.directoryShareMode == .webdav // virtfs cannot dynamically change
usbToolbarItem.isEnabled = qemuVM.hasUsbRedirection
window!.title = defaultTitle
window!.subtitle = defaultSubtitle
Expand Down

0 comments on commit aa70cd3

Please sign in to comment.