Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network: fix embedded library #228

Merged
merged 2 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified embedded/network/vmnet_arm64.tar.gz
Binary file not shown.
Binary file modified embedded/network/vmnet_x86_64.tar.gz
Binary file not shown.
37 changes: 23 additions & 14 deletions environment/vm/lima/network/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ import (
type rootfulInstaller struct{ host environment.HostActions }

func (r rootfulInstaller) Installed(file rootfulFile) bool {
stat, err := os.Stat(file.Path())
if err != nil {
return false
}
if file.Executable() {
return stat.Mode()&0100 != 0
for _, f := range file.Paths() {
stat, err := os.Stat(f)
if err != nil {
return false
}
if file.Executable() {
if stat.Mode()&0100 == 0 {
return false
}
}
}
return true
}
func (r rootfulInstaller) Install(file rootfulFile) error { return file.Install(r.host) }

type rootfulFile interface {
Path() string
Paths() []string
Executable() bool
Install(host environment.HostActions) error
}
Expand All @@ -35,7 +39,7 @@ var _ rootfulFile = sudoerFile{}

type sudoerFile struct{}

func (s sudoerFile) Path() string { return "/etc/sudoers.d/colima" }
func (s sudoerFile) Paths() []string { return []string{"/etc/sudoers.d/colima"} }
func (s sudoerFile) Executable() bool { return false }
func (s sudoerFile) Install(host environment.HostActions) error {
// read embedded file contents
Expand All @@ -44,11 +48,13 @@ func (s sudoerFile) Install(host environment.HostActions) error {
return fmt.Errorf("error retrieving embedded sudo file: %w", err)
}
// ensure parent directory exists
if err := host.RunInteractive("sudo", "mkdir", "-p", filepath.Dir(s.Path())); err != nil {
path := s.Paths()[0]
dir := filepath.Dir(path)
if err := host.RunInteractive("sudo", "mkdir", "-p", dir); err != nil {
return fmt.Errorf("error preparing sudoers directory: %w", err)
}
// persist file to desired location
if err := host.RunInteractive("sudo", "sh", "-c", fmt.Sprintf(`echo "%s" > %s`, txt, s.Path())); err != nil {
if err := host.RunInteractive("sudo", "sh", "-c", fmt.Sprintf(`echo "%s" > %s`, txt, path)); err != nil {
return fmt.Errorf("error writing sudoers file: %w", err)
}
return nil
Expand All @@ -57,10 +63,13 @@ func (s sudoerFile) Install(host environment.HostActions) error {
var _ rootfulFile = vmnetFile{}

const VmnetBinary = "/opt/colima/bin/vde_vmnet"
const vmnetLibrary = "/opt/colima/lib/libvdeplug.3.dylib"

type vmnetFile struct{}

func (s vmnetFile) Path() string { return VmnetBinary }
func (s vmnetFile) Paths() []string {
return []string{VmnetBinary, vmnetLibrary}
}
func (s vmnetFile) Executable() bool { return true }
func (s vmnetFile) Install(host environment.HostActions) error {
arch := "x86_64"
Expand All @@ -85,7 +94,7 @@ func (s vmnetFile) Install(host environment.HostActions) error {
_ = f.Close() // not a fatal error

// extract tar to desired location
dir := filepath.Dir(s.Path())
dir := "/opt/colima"
if err := host.RunInteractive("sudo", "mkdir", "-p", dir); err != nil {
return fmt.Errorf("error preparing colima privileged dir: %w", err)
}
Expand All @@ -99,14 +108,14 @@ var _ rootfulFile = colimaVmnetFile{}

type colimaVmnetFile struct{}

func (s colimaVmnetFile) Path() string { return "/opt/colima/bin/colima-vmnet" }
func (s colimaVmnetFile) Paths() []string { return []string{"/opt/colima/bin/colima-vmnet"} }
func (s colimaVmnetFile) Executable() bool { return true }
func (s colimaVmnetFile) Install(host environment.HostActions) error {
arg0, _ := exec.LookPath(os.Args[0])
if arg0 == "" { // should never happen
arg0 = os.Args[0]
}
if err := host.RunInteractive("sudo", "ln", "-sfn", arg0, s.Path()); err != nil {
if err := host.RunInteractive("sudo", "ln", "-sfn", arg0, s.Paths()[0]); err != nil {
return fmt.Errorf("error creating colima-vmnet binary: %w", err)
}
return nil
Expand Down
39 changes: 21 additions & 18 deletions scripts/build_vmnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ clone https://github.com/lima-vm/vde_vmnet.git $DIR_VMNET
clone https://github.com/virtualsquare/vde-2.git $DIR_VDE

build_x86_64() (
export PREFIX=$DIR_BUILD/dist/x86_64
mkdir -p $PREFIX
export PREFIX=/opt/colima
sudo rm -rf $PREFIX
sudo mkdir -p $PREFIX

# vde-2
(
Expand All @@ -31,7 +32,7 @@ build_x86_64() (
./configure --prefix=$PREFIX

make PREFIX=$PREFIX
make PREFIX=$PREFIX install
sudo make PREFIX=$PREFIX install
# cleanup
make distclean
)
Expand All @@ -40,22 +41,23 @@ build_x86_64() (
(
cd $DIR_VMNET
make PREFIX=$PREFIX
make PREFIX=$PREFIX install.bin
sudo make PREFIX=$PREFIX install.bin
)

# copy to embed directory
(
mkdir -p $EMBED_DIR
VMNET_FILE=$PREFIX/bin/vde_vmnet
cp $VMNET_FILE $EMBED_DIR
cd "$(dirname ${VMNET_FILE})" && tar cvfz $EMBED_DIR/vmnet_x86_64.tar.gz "$(basename ${VMNET_FILE})"
rm $EMBED_DIR/vde_vmnet
mkdir -p $EMBED_DIR/vmnet/bin $EMBED_DIR/vmnet/lib
cp $PREFIX/bin/vde_vmnet $EMBED_DIR/vmnet/bin
cp $PREFIX/lib/libvdeplug.3.dylib $EMBED_DIR/vmnet/lib
cd $EMBED_DIR/vmnet && tar cvfz $EMBED_DIR/vmnet_x86_64.tar.gz bin/vde_vmnet lib/libvdeplug.3.dylib
rm -rf $EMBED_DIR/vmnet
sudo rm -rf /opt/colima
)
)

build_arm64() (
export PREFIX=$DIR_BUILD/dist/arm64
mkdir -p $PREFIX
export PREFIX=/opt/colima
sudo mkdir -p $PREFIX

export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
export CC=$(xcrun --sdk macosx --find clang)
Expand All @@ -73,7 +75,7 @@ build_arm64() (
./configure --prefix=$PREFIX --host=arm-apple-darwin --target=arm-apple-darwin --build=x86_64-apple-darwin

make PREFIX=$PREFIX
make PREFIX=$PREFIX install
sudo make PREFIX=$PREFIX install
# cleanup
make distclean
)
Expand All @@ -82,16 +84,17 @@ build_arm64() (
(
cd $DIR_VMNET
make PREFIX=$PREFIX
make PREFIX=$PREFIX install.bin
sudo make PREFIX=$PREFIX install.bin
)

# copy to embed directory
(
mkdir -p $EMBED_DIR
VMNET_FILE=$PREFIX/bin/vde_vmnet
cp $VMNET_FILE $EMBED_DIR
cd "$(dirname ${VMNET_FILE})" && tar cvfz $EMBED_DIR/vmnet_arm64.tar.gz "$(basename ${VMNET_FILE})"
rm $EMBED_DIR/vde_vmnet
mkdir -p $EMBED_DIR/vmnet/bin $EMBED_DIR/vmnet/lib
cp $PREFIX/bin/vde_vmnet $EMBED_DIR/vmnet/bin
cp $PREFIX/lib/libvdeplug.3.dylib $EMBED_DIR/vmnet/lib
cd $EMBED_DIR/vmnet && tar cvfz $EMBED_DIR/vmnet_arm64.tar.gz bin/vde_vmnet lib/libvdeplug.3.dylib
rm -rf $EMBED_DIR/vmnet
sudo rm -rf /opt/colima
)

)
Expand Down