liblocstorage.pm 49.8 KB
Newer Older
1
2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2013-2018 University of Utah and the Flux Group.
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
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

#
# Linux specific routines and constants for the client bootime setup stuff.
#
package liblocstorage;
use Exporter;
@ISA = "Exporter";
@EXPORT =
    qw (
	os_init_storage os_check_storage os_create_storage os_remove_storage
34
	os_show_storage os_get_diskinfo
35
36
37
38
39
40
       );

sub VERSION()	{ return 1.0; }

# Must come after package declaration!
use English;
41
use Cwd 'abs_path';
42
43
use libsetup;
use libtmcc;
44

45
46
my $VGNAME;

47
48
49
50
51
52
53
54
55
# Load up the paths. Its conditionalized to be compatabile with older images.
# Note this file has probably already been loaded by the caller.
BEGIN
{
    if (-e "/etc/emulab/paths.pm") {
	require "/etc/emulab/paths.pm";
	import emulabpaths;
    }
    else {
56
57
58
59
60
61
62
	$ETCDIR  = "/etc/rc.d/testbed";
	$BINDIR  = "/etc/rc.d/testbed";
	$VARDIR  = "/etc/rc.d/testbed";
	$BOOTDIR = "/etc/rc.d/testbed";
    }

    $VGNAME = "emulab";
63
64
65
    if (GENVNODEHOST() && !SHAREDHOST()) {
	$VGNAME = "xen-vg";
    } elsif (INXENVM() && -r "$VARDIR/boot/vmname") {
66
67
68
69
70
	my $vname = `cat $VARDIR/boot/vmname`;
	chomp $vname;
	if ($vname =~ /^([-\w]+)$/) {
	    $VGNAME = "emulab-$1";
	}
71
72
73
74
75
76
    }
}

my $MOUNT	= "/bin/mount";
my $UMOUNT	= "/bin/umount";
my $MKDIR	= "/bin/mkdir";
77
78
my $MKFS	= "/sbin/mke2fs";
my $FSCK	= "/sbin/e2fsck";
79
80
my $DOSTYPE	= "$BINDIR/dostype";
my $ISCSI	= "/sbin/iscsiadm";
81
my $ISCSI_ALT	= "/usr/bin/iscsiadm";
82
my $ISCSINAME	= "/etc/iscsi/initiatorname.iscsi";
83
my $SMARTCTL	= "/usr/sbin/smartctl";
84
my $BLKID	= "/sbin/blkid";
85
86
87
88
my $SFDISK	= "/sbin/sfdisk";
my $SGDISK	= "/sbin/sgdisk";
my $GDISK	= "/sbin/gdisk";
my $PPROBE	= "/sbin/partprobe";
89
my $FRISBEE     = "/usr/local/bin/frisbee";
90
my $HDPARM	= "/sbin/hdparm";
91
92
93
94
95
96
97
98
99
100
101

#
#
# To find the block stores exported from a target portal:
#
#   iscsiadm -m discovery -t sendtargets -p <storage-host>
#
# Display all data for a given node record:
#
#   iscsiadm -m node -T <iqn> -p <storage-host>
#
102
103
# Here are the commands to add a remote iSCSI target, set it to NOT be
# mounted automatically at startup, and startup a session (login):
104
105
106
# 
#   iscsiadm -m node -T <iqn> -p <storage-host> -o new
#   iscsiadm -m node -T <iqn> -p <storage-host> -o update \
107
#              -n node.startup -v manual
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
#   iscsiadm -m node -T <iqn> -p <storage-host> -l
# 
# To show active sessions:
# 
#   iscsiadm -m session
# 
# To stop a specific session (logout) and kill its record:
#
#   iscsiadm -m node -T <iqn> -p <storage-host> -u
#   iscsiadm -m node -T <iqn> -p <storage-host> -o delete
#
# To stop all iscsi sessions and kill all records:
# 
#   iscsiadm -m node -U all
#   iscsiadm -m node -o delete
# 
# Once a blockstore is added, you have to use "fdisk -l" or possibly
# crap out in /proc to discover what the name of the disk is.  I've been
# looking for uniform way to query the set of disks on a machine, but
# haven't quite figured this out yet.  The closest thing I've found is
# "fdisk -l".  There are some libraries and such, but there are enough
# of them that I'm not sure which one is best / most standard.
# 

sub iscsi_to_dev($)
{
    my ($session) = @_;

    #
    # XXX this is a total hack and maybe distro dependent?
    #
139
    my @lines = `ls -l /sys/block/sd[a-z] /sys/block/sd[a-z][a-z] 2>&1`;
140
    foreach (@lines) {
141
	if (m#/sys/block/(sd[a-z][a-z]?) -> ../devices/platform/host\d+/session(\d+)#) {
142
143
144
145
146
147
148
149
150
	    if ($2 == $session) {
		return $1;
	    }
	}
    }

    return undef;
}

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#
# Returns one if the indicated device is an iSCSI-provided one
# XXX another total hack
#
sub is_iscsi_dev($)
{
    my ($dev) = @_;

    if (-e "/sys/block/$dev") {
	my $line = `ls -l /sys/block/$dev 2>/dev/null`;
	if ($line =~ m#/sys/block/$dev -> ../devices/platform/host\d+/session\d+#) {
	    return 1;
	    }
    }
    return 0;
}

168
sub find_serial($)
169
{
170
171
172
173
174
175
176
177
178
    my ($dev) = @_;
    my @lines;

    #
    # Try using "smartctl -i" first
    #
    if (-x "$SMARTCTL") {
	@lines = `$SMARTCTL -i /dev/$dev 2>&1`;
	foreach (@lines) {
179
	    if (/^serial number:\s+(\S.*)/i) {
180
181
182
183
		return $1;
	    }
	}
    }
184
185

    #
186
    # Try /dev/disk/by-id.
187
188
    # XXX this is a total hack and maybe distro dependent?
    #
189
    @lines = `ls -l /dev/disk/by-id/ 2>&1`;
190
    foreach (@lines) {
191
	if (m#.*_([^_\s]+) -> ../../(sd[a-z][a-z]?)$#) {
192
193
	    if ($2 eq $dev) {
		return $1;
194
195
196
197
	    }
	}
    }

198
199
200
201
202
203
204
205
206
207
208
209
210
211
    # XXX Parse dmesg output?

    return undef;
}

