Files
libguestfs/tests/disk-labels/test-disk-labels.pl
Richard W.M. Jones 7786d56db8 launch: Add add_drive 'label' option.
New API: list-disk-labels

Allow the user to pass an optional disk label when adding a drive.

This is passed through to qemu / libvirt using the disk serial field,
and from there to the appliance which exposes it through udev,
creating a special alias of the device /dev/disk/guestfs/<label>.
Partitions are named /dev/disk/guestfs/<label><partnum>.

virtio-blk and virtio-scsi limit the serial field to 20 bytes.  We
further limit the name to maximum 20 ASCII characters in [a-zA-Z].

list-devices and list-partitions are not changed: these calls still
return raw block device names.  However a new call, list-disk-labels,
returns a hash table allowing callers to map between disk labels, and
block device and partition names.

This commit also includes a test.
2012-10-08 20:04:47 +01:00

73 lines
2.3 KiB
Perl
Executable File

#!/usr/bin/perl
# Copyright (C) 2012 Red Hat 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Test using the 'label' option of add_drive, and the
# list_disk_labels call.
use strict;
use warnings;
use Sys::Guestfs;
my $g = Sys::Guestfs->new ();
# Add two drives.
foreach (["test1.img", "a"], ["test2.img", "b"]) {
my ($output, $label) = @$_;
open FILE, ">$output" or die "$output: $!";
truncate FILE, 512 * 1024 * 1024 or die "$output: truncate: $!";
close FILE or die "$output: $!";
$g->add_drive ($output, readonly => 0, format => "raw", label => $label);
}
$g->launch ();
# Partition the drives.
$g->part_disk ("/dev/disk/guestfs/a", "mbr");
$g->part_init ("/dev/disk/guestfs/b", "mbr");
$g->part_add ("/dev/disk/guestfs/b", "p", 64, 100 * 1024 * 2 - 1);
$g->part_add ("/dev/disk/guestfs/b", "p", 100 * 1024 * 2, -64);
# Check the partitions exist using both the disk label and raw name.
die unless
$g->blockdev_getsize64 ("/dev/disk/guestfs/a1") ==
$g->blockdev_getsize64 ("/dev/sda1");
die unless
$g->blockdev_getsize64 ("/dev/disk/guestfs/b1") ==
$g->blockdev_getsize64 ("/dev/sdb1");
die unless
$g->blockdev_getsize64 ("/dev/disk/guestfs/b2") ==
$g->blockdev_getsize64 ("/dev/sdb2");
# Check list_disk_labels
my %labels = $g->list_disk_labels ();
die unless exists $labels{"a"};
die unless $labels{"a"} eq "/dev/sda";
die unless exists $labels{"b"};
die unless $labels{"b"} eq "/dev/sdb";
die unless exists $labels{"a1"};
die unless $labels{"a1"} eq "/dev/sda1";
die unless exists $labels{"b1"};
die unless $labels{"b1"} eq "/dev/sdb1";
die unless exists $labels{"b2"};
die unless $labels{"b2"} eq "/dev/sdb2";
unlink "test1.img";
unlink "test2.img";
exit 0