Jump to content

Lightweight way to start/stop VM


Recommended Posts

Hi everybody,

 

just want to ask if theres a lightweight way to start / stop a vm. 

Maybe with a CLI command that can be packed within a automator script or an app or something more efficent?

 

The way via webGUI is ok, but maybe there is an even more efficient way... because first you need to start the browser, than you have to login in unRAID and then you can start the VM. 

But imho there should be better way - similiar to WoL-Apps...

yes, it`s also possible to fire up a wake on LAN package via CLI command running a CLI, loggin in and so on ... but a simple (double)click on an app-icon is even better :)

 

Thanks for any hint. Have a nice sunday

Link to comment
49 minutes ago, Maddeen said:

Thanks - works flawless. 

I`ll try to build a simple Automator script and give a feedback. 
Process should be very simple like.... 

  • open terminal app (hided)
  • ssh into server
  • virsh start <Name of VM>

 

 

The putty program includes a command line "plink" that does all that in one line.

plink -pw <unraid root password> root@<unraid IP> virsh start <Name of VM>

  • Like 1
Link to comment

@testdasiok - the commands themself works as predicted. But now I got another problem. 

Due that I'm a MacOS user I can't get "sshpass" to work. So I can't build a cli command like 

sshpass -p "password" ssh -o StrictHostKeyChecking=no root@YOUR_IP_ADRESS

I need to have a passwordless login via ssh-key.
Normally this wouldn't be a problem for me - I set this up on my Raspberry with no struggles - but on Raspberry it was quite easy.

Copy pub-key to the PI - ready!

 

But the manual I found HERE in the forum, is outdated (Unraid 6.8 and above does not allow executing scripts from the flash (/boot) drive) and nobody have updated this for a current solution

 

Is it possible that I just copy the id_rsa.pub (generated on my mac) to the folder /boot/config/ssh/ - reboot - and everythings fine?

 

 

UPDATE: @jonathanm - thanks for this hint. I dont saw it when I wrote the lines above. I'll try it and give a feedback asap

UPDATE2: @jonathanm - sadly putty is a windows software and not be available for mac :( So the question above is still applies ;-)

Edited by Maddeen
Link to comment

@jonathanm - thanks. The good news - I managed to install it.

But know I need your assistance again hoping that the linux adaption is likely the macport. 

After installation I can run the command "putty" .. but it just opens the typical putty GUI :( (see Screen)

But how about using the command plink as you described above? 

Bildschirmfoto 2020-08-25 um 22.03.37.png

Edited by Maddeen
Link to comment
10 minutes ago, Maddeen said:

how about using the command plink as you described above? 

Does the plink file not exist in the same spot as putty?

In the linux distribution, both files are put into /usr/bin

In the windows version, putty.exe and plink.exe are both in the program files/putty folder.

Not having a mac, I'm going to be of limited help here.

  • Like 1
Link to comment

I got it  ... just need to enter "plink" in the cli instead of "putty" - the typical ?help information appeared :) 

If I match your commands

plink -pw <unraid root password> root@<unraid IP> virsh start <Name of VM>

with my credentials .... magic happens :) 
Now I just need to find a way to wrap this command in an Automator script or a shortcut to start that command with a simple double click without seeing the terminal window appear ;) 

 

Thanks again for you help. As soon as I got a solution I'll make a short summarized guide/documentation for other MacOS users out there :)

runs.jpg

Edited by Maddeen
Link to comment
  • 3 years later...
Posted (edited)

My approach, a simple program in golang. It can be run by a script at Unraid startup.
./vmhook 192.168.1.2:8151 windows10

At http://192.168.1.2:8151 there will be a “start” button, below is the code and the compiled version of the program. You can add other behavior or other buttons and build the program yourself.
 

// Command to build program:
// go build -trimpath -ldflags "-s -w" -o vmhook main.go

package main

import (
    "fmt"
    "net/http"
    "os"
    "os/exec"
)

func main() {
    if len(os.Args) < 3 {
        fmt.Println("Usage: ./vmhook <address>:<port> <VM_name>")
        return
    }

    addr := os.Args[1]
    vmName := os.Args[2]

    // Handler for the index page
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // Serve a simple HTML page with a button
        html := `
        <html>
            <head>
                <title>Start Windows10 VM</title>
            </head>
            <body>
                <form action="/start" method="post">
                    <input type="submit" value="Start">
                </form>
            </body>
        </html>
        `
        fmt.Fprintf(w, html)
    })

    // Handler for the start button
    http.HandleFunc("/start", func(w http.ResponseWriter, r *http.Request) {
        // Execute the command "virsh start windows10"
        cmd := exec.Command("virsh", "start", vmName)
        err := cmd.Run()
        if err != nil {
            fmt.Fprintf(w, "Error starting VM: %v", err)
            return
        }
        fmt.Fprintf(w, vmName+" VM has been started successfully")
    })

    // Start the HTTP server
    fmt.Printf("Server is starting on %s\n", addr)
    err := http.ListenAndServe(addr, nil)
    if err != nil {
        fmt.Printf("Server failed to start: %v\n", err)
    }
}

 

vmhook

Edited by leporel
Link to comment
  • 3 months later...
On 8/23/2020 at 9:40 AM, Maddeen said:

@testdasiok - the commands themself works as predicted. But now I got another problem. 

Due that I'm a MacOS user I can't get "sshpass" to work. So I can't build a cli command like 

sshpass -p "password" ssh -o StrictHostKeyChecking=no root@YOUR_IP_ADRESS

I need to have a passwordless login via ssh-key.
Normally this wouldn't be a problem for me - I set this up on my Raspberry with no struggles - but on Raspberry it was quite easy.

Copy pub-key to the PI - ready!

 

But the manual I found HERE in the forum, is outdated (Unraid 6.8 and above does not allow executing scripts from the flash (/boot) drive) and nobody have updated this for a current solution

 

Is it possible that I just copy the id_rsa.pub (generated on my mac) to the folder /boot/config/ssh/ - reboot - and everythings fine?

 

 

UPDATE: @jonathanm - thanks for this hint. I dont saw it when I wrote the lines above. I'll try it and give a feedback asap

UPDATE2: @jonathanm - sadly putty is a windows software and not be available for mac :( So the question above is still applies ;-)

Sorry - very late to the party.

 

To setup auto login with ssh keys I generally use :

ssh-copy-id user@host

Would that not work here?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...