LXR Guide: Difference between revisions

From Leo's Notes
This page was last edited on 1 September 2019, at 06:20.
No edit summary
m Text replacement - "Category:Linux{{Navbox Linux}}" to "{{Navbox Linux}}Category:Linux"
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
LXR, shortened for Linux Cross-Reference, is a perl web application that lets you read linux kernel source code of different versions with the benefit of having static values and functions 'linked' to each other which makes it a good reference when dealing with kernel source.
LXR, shortened for Linux Cross-Reference, is a perl web application that lets you read different versions of the Linux kernel source code with the added benefit of quickly searching the source and having static values and functions cross-referenced with each other.


== Step by Step Guide ==
== Step by Step Guide ==
The installation of LXRng are detailed in this guide for a CentOS 7 server. We will be using:
* LXRng
* PostgreSQL (as a fulltext index)
* Apache 2.4 / mod_perl
=== Prerequisites ===
=== Prerequisites ===
On a clean install of CentOS 7, install the following dependencies. A few Perl modules must be installed outside of yum and I recommend using cpanm.


Install required packages. Unfortunately, there are a few packages that are not available through yum (through default repos), and must be built through CPAN. <code>screen</code> is not a dependency, but it is nice to run the indexer through it as it can take from hours to days depending on your hardware.
{{highlight|lang=terminal|code=
# yum install postgresql postgresql-server \
perl-App-cpanminus \
xapian-core xapian-core-devel \
perl-DBI perl-DBD-Pg \
perl-Term-ProgressBar \
perl-Pod-POM \
perl-CGI \
perl-HTML-Template \
perl-XML-Parser \
perl-XML-NamespaceSupport \
perl-XML-LibXML \
perl-XML-SAX-Base \
perl-Image-Xbm \
perl-Image-Base \
perl-Template-Toolkit \
perl-Linux-Pid \
perl-BSD-Resource \
make gcc gcc-c++  \
ctags \
httpd \
mod_perl mod_ssl \
git \
screen
}}


<syntaxhighlight lang="bash" line start="1" enclose="div">
lxr expects it as {{code|ctag}} as {{code|ctags-exuberant}}.
yum --nogpgcheck install \
    perl-CGI-Ajax \
    perl-CGI-Simple \
    perl-CPAN \
    perl-DBD-Pg \
    perl-DBI \
    perl-Devel-Size \
    perl-ExtUtils-CBuilder \
    perl-ExtUtils-MakeMaker \
    perl-ExtUtils-Embed \
    perl-PerlIO-gzip \
    perl-HTML-Entities* \
    perl-Term-ProgressBar \
    perl-Template* \
    xapian* \
    postgresql postgresql-server \
    httpd \
    mod_perl \
    mod_perl-devel \
    ctags-etags \
    make gcc gcc-c++ \
    git \
    screen
</syntaxhighlight>


lxr expects it as <code>ctag</code> as <code>ctags-exuberant</code>.
{{highlight|lang=terminal|code=
# ln -s /usr/bin/ctags /usr/bin/ctags-exuberant
}}


<syntaxhighlight lang="bash" line start="1" enclose="div">
Install the missing dependencies through cpanm:
ln -s /usr/bin/ctags /usr/bin/ctags-exuberant
</syntaxhighlight>


Install the missing dependencies through CPAN:
{{highlight|lang=terminal|code=
# cpanm Search::Xapian
# cpanm Test::More
# cpanm Devel::Size
}}


<syntaxhighlight lang="bash" line start="1" enclose="div">
Pull the lxrng source into {{code|/var/www}}. Apache will own everything since the web server will need to run the web application.
perl -MCPAN -e "install Search::Xapian Apache2::Reload"
</syntaxhighlight>


{{highlight|lang=terminal|code=
# cd /var/www
# chgrp apache .
# sudo -u apache git clone git://lxr.linux.no/git/lxrng.git
}}
If this repository is down, a copy is also available at https://git.steamr.com/leo/lxrng.git


