Use the WSL convention for the windows machines default volumes

Signed-off-by: Mario Loriedo <mario.loriedo@gmail.com>
This commit is contained in:
Mario Loriedo 2024-07-25 15:14:59 +02:00
parent 930e07774a
commit 755e156c93
2 changed files with 82 additions and 1 deletions

View File

@ -42,11 +42,17 @@ func getLibpodTmpDir() string {
}
// getDefaultMachineVolumes returns default mounted volumes (possibly with env vars, which will be expanded)
// It is executed only if the machine provider is Hyper-V and it mimics WSL
// behavior where the host %USERPROFILE% drive (e.g. C:\) is automatically
// mounted in the guest under /mnt/ (e.g. /mnt/c/)
func getDefaultMachineVolumes() []string {
hd := homedir.Get()
vol := filepath.VolumeName(hd)
hostMnt := filepath.ToSlash(strings.TrimPrefix(hd, vol))
return []string{fmt.Sprintf("%s:%s", hd, hostMnt)}
return []string{
fmt.Sprintf("%s:%s", hd, hostMnt),
fmt.Sprintf("%s:%s", vol+"\\", "/mnt/"+strings.ToLower(vol[0:1])),
}
}
func getDefaultComposeProviders() []string {

View File

@ -0,0 +1,75 @@
package config
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetDefaultMachineVolumes(t *testing.T) {
for _, tc := range []struct {
envs [][]string
output []string
}{
{
envs: [][]string{
{"USERPROFILE", "C:\\Users\\test"},
},
output: []string{
"C:\\Users\\test:/Users/test",
"C:\\:/mnt/c",
},
},
{
envs: [][]string{
{"USERPROFILE", "C:\\Users\\test\\AppData\\Local\\Temp\\podman_test123456789"},
},
output: []string{
"C:\\Users\\test\\AppData\\Local\\Temp\\podman_test123456789:/Users/test/AppData/Local/Temp/podman_test123456789",
"C:\\:/mnt/c",
},
},
{
envs: [][]string{
{"USERPROFILE", "D:\\Users\\test"},
},
output: []string{
"D:\\Users\\test:/Users/test",
"D:\\:/mnt/d",
},
},
{
envs: [][]string{
{"USERPROFILE", "c:\\users\\test"},
},
output: []string{
"c:\\users\\test:/users/test",
"c:\\:/mnt/c",
},
},
{
envs: [][]string{
{"USERPROFILE", "C:/Users/test"},
},
output: []string{
"C:/Users/test:/Users/test",
"C:\\:/mnt/c",
},
},
{
envs: [][]string{
{"USERPROFILE", "C:\\Users\\test\\"},
},
output: []string{
"C:\\Users\\test\\:/Users/test/",
"C:\\:/mnt/c",
},
},
} {
for _, env := range tc.envs {
t.Setenv(env[0], env[1])
}
output := getDefaultMachineVolumes()
assert.Equal(t, tc.output, output)
}
}