tstest/tailmac: add support for mounting host directories in the guest (#13957)

updates tailscale/corp#24197

tailmac run now supports the --share option which will allow you
to specify a directory on the host which can be mounted in the guest
using  mount_virtiofs vmshare <path>.

Signed-off-by: Jonathan Nobels <jonathan@tailscale.com>
This commit is contained in:
Jonathan Nobels
2024-10-29 13:49:51 -04:00
committed by GitHub
parent 0f9a054cba
commit aecb0ab76b
5 changed files with 35 additions and 14 deletions

View File

@@ -95,6 +95,7 @@ extension Tailmac {
extension Tailmac {
struct Run: ParsableCommand {
@Option(help: "The vm identifier") var id: String
@Option(help: "Optional share directory") var share: String?
@Flag(help: "Tail the TailMac log output instead of returning immediatly") var tail
mutating func run() {
@@ -115,7 +116,12 @@ extension Tailmac {
fatalError("Could not find Host.app at \(appPath). This must be co-located with the tailmac utility")
}
process.arguments = ["run", "--id", id]
var args = ["run", "--id", id]
if let share {
args.append("--share")
args.append(share)
}
process.arguments = args
do {
process.standardOutput = stdOutPipe
@@ -124,26 +130,18 @@ extension Tailmac {
fatalError("Unable to launch the vm process")
}
// This doesn't print until we exit which is not ideal, but at least we
// get the output
if tail != 0 {
// (jonathan)TODO: How do we get the process output in real time?
// The child process only seems to flush to stdout on completion
let outHandle = stdOutPipe.fileHandleForReading
let queue = OperationQueue()
NotificationCenter.default.addObserver(
forName: NSNotification.Name.NSFileHandleDataAvailable,
object: outHandle, queue: queue)
{
notification -> Void in
let data = outHandle.availableData
outHandle.readabilityHandler = { handle in
let data = handle.availableData
if data.count > 0 {
if let str = String(data: data, encoding: String.Encoding.utf8) {
print(str)
}
}
outHandle.waitForDataInBackgroundAndNotify()
}
outHandle.waitForDataInBackgroundAndNotify()
process.waitUntilExit()
}
}