Skip to content

Commit a3446e1

Browse files
cfergeaupraveenkumar
authored andcommitted
Issue #438: Load correct bundle on VM restart
Currently, when they have a preexisting VM, and then upgrade the crc binary, the user will experience various misbehaviours: 1. if the binary embeds a bundle, `crc start` will complain that ~/.crc/crc_libvirt_4.1.9.crcbundle does not exist, and that `crc setup` must be run first. Once that is done, `crc start` will unpack the bundle, but will not make use of it since an older VM already exists. 2. if the binary does not embed a bundle, `crc start` will still expect to be passed a bundle, and will uncompress it even if once again it's not going to be able to use it This commit uses the BundleName data which was added to code-ready/machine in order to load the correct bundle when starting a pre-existing VM, which solves both issues. This fixes #438
1 parent 3701ab5 commit a3446e1

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

pkg/crc/machine/machine.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,21 @@ func getCrcBundleInfo(bundlePath string) (*bundle.CrcBundleInfo, error) {
6161
return bundle.Extract(bundlePath)
6262
}
6363

64+
func loadCrcBundleInfo(startConfig *StartConfig, bundleName string) (*bundle.CrcBundleInfo, error) {
65+
crcBundleMetadata, err := bundle.GetCachedBundleInfo(bundleName)
66+
if err != nil {
67+
return nil, err
68+
}
69+
if bundleName != filepath.Base(startConfig.BundlePath) {
70+
logging.Warnf("Bundle '%s' was requested, but loaded VM is using '%s'",
71+
filepath.Base(startConfig.BundlePath), bundleName)
72+
}
73+
74+
return crcBundleMetadata, nil
75+
}
76+
6477
func Start(startConfig StartConfig) (StartResult, error) {
78+
var crcBundleMetadata *bundle.CrcBundleInfo
6579
defer unsetMachineLogging()
6680

6781
result := &StartResult{Name: startConfig.Name}
@@ -75,17 +89,10 @@ func Start(startConfig StartConfig) (StartResult, error) {
7589
libMachineAPIClient := libmachine.NewClient(constants.MachineBaseDir, constants.MachineCertsDir)
7690
defer libMachineAPIClient.Close()
7791

78-
crcBundleMetadata, err := getCrcBundleInfo(startConfig.BundlePath)
79-
if err != nil {
80-
result.Error = err.Error()
81-
return *result, errors.Newf("Error to get bundle Metadata %v", err)
82-
}
83-
8492
// Pre-VM start
8593
driverInfo, _ := getDriverInfo(startConfig.VMDriver)
8694
exists, err := existVM(libMachineAPIClient, startConfig.Name)
8795
if !exists {
88-
logging.Infof("Creating VM ...")
8996
machineConfig := config.MachineConfig{
9097
Name: startConfig.Name,
9198
BundleName: filepath.Base(startConfig.BundlePath),
@@ -94,6 +101,13 @@ func Start(startConfig StartConfig) (StartResult, error) {
94101
Memory: startConfig.Memory,
95102
}
96103

104+
crcBundleMetadata, err = getCrcBundleInfo(startConfig.BundlePath)
105+
if err != nil {
106+
result.Error = err.Error()
107+
return *result, errors.Newf("Error to get bundle Metadata %v", err)
108+
}
109+
110+
logging.Infof("Creating VM ...")
97111
// Retrieve metadata info
98112
diskPath := crcBundleMetadata.GetDiskImagePath()
99113
machineConfig.DiskPathURL = fmt.Sprintf("file://%s", filepath.ToSlash(diskPath))
@@ -122,6 +136,17 @@ func Start(startConfig StartConfig) (StartResult, error) {
122136
return *result, errors.Newf("Error loading host: %v", err)
123137
}
124138

139+
bundleName, _ := host.Driver.GetBundleName()
140+
if bundleName == "" {
141+
err := errors.Newf("Error getting bundle name from CodeReady Containers instance, make sure you ran 'crc setup' and are using the latest bundle")
142+
result.Error = err.Error()
143+
return *result, err
144+
}
145+
crcBundleMetadata, err = loadCrcBundleInfo(&startConfig, bundleName)
146+
if err != nil {
147+
result.Error = err.Error()
148+
return *result, errors.Newf(err.Error())
149+
}
125150
if host.Driver.DriverName() != startConfig.VMDriver {
126151
err := errors.Newf("VM driver '%s' was requested, but loaded VM is using '%s' instead",
127152
startConfig.VMDriver, host.Driver.DriverName())

0 commit comments

Comments
 (0)