#
# Do a one-time initialization of a serial number -> /dev/sd? map.
#
sub init_serial_map()
{
    #
    # XXX this is a total hack and maybe distro dependent?
    #
    my %snmap = ();
212
    my @lines = `ls -l /sys/block/sd[a-z] /sys/block/sd[a-z][a-z] /sys/block/nvme[0-9]* 2>&1`;
213
214
    foreach (@lines) {
	# XXX if a pci device, assume a local disk
215
	# XXX for moonshots (arm64), it is different
216
	if (m#/sys/block/(sd[a-z][a-z]?) -> ../devices/pci\d+# ||
217
	    m#/sys/block/(nvme\d+n\d+) -> ../devices/pci\d+# ||
218
	    m#/sys/block/(sd[a-z][a-z]?) -> ../devices/soc.\d+#) {
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
	    my $dev = $1;
	    $sn = find_serial($dev);
	    if ($sn) {
		$snmap{$sn} = $dev;
	    }
	}
    }

    return \%snmap;
}

sub serial_to_dev($$)
{
    my ($so, $sn) = @_;

    if (defined($so->{'LOCAL_SNMAP'})) {
	my $snmap = $so->{'LOCAL_SNMAP'};
	if (exists($snmap->{$sn})) {
	    return $snmap->{$sn};
	}
    }
240
241
242
    return undef;
}

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
#
# Determine if a disk is "SSD" or "HDD"
#
sub get_disktype($)
{
    my ($dev) = @_;
    my @lines;

    #
    # Assume NVMe is SSSD.
    # Older hdparm and smartctl don't seem to handle NVMe
    #
    if ($dev =~ /^nvme\d+n\d+/) {
	return "SSD";
    }

    #
    # Try hdparm first since it is a standard utility
    #
    if (-x "$HDPARM") {
	if (open(HFD, "$HDPARM -I /dev/$dev 2>/dev/null |")) {
	    my $isssd = 0;

	    while (my $line = <HFD>) {
		chomp($line);
		if ($line =~ /:\s+solid state device$/i) {
		    $isssd = 1;
		    last;
		}
	    }
	    close(HFD);

	    return ($isssd ? "SSD" : "HDD");
	}
    }

    #
    # Try using "smartctl -i"
    #
    if (-x "$SMARTCTL") {
Mike Hibler's avatar
Mike Hibler committed
283
	if (open(HFD, "$SMARTCTL -i /dev/$dev 2>&1 |")) {
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
	    my $isssd = -1;
	    my $model ="";

	    while (my $line = <HFD>) {
		chomp($line);
		if ($line =~ /^rotation rate:\s+(\S.*)/i) {
		    if ($1 =~ /solid state device/i) {
			$isssd = 1;
		    } else {
			$isssd = 0;
		    }
		    last;
		}
		# XXX if we don't find rotation rate, we will fall back on this
		if ($line =~ /^device model:\s+(\S.*)/i) {
		    $model = $1;
		    next;
		}
	    }
	    close(HFD);

	    if ($isssd >= 0) {
		return ($isssd ? "SSD" : "HDD");
	    }
	    
	    #
	    # XXX older versions of smartctl (e.g., in CentOS 6-ish)
	    # don't return "Rotation Rate". This is a fall-back hack as
	    # we know that at least Intel SSDs have SSD in the model name.
	    #
	    if ($model =~ /SSD/) {
		return "SSD";
	    }
	}
    }

    # Assume it is a spinning disk.
    return "HDD";
}

324
325
326
327
328
329
330
331
#
# Return the name (e.g., "sda") of the boot disk, aka the "system volume".
#
sub get_bootdisk()
{
    my $disk = undef;
    my $line = `$MOUNT | grep ' on / '`;

332
333
    if ($line && $line =~ qr{^(/dev/\S+) on /}) {
	my $device = abs_path($1);
334
335
336
	if ($device &&
	    ($device =~ qr{^/dev/(nvme\S+)p\d+} ||
	     $device =~ qr{^/dev/(\S+)\d+})) {
337
338
	    $disk = $1;
	}
339
    }
340

341
342
343
    return $disk;
}

344
sub get_ptabtype($)
345
{
346
347
348
349
350
351
352
353
    my ($dev) = @_;

    # if device doesn't exist, assume unknown
    if (! -e "/dev/$dev") {
	return "unknown";
    }

    # if sfdisk fails, assume unknown
354
    my $pinfo = `$SFDISK -l /dev/$dev 2>&1`;
355
356
357
358
359
360
361
362
363
    if ($?) {
	return "unknown";
    }

    # if sfdisk doesn't recognize it, assume unknown
    if ($pinfo =~ /unrecognized partition table type/) {
	return "unknown";
    }

364
365
366
367
368
    # newer sfdisk recognizes GPT
    if ($pinfo =~ /Disklabel type: gpt/) {
	return "GPT";
    }

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
    # if sfdisk detects a GPT, go with it
    if ($pinfo =~ /WARNING: GPT \(GUID Partition Table\) detected/) {
	return "GPT";
    }

    # otherwise MBR
    return "MBR";
}

sub get_partsize($)
{
    my ($dev) = @_;
    my $size = 0;

    if (!open(FD, "/proc/partitions")) {
	warn("*** get_disksize: could not get disk info from /proc/partitions\n");
	return $size;
    }
    while (<FD>) {
388
389
	if (/^\s+\d+\s+\d+\s+(\d+)\s+((?:xvd|sd)[a-z][a-z]?(?:\d+)?)/ ||
	    /^\s+\d+\s+\d+\s+(\d+)\s+(nvme\d+n\d+(?:p\d+)?)/) {
390
391
392
393
394
395
396
397
398
	    my ($_size,$_dev) = ($1,$2);

	    if ($dev eq $_dev) {
		$size = int($_size / 1024);
		last;
	    }
	}
    }
    close(FD);
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
    return $size;
}