=== Setting up PostgreSQL ===
=== Setting up PostgreSQL ===
Line 49: Line 64:
With PostgreSQL installed, set it up with a lxrng database. You may want to set up permissions as well, but I will be using the root account.
With PostgreSQL installed, set it up with a lxrng database. You may want to set up permissions as well, but I will be using the root account.


<syntaxhighlight lang="bash" line start="1" enclose="div">
{{highlight|lang=terminal|code=
/etc/init.d/postgresql initdb
# postgresql-setup initdb
/etc/init.d/postgresql start
# systemctl start postgresql
# systemctl enable postgresql
}}


sudo -u postgres psql
By default, Postgres should be configured to use system accounts for authentication. Since we will have the Apache web server running as {{code|apache}}, we will create a Postgres account named {{code|apache}} that should have access to the lxrng database.
  ALTER USER postgres WITH ENCRYPTED PASSWORD 'lxrng';
  CREATE DATABASE lxrng;
  \q


# Use passwords
sed -i "s/ident$/md5/g" /var/lib/pgsql/data/pg_hba.conf


# Apply changes by restarting
{{highlight|lang=terminal|code=
/etc/init.d/postgresql restart
# su postgres
$ createuser -d apache
}}


# Make sure postgres starts on startup
chkconfig postgresql on --level 345
</syntaxhighlight>


=== LXRng Setup ===
=== LXRng Setup ===
We will be putting LXRng into <code>/export</code>
The {{code|lxrng.conf}} configuration file located at {{code|/var/www/lxrng/lxrng.conf}} should contain the following:


<syntaxhighlight lang="bash" line start="1" enclose="div">
{{highlight|lang=text|code=
mkdir /export
# -*- mode: perl -*-
cd /export
# Configuration file
# Source is from git. If unavailable, there is a copy at http://pages.cpsc.ucalgary.ca/~leo/support/packages/src/lxrng*.tar.gz
#
git clone git://lxr.linux.no/git/lxrng.git
#
</syntaxhighlight>


Edit LXRng's configuration at lxrng.conf. Make sure the database settings and URL is correct.
use LXRng::Index::PgBatch;
use LXRng::Repo::Git;
use LXRng::Search::Xapian;


<syntaxhighlight lang="bash" line start="1" enclose="div">
my $gitrepo = LXRng::Repo::Git
cd lxrng
    ->new('/var/www/lxrng/repos/linux-2.6/.git',
cp lxrng.conf-dist lxrng.conf
          release_re => qr/^v[^-]*$/,
sed -i "s/\/var\/lib/\/export/g" /export/lxrng/lxrng.conf
          author_timestamp => 0);
sed -i "s/db_user => \"\"/db_user => \"postgres\"/g" /export/lxrng/lxrng.conf
sed -i "s/db_pass => \"\"/db_pass => \"lxrng\"/g" /export/lxrng/lxrng.conf
sed -i "s/localhost/lxr.lab.cpsc.ucalgary.ca/g" /export/lxrng/lxrng.conf
sed -i "s/'ver_default'\s*=>\s*'[a-z0-9.]*'/'ver_default' => 'v2.6.32'/g" /export/lxrng/lxrng.conf
</syntaxhighlight>


Set up a few directories such as <code>repos</code> which will contain all the source code to be referenced.
my $index  = LXRng::Index::PgBatch->new(db_spec => 'dbname=lxrng;port=5432',
                                        db_user => "apache", db_pass => "",
                                        # table_prefix => 'lxr'
                                        );
my $search  = LXRng::Search::Xapian->new('/var/www/lxrng/text-db/linux-2.6');


<syntaxhighlight lang="bash" line start="1" enclose="div">
return {
mkdir repos
    'linux' => {
mkdir -p text-db/linux-2.6
        'repository'  => $gitrepo,
mkdir cache
        'index'      => $index,
chmod 777 cache
        'search'      => $search,
touch lxr-ctags-quirks
</syntaxhighlight>


=== Fetch the Kernel Source ===
        'base_url'    => 'http://lxr.cpsc.ucalgary.ca/',
Get the source code either via git or from an existing tarball.
        # Must be writable by httpd user:
        'cache'      => '/var/www/lxrng/cache',


<syntaxhighlight lang="bash" line start="1" enclose="div">
        'fs_charset'  => 'iso-8859-1',
cd repos
        # Tried successively
        'content_charset' => ['utf-8', 'iso-8859-1'],


# wget http://pages.cpsc.ucalgary.ca/~leo/support/linux-2.6-source.tar.gz -O - | tar -xzf -
        'languages'  => ['C', 'GnuAsm', 'Kconfig'],
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
        #'ctags_flags' => ["-I\@$LXRng::ROOT/lxr-ctags-quirks"],
        'ver_list'    => [$gitrepo->allversions],


# or for the newest post-2.6 kernels:
        'ver_default' => 'v4.9',
git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
</syntaxhighlight>


Start the indexing process:
        'include_maps' =>
            [
            [qr{{!}}^arch/(.*?)/{{!}}, qr{{!}}^asm/(.*){{!}},
              sub { "include/asm-$_[0]/$_[1]" }],
            [qr{{!}}^include/asm-(.*?)/{{!}}, qr{{!}}^asm/(.*){{!}},
              sub { "include/asm-$_[0]/$_[1]" }],
            [qr{{!}}^{{!}}, qr{{!}}^asm/(.*){{!}},
              sub { map { "include/asm-$_/$_[0]" }
                    qw(i386 alpha arm ia64 m68k mips mips64),
                    qw(ppc s390 sh sparc sparc64 x86_64) }],
            [qr{{!}}^{{!}}, qr{{!}}(.*){{!}},
              sub { "include/$_[0]" }],
            ],
    },
};


<syntaxhighlight lang="bash" line start="1" enclose="div">
}}
cd /export/lxrng
./lxr-db-admin linux --init
screen -d -m ./lxr-genxref linux
</syntaxhighlight>


