Skip to content

Commit

Permalink
Add new macOS vfkit driver, like hyperkit and qemu
Browse files Browse the repository at this point in the history
It uses the new Virtualization.framework from macOS 11,
instead of the older Hypervisor.framework (hvf) in QEMU.
  • Loading branch information
afbjorklund committed Aug 16, 2024
1 parent b0c504b commit 805b0cd
Show file tree
Hide file tree
Showing 9 changed files with 777 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/drivers/vfkit/iso.go
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)
}
85 changes: 85 additions & 0 deletions pkg/drivers/vfkit/iso_test.go
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 added pkg/drivers/vfkit/iso_test.iso
Binary file not shown.
Loading

0 comments on commit 805b0cd

Please sign in to comment.