@@ -5135,16 +5135,14 @@ func (d *lxc) Update(args db.InstanceArgs, userRequested bool) error {
5135
5135
}
5136
5136
5137
5137
// Export backs up the instance.
5138
- func (d * lxc ) Export (w io.Writer , properties map [string ]string , expiration time.Time , tracker * ioprogress.ProgressTracker ) (api.ImageMetadata , error ) {
5138
+ func (d * lxc ) Export (w io.Writer , properties map [string ]string , expiration time.Time , tracker * ioprogress.ProgressTracker ) (* api.ImageMetadata , error ) {
5139
5139
ctxMap := logger.Ctx {
5140
5140
"created" : d .creationDate ,
5141
5141
"ephemeral" : d .ephemeral ,
5142
5142
"used" : d .lastUsedDate }
5143
5143
5144
- meta := api.ImageMetadata {}
5145
-
5146
5144
if d .IsRunning () {
5147
- return meta , fmt .Errorf ("Cannot export a running instance as an image" )
5145
+ return nil , fmt .Errorf ("Cannot export a running instance as an image" )
5148
5146
}
5149
5147
5150
5148
d .logger .Info ("Exporting instance" , ctxMap )
@@ -5153,7 +5151,7 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time.
5153
5151
_ , err := d .mount ()
5154
5152
if err != nil {
5155
5153
d .logger .Error ("Failed exporting instance" , ctxMap )
5156
- return meta , err
5154
+ return nil , err
5157
5155
}
5158
5156
5159
5157
defer func () { _ = d .unmount () }()
@@ -5162,7 +5160,7 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time.
5162
5160
idmap , err := d .DiskIdmap ()
5163
5161
if err != nil {
5164
5162
d .logger .Error ("Failed exporting instance" , ctxMap )
5165
- return meta , err
5163
+ return nil , err
5166
5164
}
5167
5165
5168
5166
// Create the tarball.
@@ -5188,164 +5186,115 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time.
5188
5186
return nil
5189
5187
}
5190
5188
5191
- // Look for metadata.yaml .
5192
- fnam := filepath . Join ( cDir , "metadata.yaml" )
5193
- if ! util . PathExists ( fnam ) {
5194
- // Generate a new metadata.yaml.
5195
- tempDir , err := os . MkdirTemp ( "" , "incus_metadata_" )
5189
+ // Get the instance's architecture .
5190
+ var arch string
5191
+ if d . IsSnapshot ( ) {
5192
+ parentName , _ , _ := api . GetParentAndSnapshotName ( d . name )
5193
+ parent , err := instance . LoadByProjectAndName ( d . state , d . project . Name , parentName )
5196
5194
if err != nil {
5197
5195
_ = tarWriter .Close ()
5198
5196
d .logger .Error ("Failed exporting instance" , ctxMap )
5199
- return meta , err
5200
- }
5201
-
5202
- defer func () { _ = os .RemoveAll (tempDir ) }()
5203
-
5204
- // Get the instance's architecture.
5205
- var arch string
5206
- if d .IsSnapshot () {
5207
- parentName , _ , _ := api .GetParentAndSnapshotName (d .name )
5208
- parent , err := instance .LoadByProjectAndName (d .state , d .project .Name , parentName )
5209
- if err != nil {
5210
- _ = tarWriter .Close ()
5211
- d .logger .Error ("Failed exporting instance" , ctxMap )
5212
- return meta , err
5213
- }
5214
-
5215
- arch , _ = osarch .ArchitectureName (parent .Architecture ())
5216
- } else {
5217
- arch , _ = osarch .ArchitectureName (d .architecture )
5218
- }
5219
-
5220
- if arch == "" {
5221
- arch , err = osarch .ArchitectureName (d .state .OS .Architectures [0 ])
5222
- if err != nil {
5223
- d .logger .Error ("Failed exporting instance" , ctxMap )
5224
- return meta , err
5225
- }
5226
- }
5227
-
5228
- // Fill in the metadata.
5229
- meta .Architecture = arch
5230
- meta .CreationDate = time .Now ().UTC ().Unix ()
5231
- meta .Properties = properties
5232
- if ! expiration .IsZero () {
5233
- meta .ExpiryDate = expiration .UTC ().Unix ()
5197
+ return nil , err
5234
5198
}
5235
5199
5236
- data , err := yaml .Marshal (& meta )
5237
- if err != nil {
5238
- _ = tarWriter .Close ()
5239
- d .logger .Error ("Failed exporting instance" , ctxMap )
5240
- return meta , err
5241
- }
5200
+ arch , _ = osarch .ArchitectureName (parent .Architecture ())
5201
+ } else {
5202
+ arch , _ = osarch .ArchitectureName (d .architecture )
5203
+ }
5242
5204
5243
- // Write the actual file.
5244
- fnam = filepath .Join (tempDir , "metadata.yaml" )
5245
- err = os .WriteFile (fnam , data , 0644 )
5205
+ if arch == "" {
5206
+ arch , err = osarch .ArchitectureName (d .state .OS .Architectures [0 ])
5246
5207
if err != nil {
5247
- _ = tarWriter .Close ()
5248
5208
d .logger .Error ("Failed exporting instance" , ctxMap )
5249
- return meta , err
5209
+ return nil , err
5250
5210
}
5211
+ }
5251
5212
5252
- fi , err := os .Lstat (fnam )
5253
- if err != nil {
5254
- _ = tarWriter .Close ()
5255
- d .logger .Error ("Failed exporting instance" , ctxMap )
5256
- return meta , err
5257
- }
5213
+ // Generate metadata.yaml.
5214
+ meta := api.ImageMetadata {}
5215
+ fnam := filepath .Join (cDir , "metadata.yaml" )
5258
5216
5259
- tmpOffset := len (path .Dir (fnam )) + 1
5260
- err = tarWriter .WriteFile (fnam [tmpOffset :], fnam , fi , false )
5261
- if err != nil {
5262
- _ = tarWriter .Close ()
5263
- d .logger .Debug ("Error writing to tarfile" , logger.Ctx {"err" : err })
5264
- d .logger .Error ("Failed exporting instance" , ctxMap )
5265
- return meta , err
5266
- }
5267
- } else {
5217
+ if util .PathExists (fnam ) {
5268
5218
// Parse the metadata.
5269
5219
content , err := os .ReadFile (fnam )
5270
5220
if err != nil {
5271
5221
_ = tarWriter .Close ()
5272
5222
d .logger .Error ("Failed exporting instance" , ctxMap )
5273
- return meta , err
5223
+ return nil , err
5274
5224
}
5275
5225
5276
5226
err = yaml .Unmarshal (content , & meta )
5277
5227
if err != nil {
5278
5228
_ = tarWriter .Close ()
5279
5229
d .logger .Error ("Failed exporting instance" , ctxMap )
5280
- return meta , err
5230
+ return nil , err
5281
5231
}
5232
+ }
5282
5233
5283
- if ! expiration . IsZero () {
5284
- meta .ExpiryDate = expiration . UTC (). Unix ()
5285
- }
5234
+ // Fill in the metadata.
5235
+ meta .Architecture = arch
5236
+ meta . CreationDate = time . Now (). UTC (). Unix ()
5286
5237
5287
- if properties ! = nil {
5288
- meta .Properties = properties
5289
- }
5238
+ if meta . Properties = = nil {
5239
+ meta .Properties = map [ string ] string {}
5240
+ }
5290
5241
5291
- if properties != nil || ! expiration .IsZero () {
5292
- // Generate a new metadata.yaml.
5293
- tempDir , err := os .MkdirTemp ("" , "incus_metadata_" )
5294
- if err != nil {
5295
- _ = tarWriter .Close ()
5296
- d .logger .Error ("Failed exporting instance" , ctxMap )
5297
- return meta , err
5298
- }
5242
+ for k , v := range properties {
5243
+ meta .Properties [k ] = v
5244
+ }
5299
5245
5300
- defer func () { _ = os .RemoveAll (tempDir ) }()
5246
+ if ! expiration .IsZero () {
5247
+ meta .ExpiryDate = expiration .UTC ().Unix ()
5248
+ }
5301
5249
5302
- data , err := yaml .Marshal (& meta )
5303
- if err != nil {
5304
- _ = tarWriter .Close ()
5305
- d .logger .Error ("Failed exporting instance" , ctxMap )
5306
- return meta , err
5307
- }
5250
+ // Write the new metadata.yaml.
5251
+ tempDir , err := os .MkdirTemp ("" , "incus_metadata_" )
5252
+ if err != nil {
5253
+ _ = tarWriter .Close ()
5254
+ d .logger .Error ("Failed exporting instance" , ctxMap )
5255
+ return nil , err
5256
+ }
5308
5257
5309
- // Write the actual file.
5310
- fnam = filepath .Join (tempDir , "metadata.yaml" )
5311
- err = os .WriteFile (fnam , data , 0644 )
5312
- if err != nil {
5313
- _ = tarWriter .Close ()
5314
- d .logger .Error ("Failed exporting instance" , ctxMap )
5315
- return meta , err
5316
- }
5317
- }
5258
+ defer func () { _ = os .RemoveAll (tempDir ) }()
5318
5259
5319
- // Include metadata.yaml in the tarball.
5320
- fi , err := os .Lstat (fnam )
5321
- if err != nil {
5322
- _ = tarWriter .Close ()
5323
- d .logger .Debug ("Error statting during export" , logger.Ctx {"fileName" : fnam })
5324
- d .logger .Error ("Failed exporting instance" , ctxMap )
5325
- return meta , err
5326
- }
5260
+ data , err := yaml .Marshal (& meta )
5261
+ if err != nil {
5262
+ _ = tarWriter .Close ()
5263
+ d .logger .Error ("Failed exporting instance" , ctxMap )
5264
+ return nil , err
5265
+ }
5327
5266
5328
- if properties != nil || ! expiration .IsZero () {
5329
- tmpOffset := len (path .Dir (fnam )) + 1
5330
- err = tarWriter .WriteFile (fnam [tmpOffset :], fnam , fi , false )
5331
- } else {
5332
- err = tarWriter .WriteFile (fnam [offset :], fnam , fi , false )
5333
- }
5267
+ fnam = filepath .Join (tempDir , "metadata.yaml" )
5268
+ err = os .WriteFile (fnam , data , 0644 )
5269
+ if err != nil {
5270
+ _ = tarWriter .Close ()
5271
+ d .logger .Error ("Failed exporting instance" , ctxMap )
5272
+ return nil , err
5273
+ }
5334
5274
5335
- if err != nil {
5336
- _ = tarWriter .Close ()
5337
- d .logger .Debug ("Error writing to tarfile" , logger.Ctx {"err" : err })
5338
- d .logger .Error ("Failed exporting instance" , ctxMap )
5339
- return meta , err
5340
- }
5275
+ // Add metadata.yaml to the tarball.
5276
+ fi , err := os .Lstat (fnam )
5277
+ if err != nil {
5278
+ _ = tarWriter .Close ()
5279
+ d .logger .Error ("Failed exporting instance" , ctxMap )
5280
+ return nil , err
5281
+ }
5282
+
5283
+ tmpOffset := len (filepath .Dir (fnam )) + 1
5284
+ err = tarWriter .WriteFile (fnam [tmpOffset :], fnam , fi , false )
5285
+ if err != nil {
5286
+ _ = tarWriter .Close ()
5287
+ d .logger .Debug ("Error writing to tarfile" , logger.Ctx {"err" : err })
5288
+ d .logger .Error ("Failed exporting instance" , ctxMap )
5289
+ return nil , err
5341
5290
}
5342
5291
5343
5292
// Include all the rootfs files.
5344
5293
fnam = d .RootfsPath ()
5345
5294
err = filepath .Walk (fnam , writeToTar )
5346
5295
if err != nil {
5347
5296
d .logger .Error ("Failed exporting instance" , ctxMap )
5348
- return meta , err
5297
+ return nil , err
5349
5298
}
5350
5299
5351
5300
// Include all the templates.
@@ -5354,18 +5303,18 @@ func (d *lxc) Export(w io.Writer, properties map[string]string, expiration time.
5354
5303
err = filepath .Walk (fnam , writeToTar )
5355
5304
if err != nil {
5356
5305
d .logger .Error ("Failed exporting instance" , ctxMap )
5357
- return meta , err
5306
+ return nil , err
5358
5307
}
5359
5308
}
5360
5309
5361
5310
err = tarWriter .Close ()
5362
5311
if err != nil {
5363
5312
d .logger .Error ("Failed exporting instance" , ctxMap )
5364
- return meta , err
5313
+ return nil , err
5365
5314
}
5366
5315
5367
5316
d .logger .Info ("Exported instance" , ctxMap )
5368
- return meta , nil
5317
+ return & meta , nil
5369
5318
}
5370
5319
5371
5320
func collectCRIULogFile (d instance.Instance , imagesDir string , function string , method string ) error {
0 commit comments