=== Setting up Apache ===
There are a few important values which you may want to change:
* {{code|$gitrepo}} defines where the source code (as a git repo) will be placed. In our case, {{code|/var/www/lxrng/repos}} should contain a directory called {{code|linux-2.6/.git}} containing the git repository.
* {{code|$search}} defines where the text index should reside
* {{code|base_url}} is the URL of the web application
* {{code|cache}} is the cache directory used by lxr.


<syntaxhighlight lang="bash" line start="1" enclose="div">
Create these directories, and ensure they are readable and writable by apache:
cd /export/lxrng
{{highlight|lang=terminal|code=
cp apache2-site.conf-dist-mod_perl /etc/httpd/conf.d/lxrng.conf
# cd /var/www/lxrng
sed -i "s/@@LXRROOT@@/\/export\/lxrng/g" /etc/httpd/conf.d/lxrng.conf
# mkdir repos
sed -i "s/@@LXRURL@@/lxr/g" /etc/httpd/conf.d/lxrng.conf
# mkdir -p text-db/linux-2.6
sed -i "s/^[\(^#\)]*/# /" /etc/httpd/conf.d/welcome.conf
# mkdir cache
/etc/init.d/httpd start
# chmod 777 cache
chkconfig httpd on --level 345
}}
</syntaxhighlight>


=== Fetch the Kernel Source ===
Get the source code either via git or from an existing tarball.


Run 'screen -r' to view the progress of the cross reference generation progress
{{highlight|lang=terminal|code=
# cd /var/www/lxrng/repos
# git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git


## or for the newest post-2.6 kernels:
# git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
}}


<!--
Start the indexing process:


