This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
vbr.c
2987 lines (2720 loc) · 112 KB
/
vbr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
/*************************************************************************
* Copyright 2009-2012 Eucalyptus Systems, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
*
* This file may incorporate work covered under the following copyright
* and permission notice:
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Regents of the University of California
* All rights reserved.
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
* THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
* COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
* AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
* SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
* WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
* REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
* IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
* NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
************************************************************************/
//!
//! @file storage/vbr.c
//! Need to provide description
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // strcasestr
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h> // waitpid
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <assert.h>
#include <dirent.h>
#include <eucalyptus.h>
#include <misc.h> // logprintfl, ensure_...
#include <hash.h>
#include <data.h>
#include <euca_string.h>
#include "vbr.h"
#include "walrus.h"
#include "blobstore.h"
#include "diskutil.h"
//#include "iscsi.h"
#include "http.h"
#include "ebs_utils.h"
#include <ipc.h>
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
#define ART_SIG_MAX 262144 //!< must be big enough for a digest and then some
#define FIND_BLOB_TIMEOUT_USEC 50000LL //!< @TODO: use 100 or less to induce rare timeouts
#define DELETE_BLOB_TIMEOUT_USEC 50000LL
#define FIND 0
#define CREATE 1
#define ARTIFACT_RETRY_SLEEP_USEC 500000LL
#ifdef _UNIT_TEST
#define BS_SIZE 20000000000 / 512
#define KEY1 "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCVWU+h3gDF4sGjUB7t...\n"
#define KEY2 "ssh-rsa BBBBB3NzaC1yc2EAAAADAQABAAABAQCVWU+h3gDF4sGjUB7t...\n"
#define EKI1 "eki-1ABC123"
#define ERI1 "eri-1BCD234"
#define EMI1 "emi-1CDE345"
#define EMI2 "emi-2DEF456"
#define SERIAL_ITERATIONS 3
#define COMPETITIVE_PARTICIPANTS 3
#define COMPETITIVE_ITERATIONS 3
#define TOTAL_VMS 1 + SERIAL_ITERATIONS + COMPETITIVE_ITERATIONS * COMPETITIVE_PARTICIPANTS
#define VBR_SIZE ( 2LL * MEGABYTE )
#define EKI_SIZE ( 1024LL )
#endif /* _UNIT_TEST */
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
#ifdef _UNIT_TEST
const char *euca_this_component_name = "sc"; //!< Eucalyptus Component Name
const char *euca_client_component_name = "nc"; //!< The client component name
#endif /*_UNIT_TEST */
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
static __thread char current_instanceId[512] = ""; //!< instance ID that is being serviced, for logging only
static sem *hostconfig_sem;
#ifdef _UNIT_TEST
static blobstore *cache_bs = NULL;
static blobstore *work_bs = NULL;
static int next_instances_slot = 0;
static int provisioned_instances = 0;
static pthread_mutex_t competitors_mutex = PTHREAD_MUTEX_INITIALIZER; //!< process-global mutex
static virtualMachine vm_slots[TOTAL_VMS] = { {0} };
static char vm_ids[TOTAL_VMS][PATH_MAX] = { {0} };
static boolean do_fork = 0;
#endif /* _UNIT_TEST */
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
static int prep_location(virtualBootRecord * vbr, ncMetadata * pMeta, const char *typeName);
static int parse_rec(virtualBootRecord * vbr, virtualMachine * vm, ncMetadata * pMeta);
static void update_vbr_with_backing_info(artifact * a);
//! @{
//! @name Creator Functions
//! The following *_creator funcitons produce an artifact in blobstore, either from scratch (such as Walrus
//! download or a new partition) or by converting, combining, and augmenting existing artifacts.
//!
//! When invoked, creators can assume that any input blobs and the output blob are open (and thus locked for
//! their exclusive use).
//!
//! Creators return OK or an error code: either generic one (ERROR) or a code specific to a failed blobstore
//! operation, which can be obtained using blobstore_get_error().
static int url_creator(artifact * a);
static int walrus_creator(artifact * a);
static int partition_creator(artifact * a);
static void set_disk_dev(virtualBootRecord * vbr);
static int disk_creator(artifact * a);
#ifndef _NO_EBS
static int iqn_creator(artifact * a);
#endif // ! _NO_EBS
static int copy_creator(artifact * a);
//! @}
static void art_print_tree(const char *prefix, artifact * a);
static int art_gen_id(char *buf, unsigned int buf_size, const char *first, const char *sig);
static void convert_id(const char *src, char *dst, unsigned int size);
static char *url_get_digest(const char *url);
static artifact *art_alloc_vbr(virtualBootRecord * vbr, boolean do_make_work_copy, boolean is_migration_dest, boolean must_be_file, const char *sshkey);
static artifact *art_alloc_disk(virtualBootRecord * vbr, artifact * prereqs[], int num_prereqs, artifact * parts[], int num_parts,
artifact * emi_disk, boolean do_make_bootable, boolean do_make_work_copy, boolean is_migration_dest);
static int find_or_create_blob(int flags, blobstore * bs, const char *id, long long size_bytes, const char *sig, blockblob ** bbp);
static int find_or_create_artifact(int do_create, artifact * a, blobstore * work_bs, blobstore * cache_bs, const char *work_prefix, blockblob ** bbp);
#ifdef _UNIT_TEST
static blobstore *create_teststore(int size_blocks, const char *base, const char *name, blobstore_format_t format, blobstore_revocation_t revocation,
blobstore_snapshot_t snapshot);
static void add_vbr(virtualMachine * vm, long long size, ncResourceFormatType format, char *formatName, const char *id, ncResourceType type,
ncResourceLocationType locationType, int diskNumber, int partitionNumber, libvirtBusType guestDeviceBus, char *preparedResourceLocation);
static int provision_vm(const char *id, const char *sshkey, const char *eki, const char *eri, const char *emi, blobstore * cache_bs,
blobstore * work_bs, boolean do_make_work_copy);
static int cleanup_vms(void);
static char *gen_id(char *id, unsigned int id_len, const char *prefix);
static void *competitor_function(void *ptr);
static int check_blob(blobstore * bs, const char *keyword, int expect);
static void dummy_err_fn(const char *msg);
#endif /* _UNIT_TEST */
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
//! A safe macro to free an artifact. Forces the pointer to NULL afterwards
#define ART_FREE(_a) \
{ \
art_free((_a)); \
(_a) = NULL; \
}
#ifdef _UNIT_TEST
#define GEN_ID() gen_id(id, sizeof(id), "12345678")
#endif /* _UNIT_TEST */
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
//!
//! Initializes the global host config
//!
//! @param[in] hostIqn
//! @param[in] hostIp
//! @param[in] ws_sec_policy_file
//! @param[in] use_ws_sec
//!
//! @return EUCA_OK
//!
//! @pre
//!
//! @note
//!
int vbr_init_hostconfig(char *hostIqn, char *hostIp, char *ws_sec_policy_file, int use_ws_sec)
{
LOGDEBUG("Initializing host config for VBR. Setting IP, IQN, and security policy\n");
euca_strncpy(localhost_config.iqn, hostIqn, CHAR_BUFFER_SIZE);
euca_strncpy(localhost_config.ip, hostIp, HOSTNAME_SIZE);
euca_strncpy(localhost_config.ws_sec_policy_file, ws_sec_policy_file, EUCA_MAX_PATH);
localhost_config.use_ws_sec = use_ws_sec;
LOGDEBUG("VBR host config set to ip: %s iqn: %s, use_sec = %d, policy file = %s\n", localhost_config.ip, localhost_config.iqn, localhost_config.use_ws_sec,
localhost_config.ws_sec_policy_file);
hostconfig_sem = sem_alloc(1, IPC_MUTEX_SEMAPHORE);
return EUCA_OK;
}
//!
//! Semaphore protected read of the sc_url from the config
//! Destination buffer must be at least 512 in size
//!
//! @param[in] dest
//!
//! @return EUCA_OK on success or EUCA_ERROR on failure
//!
//! @pre
//!
//! @note
//!
int get_localhost_sc_url(char *dest)
{
int ret = 0;
sem_p(hostconfig_sem);
{
if (strlen(localhost_config.sc_url) == 0) {
LOGERROR("No sc url found in localhost_config.\n");
ret = EUCA_ERROR;
} else {
if (!euca_strncpy(dest, localhost_config.sc_url, 512)) {
LOGERROR("Failed up copy VBR hostconfig SC URL %s to destination buffer\n", localhost_config.sc_url);
ret = EUCA_ERROR;
} else {
ret = EUCA_OK;
}
}
}
sem_v(hostconfig_sem);
return ret;
}
//!
//! Semaphore protected hostconfig update.
//!
//! @param[in] new_sc_url
//!
//! @return
//!
//! @pre
//!
//! @note
//!
int vbr_update_hostconfig_scurl(char *new_sc_url)
{
sem_p(hostconfig_sem);
if (!euca_strncpy(localhost_config.sc_url, new_sc_url, 512)) {
LOGERROR("Failed up update VBR hostconfig SC URL to %s from %s\n", new_sc_url, localhost_config.sc_url);
sem_v(hostconfig_sem);
return EUCA_ERROR;
} else {
LOGTRACE("Updated sc url in VBR hostconfig to %s\n", localhost_config.sc_url);
sem_v(hostconfig_sem);
return EUCA_OK;
}
}
//!
//! Picks a service URI and prepends it to resourceLocation in VBR
//!
//! @param[in] vbr
//! @param[in] pMeta a pointer to the node controller (NC) metadata structure
//! @param[in] typeName
//!
//! @return
//!
//! @pre
//!
//! @note
//!
static int prep_location(virtualBootRecord * vbr, ncMetadata * pMeta, const char *typeName)
{
int i;
for (i = 0; i < pMeta->servicesLen; i++) {
serviceInfoType *service = &(pMeta->services[i]);
if (strncmp(service->type, typeName, strlen(typeName) - 3) == 0 && service->urisLen > 0) {
if (strcmp(typeName, "storage")) {
//Anything other than storage/ebs
char *l = vbr->resourceLocation + (strlen(typeName) + 3); // +3 for "://", so 'l' points past, e.g., "walrus:"
snprintf(vbr->preparedResourceLocation, sizeof(vbr->preparedResourceLocation), "%s/%s", service->uris[0], l); //! @TODO for now we just pick the first one
snprintf(vbr->resourceLocation, sizeof(vbr->resourceLocation), vbr->preparedResourceLocation); //! @TODO trying this out
} else {
//For storage, just copy the url for the SC into the preparedResourceLocation slot
snprintf(vbr->preparedResourceLocation, sizeof(vbr->preparedResourceLocation), "%s", service->uris[0]); //! @TODO for now we just pick the first one
}
return EUCA_OK;
}
}
LOGERROR("failed to find service '%s' in eucalyptusMessage\n", typeName);
return EUCA_ERROR;
}
//!
//! Parse spec_str as a VBR record and add it to vm_type->virtualBootRecord[virtualBootRecordLen]
//!
//! @param[in] spec_str
//! @param[in] vm_type
//!
//! @return
//!
//! @pre
//!
//! @note
//!
int vbr_add_ascii(const char *spec_str, virtualMachine * vm_type)
{
if (vm_type->virtualBootRecordLen == EUCA_MAX_VBRS) {
LOGERROR("too many entries in VBR already\n");
return 1;
}
virtualBootRecord *vbr = &(vm_type->virtualBootRecord[vm_type->virtualBootRecordLen++]);
char *spec_copy = strdup(spec_str);
char *type_spec = strtok(spec_copy, ":");
char *id_spec = strtok(NULL, ":");
char *size_spec = strtok(NULL, ":");
char *format_spec = strtok(NULL, ":");
char *dev_spec = strtok(NULL, ":");
char *loc_spec = strtok(NULL, ":");
if (type_spec == NULL) {
LOGERROR("invalid 'type' specification in VBR '%s'\n", spec_str);
goto out_error;
}
euca_strncpy(vbr->typeName, type_spec, sizeof(vbr->typeName));
if (id_spec == NULL) {
LOGERROR("invalid 'id' specification in VBR '%s'\n", spec_str);
goto out_error;
}
euca_strncpy(vbr->id, id_spec, sizeof(vbr->id));
if (size_spec == NULL) {
LOGERROR("invalid 'size' specification in VBR '%s'\n", spec_str);
goto out_error;
}
vbr->sizeBytes = atoll(size_spec);
if (format_spec == NULL) {
LOGERROR("invalid 'format' specification in VBR '%s'\n", spec_str);
goto out_error;
}
euca_strncpy(vbr->formatName, format_spec, sizeof(vbr->formatName));
if (dev_spec == NULL) {
LOGERROR("invalid 'guestDeviceName' specification in VBR '%s'\n", spec_str);
goto out_error;
}
euca_strncpy(vbr->guestDeviceName, dev_spec, sizeof(vbr->guestDeviceName));
if (loc_spec == NULL) {
LOGERROR("invalid 'resourceLocation' specification in VBR '%s'\n", spec_str);
goto out_error;
}
euca_strncpy(vbr->resourceLocation, spec_str + (loc_spec - spec_copy), sizeof(vbr->resourceLocation));
EUCA_FREE(spec_copy);
return 0;
out_error:
vm_type->virtualBootRecordLen--;
EUCA_FREE(spec_copy);
return 1;
}
//!
//! Parses the VBR as supplied by a client or user, checks values, and fills out almost the rest of the struct with typed values
//!
//! @param[in] vbr pointer to a VBR record to parse and verify
//! @param[in] vm OPTIONAL parameter for setting image/kernel/ramdik pointers in the virtualMachine struct
//! @param[in] pMeta a pointer to the node controller (NC) metadata structure
//!
//! @return
//!
//! @pre
//!
//! @note
//!
static int parse_rec(virtualBootRecord * vbr, virtualMachine * vm, ncMetadata * pMeta)
{
// check the type (the only mandatory field)
if (strstr(vbr->typeName, "machine") == vbr->typeName) {
vbr->type = NC_RESOURCE_IMAGE;
if (vm)
vm->root = vbr;
} else if (strstr(vbr->typeName, "kernel") == vbr->typeName) {
vbr->type = NC_RESOURCE_KERNEL;
if (vm)
vm->kernel = vbr;
} else if (strstr(vbr->typeName, "ramdisk") == vbr->typeName) {
vbr->type = NC_RESOURCE_RAMDISK;
if (vm)
vm->ramdisk = vbr;
} else if (strstr(vbr->typeName, "ephemeral") == vbr->typeName) {
vbr->type = NC_RESOURCE_EPHEMERAL;
if (strstr(vbr->typeName, "ephemeral0") == vbr->typeName) {
if (vm) {
vm->ephemeral0 = vbr;
}
}
} else if (strstr(vbr->typeName, "swap") == vbr->typeName) {
vbr->type = NC_RESOURCE_SWAP;
if (vm)
vm->swap = vbr;
} else if (strstr(vbr->typeName, "ebs") == vbr->typeName) {
vbr->type = NC_RESOURCE_EBS;
} else if (strstr(vbr->typeName, "boot") == vbr->typeName) {
vbr->type = NC_RESOURCE_BOOT;
if (vm)
vm->boot = vbr;
} else {
LOGERROR("failed to parse resource type '%s'\n", vbr->typeName);
return EUCA_ERROR;
}
// identify the type of resource location from location string
int error = EUCA_OK;
if (strcasestr(vbr->resourceLocation, "http://") == vbr->resourceLocation || strcasestr(vbr->resourceLocation, "https://") == vbr->resourceLocation) {
if (strcasestr(vbr->resourceLocation, "/services/Walrus/")) {
vbr->locationType = NC_LOCATION_WALRUS;
} else {
vbr->locationType = NC_LOCATION_URL;
}
euca_strncpy(vbr->preparedResourceLocation, vbr->resourceLocation, sizeof(vbr->preparedResourceLocation));
} else if (strcasestr(vbr->resourceLocation, "walrus://") == vbr->resourceLocation) {
vbr->locationType = NC_LOCATION_WALRUS;
if (pMeta)
error = prep_location(vbr, pMeta, "walrus");
} else if (strcasestr(vbr->resourceLocation, "cloud://") == vbr->resourceLocation) {
vbr->locationType = NC_LOCATION_CLC;
if (pMeta)
error = prep_location(vbr, pMeta, "cloud");
} else if (strcasestr(vbr->resourceLocation, "sc://") == vbr->resourceLocation || strcasestr(vbr->resourceLocation, "storage://") == vbr->resourceLocation) { //! @TODO is it 'sc' or 'storage'?
vbr->locationType = NC_LOCATION_SC;
if (pMeta)
error = prep_location(vbr, pMeta, "storage");
} else if (strcasestr(vbr->resourceLocation, "none") == vbr->resourceLocation) {
if (vbr->type != NC_RESOURCE_EPHEMERAL && vbr->type != NC_RESOURCE_SWAP && vbr->type != NC_RESOURCE_BOOT) {
LOGERROR("resourceLocation not specified for non-ephemeral resource '%s'\n", vbr->resourceLocation);
return EUCA_ERROR;
}
vbr->locationType = NC_LOCATION_NONE;
} else {
LOGERROR("failed to parse resource location '%s'\n", vbr->resourceLocation);
return EUCA_ERROR;
}
if (error != EUCA_OK) {
LOGERROR("URL for resourceLocation '%s' is not in the message\n", vbr->resourceLocation);
return EUCA_ERROR;
}
// device can be 'none' only for kernel and ramdisk types
if (!strcmp(vbr->guestDeviceName, "none")) {
if (vbr->type != NC_RESOURCE_KERNEL && vbr->type != NC_RESOURCE_RAMDISK) {
LOGERROR("guestDeviceName not specified for resource '%s'\n", vbr->resourceLocation);
return EUCA_ERROR;
}
} else { // should be a valid device
// trim off "/dev/" prefix, if present, and verify the rest
if (strstr(vbr->guestDeviceName, "/dev/") == vbr->guestDeviceName) {
LOGWARN("trimming off invalid prefix '/dev/' from guestDeviceName '%s'\n", vbr->guestDeviceName);
char buf[10];
euca_strncpy(buf, vbr->guestDeviceName + 5, sizeof(buf));
euca_strncpy(vbr->guestDeviceName, buf, sizeof(vbr->guestDeviceName));
}
if (strlen(vbr->guestDeviceName) < 3 || (vbr->guestDeviceName[0] == 'x' && strlen(vbr->guestDeviceName) < 4)) {
LOGERROR("invalid guestDeviceName '%s'\n", vbr->guestDeviceName);
return EUCA_ERROR;
}
{
char t = vbr->guestDeviceName[0]; // type
switch (t) {
case 'h':
vbr->guestDeviceType = DEV_TYPE_DISK;
vbr->guestDeviceBus = BUS_TYPE_IDE;
break;
case 's':
vbr->guestDeviceType = DEV_TYPE_DISK;
vbr->guestDeviceBus = BUS_TYPE_SCSI;
break;
case 'f':
vbr->guestDeviceType = DEV_TYPE_FLOPPY;
vbr->guestDeviceBus = BUS_TYPE_IDE;
break;
case 'v':
vbr->guestDeviceType = DEV_TYPE_DISK;
vbr->guestDeviceBus = BUS_TYPE_VIRTIO;
break;
case 'x':
vbr->guestDeviceType = DEV_TYPE_DISK;
vbr->guestDeviceBus = BUS_TYPE_XEN;
break;
default:
LOGERROR("failed to parse disk type guestDeviceName '%s'\n", vbr->guestDeviceName);
return EUCA_ERROR;
}
size_t letters_len = 3; // e.g. "sda"
if (t == 'x')
letters_len = 4; // e.g., "xvda"
if (t == 'f')
letters_len = 2; // e.g., "fd0"
char d = vbr->guestDeviceName[letters_len - 2]; // when 3+, the 'd'
char n = vbr->guestDeviceName[letters_len - 1]; // when 3+, the disk number
if (strlen(vbr->guestDeviceName) > letters_len) {
long long int p = 0; // partition or floppy drive number
errno = 0;
p = strtoll(vbr->guestDeviceName + letters_len, NULL, 10);
if (errno != 0) {
LOGERROR("failed to parse partition number in guestDeviceName '%s'\n", vbr->guestDeviceName);
return EUCA_ERROR;
}
if (p < 0 || p > EUCA_MAX_PARTITIONS) {
LOGERROR("unexpected partition or disk number '%lld' in guestDeviceName '%s'\n", p, vbr->guestDeviceName);
return EUCA_ERROR;
}
if (t == 'f') {
vbr->diskNumber = p;
} else {
if (p < 1) {
LOGERROR("unexpected partition number '%lld' in guestDeviceName '%s'\n", p, vbr->guestDeviceName);
return EUCA_ERROR;
}
vbr->partitionNumber = p;
}
} else {
vbr->partitionNumber = 0;
}
if (vbr->guestDeviceType != DEV_TYPE_FLOPPY) {
if (d != 'd') {
LOGERROR("failed to parse disk type guestDeviceName '%s'\n", vbr->guestDeviceName);
return EUCA_ERROR;
}
assert(EUCA_MAX_DISKS >= 'z' - 'a');
if (!(n >= 'a' && n <= 'z')) {
LOGERROR("failed to parse disk type guestDeviceName '%s'\n", vbr->guestDeviceName);
return EUCA_ERROR;
}
vbr->diskNumber = n - 'a';
}
}
}
// parse ID
if (strlen(vbr->id) < 4) {
LOGERROR("failed to parse VBR resource ID '%s' (use 'none' when no ID)\n", vbr->id);
return EUCA_ERROR;
}
// parse disk formatting instructions (none = do not format)
if (strstr(vbr->formatName, "none") == vbr->formatName) {
vbr->format = NC_FORMAT_NONE;
} else if (strstr(vbr->formatName, "ext2") == vbr->formatName) {
vbr->format = NC_FORMAT_EXT2;
} else if (strstr(vbr->formatName, "ext3") == vbr->formatName) {
vbr->format = NC_FORMAT_EXT3;
} else if (strstr(vbr->formatName, "ntfs") == vbr->formatName) {
vbr->format = NC_FORMAT_NTFS;
} else if (strstr(vbr->formatName, "swap") == vbr->formatName) {
vbr->format = NC_FORMAT_SWAP;
} else {
LOGERROR("failed to parse resource format '%s'\n", vbr->formatName);
return EUCA_ERROR;
}
if (vbr->type == NC_RESOURCE_EPHEMERAL || vbr->type == NC_RESOURCE_SWAP || vbr->type == NC_RESOURCE_BOOT) { //! @TODO should we allow ephemeral/swap that reside remotely?
if (vbr->sizeBytes < 1) {
LOGERROR("invalid size '%lld' for ephemeral resource '%s'\n", vbr->sizeBytes, vbr->resourceLocation);
return EUCA_ERROR;
}
} else {
// if (vbr->sizeBytes!=1 || vbr->format!=NC_FORMAT_NONE) { //! @TODO check for sizeBytes!=-1
if (vbr->format != NC_FORMAT_NONE) {
LOGERROR("invalid format '%s' for non-ephemeral resource '%s'\n", vbr->formatName, vbr->resourceLocation);
return EUCA_ERROR;
}
}
return EUCA_OK;
}
//!
//! Parses and verifies all VBR entries in the virtual machine definition
//!
//! @param[in] vm pointer to vm definition containing VBR records
//! @param[in] pMeta a pointer to the node controller (NC) metadata structure
//!
//! @return
//!
//! @pre
//!
//! @note
//!
int vbr_parse(virtualMachine * vm, ncMetadata * pMeta)
{
virtualBootRecord *partitions[BUS_TYPES_TOTAL][EUCA_MAX_DISKS][EUCA_MAX_PARTITIONS]; // for validating partitions
bzero(partitions, sizeof(partitions)); //! @fixme: chuck - this is not zeroing out the hole structure!!!
for (int i = 0; i < EUCA_MAX_VBRS && i < vm->virtualBootRecordLen; i++) {
virtualBootRecord *vbr = &(vm->virtualBootRecord[i]);
if (strlen(vbr->typeName) == 0) { // this must be the combined disk's VBR
return EUCA_OK;
}
if (parse_rec(vbr, vm, pMeta) != EUCA_OK)
return EUCA_ERROR;
if (vbr->type != NC_RESOURCE_KERNEL && vbr->type != NC_RESOURCE_RAMDISK)
partitions[vbr->guestDeviceBus][vbr->diskNumber][vbr->partitionNumber] = vbr;
if (vm->root == NULL) { // we have not identified the EMI yet
if (vbr->type == NC_RESOURCE_IMAGE || (vbr->type == NC_RESOURCE_EBS && vbr->diskNumber == 0 && vbr->partitionNumber == 0)) {
vm->root = vbr;
}
} else {
if (vm->root != vbr && vbr->type == NC_RESOURCE_IMAGE) {
LOGERROR("more than one EMI specified in the boot record\n");
return EUCA_ERROR;
}
}
}
// ensure that partitions are contiguous and that partitions and disks are not mixed
for (int i = 0; i < BUS_TYPES_TOTAL; i++) { // each bus type is treated separatedly
for (int j = 0; j < EUCA_MAX_DISKS; j++) {
int has_partitions = 0;
for (int k = EUCA_MAX_PARTITIONS - 1; k >= 0; k--) { // count down
if (partitions[i][j][k]) {
if (k == 0 && has_partitions) {
LOGERROR("specifying both disk and a partition on the disk is not allowed\n");
return EUCA_ERROR;
}
has_partitions = 1;
} else {
if (k != 0 && has_partitions) {
LOGERROR("gaps in partition table are not allowed\n");
return EUCA_ERROR;
}
}
if (vm->root == NULL) { // root partition or disk have not been found yet (no NC_RESOURCE_IMAGE)
virtualBootRecord *vbr;
if (has_partitions)
vbr = partitions[i][j][1];
else
vbr = partitions[i][j][0];
if (vbr && (vbr->type == NC_RESOURCE_EBS))
vm->root = vbr;
}
}
}
}
if (vm->root == NULL) {
LOGERROR("no root partition or disk have been found\n");
return EUCA_ERROR;
}
return EUCA_OK;
}
//!
//! Constructs VBRs for {image|kernel|ramdisk}x{Id|URL} entries (DEPRECATED)
//!
//! @param[in] instanceId the instance identifier string (i-XXXXXXXX)
//! @param[in] params pointer to the virtual machine parameters
//! @param[in] imageId OPTIONAL
//! @param[in] imageURL OPTIONALL
//! @param[in] kernelId OPTIONAL
//! @param[in] kernelURL OPTIONAL
//! @param[in] ramdiskId OPTIONAL
//! @param[in] ramdiskURL OPTIONAL
//!
//! @return
//!
//! @pre
//!
//! @note
//!
int vbr_legacy(const char *instanceId, virtualMachine * params, char *imageId, char *imageURL, char *kernelId, char *kernelURL, char *ramdiskId, char *ramdiskURL)
{
int i;
int found_image = 0;
int found_kernel = 0;
int found_ramdisk = 0;
for (i = 0; i < EUCA_MAX_VBRS && i < params->virtualBootRecordLen; i++) {
virtualBootRecord *vbr = &(params->virtualBootRecord[i]);
if (strlen(vbr->resourceLocation) > 0) {
LOGDEBUG("[%s] VBR[%d] type=%s id=%s dev=%s size=%lld format=%s %s\n",
instanceId, i, vbr->typeName, vbr->id, vbr->guestDeviceName, vbr->sizeBytes, vbr->formatName, vbr->resourceLocation);
if (!strcmp(vbr->typeName, "machine"))
found_image = 1;
if (!strcmp(vbr->typeName, "kernel"))
found_kernel = 1;
if (!strcmp(vbr->typeName, "ramdisk"))
found_ramdisk = 1;
} else {
break;
}
}
// legacy support for image{Id|URL}
if (imageId && imageURL) {
if (found_image) {
LOGWARN("[%s] IGNORING image %s passed outside the virtual boot record\n", instanceId, imageId);
} else {
LOGWARN("[%s] LEGACY pre-VBR image id=%s URL=%s\n", instanceId, imageId, imageURL);
if (i >= EUCA_MAX_VBRS - 2) {
LOGERROR("[%s] out of room in the Virtual Boot Record for legacy image %s\n", instanceId, imageId);
return EUCA_ERROR;
}
{ // create root partition VBR
virtualBootRecord *vbr = &(params->virtualBootRecord[i++]);
euca_strncpy(vbr->resourceLocation, imageURL, sizeof(vbr->resourceLocation));
euca_strncpy(vbr->guestDeviceName, "sda1", sizeof(vbr->guestDeviceName));
euca_strncpy(vbr->id, imageId, sizeof(vbr->id));
euca_strncpy(vbr->typeName, "machine", sizeof(vbr->typeName));
vbr->sizeBytes = -1;
euca_strncpy(vbr->formatName, "none", sizeof(vbr->formatName));
params->virtualBootRecordLen++;
}
{ // create ephemeral partition VBR
virtualBootRecord *vbr = &(params->virtualBootRecord[i++]);
euca_strncpy(vbr->resourceLocation, "none", sizeof(vbr->resourceLocation));
euca_strncpy(vbr->guestDeviceName, "sda2", sizeof(vbr->guestDeviceName));
euca_strncpy(vbr->id, "none", sizeof(vbr->id));
euca_strncpy(vbr->typeName, "ephemeral0", sizeof(vbr->typeName));
vbr->sizeBytes = 536870912; // we cannot compute it here, so pick something
euca_strncpy(vbr->formatName, "ext2", sizeof(vbr->formatName));
params->virtualBootRecordLen++;
}
{ // create swap partition VBR
virtualBootRecord *vbr = &(params->virtualBootRecord[i++]);
euca_strncpy(vbr->resourceLocation, "none", sizeof(vbr->resourceLocation));
euca_strncpy(vbr->guestDeviceName, "sda3", sizeof(vbr->guestDeviceName));
euca_strncpy(vbr->id, "none", sizeof(vbr->id));
euca_strncpy(vbr->typeName, "swap", sizeof(vbr->typeName));
vbr->sizeBytes = 536870912;
euca_strncpy(vbr->formatName, "swap", sizeof(vbr->formatName));
params->virtualBootRecordLen++;
}
}
}
// legacy support for kernel{Id|URL}
if (kernelId && kernelURL) {
if (found_kernel) {
LOGINFO("[%s] IGNORING kernel %s passed outside the virtual boot record\n", instanceId, kernelId);
} else {
LOGINFO("[%s] LEGACY pre-VBR kernel id=%s URL=%s\n", instanceId, kernelId, kernelURL);
if (i == EUCA_MAX_VBRS) {
LOGERROR("[%s] out of room in the Virtual Boot Record for legacy kernel %s\n", instanceId, kernelId);
return EUCA_ERROR;
}
virtualBootRecord *vbr = &(params->virtualBootRecord[i++]);
euca_strncpy(vbr->resourceLocation, kernelURL, sizeof(vbr->resourceLocation));
euca_strncpy(vbr->guestDeviceName, "none", sizeof(vbr->guestDeviceName));
euca_strncpy(vbr->id, kernelId, sizeof(vbr->id));
euca_strncpy(vbr->typeName, "kernel", sizeof(vbr->typeName));
vbr->sizeBytes = -1;
euca_strncpy(vbr->formatName, "none", sizeof(vbr->formatName));
params->virtualBootRecordLen++;
}
}
// legacy support for ramdisk{Id|URL}
if (ramdiskId && ramdiskURL) {
if (found_ramdisk) {
LOGINFO("[%s] IGNORING ramdisk %s passed outside the virtual boot record\n", instanceId, ramdiskId);
} else {
LOGINFO("[%s] LEGACY pre-VBR ramdisk id=%s URL=%s\n", instanceId, ramdiskId, ramdiskURL);
if (i == EUCA_MAX_VBRS) {
LOGERROR("[%s] out of room in the Virtual Boot Record for legacy ramdisk %s\n", instanceId, ramdiskId);
return EUCA_ERROR;
}
virtualBootRecord *vbr = &(params->virtualBootRecord[i++]);
euca_strncpy(vbr->resourceLocation, ramdiskURL, sizeof(vbr->resourceLocation));
euca_strncpy(vbr->guestDeviceName, "none", sizeof(vbr->guestDeviceName));
euca_strncpy(vbr->id, ramdiskId, sizeof(vbr->id));
euca_strncpy(vbr->typeName, "ramdisk", sizeof(vbr->typeName));
vbr->sizeBytes = -1;
euca_strncpy(vbr->formatName, "none", sizeof(vbr->formatName));
params->virtualBootRecordLen++;
}
}
return EUCA_OK;
}
//!
//!
//!
//! @param[in] a
//!
//! @pre
//!
//! @note
//!
static void update_vbr_with_backing_info(artifact * a)
{
assert(a);
if (a->vbr == NULL)
return;
virtualBootRecord *vbr = a->vbr;
assert(a->bb);
if (!a->must_be_file && // not required to be a file
strlen(blockblob_get_dev(a->bb)) && // there is a block device
blockblob_get_file(a->bb) == NULL && // there is NO file access
(blobstore_snapshot_t) a->bb->store->snapshot_policy != BLOBSTORE_SNAPSHOT_NONE) { // without snapshots we can use files
euca_strncpy(vbr->backingPath, blockblob_get_dev(a->bb), sizeof(vbr->backingPath));
vbr->backingType = SOURCE_TYPE_BLOCK;
} else {
assert(blockblob_get_file(a->bb));
euca_strncpy(vbr->backingPath, blockblob_get_file(a->bb), sizeof(vbr->backingPath));
vbr->backingType = SOURCE_TYPE_FILE;
}
vbr->sizeBytes = a->bb->size_bytes;
// create a symlink so backing file/dev is predictable regadless
// of whether the backing is a loopback dev or a device mapper dev
// or a file: this is needed for migration to work
{
const char *path = vbr->backingPath;
char bbdirpath[PATH_MAX];
blockblob_get_dir(a->bb, bbdirpath, sizeof(bbdirpath)); // fill in blob's base directory
char *linkname = "[invalid]";
if (!strcmp(vbr->guestDeviceName, "none")) {
linkname = vbr->typeName;;
} else {
linkname = vbr->guestDeviceName;
}
char linkpath[PATH_MAX];
snprintf(linkpath, sizeof(linkpath), "%s/link-to-%s", bbdirpath, linkname);
if (symlink(path, linkpath) == 0) {
LOGDEBUG("[%s] symlinked %s to %s\n", a->instanceId, path, linkpath);
euca_strncpy(vbr->backingPath, linkpath, sizeof(vbr->backingPath));
}
}
}
//!
//!
//!
//! @param[in] a
//!
//! @return
//!
//! @pre
//!
//! @note
//!
static int url_creator(artifact * a)
{
assert(a->bb);
assert(a->vbr);
virtualBootRecord *vbr = a->vbr;
const char *dest_path = blockblob_get_file(a->bb);
assert(vbr->preparedResourceLocation);
if (a->do_not_download) {
LOGINFO("[%s] skipping download of %s\n", a->instanceId, vbr->preparedResourceLocation);
return EUCA_OK;
}
LOGINFO("[%s] downloading %s\n", a->instanceId, vbr->preparedResourceLocation);
if (http_get(vbr->preparedResourceLocation, dest_path) != EUCA_OK) {
LOGERROR("[%s] failed to download component %s\n", a->instanceId, vbr->preparedResourceLocation);
return EUCA_ERROR;
}
return EUCA_OK;
}
//!
//! Creates an artifact by downloading it from Walrus
//!
//! @param[in] a
//!
//! @return
//!
//! @pre
//!
//! @note
//!
static int walrus_creator(artifact * a)
{
assert(a->bb);
assert(a->vbr);
virtualBootRecord *vbr = a->vbr;
const char *dest_path = blockblob_get_file(a->bb);
assert(vbr->preparedResourceLocation);
if (a->do_not_download) {
LOGINFO("[%s] skipping download of %s\n", a->instanceId, vbr->preparedResourceLocation);
return EUCA_OK;