From 86eefcc5ab3be1ec89d4ee8743eea0691057156e Mon Sep 17 00:00:00 2001 From: Szubie Date: Mon, 19 Dec 2022 02:39:12 +0000 Subject: [PATCH 1/2] create new user profile and storage in multiuser multipass scenario --- platform/multipass.go | 64 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/platform/multipass.go b/platform/multipass.go index 40aef83..9484566 100644 --- a/platform/multipass.go +++ b/platform/multipass.go @@ -64,9 +64,71 @@ func (vm Multipass) BraveBackendInit() error { return err } - // If a VM with this name already exists skip init - we will reuse the existing VM + // If a VM with this name already exists skip init - we will reuse the existing VM and LXD bridge _, err = shared.ExecCommandWReturn("multipass", "info", vm.Settings.Name) if err == nil { + // Set up new user's profile and storage space + err = shared.ExecCommand("multipass", + "exec", + vm.Settings.Name, + "--", + shared.SnapLXC, + "profile", + "create", + vm.Settings.Profile) + if err != nil { + return errors.New("failed to create LXD profile: " + err.Error()) + } + + err = shared.ExecCommand("multipass", + "exec", + vm.Settings.Name, + "--", + shared.SnapLXC, + "storage", + "create", + vm.Settings.StoragePool.Name, + vm.Settings.StoragePool.Type, + "size="+vm.Settings.StoragePool.Size) + if err != nil { + return errors.New("failed to create storage pool: " + err.Error()) + } + + err = shared.ExecCommand("multipass", + "exec", + vm.Settings.Name, + "--", + shared.SnapLXC, + "profile", + "device", + "add", + vm.Settings.Profile, + "root", + "disk", + "path=/", + "pool="+vm.Settings.StoragePool.Name) + if err != nil { + return errors.New("failed to add storage disk to profile: " + err.Error()) + } + + err = shared.ExecCommand("multipass", + "exec", + vm.Settings.Name, + "--", + shared.SnapLXC, + "profile", + "device", + "add", + vm.Settings.Profile, + "eth0", + "nic", + "nictype=bridged", + "parent="+vm.Settings.Network.Name, + "name=eth0") + if err != nil { + return errors.New("failed to add network device to profile: " + err.Error()) + } + vm.Settings.Status = "active" err = UpdateBraveSettings(vm.Settings) if err != nil { From 177e052d68143242add644772903fb58d8cd95ae Mon Sep 17 00:00:00 2001 From: Szubie Date: Mon, 19 Dec 2022 13:42:54 +0000 Subject: [PATCH 2/2] Check for existing container of same name before deploying unit --- platform/host_api.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/platform/host_api.go b/platform/host_api.go index 90608c6..9802dcc 100644 --- a/platform/host_api.go +++ b/platform/host_api.go @@ -947,6 +947,11 @@ func (bh *BraveHost) InitUnit(backend Backend, unitParams shared.Service) (err e return err } + // Check for existing container with this unit name + if _, _, err := lxdServer.GetInstance(unitName); err == nil { + return fmt.Errorf("container with name %q is already running on %q remote", unitName, deployRemoteName) + } + deployArch, err := GetLXDServerArch(lxdServer) if err != nil { return err