== Scripted Guide ==
{{highlight|lang=terminal|code=
# cd /var/www/lxrng/
# sudo -u apache sh
$ ./lxr-db-admin linux --init
$ ./lxr-genxref linux
## Or alternatively, run it in screen: screen -d -m ./lxr-genxref linux
##  and then view progress by running screen -r
}}


The scripted guide is displayed below. It was used and tested on a clean install of ScientificLinux 6.3.
=== Setting up Apache ===
 
I recommend doing everything one step at a time as this was just the list of steps I used to configure/install it.
 
<syntaxhighlight lang="bash" line start="1" enclose="div">
#!/bin/sh
 
cd /root
 
# Installing dependencies.
# Note: Screen isn't a dependency, but it is nice to run the indexer in the background.
yum --nogpgcheck install \
    perl-CGI-Ajax \
    perl-CGI-Simple \
    perl-CPAN \
    perl-DBD-Pg \
    perl-DBI \
    perl-Devel-Size \
    perl-ExtUtils-CBuilder \
    perl-ExtUtils-MakeMaker \
    perl-ExtUtils-Embed \
    perl-PerlIO-gzip \
    perl-HTML-Entities* \
    perl-Term-ProgressBar \
    perl-Template* \
    xapian* \
    postgresql postgresql-server \
    httpd \
    mod_perl \
    mod_perl-devel \
    ctags-etags \
    make gcc gcc-c++ \
    git \
    screen
 
# Ctags setup. lxr expects it as ctags-exuberant.
ln -s /usr/bin/ctags /usr/bin/ctags-exuberant
 
# Perl setup
#echo "I need you to init cpan, then run:"
#echo "cpan> install Search::Xapian"
#echo "cpan> install Apache2::Reload"
perl -MCPAN -e "install Search::Xapian Apache2::Reload"
 
#
# Postgres Setup
#
/etc/init.d/postgresql initdb
/etc/init.d/postgresql start
echo "I need you to enter:"
echo "postgres=# ALTER USER postgres WITH ENCRYPTED PASSWORD 'lxrng';"
echo "postgres=# CREATE DATABASE lxrng;"
echo "postgres=# \q"
sudo -u postgres psql
 
# Use passwords
sed -i "s/ident$/md5/g" /var/lib/pgsql/data/pg_hba.conf
 
# Apply - restart required
/etc/init.d/postgresql restart
 
chkconfig postgresql on --level 345
 
#
# LXR Setup
#
mkdir /export
cd /export
# Source is from git. If unavailable, there is a copy at http://pages.cpsc.ucalgary.ca/~leo/support/packages/src/lxrng*.tar.gz
git clone git://lxr.linux.no/git/lxrng.git
 
# Configure lxrng - change the settings here as necessary
cd lxrng
cp lxrng.conf-dist lxrng.conf
sed -i "s/\/var\/lib/\/export/g" /export/lxrng/lxrng.conf
sed -i "s/db_user => \"\"/db_user => \"postgres\"/g" /export/lxrng/lxrng.conf
sed -i "s/db_pass => \"\"/db_pass => \"lxrng\"/g" /export/lxrng/lxrng.conf
sed -i "s/localhost/lxr.lab.cpsc.ucalgary.ca/g" /export/lxrng/lxrng.conf
sed -i "s/'ver_default'\s*=>\s*'[a-z0-9.]*'/'ver_default' => 'v2.6.32'/g" /export/lxrng/lxrng.conf


mkdir repos
{{highlight|lang=terminal|code=
mkdir -p text-db/linux-2.6
# cd /export/lxrng
mkdir cache
# cp apache2-site.conf-dist-mod_perl /etc/httpd/conf.d/lxrng.conf
chmod 777 cache
# sed -i "s/@@LXRROOT@@/\/export\/lxrng/g" /etc/httpd/conf.d/lxrng.conf
touch lxr-ctags-quirks
# sed -i "s/@@LXRURL@@/lxr/g" /etc/httpd/conf.d/lxrng.conf
# sed -i "s/^[\(^#\)]*/# /" /etc/httpd/conf.d/welcome.conf
# /etc/init.d/httpd start
# chkconfig httpd on --level 345
}}


