From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 517C564536 for ; Tue, 6 Oct 2020 15:33:11 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 443EC26F11 for ; Tue, 6 Oct 2020 15:32:36 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id A767726EDE for ; Tue, 6 Oct 2020 15:32:32 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6646545AE7 for ; Tue, 6 Oct 2020 15:32:32 +0200 (CEST) From: Stefan Reiter To: pve-devel@lists.proxmox.com Date: Tue, 6 Oct 2020 15:32:14 +0200 Message-Id: <20201006133218.25403-4-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201006133218.25403-1-s.reiter@proxmox.com> References: <20201006133218.25403-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.043 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [drive.pm, qemuserver.pm] Subject: [pve-devel] [PATCH v2 qemu-server 3/7] add new 'boot' property format and introduce legacy conversion helpers X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 06 Oct 2020 13:33:11 -0000 The format is unused in this commit, but will replace the current string-based format of the 'boot' property. It is included since the parameter of bootorder_from_legacy follows it. Two helper methods are introduced: * bootorder_from_legacy: Parses the legacy format into a hash closer to what the new format represents * get_default_bootdevices: Encapsulates the legacy default behaviour if nothing is specified in the boot order resolve_first_disk is simplified and gets a new $cdrom parameter to control the behaviour of excluding CD-ROMs or instead searching for only them. Signed-off-by: Stefan Reiter --- PVE/QemuServer.pm | 130 ++++++++++++++++++++++++++++++++++++++++ PVE/QemuServer/Drive.pm | 11 ++-- 2 files changed, 135 insertions(+), 6 deletions(-) diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index bd59616..cfac03a 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -1091,6 +1091,68 @@ for (my $i = 0; $i < $MAX_USB_DEVICES; $i++) { $confdesc->{"usb$i"} = $usbdesc; } +my $boot_fmt = { + legacy => { + optional => 1, + default_key => 1, + type => 'string', + description => "Boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)." + . " Deprecated, use 'order=' instead.", + pattern => '[acdn]{1,4}', + format_description => "[acdn]{1,4}", + + # note: this is also the fallback if boot: is not given at all + default => 'cdn', + }, + order => { + optional => 1, + type => 'string', + format => 'pve-qm-bootdev-list', + format_description => "device[;device...]", + description => <{$dev}; + return 1; + }; + + return $dev if $check->("net"); + return $dev if $check->("usb"); + return $dev if $check->("hostpci"); + + return undef if $noerr; + die "invalid boot device '$dev'\n"; +} + +sub print_bootorder { + my ($devs) = @_; + my $data = { order => join(';', @$devs) }; + return PVE::JSONSchema::print_property_string($data, $boot_fmt); +} + my $kvm_api_version = 0; sub kvm_version { @@ -7152,6 +7214,74 @@ sub clear_reboot_request { return $res; } +sub bootorder_from_legacy { + my ($conf, $bootcfg) = @_; + + my $boot = $bootcfg->{legacy} || $boot_fmt->{legacy}->{default}; + my $bootindex_hash = {}; + my $i = 1; + foreach my $o (split(//, $boot)) { + $bootindex_hash->{$o} = $i*100; + $i++; + } + + my $bootorder = {}; + + PVE::QemuConfig->foreach_volume($conf, sub { + my ($ds, $drive) = @_; + + if (drive_is_cdrom ($drive, 1)) { + if ($bootindex_hash->{d}) { + $bootorder->{$ds} = $bootindex_hash->{d}; + $bootindex_hash->{d} += 1; + } + } elsif ($bootindex_hash->{c}) { + $bootorder->{$ds} = $bootindex_hash->{c} + if $conf->{bootdisk} && $conf->{bootdisk} eq $ds; + $bootindex_hash->{c} += 1; + } + }); + + if ($bootindex_hash->{n}) { + for (my $i = 0; $i < $MAX_NETS; $i++) { + my $netname = "net$i"; + next if !$conf->{$netname}; + $bootorder->{$netname} = $bootindex_hash->{n}; + $bootindex_hash->{n} += 1; + } + } + + return $bootorder; +} + +# Generate default device list for 'boot: order=' property. Matches legacy +# default boot order, but with explicit device names. This is important, since +# the fallback for when neither 'order' nor the old format is specified relies +# on 'bootorder_from_legacy' above, and it would be confusing if this diverges. +sub get_default_bootdevices { + my ($conf) = @_; + + my @ret = (); + + # harddisk + my $first = PVE::QemuServer::Drive::resolve_first_disk($conf, 0); + push @ret, $first if $first; + + # cdrom + $first = PVE::QemuServer::Drive::resolve_first_disk($conf, 1); + push @ret, $first if $first; + + # network + for (my $i = 0; $i < $MAX_NETS; $i++) { + my $netname = "net$i"; + next if !$conf->{$netname}; + push @ret, $netname; + last; + } + + return \@ret; +} + # bash completion helper sub complete_backup_archives { diff --git a/PVE/QemuServer/Drive.pm b/PVE/QemuServer/Drive.pm index 91c33f8..b71fc93 100644 --- a/PVE/QemuServer/Drive.pm +++ b/PVE/QemuServer/Drive.pm @@ -584,16 +584,15 @@ sub is_volume_in_use { } sub resolve_first_disk { - my $conf = shift; + my ($conf, $cdrom) = @_; my @disks = valid_drive_names(); - my $firstdisk; - foreach my $ds (reverse @disks) { + foreach my $ds (@disks) { next if !$conf->{$ds}; my $disk = parse_drive($ds, $conf->{$ds}); - next if drive_is_cdrom($disk); - $firstdisk = $ds; + next if drive_is_cdrom($disk) xor $cdrom; + return $ds; } - return $firstdisk; + return undef; } 1; -- 2.20.1