Skip to content

Commit 36dce69

Browse files
committed
fix #16
1 parent 02dda9e commit 36dce69

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

cloud/scope/remote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewSSHClient(addr, user, password string) (*SSHClient, error) {
2828
return &SSHClient{conn, password}, nil
2929
}
3030

31-
func (conn *SSHClient) RunCommand(cmd string, stdin ...string) (string, error) {
31+
func (conn *SSHClient) RunCommand(cmd string) (string, error) {
3232
session, err := conn.NewSession()
3333
if err != nil {
3434
return "", err

cloud/services/compute/instance/image.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"path"
7+
"strings"
78

89
"github.com/pkg/errors"
910
"github.com/sp-yduck/proxmox/pkg/service/node/vm"
@@ -53,7 +54,19 @@ func SetCloudImage(ctx context.Context, vmid int, storage infrav1.Storage, image
5354
return errors.Errorf("failed to download image: %s : %v", out, err)
5455
}
5556

56-
// to do: should confirm if the checksum matchies
57+
// checksum
58+
if image.Checksum != "" {
59+
cscmd, err := findValidChecksumCommand(*image.ChecksumType)
60+
if err != nil {
61+
return err
62+
}
63+
cmd := fmt.Sprintf("%s --check -", cscmd)
64+
stdin := fmt.Sprintf("%s %s", image.Checksum, rawImageFilePath)
65+
out, err = ssh.RunWithStdin(cmd, stdin)
66+
if err != nil {
67+
return errors.Errorf("failed to confirm checksum: %s : %v", out, err)
68+
}
69+
}
5770

5871
destPath := fmt.Sprintf("%s/images/%d/vm-%d-disk-0.raw", storage.Path, vmid, vmid)
5972
out, err = ssh.RunCommand(fmt.Sprintf("/usr/bin/qemu-img convert -O raw %s %s", rawImageFilePath, destPath))
@@ -62,3 +75,15 @@ func SetCloudImage(ctx context.Context, vmid int, storage infrav1.Storage, image
6275
}
6376
return nil
6477
}
78+
79+
func findValidChecksumCommand(csType string) (string, error) {
80+
csType = strings.ToLower(csType)
81+
switch csType {
82+
case "sha256", "sha256sum":
83+
return "sha256sum", nil
84+
case "md5", "md5sum":
85+
return "md5sum", nil
86+
default:
87+
return "", errors.Errorf("checksum type %s is not supported", csType)
88+
}
89+
}

examples/machine_deployment/ubuntu2204.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ spec:
4242
spec:
4343
image:
4444
url: https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64-disk-kvm.img
45+
checksum: 86e996f35732d26cd8b0888c46c4309d4d3b04eb6980378cf82b4d3eb2796549
46+
checksumType: sha256
4547
cloudInit:
4648
user:
4749
packages:

templates/cluster-template.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ spec:
8383
spec:
8484
image:
8585
url: https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64-disk-kvm.img
86+
checksum: 86e996f35732d26cd8b0888c46c4309d4d3b04eb6980378cf82b4d3eb2796549
87+
checksumType: sha256
8688
hardware:
8789
cpu: 4
8890
memory: 8192

0 commit comments

Comments
 (0)