# Either get everything from git by running:
#  git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
# or get it from an existing source.
cd repos
wget http://pages.cpsc.ucalgary.ca/~leo/support/linux-2.6-source.tar.gz -O - | tar -xzf -
cd /export/lxrng
./lxr-db-admin linux --init
screen -d -m ./lxr-genxref linux
#
# Setup apache
#
cd /export/lxrng
cp apache2-site.conf-dist-mod_perl /etc/httpd/conf.d/lxrng.conf
sed -i "s/@@LXRROOT@@/\/export\/lxrng/g" /etc/httpd/conf.d/lxrng.conf
sed -i "s/@@LXRURL@@/lxr/g" /etc/httpd/conf.d/lxrng.conf
sed -i "s/^[\(^#\)]*/# /" /etc/httpd/conf.d/welcome.conf
/etc/init.d/httpd start
chkconfig httpd on --level 345
echo "Done."
echo "Run 'screen -r' to view the progress of the cross reference generation progress"
</syntaxhighlight>
-->


== Updating Kernel Source ==
== Updating Kernel Source ==
{{highlight|lang=terminal|code=
# cd repos
# git pull git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git master
}}


cd repos
Then, rerun the xref to update.
git pull git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git master
{{highlight|lang=terminal|code=
 
$ ./lxr-genxref linux
}}


[[Category:Linux]]
{{Navbox Linux}}[[Category:Linux]]


[[Category:Guides]]
[[Category:Guides]]

Latest revision as of 06:20, 1 September 2019

LXR, shortened for Linux Cross-Reference, is a perl web application that lets you read different versions of the Linux kernel source code with the added benefit of quickly searching the source and having static values and functions cross-referenced with each other.

Step by Step Guide

The installation of LXRng are detailed in this guide for a CentOS 7 server. We will be using:

  • LXRng
  • PostgreSQL (as a fulltext index)
  • Apache 2.4 / mod_perl

Prerequisites

On a clean install of CentOS 7, install the following dependencies. A few Perl modules must be installed outside of yum and I recommend using cpanm.

# yum install postgresql postgresql-server \
perl-App-cpanminus \
xapian-core xapian-core-devel \
perl-DBI perl-DBD-Pg \
perl-Term-ProgressBar \
perl-Pod-POM \
perl-CGI \
perl-HTML-Template \
perl-XML-Parser \
perl-XML-NamespaceSupport \
perl-XML-LibXML \
perl-XML-SAX-Base \
perl-Image-Xbm \
perl-Image-Base \
perl-Template-Toolkit \
perl-Linux-Pid \
perl-BSD-Resource \
make gcc gcc-c++  \
ctags \
httpd \
mod_perl mod_ssl \
git \
screen

lxr expects it as ctag as ctags-exuberant.

# ln -s /usr/bin/ctags /usr/bin/ctags-exuberant

Install the missing dependencies through cpanm:

# cpanm Search::Xapian
# cpanm Test::More
# cpanm Devel::Size

Pull the lxrng source into /var/www. Apache will own everything since the web server will need to run the web application.

# cd /var/www
# chgrp apache .
# sudo -u apache git clone git://lxr.linux.no/git/lxrng.git

If this repository is down, a copy is also available at https://git.steamr.com/leo/lxrng.git

Setting up PostgreSQL

With PostgreSQL installed, set it up with a lxrng database. You may want to set up permissions as well, but I will be using the root account.

# postgresql-setup initdb
# systemctl start postgresql
# systemctl enable postgresql

By default, Postgres should be configured to use system accounts for authentication. Since we will have the Apache web server running as apache, we will create a Postgres account named apache that should have access to the lxrng database.


# su postgres
$ createuser -d apache