sub get_parttype($$$)
{
    my ($dev,$pnum,$pttype) = @_;

    if ($pttype eq "GPT") {
	my $looking = 0;
	my @lines = `$GDISK -l /dev/$dev 2>/dev/null`;
	chomp @lines;
	foreach my $line (@lines) {
	    if (!$looking && $line =~ /^Number\s+Start \(sector\)/) {
		$looking = 1;
		next;
	    }
	    #
	    # XXX I am afraid of ambiguity if I keep this RE too simple
	    # since we are looking for 4 hex digits for the type (code),
	    # but a simple RE could match the start or end sector or even
	    # possibly the size.
	    #
	    if ($looking &&
		$line =~ /^\s*(\d+)\s+\d+\s+\d+\s+[\d\.]+ \S+\s+([\dA-F]{4})\s/) {
		if ($1 == $pnum) {
		    return hex($2);
		}
	    }
	}

	return -1;
    }

    my $ptype = `$SFDISK /dev/$dev -c $pnum 2>/dev/null`;
434
    if ($? == 0 && $ptype ne "") {
435
	chomp($ptype);
436
	if ($ptype =~ /^\s*([\da-fA-F]+)$/) {
437
438
439
440
441
442
443
444
445
446
447
	    $ptype = hex($1);
	} else {
	    $ptype = -1;
	}
    } else {
	$ptype = -1;
    }

    return $ptype;
}

448
449
450
#
# Returns 1 if the volume manager has been initialized.
# For LVM this means that the "emulab" volume group exists.
451
# If an argument is provided, we return the number of PVs in the VG.
452
#
453
sub is_lvm_initialized($)
454
{
455
456
    my $pvsp = shift;

457
    my $vg = `vgs -o vg_name,pv_count --noheadings $VGNAME 2>/dev/null`;
458
    if ($vg) {
459
	if ($pvsp) {
460
	    if ($vg =~ /${VGNAME}\s+(\d+)/) {
461
462
463
464
465
		$$pvsp = $1;
	    } else {
		$$pvsp = 1;
	    }
	}
466
467
468
469
470
	return 1;
    }
    return 0;
}

471
#
472
473
474
475
476
477
# Get information about local disks.
#
# Ideally, this comes from the list of ELEMENTs passed in.
#
# But if that is not available, we figure it out outselves by using
# a simplified version of the libvnode findSpareDisks.
478
479
480
# XXX the various "get space on the local disk" mechanisms should be
# reconciled.
#
481
sub get_diskinfo()
482
483
484
485
486
{
    my %geominfo = ();

    #
    # Get the list of partitions.
487
    # XXX only care about xvd|sd[a-z] devices and their partitions.
488
489
    #
    if (!open(FD, "/proc/partitions")) {
490
	warn("*** get_diskinfo: could not get disk info from /proc/partitions\n");
491
492
493
	return undef;
    }
    while (<FD>) {
494
495
496
	if (/^\s+\d+\s+\d+\s+(\d+)\s+((?:xvd|sd)[a-z][a-z]?)(\d+)?/ ||
	    /^\s+\d+\s+\d+\s+(\d+)\s+(nvme\d+n\d+)(?:p(\d+))?/) {
	    my ($size,$dev,$part) = ($1,$2,$3);
497
498
	    # DOS partition
	    if (defined($part)) {
499
500
501
502
503
		my $pttype = "MBR";
		if (exists($geominfo{$dev}{'ptabtype'})) {
		    $pttype = $geominfo{$dev}{'ptabtype'};
		}

504
		# XXX avoid garbage and extended partitions
505
		next if ($pttype eq "MBR" && ($part < 1 || $part > 4));
506
507

		my $pdev = "$dev$part";
508
509
510
		if ($dev =~ /^nvme/) {
		    $pdev = "${dev}p${part}";
		}
511
512
513
		$geominfo{$pdev}{'level'} = 1;
		$geominfo{$pdev}{'type'} = "PART";
		$geominfo{$pdev}{'size'} = int($size / 1024);
514
		$geominfo{$pdev}{'inuse'} = get_parttype($dev, $part, $pttype);
515
	    }
516
517
518
519
520
521
522
523
	    # XXX iSCSI disk
	    elsif (is_iscsi_dev($dev)) {
		$geominfo{$dev}{'level'} = 0;
		$geominfo{$dev}{'type'} = "iSCSI";
		$geominfo{$dev}{'size'} = int($size / 1024);
		$geominfo{$dev}{'inuse'} = -1;
	    }
	    # raw local disk
524
525
526
527
528
	    else {
		$geominfo{$dev}{'level'} = 0;
		$geominfo{$dev}{'type'} = "DISK";
		$geominfo{$dev}{'size'} = int($size / 1024);
		$geominfo{$dev}{'inuse'} = 0;
529
		$geominfo{$dev}{'ptabtype'} = get_ptabtype($dev);
530
		$geominfo{$dev}{'disktype'} = get_disktype($dev);
531
532
533
534
535
536
537
	    }
	}
    }
    close(FD);

    # XXX watch out for mounted disks/partitions (DOS type may be 0)
    if (!open(FD, "/etc/fstab")) {
538
	warn("*** get_diskinfo: could not get mount info from /etc/fstab\n");
539
540
541
	return undef;
    }
    while (<FD>) {
542
	if (/^\/dev\/((?:xvd|sd|nvme)\S+)/) {
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
	    my $dev = $1;
	    if (exists($geominfo{$dev}) && $geominfo{$dev}{'inuse'} == 0) {
		$geominfo{$dev}{'inuse'} = -1;
	    }
	}
    }
    close(FD);

    #
    # Make a pass through and mark disks that are in use where "in use"
    # means "has a partition".
    #
    foreach my $dev (keys %geominfo) {
	if ($geominfo{$dev}{'type'} eq "PART" &&
	    $geominfo{$dev}{'level'} == 1 &&
558
	    ($dev =~ /^(nvme\d+n\d+)p\d+$/ || $dev =~ /^(.*)\d+$/)) {
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
	    if (exists($geominfo{$1}) && $geominfo{$1}{'inuse'} == 0) {
		$geominfo{$1}{'inuse'} = 1;
	    }
	}
    }

    #
    # Find disks/partitions in use by LVM and update the available size
    #
    if (open(FD, "pvs -o pv_name,pv_size --units m --noheadings|")) {
	while (<FD>) {
	    if (/^\s+\/dev\/(\S+)\s+(\d+)\.\d+m$/) {
		my $dev = $1;
		my $size = $2;
		$geominfo{$dev}{'size'} = $size;
		$geominfo{$dev}{'inuse'} = -1;
	    }
	}
	close(FD);
    }
    #
    # See if there are any volume groups.
    # We don't care about the specific output
    #
    my $gotvgs = 0;
    my $vgs = `vgs -o vg_name --noheadings 2>/dev/null`;
    if ($vgs) {
	$gotvgs = 1;
    }

    #
    # Record any LVs as well.
    # We only do this if we know there are volume groups, else lvs will fail.
    #
    if ($gotvgs &&
594
	open(FD, "lvs -o vg_name,lv_name,lv_size,lv_attr --units m --noheadings|")) {
595
	while (<FD>) {
Mike Hibler's avatar
Mike Hibler committed
596
	    if (/^\s+(\S+)\s+(\S+)\s+(\d+)\.\d+m\s+([-a-zA-Z]{9,})$/) {
597
598
599
		my $vg = $1;
		my $lv = $2;
		my $size = $3;
600
		my $attrs = $4;
601
602
603
604
605
606
		my $dev = "$vg/$lv";

		$geominfo{$dev}{'level'} = 2;
		$geominfo{$dev}{'type'} = "LVM";
		$geominfo{$dev}{'size'} = $size;
		$geominfo{$dev}{'inuse'} = 1;
Mike Hibler's avatar
Mike Hibler committed
607
		if ($attrs =~ /^....a/) {
608
609
610
611
		    $geominfo{$dev}{'active'} = 1;
		} else {
		    $geominfo{$dev}{'active'} = 0;
		}
612
613
614
615
616
617
618
619
	    }
	}
	close(FD);
    }

    return \%geominfo;
}

