-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new macOS vfkit driver, like hyperkit and qemu
It uses the new Virtualization.framework from macOS 11, instead of the older Hypervisor.framework (hvf) in QEMU.
- Loading branch information
1 parent
b0c504b
commit 805b0cd
Showing
9 changed files
with
777 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
Copyright 2016 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package vfkit | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/hooklift/iso9660" | ||
) | ||
|
||
// ExtractFile extracts a file from an ISO | ||
func ExtractFile(isoPath, srcPath, destPath string) error { | ||
iso, err := os.Open(isoPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer iso.Close() | ||
|
||
r, err := iso9660.NewReader(iso) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
f, err := findFile(r, srcPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dst, err := os.Create(destPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer dst.Close() | ||
|
||
_, err = io.Copy(dst, f.Sys().(io.Reader)) | ||
return err | ||
} | ||
|
||
func findFile(r *iso9660.Reader, path string) (os.FileInfo, error) { | ||
// Look through the ISO for a file with a matching path. | ||
for f, err := r.Next(); err != io.EOF; f, err = r.Next() { | ||
// For some reason file paths in the ISO sometimes contain a '.' character at the end, so strip that off. | ||
if strings.TrimSuffix(f.Name(), ".") == path { | ||
return f, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("unable to find file %s", path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package vfkit | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestExtractFile(t *testing.T) { | ||
testDir := t.TempDir() | ||
|
||
tests := []struct { | ||
name string | ||
isoPath string | ||
srcPath string | ||
destPath string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "all is right", | ||
isoPath: "iso_test.iso", | ||
srcPath: "/test1.txt", | ||
destPath: testDir + "/test1.txt", | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "isoPath is error", | ||
isoPath: "tests.iso", | ||
srcPath: "/test1.txt", | ||
destPath: testDir + "/test1.txt", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "srcPath is empty", | ||
isoPath: "iso_tests.iso", | ||
srcPath: "", | ||
destPath: testDir + "/test1.txt", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "srcPath is error", | ||
isoPath: "iso_tests.iso", | ||
srcPath: "/t1.txt", | ||
destPath: testDir + "/test1.txt", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "destPath is empty", | ||
isoPath: "iso_test.iso", | ||
srcPath: "/test1.txt", | ||
destPath: "", | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "find files in a folder", | ||
isoPath: "./iso_test.iso", | ||
srcPath: "/test2/test2.txt", | ||
destPath: testDir + "/test2.txt", | ||
expectedError: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := ExtractFile(tt.isoPath, tt.srcPath, tt.destPath) | ||
if (nil != err) != tt.expectedError { | ||
t.Errorf("expectedError = %v, get = %v", tt.expectedError, err) | ||
return | ||
} | ||
}) | ||
} | ||
} |
Binary file not shown.
Oops, something went wrong.