LXRng Setup

The lxrng.conf configuration file located at /var/www/lxrng/lxrng.conf should contain the following:

# -*- mode: perl -*-
# Configuration file
#
#

use LXRng::Index::PgBatch;
use LXRng::Repo::Git;
use LXRng::Search::Xapian;

my $gitrepo = LXRng::Repo::Git
    ->new('/var/www/lxrng/repos/linux-2.6/.git',
          release_re => qr/^v[^-]*$/,
          author_timestamp => 0);

my $index   = LXRng::Index::PgBatch->new(db_spec => 'dbname=lxrng;port=5432',
                                         db_user => "apache", db_pass => "",
                                         # table_prefix => 'lxr'
                                         );
my $search  = LXRng::Search::Xapian->new('/var/www/lxrng/text-db/linux-2.6');

return {
    'linux' => {
        'repository'  => $gitrepo,
        'index'       => $index,
        'search'      => $search,

        'base_url'    => 'http://lxr.cpsc.ucalgary.ca/',
        # Must be writable by httpd user:
        'cache'       => '/var/www/lxrng/cache',

        'fs_charset'  => 'iso-8859-1',
        # Tried successively
        'content_charset' => ['utf-8', 'iso-8859-1'],

        'languages'   => ['C', 'GnuAsm', 'Kconfig'],
        #'ctags_flags' => ["-I\@$LXRng::ROOT/lxr-ctags-quirks"],
        'ver_list'    => [$gitrepo->allversions],

        'ver_default' => 'v4.9',

        'include_maps' =>
            [
             [qr|^arch/(.*?)/|, qr|^asm/(.*)|,
              sub { "include/asm-$_[0]/$_[1]" }],
             [qr|^include/asm-(.*?)/|, qr|^asm/(.*)|,
              sub { "include/asm-$_[0]/$_[1]" }],
             [qr|^|, qr|^asm/(.*)|,
              sub { map { "include/asm-$_/$_[0]" }
                    qw(i386 alpha arm ia64 m68k mips mips64),
                    qw(ppc s390 sh sparc sparc64 x86_64) }],
             [qr|^|, qr|(.*)|,
              sub { "include/$_[0]" }],
             ],
    },
};

There are a few important values which you may want to change:

  • $gitrepo defines where the source code (as a git repo) will be placed. In our case, /var/www/lxrng/repos should contain a directory called linux-2.6/.git containing the git repository.
  • $search defines where the text index should reside
  • base_url is the URL of the web application
  • cache is the cache directory used by lxr.

Create these directories, and ensure they are readable and writable by apache:

# cd /var/www/lxrng
# mkdir repos
# mkdir -p text-db/linux-2.6
# mkdir cache
# chmod 777 cache

Fetch the Kernel Source

Get the source code either via git or from an existing tarball.

# cd /var/www/lxrng/repos
# git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

## or for the newest post-2.6 kernels:
# git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Start the indexing process:

# cd /var/www/lxrng/
# sudo -u apache sh
$ ./lxr-db-admin linux --init
$ ./lxr-genxref linux
## Or alternatively, run it in screen: screen -d -m ./lxr-genxref linux
##  and then view progress by running screen -r

Setting up Apache

# cd /export/lxrng
# cp apache2-site.conf-dist-mod_perl /etc/httpd/conf.d/lxrng.conf
# sed -i "s/@@LXRROOT@@/\/export\/lxrng/g" /etc/httpd/conf.d/lxrng.conf
# sed -i "s/@@LXRURL@@/lxr/g" /etc/httpd/conf.d/lxrng.conf
# sed -i "s/^[\(^#\)]*/# /" /etc/httpd/conf.d/welcome.conf
# /etc/init.d/httpd start
# chkconfig httpd on --level 345


Updating Kernel Source

# cd repos
# git pull git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git master

Then, rerun the xref to update.

$ ./lxr-genxref linux