Skip to content

Commit

Permalink
Merge pull request #163 from ned21/ncm-grub
Browse files Browse the repository at this point in the history
ncm-grub: Add support for configuring a console argument
  • Loading branch information
Piojo committed May 5, 2014
2 parents ecc2bc0 + d21ce8e commit 0bde607
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
106 changes: 106 additions & 0 deletions ncm-grub/src/main/perl/grub.pm
Expand Up @@ -16,13 +16,18 @@ package NCM::Component::grub;
#

use strict;
use CAF::FileEditor;
use CAF::FileWriter;
use NCM::Component;
use Readonly;
use EDG::WP4::CCM::Element;
use vars qw(@ISA $EC);
@ISA = qw(NCM::Component);
$EC=LC::Exception::Context->new->will_store_all;
$NCM::Component::grub::NoActionSupported = 1;

Readonly::Scalar my $GRUBCONF => '/boot/grub/grub.conf';

sub parseKernelArgs {
my ($kernelargs)=@_;

Expand Down Expand Up @@ -86,6 +91,38 @@ sub Configure {
my $kernelversion=$config->getValue($kernelpath);
my $fulldefaultkernelpath=$prefix.'/'.$kernelname.'-'.$kernelversion;

my $cons = undef;
my $consolepath = "/hardware/console/serial";
my $serialcons = undef;
my $newserial = "";
my $newterminal = "";
if ($config->elementExists($consolepath)) {
my %consnode = $config->getElement($consolepath)->getHash();

my $unit = "0";
if (exists $consnode{"unit"}) {
$unit = $consnode{"unit"}->getValue();
}

my $speed = "9600";
if (exists $consnode{"speed"}) {
$speed = $consnode{"speed"}->getValue();
}

my $word = "8";
if (exists $consnode{"word"}) {
$word = $consnode{"word"}->getValue();
}

my $parity = "n";
if (exists $consnode{"parity"}) {
$parity = $consnode{"parity"}->getValue();
}
$cons = " console=ttyS$unit,$speed$parity$word";
$newserial = "serial --unit=$unit --speed=$speed --parity=$parity --word=$word\n";
$newterminal = "terminal serial console\n";
}

my $path = "/software/components/grub/kernels";

my @kernels;
Expand Down Expand Up @@ -130,6 +167,70 @@ sub Configure {
$self->verbose("This version of grubby has no support for multiboot kernels");
}


# Check the serial console settings if neccessary
if ($newserial) {
my $modified = 0;
# grab a copy of the old file (if it exists)
my @grublines = ();
if (open(my $fh, '<', $GRUBCONF)) {
@grublines = <$fh>;
} else {
$self->warn("Unable to open $GRUBCONF");
@grublines = (
"# Generated by ncm-grub\n",
$newserial,
$newterminal,
);
$modified++;
}

# must find out how to use the std library, for now we'll just manually
# do it.... If there are no serial console settings in there already,
# then we insert them just before the first kernel (the first title line)
my $gotserial = 0;
my $gotterminal = 0;
foreach my $g (@grublines) {
if ($g =~ /^title/) {
my $add = "";
if (!$gotserial) {
$add = $newserial;
}
if (!$gotterminal) {
$add .= $newterminal;
}
if ($add) {
$g = "$add$g";
}
$modified++;
last;
}

if ($g =~ /^serial/) {
if ($g ne $newserial) {
$g = $newserial;
$modified++;
}
$gotserial++;
}
if ($g =~ /^terminal/) {
if ($g ne $newterminal) {
$g = $newterminal;
$modified++;
}
$gotterminal++;
}
}

my $fw = CAF::FileWriter->new($GRUBCONF,
owner => "root",
group => "root",
mode => 0400,
log => $self);
print $fw @grublines;
$fw->close();
}

foreach my $kernel (@kernels) {

my ($kernelpath, $kernelargs, $kerneltitle, $kernelinitrd,
Expand All @@ -146,6 +247,11 @@ sub Configure {

if ($kernel->{'kernelargs'}) {
$kernelargs=$kernel->{'kernelargs'};
if ($cons) {
# by $cons we mean serial cons, so we should only sub serial entries.
$kernelargs =~ s{console=(ttyS[^ ]*)}{};
$kernelargs .= $cons;
}
}

if ($kernel->{'title'}) {
Expand Down
5 changes: 4 additions & 1 deletion ncm-grub/src/main/perl/grub.pod
Expand Up @@ -104,6 +104,10 @@ Optionally set an initial ramdisk image to be loaded when booting.

The title string that will be used to describe this entry in grub.conf.

=item /hardware/console ? string

The console settings may be defined here.

=back

=head1 FILES MODIFIED
Expand Down Expand Up @@ -161,7 +165,6 @@ title Xen 3 / XenLinux 2.6.16
module /vmlinuz-2.6.16-xen3_86.1_rhel4.1 max_loop=128 root=/dev/hda2 ro
module /initrd-2.6.16-xen3_86.1_rhel4.1


=head1 BUGS

none known.
Expand Down

0 comments on commit 0bde607

Please sign in to comment.