620
621
622
623
#
# See if this is a filesystem type we can deal with.
# If so, return the type suitable for use by fsck and mount.
#
624
sub get_fstype($$;$$)
625
{
626
    my ($href,$dev,$rwref,$silent) = @_;
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
    my $type = "";

    #
    # If there is an explicit type set, believe it.
    #
    if (exists($href->{'FSTYPE'})) {
	$type = $href->{'FSTYPE'};
    }

    #
    # No explicit type set, see if we can intuit what the FS is.
    #
    else {
	my $blkid = `$BLKID -s TYPE -o value $dev`;
	if ($? == 0) {
	    chomp($blkid);
	    $type = $blkid;
	}

	if ($type && !exists($href->{'FSTYPE'})) {
	    $href->{'FSTYPE'} = $type;
	}
    }

    # ext? is okay
    if ($type =~ /^ext[234]$/) {
	if ($rwref) {
	    $$rwref = 1;
	}
	return $type;
    }

    # UFS can be mounted RO
    if ($type eq "ufs") {
	if ($rwref) {
	    $$rwref = 0;
	}
	return "ufs";
    }

667
668
669
670
671
672
673
674
    if (!$silent) {
	my $lv = $href->{'VOLNAME'};
	if ($type) {
	    warn("*** $lv: unsupported FS ($type) on $dev\n");
	} else {
	    warn("*** $lv: unknown or no FS on $dev\n");
	}
    }
675
676
677
678
679
680
    if ($rwref) {
	$$rwref = 0;
    }
    return undef;
}

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
#
# Check that the device for a blockstore has a valid filesystem by
# fscking it. If $fixit is zero, run the fsck RO just to report if
# the filesystem is there and consistent, otherwise attempt to fix it.
# $redir is a redirect string for output of the fsck command.
#
# Returns 1 if all is well, 0 otherwise.
#
sub checkfs($$$)
{
    my ($href,$fixit,$redir) = @_;
    my $lv = $href->{'VOLNAME'};
    my $mdev = $href->{'LVDEV'};

    # determine the filesystem type
    my $fstype = get_fstype($href, $mdev);
    if (!$fstype) {
	return 0;
    }

    my $fopt = "-p";
    if (!$fixit || $href->{'PERMS'} eq "RO") {
	$fopt = "-n";
    }

    # XXX cannot fsck ufs, right now we just pretend everything is okay
    if ($fstype ne "ufs" &&
	mysystem("$FSCK $fopt $mdev $redir")) {
	warn("*** $lv: fsck of $mdev failed\n");
	return 0;
    }

    return 1;
}

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
sub set_iname($$)
{
    my ($iqn,$redir) = @_;

    if (!defined($iqn)) {
	warn("*** storage: could not determine IQN prefix, initiator name is not unique!\n");
	return 0;
    }

    my $hname = `hostname 2>/dev/null`;
    chomp($hname);

    # See if the existing initiator name is correct
    my $iname = "$iqn:$hname";
    if (open(FD, "<$ISCSINAME")) {
	while (<FD>) {
	    if (/^InitiatorName=(.*)/) {
		# existing name is correct
		if ($1 eq $iname) {
		    close(FD);
		    return 1;
		}
	    }
	}
	close(FD);
    }

    # name is incorrect, try setting it
    if (!open(FD, ">$ISCSINAME")) {
	warn("*** storage: unable to set unique initiator name!\n");
	return 0;
    }

    print FD "# Created by Emulab liblocstorage.pm\n";
    print FD "InitiatorName=$iname\n";
    close(FD);

    # restart iscsid
    if (mysystem("service open-iscsi restart $redir")) {
	warn("*** storage: could not restart iscsid!\n");
	return 0;
    }

    return 1;
}

