/
vbr.c
2247 lines (1984 loc) · 94.8 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.
************************************************************************/
#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 "misc.h" // logprintfl, ensure_...
#include "hash.h"
#include "data.h"
#include "vbr.h"
#include "walrus.h"
#include "blobstore.h"
#include "diskutil.h"
#include "iscsi.h"
#include "http.h"
#define VBR_SIZE_SCALING 1024 // TODO: remove this adjustment after CLC sends bytes instead of KBs
static int // returns OK or ERROR
prep_location ( // picks a service URI and prepends it to resourceLocation in VBR
virtualBootRecord * vbr,
ncMetadata * meta,
const char * typeName)
{
int i;
for (i=0; i<meta->servicesLen; i++) {
serviceInfoType * service = &(meta->services[i]);
if (strncmp(service->type, typeName, strlen(typeName)-3)==0 && service->urisLen>0) {
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
return OK;
}
}
logprintfl (EUCAERROR, "failed to find service '%s' in eucalyptusMessage\n", typeName);
return ERROR;
}
// parse spec_str as a VBR record and add it to
// vm_type->virtualBootRecord[virtualBootRecordLen]
// return 0 if OK, return 1 on error
int vbr_add_ascii (const char * spec_str, virtualMachine * vm_type)
{
if (vm_type->virtualBootRecordLen==EUCA_MAX_VBRS) {
logprintfl (EUCAERROR, "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) { logprintfl (EUCAERROR, "error: invalid 'type' specification in VBR '%s'\n", spec_str); goto out_error; }
safe_strncpy (vbr->typeName, type_spec, sizeof (vbr->typeName));
if (id_spec==NULL) { logprintfl (EUCAERROR, "error: invalid 'id' specification in VBR '%s'\n", spec_str); goto out_error; }
safe_strncpy (vbr->id, id_spec, sizeof (vbr->id));
if (size_spec==NULL) { logprintfl (EUCAERROR, "error: invalid 'size' specification in VBR '%s'\n", spec_str); goto out_error; }
vbr->size = atoi (size_spec);
if (format_spec==NULL) { logprintfl (EUCAERROR, "error: invalid 'format' specification in VBR '%s'\n", spec_str); goto out_error; }
safe_strncpy (vbr->formatName, format_spec, sizeof (vbr->formatName));
if (dev_spec==NULL) { logprintfl (EUCAERROR, "error: invalid 'guestDeviceName' specification in VBR '%s'\n", spec_str); goto out_error; }
safe_strncpy (vbr->guestDeviceName, dev_spec, sizeof (vbr->guestDeviceName));
if (loc_spec==NULL) { logprintfl (EUCAERROR, "error: invalid 'resourceLocation' specification in VBR '%s'\n", spec_str); goto out_error; }
safe_strncpy (vbr->resourceLocation, spec_str + (loc_spec - spec_copy), sizeof (vbr->resourceLocation));
free (spec_copy);
return 0;
out_error:
vm_type->virtualBootRecordLen--;
free (spec_copy);
return 1;
}
static int // returns OK or ERROR
parse_rec ( // parses the VBR as supplied by a client or user, checks values, and fills out almost the rest of the struct with typed values
virtualBootRecord * vbr, // a VBR record to parse and verify
virtualMachine * vm, // OPTIONAL parameter for setting image/kernel/ramdik pointers in the virtualMachine struct
ncMetadata * meta) // OPTIONAL parameter for translating, e.g., walrus:// URI into http:// URI
{
// 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) { // TODO: remove
if (vm) {
vm->ephemeral0 = vbr;
}
}
} else if (strstr (vbr->typeName, "swap") == vbr->typeName) { // TODO: remove
vbr->type = NC_RESOURCE_SWAP;
if (vm)
vm->swap = vbr;
} else if (strstr (vbr->typeName, "ebs") == vbr->typeName) {
vbr->type = NC_RESOURCE_EBS;
} else {
logprintfl (EUCAERROR, "Error: failed to parse resource type '%s'\n", vbr->typeName);
return ERROR;
}
// identify the type of resource location from location string
int error = 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;
}
safe_strncpy (vbr->preparedResourceLocation, vbr->resourceLocation, sizeof(vbr->preparedResourceLocation));
} else if (strcasestr (vbr->resourceLocation, "iqn://") == vbr->resourceLocation ||
strchr (vbr->resourceLocation, ',')) { // TODO: remove this transitionary iSCSI crutch?
vbr->locationType = NC_LOCATION_IQN;
} else if (strcasestr (vbr->resourceLocation, "aoe://") == vbr->resourceLocation ||
strcasestr (vbr->resourceLocation, "/dev/") == vbr->resourceLocation ) { // TODO: remove this transitionary AoE crutch
vbr->locationType = NC_LOCATION_AOE;
} else if (strcasestr (vbr->resourceLocation, "walrus://") == vbr->resourceLocation) {
vbr->locationType = NC_LOCATION_WALRUS;
if (meta)
error = prep_location (vbr, meta, "walrus");
} else if (strcasestr (vbr->resourceLocation, "cloud://") == vbr->resourceLocation) {
vbr->locationType = NC_LOCATION_CLC;
if (meta)
error = prep_location (vbr, meta, "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 (meta)
error = prep_location (vbr, meta, "sc");
} else if (strcasestr (vbr->resourceLocation, "none") == vbr->resourceLocation) {
if (vbr->type!=NC_RESOURCE_EPHEMERAL && vbr->type!=NC_RESOURCE_SWAP) {
logprintfl (EUCAERROR, "Error: resourceLocation not specified for non-ephemeral resource '%s'\n", vbr->resourceLocation);
return ERROR;
}
vbr->locationType = NC_LOCATION_NONE;
} else {
logprintfl (EUCAERROR, "Error: failed to parse resource location '%s'\n", vbr->resourceLocation);
return ERROR;
}
if (error!=OK) {
logprintfl (EUCAERROR, "Error: URL for resourceLocation '%s' is not in the message\n", vbr->resourceLocation);
return 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) {
logprintfl (EUCAERROR, "Error: guestDeviceName not specified for resource '%s'\n", vbr->resourceLocation);
return ERROR;
}
} else { // should be a valid device
// trim off "/dev/" prefix, if present, and verify the rest
if (strstr (vbr->guestDeviceName, "/dev/") == vbr->guestDeviceName) {
logprintfl (EUCAWARN, "Warning: trimming off invalid prefix '/dev/' from guestDeviceName '%s'\n", vbr->guestDeviceName);
char buf [10];
safe_strncpy (buf, vbr->guestDeviceName + 5, sizeof (buf));
strncpy (vbr->guestDeviceName, buf, sizeof (vbr->guestDeviceName));
}
if (strlen (vbr->guestDeviceName)<3 ||
(vbr->guestDeviceName [0] == 'x' && strlen(vbr->guestDeviceName) < 4)) {
logprintfl (EUCAERROR, "Error: invalid guestDeviceName '%s'\n", vbr->guestDeviceName);
return 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:
logprintfl (EUCAERROR, "Error: failed to parse disk type guestDeviceName '%s'\n", vbr->guestDeviceName);
return ERROR;
}
int 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) {
logprintfl (EUCAERROR, "Error: failed to parse partition number in guestDeviceName '%s'\n", vbr->guestDeviceName);
return ERROR;
}
if (p<0 || p>EUCA_MAX_PARTITIONS) {
logprintfl (EUCAERROR, "Error: unexpected partition or disk number '%d' in guestDeviceName '%s'\n", p, vbr->guestDeviceName);
return ERROR;
}
if (t=='f') {
vbr->diskNumber = p;
} else {
if (p<1) {
logprintfl (EUCAERROR, "Error: unexpected partition number '%d' in guestDeviceName '%s'\n", p, vbr->guestDeviceName);
return ERROR;
}
vbr->partitionNumber = p;
}
} else {
vbr->partitionNumber = 0;
}
if (vbr->guestDeviceType != DEV_TYPE_FLOPPY) {
if (d!='d') {
logprintfl (EUCAERROR, "Error: failed to parse disk type guestDeviceName '%s'\n", vbr->guestDeviceName);
return ERROR;
}
assert (EUCA_MAX_DISKS >= 'z'-'a');
if (!(n>='a' && n<='z')) {
logprintfl (EUCAERROR, "Error: failed to parse disk type guestDeviceName '%s'\n", vbr->guestDeviceName);
return ERROR;
}
vbr->diskNumber = n - 'a';
}
}
}
// parse ID
if (strlen (vbr->id)<4) {
logprintfl (EUCAERROR, "Error: failed to parse VBR resource ID '%s' (use 'none' when no ID)\n", vbr->id);
return 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 {
logprintfl (EUCAERROR, "Error: failed to parse resource format '%s'\n", vbr->formatName);
return ERROR;
}
if (vbr->type==NC_RESOURCE_EPHEMERAL || vbr->type==NC_RESOURCE_SWAP) { // TODO: should we allow ephemeral/swap that reside remotely?
if (vbr->size<1) {
logprintfl (EUCAERROR, "Error: invalid size '%d' for ephemeral resource '%s'\n", vbr->size, vbr->resourceLocation);
return ERROR;
}
} else {
// if (vbr->size!=1 || vbr->format!=NC_FORMAT_NONE) { // TODO: check for size!=-1
if (vbr->format!=NC_FORMAT_NONE) {
logprintfl (EUCAERROR, "Error: invalid size '%d' or format '%s' for non-ephemeral resource '%s'\n", vbr->size, vbr->formatName, vbr->resourceLocation);
return ERROR;
}
}
return OK;
}
int // returns OK or ERROR
vbr_parse ( // parses and verifies all VBR entries in the virtual machine definition
virtualMachine * vm, // vm definition containing VBR records
ncMetadata * meta) // OPTIONAL parameter for translating, e.g., walrus:// URI into http:// URI
{
virtualBootRecord * partitions [BUS_TYPES_TOTAL][EUCA_MAX_DISKS][EUCA_MAX_PARTITIONS]; // for validating partitions
bzero (partitions, sizeof (partitions));
for (int i=0, j=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 OK;
}
if (parse_rec (vbr, vm, meta) != OK)
return 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) {
vm->root=vbr;
}
} else {
if (vm->root!=vbr && vbr->type==NC_RESOURCE_IMAGE) {
logprintfl (EUCAERROR, "Error: more than one EMI specified in the boot record\n");
return 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) {
logprintfl (EUCAERROR, "Error: specifying both disk and a partition on the disk is not allowed\n");
return ERROR;
}
has_partitions = 1;
} else {
if (k!=0 && has_partitions) {
logprintfl (EUCAERROR, "Error: gaps in partition table are not allowed\n");
return 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) {
logprintfl (EUCAERROR, "Error: no root partition or disk have been found\n");
return ERROR;
}
return OK;
}
int // returns OK or ERROR
vbr_legacy ( // constructs VBRs for {image|kernel|ramdisk}x{Id|URL} entries (DEPRECATED)
const char * instanceId,
virtualMachine *params,
char *imageId, char *imageURL, // OPTIONAL
char *kernelId, char *kernelURL, // OPTIONAL
char *ramdiskId, char *ramdiskURL) // OPTIONAL
{
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) {
logprintfl (EUCAINFO, "[%s] VBR[%d] type=%s id=%s dev=%s size=%lld format=%s %s\n",
instanceId, i, vbr->typeName, vbr->id, vbr->guestDeviceName, vbr->size, 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) {
logprintfl (EUCAINFO, "[%s] IGNORING image %s passed outside the virtual boot record\n", instanceId, imageId);
} else {
logprintfl (EUCAINFO, "[%s] LEGACY pre-VBR image id=%s URL=%s\n", instanceId, imageId, imageURL);
if (i>=EUCA_MAX_VBRS-2) {
logprintfl (EUCAERROR, "[%s] error: out of room in the Virtual Boot Record for legacy image %s\n", instanceId, imageId);
return ERROR;
}
{ // create root partition VBR
virtualBootRecord * vbr = &(params->virtualBootRecord[i++]);
safe_strncpy (vbr->resourceLocation, imageURL, sizeof (vbr->resourceLocation));
strncpy (vbr->guestDeviceName, "sda1", sizeof (vbr->guestDeviceName));
safe_strncpy (vbr->id, imageId, sizeof (vbr->id));
strncpy (vbr->typeName, "machine", sizeof (vbr->typeName));
vbr->size = -1;
strncpy (vbr->formatName, "none", sizeof (vbr->formatName));
params->virtualBootRecordLen++;
}
{ // create ephemeral partition VBR
virtualBootRecord * vbr = &(params->virtualBootRecord[i++]);
strncpy (vbr->resourceLocation, "none", sizeof (vbr->resourceLocation));
strncpy (vbr->guestDeviceName, "sda2", sizeof (vbr->guestDeviceName));
strncpy (vbr->id, "none", sizeof (vbr->id));
strncpy (vbr->typeName, "ephemeral0", sizeof (vbr->typeName));
vbr->size = 524288; // we cannot compute it here, so pick something
strncpy (vbr->formatName, "ext2", sizeof (vbr->formatName));
params->virtualBootRecordLen++;
}
{ // create swap partition VBR
virtualBootRecord * vbr = &(params->virtualBootRecord[i++]);
strncpy (vbr->resourceLocation, "none", sizeof (vbr->resourceLocation));
strncpy (vbr->guestDeviceName, "sda3", sizeof (vbr->guestDeviceName));
strncpy (vbr->id, "none", sizeof (vbr->id));
strncpy (vbr->typeName, "swap", sizeof (vbr->typeName));
vbr->size = 524288;
strncpy (vbr->formatName, "swap", sizeof (vbr->formatName));
params->virtualBootRecordLen++;
}
}
}
// legacy support for kernel{Id|URL}
if (kernelId && kernelURL) {
if (found_kernel) {
logprintfl (EUCAINFO, "[%s] IGNORING kernel %s passed outside the virtual boot record\n", instanceId, kernelId);
} else {
logprintfl (EUCAINFO, "[%s] LEGACY pre-VBR kernel id=%s URL=%s\n", instanceId, kernelId, kernelURL);
if (i==EUCA_MAX_VBRS) {
logprintfl (EUCAERROR, "[%s] error: out of room in the Virtual Boot Record for legacy kernel %s\n", instanceId, kernelId);
return ERROR;
}
virtualBootRecord * vbr = &(params->virtualBootRecord[i++]);
strncpy (vbr->resourceLocation, kernelURL, sizeof (vbr->resourceLocation));
strncpy (vbr->guestDeviceName, "none", sizeof (vbr->guestDeviceName));
strncpy (vbr->id, kernelId, sizeof (vbr->id));
strncpy (vbr->typeName, "kernel", sizeof (vbr->typeName));
vbr->size = -1;
strncpy (vbr->formatName, "none", sizeof (vbr->formatName));
params->virtualBootRecordLen++;
}
}
// legacy support for ramdisk{Id|URL}
if (ramdiskId && ramdiskURL) {
if (found_ramdisk) {
logprintfl (EUCAINFO, "[%s] IGNORING ramdisk %s passed outside the virtual boot record\n", instanceId, ramdiskId);
} else {
logprintfl (EUCAINFO, "[%s] LEGACY pre-VBR ramdisk id=%s URL=%s\n", instanceId, ramdiskId, ramdiskURL);
if (i==EUCA_MAX_VBRS) {
logprintfl (EUCAERROR, "[%s] error: out of room in the Virtual Boot Record for legacy ramdisk %s\n", instanceId, ramdiskId);
return ERROR;
}
virtualBootRecord * vbr = &(params->virtualBootRecord[i++]);
strncpy (vbr->resourceLocation, ramdiskURL, sizeof (vbr->resourceLocation));
strncpy (vbr->guestDeviceName, "none", sizeof (vbr->guestDeviceName));
strncpy (vbr->id, ramdiskId, sizeof (vbr->id));
strncpy (vbr->typeName, "ramdisk", sizeof (vbr->typeName));
vbr->size = -1;
strncpy (vbr->formatName, "none", sizeof (vbr->formatName));
params->virtualBootRecordLen++;
}
}
return OK;
}
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 &&
strlen (blockblob_get_dev (a->bb)) &&
(blobstore_snapshot_t)a->bb->store->snapshot_policy != BLOBSTORE_SNAPSHOT_NONE) { // without snapshots we can use files
safe_strncpy (vbr->backingPath, blockblob_get_dev (a->bb), sizeof (vbr->backingPath));
vbr->backingType = SOURCE_TYPE_BLOCK;
} else {
assert (blockblob_get_file (a->bb));
safe_strncpy (vbr->backingPath, blockblob_get_file (a->bb), sizeof (vbr->backingPath));
vbr->backingType = SOURCE_TYPE_FILE;
}
vbr->size = a->bb->size_bytes;
}
// 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)
{
assert (a->bb);
assert (a->vbr);
virtualBootRecord * vbr = a->vbr;
const char * dest_path = blockblob_get_file (a->bb);
assert (vbr->preparedResourceLocation);
logprintfl (EUCAINFO, "[%s] downloading %s\n", a->instanceId, vbr->preparedResourceLocation);
if (http_get (vbr->preparedResourceLocation, dest_path) != OK) {
logprintfl (EUCAERROR, "[%s] error: failed to download component %s\n", a->instanceId, vbr->preparedResourceLocation);
return ERROR;
}
return OK;
}
static int walrus_creator (artifact * a) // creates an artifact by downloading it from Walrus
{
assert (a->bb);
assert (a->vbr);
virtualBootRecord * vbr = a->vbr;
const char * dest_path = blockblob_get_file (a->bb);
assert (vbr->preparedResourceLocation);
logprintfl (EUCAINFO, "[%s] downloading %s\n", a->instanceId, vbr->preparedResourceLocation);
if (walrus_image_by_manifest_url (vbr->preparedResourceLocation, dest_path, TRUE) != OK) {
logprintfl (EUCAERROR, "[%s] error: failed to download component %s\n", a->instanceId, vbr->preparedResourceLocation);
return ERROR;
}
return OK;
}
static int partition_creator (artifact * a) // creates a new partition from scratch
{
assert (a->bb);
assert (a->vbr);
virtualBootRecord * vbr = a->vbr;
const char * dest_dev = blockblob_get_dev (a->bb);
assert (dest_dev);
logprintfl (EUCAINFO, "[%s] creating partition of size %lld bytes and type %s in %s\n", a->instanceId, a->size_bytes, vbr->formatName, a->id);
int format = ERROR;
switch (vbr->format) {
case NC_FORMAT_NONE:
format = OK;
break;
case NC_FORMAT_EXT2: // TODO: distinguish ext2 and ext3!
case NC_FORMAT_EXT3:
format = diskutil_mkfs (dest_dev, a->size_bytes);
break;
case NC_FORMAT_SWAP:
format = diskutil_mkswap (dest_dev, a->size_bytes);
break;
default:
logprintfl (EUCAERROR, "[%s] error: format of type %d/%s is NOT IMPLEMENTED\n", a->instanceId, vbr->format, vbr->formatName);
}
if (format != OK) {
logprintfl (EUCAERROR, "[%s] failed to create partition in blob %s\n", a->instanceId, a->id);
return ERROR;
}
return OK;
}
// sets vbr->guestDeviceName based on other entries in the struct
// (guestDevice{Type|Bus}, {disk|partition}Number}
static void set_disk_dev (virtualBootRecord * vbr)
{
char type [3] = "\0\0\0";
if (vbr->guestDeviceType==DEV_TYPE_FLOPPY) {
type [0] = 'f';
} else { // a disk
switch (vbr->guestDeviceBus) {
case BUS_TYPE_IDE: type [0] = 'h'; break;
case BUS_TYPE_SCSI: type [0] = 's'; break;
case BUS_TYPE_VIRTIO: type [0] = 'v'; break;
case BUS_TYPE_XEN: type [0] = 'x'; type [1] = 'v'; break;
case BUS_TYPES_TOTAL:
default:
type [0] = '?'; // error
}
}
char disk;
if (vbr->guestDeviceType==DEV_TYPE_FLOPPY) {
assert (vbr->diskNumber >=0 && vbr->diskNumber <= 9);
disk = '0' + vbr->diskNumber;
} else { // a disk
assert (vbr->diskNumber >=0 && vbr->diskNumber <= 26);
disk = 'a' + vbr->diskNumber;
}
char part [3] = "\0";
if (vbr->partitionNumber) {
snprintf (part, sizeof(part), "%d", vbr->partitionNumber);
}
snprintf (vbr->guestDeviceName, sizeof (vbr->guestDeviceName), "%sd%c%s", type, disk, part);
}
static int disk_creator (artifact * a) // creates a 'raw' disk based on partitions
{
int ret = ERROR;
assert (a->bb);
const char * dest_dev = blockblob_get_dev (a->bb);
assert (dest_dev);
logprintfl (EUCAINFO, "[%s] constructing disk of size %lld bytes in %s (%s)\n", a->instanceId, a->size_bytes, a->id, blockblob_get_dev (a->bb));
blockmap_relation_t mbr_op = BLOBSTORE_SNAPSHOT;
blockmap_relation_t part_op = BLOBSTORE_MAP; // use map by default as it is faster
if ((blobstore_snapshot_t)a->bb->store->snapshot_policy == BLOBSTORE_SNAPSHOT_NONE) {
// but fall back to copy when snapshots are not possible or desired
mbr_op = BLOBSTORE_COPY;
part_op = BLOBSTORE_COPY;
}
blockmap map [EUCA_MAX_PARTITIONS] = { {mbr_op, BLOBSTORE_ZERO, {blob:NULL}, 0, 0, MBR_BLOCKS} }; // initially only MBR is in the map
// run through partitions, add their sizes, populate the map
virtualBootRecord * p1 = NULL;
virtualBootRecord * disk = a->vbr;
int map_entries = 1; // first map entry is for the MBR
int root_entry = -1; // we do not know the root entry
int root_part = -1; // we do not know the root partition
const char * kernel_path = NULL;
const char * ramdisk_path = NULL;
long long offset_bytes = 512 * MBR_BLOCKS; // first partition begins after MBR
assert (disk);
for (int i=0; i<MAX_ARTIFACT_DEPS && a->deps[i]; i++) {
artifact * dep = a->deps[i];
if (! dep->is_partition) {
if (dep->vbr && dep->vbr->type==NC_RESOURCE_KERNEL)
kernel_path = blockblob_get_file (dep->bb);
if (dep->vbr && dep->vbr->type==NC_RESOURCE_RAMDISK)
ramdisk_path = blockblob_get_file (dep->bb);
continue;
}
virtualBootRecord * p = dep->vbr;
assert (p);
if (p1==NULL)
p1 = p;
assert (dep->bb);
assert (dep->size_bytes>0);
map [map_entries].relation_type = part_op;
map [map_entries].source_type = BLOBSTORE_BLOCKBLOB;
map [map_entries].source.blob = dep->bb;
map [map_entries].first_block_src = 0;
map [map_entries].first_block_dst = (offset_bytes / 512);
map [map_entries].len_blocks = (dep->size_bytes / 512);
logprintfl (EUCADEBUG, "[%s] mapping partition %d from %s [%lld-%lld]\n",
a->instanceId,
map_entries,
blockblob_get_dev (a->deps[i]->bb),
map [map_entries].first_block_dst,
map [map_entries].first_block_dst + map [map_entries].len_blocks - 1);
offset_bytes+=dep->size_bytes;
if (p->type==NC_RESOURCE_IMAGE && root_entry==-1) {
root_entry = map_entries;
root_part = i;
}
map_entries++;
}
assert (p1);
// set fields in vbr that are needed for
// xml.c:gen_instance_xml() to generate correct disk entries
disk->guestDeviceType = p1->guestDeviceType;
disk->guestDeviceBus = p1->guestDeviceBus;
disk->diskNumber = p1->diskNumber;
set_disk_dev (disk);
// map the partitions to the disk
if (blockblob_clone (a->bb, map, map_entries)==-1) {
ret = blobstore_get_error();
logprintfl (EUCAERROR, "[%s] error: failed to clone partitions to created disk: %d %s\n", a->instanceId, ret, blobstore_get_last_msg());
goto cleanup;
}
// create MBR
logprintfl (EUCAINFO, "[%s] creating MBR\n", a->instanceId);
if (diskutil_mbr (blockblob_get_dev (a->bb), "msdos") == ERROR) { // issues `parted mklabel`
logprintfl (EUCAERROR, "[%s] error: failed to add MBR to disk: %d %s\n", a->instanceId, blobstore_get_error(), blobstore_get_last_msg());
goto cleanup;
}
// add partition information to MBR
for (int i=1; i<map_entries; i++) { // map [0] is for the MBR
logprintfl (EUCAINFO, "[%s] adding partition %d to partition table (%s)\n", a->instanceId, i, blockblob_get_dev (a->bb));
if (diskutil_part (blockblob_get_dev (a->bb), // issues `parted mkpart`
"primary", // TODO: make this work with more than 4 partitions
NULL, // do not create file system
map [i].first_block_dst, // first sector
map [i].first_block_dst + map [i].len_blocks - 1) == ERROR) {
logprintfl (EUCAERROR, "[%s] error: failed to add partition %d to disk: %d %s\n", a->instanceId, i, blobstore_get_error(), blobstore_get_last_msg());
goto cleanup;
}
}
// make disk bootable if necessary
if (a->do_make_bootable) {
boolean bootification_failed = 1;
logprintfl (EUCAINFO, "[%s] making disk bootable\n", a->instanceId);
if (root_entry<1 || root_part<0) {
logprintfl (EUCAERROR, "[%s] error: cannot make bootable a disk without an image\n", a->instanceId);
goto cleanup;
}
if (kernel_path==NULL) {
logprintfl (EUCAERROR, "[%s] error: no kernel found among the VBRs\n", a->instanceId);
goto cleanup;
}
if (ramdisk_path==NULL) {
logprintfl (EUCAERROR, "[%s] error: no ramdisk found among the VBRs\n", a->instanceId);
goto cleanup;
}
// `parted mkpart` causes children devices for each partition to be created
// (e.g., /dev/mapper/euca-diskX gets /dev/mapper/euca-diskXp1 or ...X1 and so on)
// we mount such a device here so as to copy files to the root partition
// (we cannot mount the dev of the partition's blob because it becomes
// 'busy' after the clone operation)
char * mapper_dev = NULL;
char dev_with_p [EUCA_MAX_PATH];
char dev_without_p [EUCA_MAX_PATH]; // on Ubuntu Precise, some dev names do not have 'p' in them
snprintf (dev_with_p, sizeof (dev_with_p), "%sp%d", blockblob_get_dev (a->bb), root_entry);
snprintf (dev_without_p, sizeof (dev_without_p), "%s%d", blockblob_get_dev (a->bb), root_entry);
if (check_path (dev_with_p) == 0) {
mapper_dev = dev_with_p;
} else if (check_path (dev_without_p) == 0) {
mapper_dev = dev_without_p;
} else {
logprintfl (EUCAERROR, "[%s] failed to stat partition device [%s]\n", a->instanceId, mapper_dev, strerror (errno));
goto cleanup;
}
logprintfl (EUCAINFO, "[%s] found partition device %s\n", a->instanceId, mapper_dev);
// point a loopback device at the partition device because grub-probe on Ubuntu Precise
// sometimes does not grok root partitions mounted from /dev/mapper/...
char loop_dev [EUCA_MAX_PATH];
if (diskutil_loop (mapper_dev, 0, loop_dev, sizeof (loop_dev)) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to attach '%s' on a loopback device\n", a->instanceId, mapper_dev);
goto cleanup;
}
assert (strncmp (loop_dev, "/dev/loop", 9) == 0);
// mount the root partition
char mnt_pt [EUCA_MAX_PATH] = "/tmp/euca-mount-XXXXXX";
if (safe_mkdtemp (mnt_pt)==NULL) {
logprintfl (EUCAINFO, "[%s] error: mkdtemp() failed: %s\n", a->instanceId, strerror (errno));
goto unloop;
}
if (diskutil_mount (loop_dev, mnt_pt) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to mount '%s' on '%s'\n", a->instanceId, loop_dev, mnt_pt);
goto unloop;
}
// copy in kernel and ramdisk and run grub over the root partition and the MBR
logprintfl (EUCAINFO, "[%s] making partition %d bootable\n", a->instanceId, root_part);
logprintfl (EUCAINFO, "[%s] with kernel %s\n", a->instanceId, kernel_path);
logprintfl (EUCAINFO, "[%s] and ramdisk %s\n", a->instanceId, ramdisk_path);
if (diskutil_grub (blockblob_get_dev (a->bb), mnt_pt, root_part, kernel_path, ramdisk_path)!=OK) {
logprintfl (EUCAERROR, "[%s] error: failed to make disk bootable\n", a->instanceId, root_part);
goto unmount;
}
// change user of the blob device back to 'eucalyptus' (grub sets it to 'root')
sleep (1); // without this, perms on dev-mapper devices can flip back, presumably because in-kernel ops complete after grub process finishes
if (diskutil_ch (blockblob_get_dev (a->bb), EUCALYPTUS_ADMIN, NULL, 0) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to change user for '%s' to '%s'\n", a->instanceId, blockblob_get_dev (a->bb), EUCALYPTUS_ADMIN);
}
bootification_failed = 0;
unmount:
// unmount partition and delete the mount point
if (diskutil_umount (mnt_pt) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to unmount %s (there may be a resource leak)\n", a->instanceId, mnt_pt);
bootification_failed = 1;
}
if (rmdir (mnt_pt) != 0) {
logprintfl (EUCAINFO, "[%s] error: failed to remove %s (there may be a resource leak): %s\n", a->instanceId, mnt_pt, strerror(errno));
bootification_failed = 1;
}
unloop:
if (diskutil_unloop (loop_dev) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to remove %s (there may be a resource leak): %s\n", a->instanceId, loop_dev, strerror(errno));
bootification_failed = 1;
}
if (bootification_failed)
goto cleanup;
}
ret = OK;
cleanup:
return ret;
}
static int iqn_creator (artifact * a)
{
assert (a);
virtualBootRecord * vbr = a->vbr;
assert (vbr);
char * dev = connect_iscsi_target (vbr->resourceLocation);
if (!dev || !strstr(dev, "/dev")) {
logprintfl(EUCAERROR, "[%s] error: failed to connect to iSCSI target\n", a->instanceId);
return ERROR;
}
// update VBR with device location
safe_strncpy (vbr->backingPath, dev, sizeof (vbr->backingPath));
vbr->backingType = SOURCE_TYPE_BLOCK;
return OK;
}
static int aoe_creator (artifact * a)
{
assert (a);
virtualBootRecord * vbr = a->vbr;
assert (vbr);
char * dev = vbr->resourceLocation;
if (!dev || !strstr(dev, "/dev") || check_block(dev)!=0) {
logprintfl(EUCAERROR, "[%s] error: failed to locate AoE device %s\n", a->instanceId, dev);
return ERROR;
}
// update VBR with device location
safe_strncpy (vbr->backingPath, dev, sizeof (vbr->backingPath));
vbr->backingType = SOURCE_TYPE_BLOCK;
return OK;
}
static int copy_creator (artifact * a)
{
assert (a->deps[0]);
assert (a->deps[1]==NULL);
artifact * dep = a->deps[0];
virtualBootRecord * vbr = a->vbr;
assert (vbr);
if (dep->bb != NULL) { // skip copy if source is NULL (as in the case of a bypassed redundant work artifact due to caching failure)
logprintfl (EUCAINFO, "[%s] copying/cloning blob %s to blob %s\n", a->instanceId, dep->bb->id, a->bb->id);
if (a->must_be_file) {
if (blockblob_copy (dep->bb, 0L, a->bb, 0L, 0L)==-1) {
logprintfl (EUCAERROR, "[%s] error: failed to copy blob %s to blob %s: %d %s\n", a->instanceId, dep->bb->id, a->bb->id, blobstore_get_error(), blobstore_get_last_msg());
return blobstore_get_error();
}
} else {
blockmap_relation_t op = BLOBSTORE_SNAPSHOT; // use snapshot by default as it is faster
if ((blobstore_snapshot_t)a->bb->store->snapshot_policy == BLOBSTORE_SNAPSHOT_NONE) {
op = BLOBSTORE_COPY; // but fall back to copy when snapshots are not possible or desired
}
blockmap map [] = {
{op, BLOBSTORE_BLOCKBLOB, {blob:dep->bb}, 0, 0, round_up_sec (dep->size_bytes) / 512}
};
if (blockblob_clone (a->bb, map, 1)==-1) {
logprintfl (EUCAERROR, "[%s] error: failed to clone/copy blob %s to blob %s: %d %s\n", a->instanceId, dep->bb->id, a->bb->id, blobstore_get_error(), blobstore_get_last_msg());
return blobstore_get_error();
}
}
}
const char * dev = blockblob_get_dev (a->bb);
const char * bbfile = blockblob_get_file(a->bb);
if (a->do_tune_fs) {
// tune file system, which is needed to boot EMIs fscked long ago
logprintfl (EUCAINFO, "[%s] tuning root file system on disk %d partition %d\n", a->instanceId, vbr->diskNumber, vbr->partitionNumber);
if (diskutil_tune (dev) == ERROR) {
logprintfl (EUCAWARN, "[%s] error: failed to tune root file system: %s\n", a->instanceId, blobstore_get_last_msg());
}
}
if (!strcmp(vbr->typeName, "kernel") || !strcmp(vbr->typeName, "ramdisk")) {
// for libvirt/kvm, kernel and ramdisk must be readable by libvirt
if (diskutil_ch (bbfile, NULL, NULL, 0664) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to change user and/or permissions for '%s' '%s'\n", a->instanceId, vbr->typeName, bbfile);
}
}
if (strlen (a->sshkey)) {
int injection_failed = 1;
logprintfl (EUCAINFO, "[%s] injecting the ssh key\n", a->instanceId);
// mount the partition
char mnt_pt [EUCA_MAX_PATH] = "/tmp/euca-mount-XXXXXX";
if (safe_mkdtemp (mnt_pt)==NULL) {
logprintfl (EUCAINFO, "[%s] error: mkdtemp() failed: %s\n", a->instanceId, strerror (errno));
goto error;
}
if (diskutil_mount (dev, mnt_pt) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to mount '%s' on '%s'\n", a->instanceId, dev, mnt_pt);
goto error;
}
// save the SSH key, with the right permissions
char path [EUCA_MAX_PATH];
snprintf (path, sizeof (path), "%s/root/.ssh", mnt_pt);
if (diskutil_mkdir (path) == -1) {
logprintfl (EUCAINFO, "[%s] error: failed to create path '%s'\n", a->instanceId, path);
goto unmount;
}
if (diskutil_ch (path, "root", NULL, 0700) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to change user and/or permissions for '%s'\n", a->instanceId, path);
goto unmount;
}
snprintf (path, sizeof (path), "%s/root/.ssh/authorized_keys", mnt_pt);
if (diskutil_write2file (path, a->sshkey) != OK) { // TODO: maybe append the key instead of overwriting?
logprintfl (EUCAINFO, "[%s] error: failed to save key in '%s'\n", a->instanceId, path);
goto unmount;
}
if (diskutil_ch (path, "root", NULL, 0600) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to change user and/or permissions for '%s'\n", a->instanceId, path);
goto unmount;
}
// change user of the blob device back to 'eucalyptus' (tune and maybe other commands above set it to 'root')
if (diskutil_ch (dev, EUCALYPTUS_ADMIN, NULL, 0) != OK) {
logprintfl (EUCAINFO, "[%s] error: failed to change user for '%s' to '%s'\n", a->instanceId, dev, EUCALYPTUS_ADMIN);
}
injection_failed = 0;
unmount: