github.com/openshift/installer@v1.4.17/pkg/infrastructure/baremetal/ignition.go (about) 1 package baremetal 2 3 import ( 4 "encoding/xml" 5 "fmt" 6 "os" 7 8 "github.com/digitalocean/go-libvirt" 9 "github.com/sirupsen/logrus" 10 ) 11 12 type defIgnition struct { 13 Name string 14 PoolName string 15 Content string 16 } 17 18 func (ign *defIgnition) createFile() (string, error) { 19 tempFile, err := os.CreateTemp("", ign.Name) 20 if err != nil { 21 return "", fmt.Errorf("error creating tmp file: %w", err) 22 } 23 defer tempFile.Close() 24 25 if _, err := tempFile.WriteString(ign.Content); err != nil { 26 return "", fmt.Errorf("cannot write Ignition object to temporary " + 27 "ignition file") 28 } 29 30 return tempFile.Name(), nil 31 } 32 33 func (ign *defIgnition) CreateAndUpload(client *libvirt.Libvirt) (string, error) { 34 pool, err := client.StoragePoolLookupByName(ign.PoolName) 35 if err != nil { 36 return "", fmt.Errorf("can't find storage pool '%s'", ign.PoolName) 37 } 38 39 err = client.StoragePoolRefresh(pool, 0) 40 if err != nil { 41 return "", fmt.Errorf("failed to refresh pool %w", err) 42 } 43 44 volumeDef := newVolume(ign.Name) 45 46 ignFile, err := ign.createFile() 47 if err != nil { 48 return "", err 49 } 50 defer func() { 51 if err = os.Remove(ignFile); err != nil { 52 logrus.Errorf("error while removing tmp Ignition file: %s", err) 53 } 54 }() 55 56 img, err := newImage(ignFile) 57 if err != nil { 58 return "", err 59 } 60 61 size, err := img.Size() 62 if err != nil { 63 return "", err 64 } 65 66 volumeDef.Capacity.Unit = "B" 67 volumeDef.Capacity.Value = size 68 volumeDef.Target.Format.Type = "raw" 69 70 volumeDefXML, err := xml.Marshal(volumeDef) 71 if err != nil { 72 return "", fmt.Errorf("error serializing libvirt volume: %w", err) 73 } 74 75 volume, err := client.StorageVolCreateXML(pool, string(volumeDefXML), 0) 76 if err != nil { 77 return "", fmt.Errorf("error creating libvirt volume for Ignition %s: %w", ign.Name, err) 78 } 79 80 err = img.Import(newCopier(client, volume, volumeDef.Capacity.Value), volumeDef) 81 if err != nil { 82 return "", fmt.Errorf("error while uploading ignition file %s: %w", img.String(), err) 83 } 84 85 return "", nil 86 }