762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#
# Handle one-time operations.
# Return a cookie (object) with current state of storage subsystem.
#
sub os_init_storage($)
{
    my ($lref) = @_;
    my $redir = ">/dev/null 2>&1";

    my $gotlocal = 0;
    my $gotnonlocal = 0;
    my $gotelement = 0;
    my $gotslice = 0;
    my $gotiscsi = 0;
    my $needavol = 0;
    my $needall = 0;
778
    my $iqn;
779
780
781
782
783
784
785
786
787

    my %so = ();

    foreach my $href (@{$lref}) {
	if ($href->{'CMD'} eq "ELEMENT") {
	    $gotelement++;
	} elsif ($href->{'CMD'} eq "SLICE") {
	    $gotslice++;
	    if ($href->{'BSID'} eq "SYSVOL" ||
788
		$href->{'BSID'} eq "NONSYSVOL") {
789
790
791
792
793
794
795
796
797
798
799
		$needavol = 1;
	    } elsif ($href->{'BSID'} eq "ANY") {
		$needall = 1;
	    }
	}
	if ($href->{'CLASS'} eq "local") {
	    $gotlocal++;
	} else {
	    $gotnonlocal++;
	    if ($href->{'PROTO'} eq "iSCSI") {
		$gotiscsi++;
800
801
802
803
804
		if (!defined($iqn) && defined($href->{'UUID'})) {
		    if ($href->{'UUID'} =~ /^([^:]+):/) {
			$iqn = $1;
		    }
		}
805
806
807
808
	    }
	}
    }

809
810
811
812
813
814
    if ($gotlocal) {
	# check for local storage incompatibility
	if ($needall && $needavol) {
	    warn("*** storage: Incompatible local volumes.\n");
	    return undef;
	}
815
	
816
817
818
	# initialize mapping of serial numbers to devices
	if ($gotelement) {
	    $so{'LOCAL_SNMAP'} = init_serial_map();
819
820
	}

821
822
823
824
825
	# initialize volume manage if needed for local slices
	if ($gotslice) {
	    #
	    # Allow for the volume group to exist.
	    #
826
827
	    my $pvs = 1;
	    if (is_lvm_initialized(\$pvs)) {
828
		$so{'LVM_VGCREATED'} = 1;
829
		$so{'LVM_VGDEVS'} = $pvs;
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
	    }

	    #
	    # Grab the bootdisk and current GEOM state
	    #
	    my $bdisk = get_bootdisk();
	    my $ginfo = get_diskinfo();
	    if (!exists($ginfo->{$bdisk}) || $ginfo->{$bdisk}->{'inuse'} == 0) {
		warn("*** storage: bootdisk '$bdisk' marked as not in use!?\n");
		return undef;
	    }

	    #
	    # Boot disk should have a partition table type
	    #
	    if (!exists($ginfo->{$bdisk}->{'ptabtype'})) {
		warn("*** storage: bootdisk '$bdisk' does not have partition table type!?\n");
		return undef;
	    }

	    $so{'BOOTDISK'} = $bdisk;
	    $so{'DISKINFO'} = $ginfo;
852
853
854
855
	}
    }

    if ($gotiscsi) {
856
857
858
859
860
	#
	# Make sure client has the correct initiator IQN, otherwise fix it.
	#
	set_iname($iqn, $redir);

861
	if (! -x "$ISCSI") {
862
863
864
865
866
	    if (! -x "$ISCSI_ALT") {
		warn("*** storage: $ISCSI does not exist, cannot continue\n");
		return undef;
	    }
	    $ISCSI = $ISCSI_ALT;
867
	}
868
869
870
871
872
873
874
875
876
	#
	# XXX don't grok the Ubuntu startup, so...
	# make sure automatic sessions are started
	#
	my $nsess = `$ISCSI -m session 2>/dev/null | grep -c ^`;
	chomp($nsess);
	if ($nsess == 0) {
	    mysystem("$ISCSI -m node --loginall=automatic $redir");
	}
877
878
879
880
881
882
    }

    $so{'INITIALIZED'} = 1;
    return \%so;
}

883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
sub os_get_diskinfo($)
{
    my ($so) = @_;

    return get_diskinfo();
}

#
# XXX debug
#
sub os_show_storage($)
{
    my ($so) = @_;

    my $bdisk = $so->{'BOOTDISK'};
    print STDERR "OS Dep info:\n";
    print STDERR "  BOOTDISK=$bdisk\n" if ($bdisk);

    my $dinfo = get_diskinfo();
    if ($dinfo) {
	print STDERR "  DISKINFO:\n";
904
	foreach my $dev (sort keys %$dinfo) {
905
906
907
	    my $type = $dinfo->{$dev}->{'type'};
	    my $lev = $dinfo->{$dev}->{'level'};
	    my $size = $dinfo->{$dev}->{'size'};
908
909
910
	    my $inuse = sprintf("%X", $dinfo->{$dev}->{'inuse'});
	    print STDERR "    name=$dev, type=$type, level=$lev, size=$size, inuse=$inuse";
	    if ($type eq "DISK") {
911
912
		print STDERR ", disktype=", $dinfo->{$dev}->{'disktype'},
		    ", pttype=", $dinfo->{$dev}->{'ptabtype'};
913
	    }
914
915
916
	    elsif ($type eq "LVM") {
		print STDERR ", active=", $dinfo->{$dev}->{'active'};
	    }
917
	    print STDERR "\n";
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
	}
    }

    # LOCAL_SNMAP
    my $snmap = $so->{'LOCAL_SNMAP'};
    if ($so->{'LOCAL_SNMAP'}) {
	my $snmap = $so->{'LOCAL_SNMAP'};

	print STDERR "  LOCAL_SNMAP:\n";
	foreach my $sn (keys %$snmap) {
	    print STDERR "    $sn -> ", $snmap->{$sn}, "\n";
	}
    }
}

933
934
935
936
937
938
939
#
# os_check_storage(sobject,confighash)
#
#   Determines if the storage unit described by confighash exists and
#   is properly configured. Returns zero if it doesn't exist, 1 if it
#   exists and is correct, -1 otherwise.
#
940
#   Side-effect: Creates the hash member $href->{'LVDEV'} with the /dev
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
#   name of the storage unit.
#
sub os_check_storage($$)
{
    my ($so,$href) = @_;

    if ($href->{'CMD'} eq "ELEMENT") {
	return os_check_storage_element($so,$href);
    }
    if ($href->{'CMD'} eq "SLICE") {
	return os_check_storage_slice($so,$href);
    }
    return -1;
}

sub os_check_storage_element($$)
{
    my ($so,$href) = @_;
    my $CANDISCOVER = 0;
960
    my $redir = ">/dev/null 2>&1";
961
962
963
964
965
966

    #
    # iSCSI:
    #  make sure the IQN exists
    #  make sure a session exists
    #
967
    if ($href->{'CLASS'} eq "SAN" && $href->{'PROTO'} eq "iSCSI") {
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
	my $hostip = $href->{'HOSTIP'};
	my $uuid = $href->{'UUID'};
	my $bsid = $href->{'VOLNAME'};
	my @lines;

	#
	# See if the block store exists on the indicated server.
	# If not, something is very wrong, return -1.
	#
	# Note that the server may not support discovery. If not, we don't
	# do it since it is only a sanity check anyway.
	#
	if ($CANDISCOVER) {
	    @lines = `$ISCSI -m discovery -t sendtargets -p $hostip 2>&1`;
	    if ($? != 0) {
		warn("*** could not find exported iSCSI block stores\n");
		return -1;
	    }
	    if (!grep(/$uuid/, @lines)) {
		warn("*** could not find iSCSI block store '$uuid'\n");
		return -1;
	    }
	}

	#
	# It exists, are we connected to it?
	# If not, we have not done the one-time initialization, return 0.
	#
	my $session;
	@lines = `$ISCSI -m session 2>&1`;
	foreach (@lines) {
999
	    if (/^tcp: \[(\d+)\].*$uuid\b/) {
1000
1001
1002
1003
1004
1005
1006
1007
1008
		$session = $1;
		last;
	    }
	}
	if (!defined($session)) {
	    return 0;
	}

	#
1009
	# If there is no session, we have a problem.
1010
1011
	#
	my $dev = iscsi_to_dev($session);
1012
	if (!defined($dev)) {
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
	    #
	    # XXX apparently the device may not show up immediately,
	    # so pause and try again.
	    #
	    sleep(1);
	    $dev = iscsi_to_dev($session);
	    if (!defined($dev)) {
		warn("*** $bsid: found iSCSI session but could not determine local device\n");
		return -1;
	    }
1023
	}
1024

1025
	$href->{'LVDEV'} = "/dev/$dev";
1026
1027

	#
1028
	# If there is a mount point, see if it is mounted.
1029
	#
1030
1031
1032
1033
1034
1035
1036
	# XXX because mounts in /etc/fstab happen before iSCSI and possibly
	# even the network are setup, we don't put our mounts there as we
	# do for local blockstores. Thus, if the blockstore device is not
	# mounted, we do it here.
	#
	my $mpoint = $href->{'MOUNTPOINT'};
	if ($mpoint) {
1037
1038
	    my $mdev = $href->{'LVDEV'};
	    my $mopt = "";
1039

1040
1041
	    my $line = `$MOUNT | grep '^$mdev on '`;
	    if (!$line) {
1042
1043
		# determine the filesystem type
		my $rw = 0;
1044
		my $fstype = get_fstype($href, $mdev, \$rw);
1045
1046
1047
1048
		if (!$fstype) {
		    return -1;
		}

1049
		# check for RO export and adjust options accordingly
1050
		if ($href->{'PERMS'} eq "RO") {
1051
		    $mopt = "-o ro";
1052
1053
1054
1055
		    # XXX for ufs
		    if ($fstype eq "ufs") {
			$mopt .= ",ufstype=ufs2";
		    }
1056
		}
1057
1058
1059
		# OS only supports RO mounting, right now we just fail
		elsif ($rw == 0) {
		    warn("*** $bsid: OS only supports RO mounting of ".
1060
			 "$fstype FSes\n");
1061
1062
		    return -1;
		}
1063

1064
1065
1066
1067
1068
		# the mountpoint should exist
		if (! -d "$mpoint") {
		    warn("*** $bsid: no mount point $mpoint\n");
		    return -1;
		}
1069

1070
1071
		# fsck the filesystem in case of an abrupt shutdown.
		if (!checkfs($href, 1, $redir)) {
1072
1073
		    return -1;
		}
1074
1075
1076
1077

		# and mount it
		if (mysystem("$MOUNT $mopt $mdev $mpoint $redir")) {
		    warn("*** $bsid: could not mount $mdev on $mpoint\n");
1078
1079
1080
		    return -1;
		}
	    }
1081
	    elsif ($line !~ /^${mdev} on (\S+) / || $1 ne $mpoint) {
1082
1083
1084
1085
1086
		warn("*** $bsid: mounted on $1, should be on $mpoint\n");
		return -1;
	    }
	}

1087
1088
1089
1090
1091
	# XXX set the fstype for reporting
	if (!exists($href->{'FSTYPE'})) {
	    get_fstype($href, "/dev/$dev");
	}

1092
	return 1;
1093
1094
1095
1096
1097
1098
    }

    #
    # local disk:
    #  make sure disk exists
    #
1099
    if ($href->{'CLASS'} eq "local") {
1100
1101
1102
	my $bsid = $href->{'VOLNAME'};
	my $sn = $href->{'UUID'};

1103
	my $dev = serial_to_dev($so, $sn);
1104
	if (defined($dev)) {
1105
	    $href->{'LVDEV'} = "/dev/$dev";
1106
1107
1108
	    return 1;
	}

1109
1110
1111
1112
1113
	# XXX not an error for now, until we can be sure that we can
	# get SN info for all disks
	$href->{'LVDEV'} = "<UNKNOWN>";
	return 1;

1114
1115
1116
1117
1118
	# for physical disks, there is no way to "create" it so return error
	warn("*** $bsid: could not find HD with serial '$sn'\n");
	return -1;
    }

1119
1120
    warn("*** $bsid: unsupported class/proto '" .
	 $href->{'CLASS'} . "/" . $href->{'PROTO'} . "'\n");
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
    return -1;
}

