Statistics
| Branch: | Tag: | Revision:

one / include / Image.h @ 0fa579fd

History | View | Annotate | Download (19.6 KB)

1
/* ------------------------------------------------------------------------ */
2
/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems              */
3
/*                                                                          */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may  */
5
/* not use this file except in compliance with the License. You may obtain  */
6
/* a copy of the License at                                                 */
7
/*                                                                          */
8
/* http://www.apache.org/licenses/LICENSE-2.0                               */
9
/*                                                                          */
10
/* Unless required by applicable law or agreed to in writing, software      */
11
/* distributed under the License is distributed on an "AS IS" BASIS,        */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
13
/* See the License for the specific language governing permissions and      */
14
/* limitations under the License.                                           */
15
/* -------------------------------------------------------------------------*/
16

    
17
#ifndef IMAGE_H_
18
#define IMAGE_H_
19

    
20
#include "PoolSQL.h"
21
#include "ImageTemplate.h"
22
#include "NebulaLog.h"
23
#include "ObjectCollection.h"
24
#include "Snapshots.h"
25

    
26
using namespace std;
27

    
28
class VirtualMachineDisk;
29

    
30
/**
31
 *  The Image class.
32
 */
33
class Image : public PoolObjectSQL
34
{
35
public:
36
    /**
37
     *  Type of Images
38
     */
39
    enum ImageType
40
    {
41
        OS        = 0, /** < Base OS image */
42
        CDROM     = 1, /** < An ISO9660 image */
43
        DATABLOCK = 2, /** < User persistent data device */
44
        KERNEL    = 3, /** < Kernel files */
45
        RAMDISK   = 4, /** < Initrd files */
46
        CONTEXT   = 5  /** < Context files */
47
    };
48

    
49
    /**
50
     *  Return the string representation of an ImageType
51
     *    @param ob the type
52
     *    @return the string
53
     */
54
    static string type_to_str(ImageType ob)
55
    {
56
        switch (ob)
57
        {
58
            case OS:        return "OS" ; break;
59
            case CDROM:     return "CDROM" ; break;
60
            case DATABLOCK: return "DATABLOCK" ; break;
61
            case KERNEL:    return "KERNEL" ; break;
62
            case RAMDISK:   return "RAMDISK" ; break;
63
            case CONTEXT:   return "CONTEXT" ; break;
64
            default:        return "";
65
        }
66
    };
67

    
68
    /**
69
     *  Return the string representation of an ImageType
70
     *    @param ob the type
71
     *    @return the string
72
     */
73
    static ImageType str_to_type(string& str_type);
74

    
75
    /**
76
     *  Type of Disks (used by the VMM_MAD). Values: BLOCK, CDROM or
77
     *  FILE
78
     */
79
    enum DiskType
80
    {
81
        FILE           = 0, /** < File-based disk */
82
        CD_ROM         = 1, /** < An ISO9660 disk */
83
        BLOCK          = 2, /** < Block-device disk */
84
        RBD            = 3, /** < CEPH RBD disk */
85
        RBD_CDROM      = 4, /** < CEPH RBD CDROM disk */
86
        GLUSTER        = 5, /** < Gluster Block Device */
87
        GLUSTER_CDROM  = 6, /** < Gluster CDROM Device Device */
88
        SHEEPDOG       = 7, /** < Sheepdog Block Device */
89
        SHEEPDOG_CDROM = 8, /** < Sheepdog CDROM Device Device */
90
        ISCSI          = 9, /** < iSCSI Volume (Devices Datastore) */
91
        NONE           = 255 /** < No disk type, error situation */
92
    };
93

    
94
    /**
95
     *  Return the string representation of a DiskType
96
     *    @param ob the type
97
     *    @return the string
98
     */
99
    static string disk_type_to_str(DiskType ob)
100
    {
101
        switch (ob)
102
        {
103
            case FILE:           return "FILE" ; break;
104
            case CD_ROM:         return "CDROM" ; break;
105
            case BLOCK:          return "BLOCK" ; break;
106
            case RBD:            return "RBD" ; break;
107
            case RBD_CDROM:      return "RBD_CDROM" ; break;
108
            case GLUSTER:        return "GLUSTER" ; break;
109
            case GLUSTER_CDROM:  return "GLUSTER_CDROM" ; break;
110
            case SHEEPDOG:       return "SHEEPDOG" ; break;
111
            case SHEEPDOG_CDROM: return "SHEEPDOG_CDROM" ; break;
112
            case ISCSI:          return "ISCSI" ; break;
113
            default:             return "";
114
        }
115
    };
116

    
117
    /**
118
     *  Return the string representation of a DiskType
119
     *    @param s_disk_type string representing the DiskTypr
120
     *    @return the DiskType (defaults to FILE)
121
     */
122
    static DiskType str_to_disk_type(string& s_disk_type);
123

    
124
    /**
125
     *  Image State
126
     */
127
    enum ImageState
128
    {
129
        INIT      = 0, /** < Initialization state */
130
        READY     = 1, /** < Image ready to use */
131
        USED      = 2, /** < Image in use */
132
        DISABLED  = 3, /** < Image can not be instantiated by a VM */
133
        LOCKED    = 4, /** < FS operation for the Image in process */
134
        ERROR     = 5, /** < Error state the operation FAILED*/
135
        CLONE     = 6, /** < Image is being cloned */
136
        DELETE    = 7, /** < DS is deleting the image */
137
        USED_PERS = 8, /** < Image is in use and persistent */
138
        LOCKED_USED = 9,      /** < FS operation in progress, VMs waiting */
139
        LOCKED_USED_PERS = 10 /** < FS operation in progress, VMs waiting. Persistent */
140
    };
141

    
142
    /**
143
     * Returns the string representation of an ImageState
144
     * @param state The state
145
     * @return the string representation
146
     */
147
    static string state_to_str(ImageState state)
148
    {
149
        switch(state)
150
        {
151
            case INIT:              return "INIT";          break;
152
            case READY:             return "READY";         break;
153
            case USED:              return "USED";          break;
154
            case DISABLED:          return "DISABLED";      break;
155
            case LOCKED:            return "LOCKED";        break;
156
            case ERROR:             return "ERROR";         break;
157
            case CLONE:             return "CLONE";         break;
158
            case DELETE:            return "DELETE";        break;
159
            case USED_PERS:         return "USED";          break;
160
            case LOCKED_USED:       return "LOCKED_USED";   break;
161
            case LOCKED_USED_PERS:  return "LOCKED_USED";   break;
162
            default:                return "";
163
        }
164
    };
165

    
166
    // *************************************************************************
167
    // Image Public Methods
168
    // *************************************************************************
169

    
170
    /**
171
     * Function to print the Image object into a string in XML format
172
     *  @param xml the resulting XML string
173
     *  @return a reference to the generated string
174
     */
175
    string& to_xml(string& xml) const;
176

    
177
    /**
178
     *  Rebuilds the object from an xml formatted string
179
     *    @param xml_str The xml-formatted string
180
     *
181
     *    @return 0 on success, -1 otherwise
182
     */
183
    int from_xml(const string &xml_str);
184

    
185
    /**
186
     *  Returns true if the image is persistent
187
     *     @return true if the image is persistent
188
     */
189
    bool is_persistent() const
190
    {
191
        return (persistent_img == 1);
192
    };
193

    
194
    bool is_managed() const
195
    {
196
        bool one_managed;
197

    
198
        if (get_template_attribute("OPENNEBULA_MANAGED", one_managed) == false)
199
        {
200
            one_managed = true;
201
        }
202

    
203
        return one_managed;
204
    }
205

    
206
    /**
207
     *  Check the PERSISTENT attribute in an image Template, if not set the
208
     *  DEFAULT_IMAGE_PERSISTENT and DEFAULT_IMAGE_PERSISTENT_NEW are check in
209
     *  user/group/oned.conf to set the attribute in the image.
210
     *    @param image_template
211
     *    @param uid of the user making the request
212
     *    @param gid of the group of the user making the request
213
     *    @param is_allocate true for one.image.allocate API Calls
214
     *    @return true if the image is set to persistent, false otherwise
215
     */
216
    static bool test_set_persistent(Template * image_template, int uid, int gid,
217
            bool is_allocate);
218

    
219
    /**
220
     *  Returns the source path of the image
221
     *     @return source of image
222
     */
223
    const string& get_source() const
224
    {
225
        return source;
226
    }
227

    
228
    /**
229
     *  Returns the original path of the image
230
     *     @return path of image
231
     */
232
    const string& get_path() const
233
    {
234
        return path;
235
    }
236

    
237
    /**
238
     *  Returns the fs_type for the image (defined for datablocks)
239
     *     @return fs_type
240
     */
241
    const string& get_fstype() const
242
    {
243
        return fs_type;
244
    }
245

    
246
    /**
247
     *  Returns the size of the image
248
     *     @return size in MB
249
     */
250
    long long get_size() const
251
    {
252
        return size_mb;
253
    }
254

    
255

    
256
    /**
257
     *  Sets the source path of the image
258
     */
259
    void set_source(const string& _source)
260
    {
261
        source = _source;
262
    }
263

    
264
    /**
265
     *  Sets the size for the image
266
     */
267
    void set_size(long long _size_mb)
268
    {
269
        size_mb = _size_mb;
270
    }
271

    
272
    /**
273
     *  Returns the type of the image
274
     *     @return type
275
     */
276
    ImageType get_type() const
277
    {
278
        return type;
279
    }
280
    /**
281
     *  Returns the image state
282
     *     @return state of image
283
     */
284
    ImageState get_state() const
285
    {
286
        return state;
287
    }
288

    
289
    /**
290
     *  Sets the image state
291
     *     @param state of image
292
     */
293
    void set_state(ImageState _state);
294

    
295
    /**
296
     * Moves the image from the locked* states to ready, used, used_persistent
297
     */
298
    void set_state_unlock();
299

    
300
    /**
301
     *  Return the ID of the image we are cloning this one from (if any)
302
     */
303
    int get_cloning_id() const
304
    {
305
        return cloning_id;
306
    }
307

    
308
    /**
309
     *  Sets the ID of the image we are cloning this one from (if any)
310
     */
311
    void set_cloning_id(int id)
312
    {
313
        cloning_id = id;
314
    }
315

    
316
    /**
317
     *  Clear the cloning state of the image
318
     */
319
    void clear_cloning_id()
320
    {
321
        cloning_id = -1;
322
    }
323

    
324
    /* ---------------------------------------------------------------------- */
325
    /*   Access Image Counters (running vms and cloning operations )          */
326
    /* ---------------------------------------------------------------------- */
327

    
328
    int dec_running (int vm_id)
329
    {
330
        if ( vm_collection.del(vm_id) == 0 )
331
        {
332
            running_vms--;
333
        }
334

    
335
        return running_vms;
336
    }
337

    
338
    int inc_running(int vm_id)
339
    {
340
        if ( vm_collection.add(vm_id) == 0 )
341
        {
342
            running_vms++;
343
        }
344

    
345
        return running_vms;
346
    }
347

    
348
    int get_running() const
349
    {
350
        return running_vms;
351
    }
352

    
353
    set<int> get_running_ids() const
354
    {
355
        return vm_collection.clone();
356
    }
357

    
358
    int get_cloning() const
359
    {
360
        return cloning_ops;
361
    }
362

    
363
    int dec_cloning(PoolObjectSQL::ObjectType ot, int oid)
364
    {
365
        int rc = -1;
366

    
367
        if (ot == PoolObjectSQL::IMAGE)
368
        {
369
            rc = img_clone_collection.del(oid);
370
        }
371
        else //if (ot == PoolObjectSQL::MARKETPLACEAPP)
372
        {
373
            rc = app_clone_collection.del(oid);
374
        }
375

    
376
        if ( rc == 0 )
377
        {
378
            cloning_ops--;
379
        }
380

    
381
        return cloning_ops;
382
    }
383

    
384
    int inc_cloning(PoolObjectSQL::ObjectType ot, int oid)
385
    {
386
        int rc = -1;
387

    
388
        if (ot == PoolObjectSQL::IMAGE)
389
        {
390
            rc = img_clone_collection.add(oid);
391
        }
392
        else //if (ot == PoolObjectSQL::MARKETPLACEAPP)
393
        {
394
            rc = app_clone_collection.add(oid);
395
        }
396

    
397
        if ( rc == 0 )
398
        {
399
            cloning_ops++;
400
        }
401

    
402
        return cloning_ops;
403
    }
404

    
405
    /**
406
     * Sets the Image type.
407
     *
408
     * @param _type the new type. It will be transformed to upper case
409
     * @return 0 on success, -1 otherwise
410
     */
411
    int set_type(string& _type, string& error);
412

    
413
    /**
414
     *  Check if the image is used for saving_as a current one
415
     *  @return true if the image will be used to save an existing image.
416
     */
417
    bool is_saving()
418
    {
419
        return (static_cast<ImageTemplate *>(obj_template))->is_saving();
420
    }
421

    
422
    /**
423
     *  Set/Unset an image as persistent
424
     *    @param persistent true to make an image persistent
425
     *    @param error_str Returns the error reason, if any
426
     *
427
     *    @return 0 on success
428
     */
429
    int persistent(bool persis, string& error_str)
430
    {
431
        ostringstream oss;
432

    
433
        if ((snapshots.size() > 0) && !persis)
434
        {
435
           error_str = "Image has snapshots.";
436
           return -1;
437
        }
438

    
439
        switch(state)
440
        {
441
            case USED:
442
            case CLONE:
443
            case USED_PERS:
444
            case LOCKED_USED:
445
            case LOCKED_USED_PERS:
446
                oss << "Image cannot be in state " << state_to_str(state) <<".";
447
                error_str = oss.str();
448
                return -1;
449

    
450
            case INIT:
451
            case READY:
452
            case DISABLED:
453
            case LOCKED:
454
            case ERROR:
455
            case DELETE:
456
                if (persis == true)
457
                {
458
                    persistent_img = 1;
459
                }
460
                else
461
                {
462
                    persistent_img = 0;
463
                }
464

    
465
                break;
466
        }
467

    
468
        return 0;
469
    }
470

    
471
    /**
472
     * Modifies the given disk attribute adding the following attributes:
473
     *  * SOURCE: the file-path.
474
     *  * TARGET: will only be set if the Image's definition includes it.
475
     *
476
     * @param disk attribute for the VM template
477
     * @param img_type will be set to the used image's type
478
     * @param dev_prefix will be set to the defined dev_prefix,
479
     *   or the default one
480
     * @param inherit_attrs Attributes to be inherited from the image template
481
     *   into the disk
482
     *
483
     */
484
    void disk_attribute(VirtualMachineDisk *  disk,
485
                        ImageType&            img_type,
486
                        string&               dev_prefix,
487
                        const vector<string>& inherit_attrs);
488
    /**
489
     *  Factory method for image templates
490
     */
491
    Template * get_new_template() const
492
    {
493
        return new ImageTemplate;
494
    }
495

    
496
    /**
497
     * Returns the Datastore ID
498
     */
499
    int get_ds_id() const
500
    {
501
        return ds_id;
502
    };
503

    
504
    /**
505
     * Returns the Datastore name
506
     */
507
    const string& get_ds_name() const
508
    {
509
        return ds_name;
510
    };
511

    
512
    /**
513
     * Updates the Datastore name
514
     */
515
    void set_ds_name(const string& name)
516
    {
517
        ds_name = name;
518
    };
519

    
520
    /**
521
     * Clones this image template including image specific attributes: NAME,
522
     * TYPE, PATH, FSTYPE, SIZE and PERSISTENT
523
     * @param new_name Value for the NAME attribute
524
     * @return Pointer to the new tempalte 0 in case of success
525
     */
526
    ImageTemplate * clone_template(const string& new_name) const;
527

    
528
    /* ---------------------------------------------------------------------- */
529
    /* Snapshots functions                                                    */
530
    /* ---------------------------------------------------------------------- */
531
    /**
532
     *  Return the snapshot list of this image
533
     */
534
    const Snapshots& get_snapshots() const
535
    {
536
        return snapshots;
537
    };
538

    
539
    /**
540
     *  Clear all the snapshots in the list
541
     */
542
    void clear_snapshots()
543
    {
544
        snapshots.clear();
545
    }
546

    
547
    /**
548
     *  Set the snapshots for this image
549
     *  @param snapshot list
550
     */
551
    void set_snapshots(const Snapshots& s)
552
    {
553
        snapshots = s;
554
        snapshots.clear_disk_id();
555
    };
556

    
557
    void delete_snapshot(int snap_id)
558
    {
559
        snapshots.delete_snapshot(snap_id);
560
    };
561

    
562
    void revert_snapshot(int snap_id)
563
    {
564
        snapshots.active_snapshot(snap_id);
565
    };
566

    
567
    void set_target_snapshot(int snap_id)
568
    {
569
        target_snapshot = snap_id;
570
    };
571

    
572
    int get_target_snapshot()
573
    {
574
        return target_snapshot;
575
    };
576

    
577
    void clear_target_snapshot()
578
    {
579
        target_snapshot = -1;
580
    };
581

    
582
private:
583

    
584
    // -------------------------------------------------------------------------
585
    // Friends
586
    // -------------------------------------------------------------------------
587

    
588
    friend class ImagePool;
589

    
590
    // -------------------------------------------------------------------------
591
    // Image Description
592
    // -------------------------------------------------------------------------
593

    
594
    /**
595
     *  Type of the Image
596
     */
597
    ImageType    type;
598

    
599
    /**
600
     *  Type for the Disk
601
     */
602
    DiskType     disk_type;
603

    
604
    /**
605
     *  Persistency of the Image
606
     */
607
    int          persistent_img;
608

    
609
    /**
610
     *  Registration time
611
     */
612
    time_t       regtime;
613

    
614
    /**
615
     *  Path to the image
616
     */
617
    string       source;
618

    
619
    /**
620
     *  Original Path to the image (optional if source is given or datablock)
621
     */
622
    string       path;
623

    
624
    /**
625
     *  File system type for the image (mandatory for datablocks)
626
     */
627
    string       fs_type;
628

    
629
    /**
630
     *  Size of the image in MB
631
     */
632
    long long size_mb;
633

    
634
     /**
635
      *  Image state
636
      */
637
    ImageState   state;
638

    
639
    /**
640
     * Number of VMs using the image
641
     */
642
    int running_vms;
643

    
644
    /**
645
     * Number of pending cloning operations, for both images and apps
646
     */
647
    int cloning_ops;
648

    
649
    /**
650
     * Indicates if this Image is a clone of another one.
651
     * Once the clone process is complete, it should be set to -1
652
     */
653
    int cloning_id;
654

    
655
    /**
656
     * Datastore ID
657
     */
658
    int ds_id;
659

    
660
    /**
661
     * Datastore name
662
     */
663
    string ds_name;
664

    
665
    /**
666
     *  Stores a collection with the VMs using the image
667
     */
668
    ObjectCollection vm_collection;
669

    
670
    /**
671
     *  Stores a collection with the Images cloning this image
672
     */
673
    ObjectCollection img_clone_collection;
674

    
675
    /**
676
     *  Stores a collection with the Marketplace apps cloning this image
677
     */
678
    ObjectCollection app_clone_collection;
679

    
680
    /**
681
     * Snapshot list for this image
682
     */
683
    Snapshots snapshots;
684

    
685
    /**
686
     * ID of the snapshot being processed (if any)
687
     */
688
    int target_snapshot;
689

    
690
    // *************************************************************************
691
    // DataBase implementation (Private)
692
    // *************************************************************************
693

    
694
    /**
695
     *  Execute an INSERT or REPLACE Sql query.
696
     *    @param db The SQL DB
697
     *    @param replace Execute an INSERT or a REPLACE
698
     *    @param error_str Returns the error reason, if any
699
     *    @return 0 on success
700
     */
701
    int insert_replace(SqlDB *db, bool replace, string& error_str);
702

    
703
    /**
704
     *  Bootstraps the database table(s) associated to the Image
705
     *    @return 0 on success
706
     */
707
    static int bootstrap(SqlDB * db)
708
    {
709
        ostringstream oss_image(Image::db_bootstrap);
710

    
711
        return db->exec_local_wr(oss_image);
712
    };
713

    
714
    /**
715
     *  "Encrypts" the password with SHA1 digest
716
     *  @param password
717
     *  @return sha1 encrypted password
718
     */
719
    static string sha1_digest(const string& pass);
720

    
721
protected:
722

    
723
    // *************************************************************************
724
    // Constructor
725
    // *************************************************************************
726

    
727
    Image(int            uid,
728
          int            gid,
729
          const string&  uname,
730
          const string&  gname,
731
          int            umask,
732
          ImageTemplate* img_template);
733

    
734
    virtual ~Image();
735

    
736
    // *************************************************************************
737
    // DataBase implementation
738
    // *************************************************************************
739

    
740
    static const char * db_names;
741

    
742
    static const char * db_bootstrap;
743

    
744
    static const char * table;
745

    
746
    /**
747
     *  Writes the Image in the database.
748
     *    @param db pointer to the db
749
     *    @return 0 on success
750
     */
751
    virtual int insert(SqlDB *db, string& error_str);
752

    
753
    /**
754
     *  Writes/updates the Images data fields in the database.
755
     *    @param db pointer to the db
756
     *    @return 0 on success
757
     */
758
    virtual int update(SqlDB *db);
759
};
760

    
761
#endif /*IMAGE_H_*/