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

Update virtual media mounting: #398

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Changes from 1 commit
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
65 changes: 31 additions & 34 deletions internal/redfishwrapper/virtual_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package redfishwrapper

import (
"context"
"errors"
"fmt"
"slices"

"github.com/pkg/errors"
rf "github.com/stmcginnis/gofish/redfish"
)

// Set the virtual media attached to the system, or just eject everything if mediaURL is empty.
func (c *Client) SetVirtualMedia(ctx context.Context, kind string, mediaURL string) (ok bool, err error) {
func (c *Client) SetVirtualMedia(ctx context.Context, kind string, mediaURL string) (bool, error) {
managers, err := c.Managers(ctx)
if err != nil {
return false, err
}
if len(managers) == 0 {
return false, errors.New("no redfish managers found")
}

var mediaKind rf.VirtualMediaType
switch kind {
Expand All @@ -29,50 +33,43 @@ func (c *Client) SetVirtualMedia(ctx context.Context, kind string, mediaURL stri
return false, errors.New("invalid media type")
}

for _, manager := range managers {
virtualMedia, err := manager.VirtualMedia()
for _, m := range managers {
virtualMedia, err := m.VirtualMedia()
if err != nil {
return false, err
}
for _, media := range virtualMedia {
if media.Inserted {
err = media.EjectMedia()
if err != nil {
return false, err
}
}
if len(virtualMedia) == 0 {
return false, errors.New("no virtual media found")
}
}

// An empty mediaURL means eject everything, so if that's the case we're done. Otherwise, we
// need to insert the media.
if mediaURL != "" {
setMedia := false
for _, manager := range managers {
virtualMedia, err := manager.VirtualMedia()
if err != nil {
return false, err
for _, vm := range virtualMedia {
if vm.Inserted {
if err := vm.EjectMedia(); err != nil {
return false, err
}
}

for _, media := range virtualMedia {
for _, t := range media.MediaTypes {
if t == mediaKind {
err = media.InsertMedia(mediaURL, true, true)
if err != nil {
if mediaURL != "" {
jacobweinstock marked this conversation as resolved.
Show resolved Hide resolved
if slices.Contains(vm.MediaTypes, mediaKind) {
jacobweinstock marked this conversation as resolved.
Show resolved Hide resolved
if err := vm.InsertMedia(mediaURL, true, true); err != nil {
// Some BMC's (Supermicro SYS-E300-D9, for example) don't support the "inserted" and "writeProtected" properties,
jacobweinstock marked this conversation as resolved.
Show resolved Hide resolved
// so we try to insert the media without them if the first attempt fails.
if err := vm.InsertMediaConfig(rf.VirtualMediaConfig{Image: mediaURL}); err != nil {
return false, err
}
setMedia = true
break
}
return true, nil
}

return false, fmt.Errorf("media kind %s not supported by BMC, supported media kinds %q", kind, vm.MediaTypes)
}
}
if !setMedia {
return false, fmt.Errorf("media kind %s not supported", kind)

// Only ejecting the media was requested.
return true, nil
}
}

return true, nil
// If we actual get here, then something very unexpected happened as there isn't a known code path that would cause this error to be returned.
return false, errors.New("unexpected error setting virtual media")
}

func (c *Client) InsertedVirtualMedia(ctx context.Context) ([]string, error) {
Expand All @@ -83,8 +80,8 @@ func (c *Client) InsertedVirtualMedia(ctx context.Context) ([]string, error) {

var inserted []string

for _, manager := range managers {
virtualMedia, err := manager.VirtualMedia()
for _, m := range managers {
virtualMedia, err := m.VirtualMedia()
if err != nil {
return nil, err
}
Expand Down
Loading