#
# Return 0 if does not exist
# Return 1 if exists and correct
# Return -1 otherwise
#
sub os_check_storage_slice($$)
{
    my ($so,$href) = @_;
    my $bsid = $href->{'BSID'};

    #
    # local storage:
    #  if BSID==SYSVOL:
    #    see if 4th part of boot disk exists (eg: da0s4) and
1138
    #    is of type linux
1139
    #  else if BSID==NONSYSVOL:
1140
    #    see if there is a logical volume with appropriate name
1141
    #  else if BSID==ANY:
1142
    #    see if there is a logical volume with appropriate name
1143
1144
1145
1146
    #  if there is a mountpoint, see if it exists in /etc/fstab
    #
    if ($href->{'CLASS'} eq "local") {
	my $lv = $href->{'VOLNAME'};
1147
	my ($dev, $devtype, $rdev);
1148

1149
	my $ginfo = $so->{'DISKINFO'};
1150
	my $bdisk = $so->{'BOOTDISK'};
1151
	my $pttype = "MBR";
1152
	my $slop = 0;
1153
1154
1155

	# figure out the device of interest
	if ($bsid eq "SYSVOL") {
1156
1157
1158
1159
1160
	    my $pchr = "";
	    if ($bdisk =~ /^nvme/) {
		$pchr = "p";
	    }
	    $dev = $rdev = "${bdisk}${pchr}4";
1161
	    $devtype = "PART";
1162
	    $pttype = $ginfo->{$bdisk}->{'ptabtype'};
1163
	} else {
1164
	    $dev = "$VGNAME/$lv";
1165
	    # XXX the real path is returned by mount
Mike Hibler's avatar
Mike Hibler committed
1166
1167
	    # XXX note that any '-'s in the mapper name are doubled
	    (my $rlv = $lv) =~ s/-/--/g;
1168
	    $rdev = "mapper/${VGNAME}-$rlv";
1169
	    $devtype = "LVM";
1170
1171
1172
1173
1174
1175
1176
	    # XXX LVM rounds up to physical extent size (4 MiB)
	    # on every physical volume that is in the VG
	    if (exists($so->{'LVM_VGDEVS'}) && $so->{'LVM_VGDEVS'} > 1) {
		$slop = (4 * $so->{'LVM_VGDEVS'}) - 1;
	    } else {
		$slop = 3;
	    }
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
	}
	my $devsize = $href->{'VOLSIZE'};

	# if the device does not exist, return 0
	if (!exists($ginfo->{$dev})) {
	    return 0;
	}
	# if it exists but is of the wrong type, we have a problem!
	my $atype = $ginfo->{$dev}->{'type'};
	if ($atype ne $devtype) {
	    warn("*** $lv: actual type ($atype) != expected type ($devtype)\n");
	    return -1;
	}
1190
1191
	#
	# Ditto for size, unless this is the SYSVOL where we ignore user size
1192
	# or if the size was not specified.
1193
	#
1194
	my $asize = $ginfo->{$dev}->{'size'};
1195
	if ($bsid ne "SYSVOL" && $devsize &&
1196
	    !($asize >= $devsize && $asize <= $devsize + $slop)) {
1197
1198
1199
1200
1201
1202
	    warn("*** $lv: actual size ($asize) != expected size ($devsize)\n");
	    return -1;
	}

	# for the system disk, ensure partition is not in use
	if ($bsid eq "SYSVOL") {
1203
	    # XXX inuse for a partition is set to MBR/GPT type
1204
	    my $ptype = $ginfo->{$dev}->{'inuse'};
1205
	    my $linuxid = ($pttype eq "GPT") ? 0x8300 : 0x83;
1206
1207
1208
1209
1210

	    # if type is 0, it is not setup
	    if ($ptype == 0) {
		return 0;
	    }
1211
1212
	    # ow, if not a Linux FS partition, there is a problem 
	    if ($ptype != $linuxid) {
1213
1214
1215
1216
1217
		warn("*** $lv: $dev already in use (type $ptype)\n");
		return -1;
	    }
	}

1218
1219
1220
1221
1222
1223
	#
	# If it is an LVM, may sure it is active.
	#
	if ($devtype eq "LVM" && $ginfo->{$dev}->{'active'} == 0) {
	    if (mysystem("lvchange -ay $dev")) {
		warn("*** $lv: could not activate LV $dev\n");
1224
1225
		return -1;
	    }
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
	    sleep(1);
	}

	#
	# If there is a mountpoint, make sure it is mounted.
	#
	my $mpoint = $href->{'MOUNTPOINT'};
	if ($mpoint) {
	    my $line = `$MOUNT | grep '^/dev/$rdev on '`;
	    if (!$line) {
		#
		# See if the mount exists in /etc/fstab.
		#
		# XXX Right now if it does not, it might be because we
		# removed it prior to creating an image. So we make some
		# additional sanity checks (right now fsck'ing the alleged FS)
		# and if it passes, re-add the mount line.
		#
1244
1245
1246
1247
		# XXX It might also be because we have re-loaded the OS
		# and not only the fstab line but the mountpoint might be
		# missing. We attempt to repair this case as well.
		#
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
		$line = `grep '^/dev/$dev\[\[:space:\]\]' /etc/fstab`;
		if (!$line) {
		    warn("  $lv: mount of /dev/$dev missing from fstab; sanity checking and re-adding...\n");
		    my $fstype = get_fstype($href, "/dev/$dev");
		    if (!$fstype) {
			return -1;
		    }

		    # XXX sanity check, is there a recognized FS on the dev?
		    # XXX checkfs needs LVDEV set
		    $href->{'LVDEV'} = "/dev/$dev";
		    if (!checkfs($href, 0, "")) {
			undef $href->{'LVDEV'};
			return -1;
		    }
		    undef $href->{'LVDEV'};
1264
1265
1266
1267
1268
1269
1270
1271

		    # make sure the mount point exists (case of reloaded OS)
		    if (! -d "$mpoint" &&
			mysystem("$MKDIR -p $mpoint")) {
			warn("*** $lv: could not create mountpoint '$mpoint'\n");
			return -1;
		    }

1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
		    if (!open(FD, ">>/etc/fstab")) {
			warn("*** $lv: could not add mount to /etc/fstab\n");
			return -1;
		    }
		    print FD "# /dev/$dev added by $BINDIR/rc/rc.storage\n";
		    print FD "/dev/$dev\t$mpoint\t$fstype\tdefaults\t0\t0\n";
		    close(FD);
		}

		if (mysystem("$MOUNT $mpoint")) {
		    warn("*** $lv: is not mounted, should be on $mpoint\n");
		    return -1;
		}
	    } else {
		if ($line !~ /^\/dev\/$rdev on (\S+) / || $1 ne $mpoint) {
		    warn("*** $lv: mounted on $1, should be on $mpoint\n");
		    return -1;
		}
1290
1291
1292
	    }
	}

1293
1294
1295
1296
	# XXX set the fstype for reporting
	if (!exists($href->{'FSTYPE'})) {
	    get_fstype($href, "/dev/$dev");
	}
1297
	$href->{'LVDEV'} = "/dev/$dev";
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
	return 1;
    }

    warn("*** $bsid: unsupported class '" . $href->{'CLASS'} . "'\n");
    return -1;
}

#
# os_create_storage(confighash)
#
#   Create the storage unit described by confighash. Unit must not exist
#   (os_check_storage should be called first to verify). Return one on
#   success, zero otherwise.
#
sub os_create_storage($$)
{
    my ($so,$href) = @_;
1315
    my $fstype;
1316
    my $rv = 0;
1317

1318
1319
1320
1321
    # record all the output for debugging
    my $log = "/var/emulab/logs/" . $href->{'VOLNAME'} . ".out";
    mysystem("cp /dev/null $log");

1322
    if ($href->{'CMD'} eq "ELEMENT") {
1323
	$rv = os_create_storage_element($so, $href, $log);
1324
    }
1325
    elsif ($href->{'CMD'} eq "SLICE") {
1326
	$rv = os_create_storage_slice($so, $href, $log);
1327
    }
1328
1329
1330
1331
    if ($rv == 0) {
	return 0;
    }

1332
1333
    my $mopt = "";

1334
    if (exists($href->{'MOUNTPOINT'})) {
1335
1336
	my $lv = $href->{'VOLNAME'};
	my $mdev = $href->{'LVDEV'};
1337
1338
1339
1340
1341
1342
1343
1344

	# record all the output for debugging
	my $redir = "";
	my $logmsg = "";
	if ($log) {
	    $redir = ">>$log 2>&1";
	    $logmsg = ", see $log";
	}
1345
1346

	#
1347
1348
1349
1350
1351
1352
	# If this is a persistent iSCSI disk, we never create the filesystem!
	# Instead, we fsck it in case it was not shutdown cleanly in its
	# previous existence.
	#
	if ($href->{'CLASS'} eq "SAN" && $href->{'PROTO'} eq "iSCSI" &&
	    $href->{'PERSIST'} != 0) {
1353
1354

	    # check for the easy errors first, before time consuming fsck
1355
1356
1357
1358
1359
1360
	    my $rw = 0;
	    $fstype = get_fstype($href, $mdev, \$rw);
	    if (!$fstype) {
		return 0;
	    }

1361
	    # check for RO export and adjust mount options accordingly
1362
	    if ($href->{'PERMS'} eq "RO") {
1363
		$mopt = "-o ro";
1364
1365
1366
1367
		# XXX for ufs
		if ($fstype eq "ufs") {
		    $mopt .= ",ufstype=ufs2";
		}
1368
	    }
1369
1370
1371
	    # OS only supports RO mounting, right now we just fail
	    elsif ($rw == 0) {
		warn("*** $lv: OS only supports RO mounting of ".
1372
		     "$fstype FSes\n");
1373
		return 0;
1374
1375
	    }

1376
1377
	    # finally do the fsck, fixing errors if possible
	    if (!checkfs($href, 1, $redir)) {
1378
1379
1380
		return 0;
	    }
	}
1381
1382
1383
1384
1385
1386
1387
	elsif (exists($href->{'DATASET'})) {
	    #
	    # Load with the dataset.
	    #
	    my $proxyopt   = "";
	    my $imageid    = $href->{'DATASET'};
	    my $imagepath  = $mdev;
1388
	    my $server     = $href->{'SERVER'};
1389
1390
1391
1392
1393
1394
1395
1396

	    if (SHAREDHOST()) {
		my $TMNODEID  = TMNODEID();
		my $node_id   = `cat $TMNODEID`;
		chomp($node_id);
		$proxyopt = "-P $nodeid";
	    }

1397
1398
1399
1400
1401
	    # Allow the server to enable heartbeat reports in the client
	    my $heartbeat = "-H 0";

	    my $command = "$FRISBEE -f -M 128 $proxyopt $heartbeat ".
		"-S $server -B 30 -F $imageid $imagepath";
1402
1403

	    print STDERR "$command\n";
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413

	    if (mysystem($command)) {
		warn("*** $lv: frisbee of dataset to $mdev failed!\n");
		return 0;
	    }
	    $fstype = get_fstype($href, $mdev);
	    if (!$fstype) {
		return 0;
	    }
	}
1414
1415
	#
	# Otherwise, create the filesystem:
1416
	#
1417
1418
	# Start by trying ext4 which is much faster when creating large FSes.
	# Otherwise fall back on ext3 and then ext2.
1419
	#
1420
1421
	else {
	    my $failed = 1;
1422
	    my $fsopts;
1423
1424
	    if ($failed) {
		$fstype = "ext4";
1425
		$fsopts = "-F -q -E lazy_itable_init=1,nodiscard";
1426
1427
1428
1429
		$failed = mysystem("$MKFS -t $fstype $fsopts $mdev $redir");
	    }
	    if ($failed) {
		$fstype = "ext3";
1430
		$fsopts = "-F -q";
1431
1432
1433
1434
		$failed = mysystem("$MKFS -t $fstype $fsopts $mdev $redir");
	    }
	    if ($failed) {
		$fstype = "ext2";
1435
		$fsopts = "-F -q";
1436
1437
1438
1439
1440
1441
		$failed = mysystem("$MKFS -t $fstype $fsopts $mdev $redir");
	    }
	    if ($failed) {
		warn("*** $lv: could not create FS\n");
		return 0;
	    }
1442
1443
1444
1445
1446
1447
	}

	#
	# Mount the filesystem
	#
	my $mpoint = $href->{'MOUNTPOINT'};
1448
1449
	if (! -d "$mpoint" && mysystem("$MKDIR -p $mpoint $redir")) {
	    warn("*** $lv: could not create mountpoint '$mpoint'$logmsg\n");
1450
1451
1452
	    return 0;
	}

1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
	#
	# XXX because mounts in /etc/fstab happen before iSCSI and possibly
	# even the network are setup, we don't put our mounts there as we
	# do for local blockstores. Instead, the check_storage call will
	# take care of these mounts.
	#
	if (!($href->{'CLASS'} eq "SAN" && $href->{'PROTO'} eq "iSCSI")) {
	    if (!open(FD, ">>/etc/fstab")) {
		warn("*** $lv: could not add mount to /etc/fstab\n");
		return 0;
	    }
Mike Hibler's avatar
Mike Hibler committed
1464
	    print FD "# $mdev added by $BINDIR/rc/rc.storage\n";
1465
1466
1467
	    print FD "$mdev\t$mpoint\t$fstype\tdefaults\t0\t0\n";
	    close(FD);
	    if (mysystem("$MOUNT $mpoint $redir")) {
1468
		warn("*** $lv: could not mount on $mpoint$logmsg\n");
1469
1470
1471
		return 0;
	    }
	} else {
1472
	    if (mysystem("$MOUNT $mopt -t $fstype $mdev $mpoint $redir")) {
1473
		warn("*** $lv: could not mount $mdev on $mpoint$logmsg\n");
1474
1475
		return 0;
	    }
1476
1477
1478
1479
	}
    }

    return 1;
1480
1481
}

1482
sub os_create_storage_element($$$)
1483
{
1484
    my ($so,$href,$log) = @_;
1485