AUTHOR: James Kimball <LordJaxom NOBOTSmail NOBOTScom>

DATE: 2013-03-15

LICENSE: Creative Commons Attribution-ShareAlike 3.0

SYNOPSIS: Installing and using pacman as a package manager for LFS

DESCRIPTION: This hint tells you how to add a basic pacman setup to LFS
and provides some tips on using it. It assumes that you are in the process
of building a LFS system or are willing to rebuild it, but it could apply
to a working system with modification. It is for people who intend to build,
use, and maintain a LFS/BLFS system over some period of time. There isn't
much use for package management if you don't.

PREREQUISITES: You should have built the LFS at least once. You need enough
general Linux knowledge to solve typical system setup issues. Building and
installing packages with a package manager will require changes to the book
directions, occasionally substantial changes. It is not that difficult, but
expect some trial and error and browsing for answers to be necessary. I
assume you can easily edit text files on the LFS partition as you are doing
the hint; how you do that is up to you. Read and understand the hint before
starting it.

HINT:

General notes:
The information herein is based on my opinion/understanding of things. I
could well be wrong in places, or your ideas could differ. I used the
development version of LFS as of 7.3, and the latest sources of the pacman
related packages as of the original time of writing. I believe it would
work for other versions, although exact directions will probably change
somewhat as LFS and packages change.

As done in the hint, you get a basic version of pacman that can build,
install, upgrade, and remove packages. If you want more capabilities, such
as getting and verifying sources, you will probably need to build and
install more of pacman's dependency tree.

Introduction to pacman:
Pacman is the Arch Linux package manager; you will find their website and
wiki helpful for information on pacman and as a source for example package
build configuration files. Pacman is a build/management system for packages,
and contains several programs and scripts. It is one of the "fakeroot" type
package managers; LFS and some other hints give some general information on
these. The two main parts of pacman as described in the hint will work on
LFS; other parts may or may not work or be useful to you depending on your
setup and intentions. The pacman system uses a script, makepkg, to build
packages, and a program, pacman, to install and manage them.

Makepkg builds the packages. It uses a configuration file, 
/etc/makepkg.conf,
to contain general defaults and build instructions: such things as system
architecture information, environment variables, build flags, and options
to control things such as stripping binaries and packaging specifics. Each
package needs a PKGBUILD file, which contains specific commands to build and
package it up. Most if not all options can be overridden here, and pretty
much anything that will work in a shell script can be done.

Pacman installs and manages the packages. It uses a configuration file,
/etc/pacman.conf, for options related to getting and installing packages.
One of those things is accessing Arch Linux package repositories; you will
probably want to comment these out. Note that it is possible to build your
own package or source repositories, local or on a server, if you wish to do
so. A package may or may not have a "pkg.install" file; this contains
commands that will be run by pacman on certain install events. The "install
line" in PKGBUILD tells what file to use, if any. Browse the Arch wiki for
makepkg, PKGBUILD, and pacman for more information.

It is always a good idea to get the Arch build files for any package you
want to install. These do not usually include the source itself, but do
contain things such as PKGBUILDS, patches, config files to install with
a package, and so on. These can be modified for your use and are a good
starting point. I generally recommend following the book instructions
as closely as possible, but sometimes you may need to change them. Usually,
commands that are part of the build/package process, including "installing"
to the fakeroot directory, go in the PKGBUILD; commands that are part of
the later actual install, if any, go in the .install file. Some things could
reasonably be done in either.


Preparation:
Get the necessary sources. As of the time of writing, these are:

   fakeroot 1.18.4
   libarchive-3.1.2
   pacman-4.0.3
   util-linux-2.22.2
   zlib-1.2.7

You may want to use the latest versions of these. You should get
pacman, at least, from the Arch sources. If you are running Arch you can
get pacman through the ABS. If not you can, at the time of writing, get
the pacman source code from ftp://ftp.archlinux.org/sources/packages
and the build files by searching for pacman in package search on the
Arch website and browsing the "source files". The build files for pacman
at the moment are:

   PKGBUILD
   makepkg.conf
   pacman.conf
   pacman.conf.x86_64
   pacman.install

Get any available patches for these or any other packages you want to build,
from LFS, the Arch site, the package author's web site, etc. You may or may
not want to use them, but you should certainly get them and see.



Stage one: Manually installing pacman to your temporary toolchain

Important!
Stage one begins immediately before you start installing packages to your
final LFS system. In LFS 7.3, that is chapter 6.7, the linux api headers.
Follow the book up to that point, stop, then start stage one. Be absolutely
certain that you have chrooted as in the book before running any of the
commands in this hint. If not, then you will be installing this stuff on
the host!

Install the following packages to your temporary toolchain. Later, you
will install them to your final LFS system. Doing it this way allows
you to install LFS with package management so you can more easily modify
it later. You probably should apply any available patches to each package;
I will not specify these in the commands below as they will likely have
changed.

Install zlib with the following commands; as in the book I will assume you
have extracted the source to a directory and made it current:

./configure --prefix=/tools
make
make install

Install libarchive:

./configure --prefix=/tools --without-xml2 --disable-shared
make
make install

Install fakeroot:

./configure --prefix=/tools --libdir=/tools/usr/lib/libfakeroot \
--with-ipc=sysv
make
make install

Fakeroot needs to know where its libraries are, so edit or create
/tools/etc/ld.so.conf and add the line:

/tools/usr/lib/libfakeroot
and run /tools/sbin/ldconfig

fakeroot also uses the getopt binary from util-linux, so you will need to
build util-linux and copy this binary to /tools/bin.

./configure --prefix=/tools --disable-su --disable-sulogin --disable-login
make

Copy the getopt binary to /tools/bin. Don't do make install, this may
install util-linux to your LFS system, and you don't want to do that yet.

Install pacman:

./configure --prefix=/tools --disable-doc --disable-shared \
--sysconfdir=/etc --localstatedir=/var
make
make install

The above commands install the pacman binaries to the tools directory,
but put the config files and package databases in your final system. This
places the makepkg.conf and pacman.conf config files in /etc; you should
edit these to suit your system. My edits were as follows; your needs may
differ.

For makepkg.conf, be sure that CARCH and CHOST are appropriate. I am using
a 64-bit PC so mine read:

CARCH="x86_64"
CHOST="x86_64-unknown-linux-gnu

I am not entirely certain, but for a 32-bit PC they probably should read:

CARCH="i686"
CHOST="i686-pc-linux-gnu

You can also put yourself in as the "PACKAGER" if you want. I left the rest
of it as is; you may or may not want to make changes.

In pacman.conf, I just commented out the CheckSpace line; you don't have a
valid mtab yet, so pacman crashes when it tries to figure out how much
free space you have.



Stage two: Setting up to build packages with pacman.

If you are getting build files from the Arch website to use/modify to build
your packages with, the .install files that may be included often call
install-info to index installed info docs, and use the full path to do so.
To simplify things, create a link from the file in tools to /usr/bin:

ln -sv /tools/bin/install-info /usr/bin

Something specific to this version of LFS and gcc and related to this is
that you get an "error" when the stock gcc.install file is run on installing
the gcc package. Indeed so, the book instructions for gcc don't build any
info files to install :) Doubtless that will change soon. For now, edit the
file or ignore the error.

You are going to need a regular user to build the packages with. So create
one by adding a users group to the end of /etc/group:

users:x:100:

and a user to the end of /etc/passwd:

bob:x:1000:100:Bob:/home/bob:/bin/bash

Create a home directory for your user and make him the owner:

mkdir /home/bob
chown -R bob:users /home/bob

Chroot another Xterm or VT over to the LFS partition for bob. The command
I used is:

chroot --userspec=1000:100 "$LFS" /tools/bin/env -i \
     HOME=/home/bob                  \
     TERM="$TERM"                \
     PS1='\u:\w\$ '              \
     PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
     /tools/bin/bash --login +h

Note that the 1000:100 in "userspec" is bob's user and group numbers. If
these are not correct or your user isn't named bob, adjust accordingly.

I found it useful to create a builds directory in bob's home directory, and
directories for each package I wanted to install under that. Since the 
pacman
system uses a regular user to build packages, but root (or su/sudo, 
which you
don't have at this stage) for actually installing them, it was easiest to
just chroot an Xterm for root and one for bob.

The chroot command from the book, and the one above, do not have /tools/sbin
as part of the path. I found it necessary to add it to the path to get my
package build of glibc to work, as it needed zic, which is installed there.
So add it to your path if needed (or create a link to zic). This is not an
issue when doing the book install manually, since glibc is installed to the
system first which installs zic to its usual place before it is called.
So do:

echo $PATH

and if /tools/sbin is not there, add it to the end. Do this both for root
and for bob.

PATH=$PATH:/tools/sbin

The gcc tests need the stack size limit set to at least 32768. Bob, as an
unprivileged user, cannot exceed the system limits as set by root. As bob
do:

ulimit -a

and be sure the stack size limit is at least 32768. If not do:

ulimit -Ss 32768
ulimit -a

and be sure the stack size is now 32768. If not, then as root do:

ulimit -s 32768

and try again as bob. The gcc tests may crash if bob cannot do this. You
should also be sure that the command ulimit -Ss 32768 is used in the check
function in your PKGBUILD for gcc.


Stage three: Installing pacman with pacman

Now you are ready to build and install pacman (just pacman for now, not its
dependencies) using pacman. Copy your pacman sources to its build directory.
Include the build files you should have gotten from the Arch website. I used
most of these as is, but you will need to edit the PKGBUILD file at least.

As a rough rule of thumb, a PKGBUILD usually has 4 sections: a number of
"config lines" near the top, a build function, a check function, and a
package function. Also, there are 5 main variables that are generally used:

srcdir, pkgdir - Makepkg creates a subdir of the directory you are building
in to unzip the source to and one to fakeroot your package into, these
variables represent these directories. They are mainly used in commands,
to make things relative to these directories rather than the real system
directories.

pkgname, pkgver, pkgrel - These tell makepkg/pacman the name of the package,
its version number, and the "package build number". So zlib would be
pkgname=zlib, pkgver=1.2.7, and pkgrel=1. If you later build an upgrade
package for some reason from the same source (like maybe a patch was 
issued),
you can just increment the pkgrel.

Here are some relevant portions of my pacman PKGBUILD. I used it largely as
was, but made some changes to suit LFS and where I needed the install to go.
Remember that this is "stage three", you will not want to use this as is in
the final install of pacman.

-----

#groups=('base')
#depends=('bash' 'glibc>=2.15' 'libarchive>=3.0.2' 'curl>=7.19.4'
#         'gpgme' 'pacman-mirrorlist' 'archlinux-keyring')
#makedepends=('asciidoc')
#optdepends=('fakeroot: for makepkg usage as normal user')

I commented out the groups and depends lines. I'm not using package groups,
and as far as pacman knows none of these dependencies are installed. I 
edited
the "source" lines to the files I was going to use. Not every file in the
build directory has to be listed here, but at least the main source file
must be and anything else you want unpacked or automatically copied to
srcdir.

./configure --prefix=/tools --sysconfdir=/etc --localstatedir=/var \
--disable-doc --disable-shared

I changed the configure command to suit. Note that this is exactly as in
stage one above.

I also commented out the check function, since I was not interested in
testing the build at this stage and the tests for pacman won't run without
python anyway. I left the "install Arch specific" stuff in the package
function as it was, since what it did was suitable for my purposes.

-----

Once your files are ready, as user bob, issue this command in the
/home/bob/builds/pacman directory:

makepkg --skipchecksums

This tells makepkg to build your package, and skip checksums on the source;
you don't have the dependencies yet to do the checksums, and your sources
may not match those in the PKGBUILD. If you want you can also feed it the
-c parameter so it cleans up afterward, but it is much, much safer to look
in the pkg directory to see what was actually "installed" and where it went.
If it doesn't look right, fix the problems in PKGBUILD and rebuild before
you actually install it.

Once your package is built, switch over to the "root" terminal, change to
the pacman directory and do:

pacman -U -f pacman-4.0.3-1-x86_64.pkg.tar.gz

The -U parameter tells pacman to add a package from the given filename.
The -f parameter, tells pacman to "force" the install, so it will overwrite
the existing files. You should not normally use this parameter, since if
pacman balks there is probably a good reason for it, but sometimes it is
appropriate and necessary. You may see a few warnings and instructions
for setting pacman up to run for Arch; you can probably ignore these.

This "stage three" install serves two purposes. First, it puts the package
entry for pacman in its database; it will now know what is in its package
and how to remove or upgrade itself. Second, the install does a little more
setup and config of pacman. So you are going to have to edit makepkg.conf
and pacman.conf again. More options and flags should be set this time;
make sure they do what you want. You may want to comment out the compiler
and linker flags to match the book, otherwise make sure they are appropriate
if you understand what they do and desire to use them.

This is a good time to backup your LFS partition.



Stage four: Continuing the book

Install the rest of the packages in the book, except instead of building
and installing packages manually, create build files and do it with pacman.
Build then install each package in the order specified in the book. As a
general rule, use the instructions from the book, but you will need to 
modify
them to work within a PKGBUILD. Get a few Arch build files and compare them
to the book instructions; you will catch on quickly.

I found glibc and udev the hardest to do because they are pretty complicated
installs; not so much the basic build/install as the setup afterward (don't
forget what the .install file is for)  Do some or all of a package manually
if you need to; however, essentially everything in the book can be done with
pacman packages in one way or another. Expect to build a few LFS 
installations
before you get things "perfect." You did back up your partition didn't you?



Stage five: Installing pacman to your final system

Stage five starts immediately after the last package in chapter 6, currently
vim.

If the book hasn't changed much you should have installed zlib and 
util-linux
as part of stage four, so you will probably not need to reinstall these. You
will probably need to reinstall libarchive, fakeroot, and pacman. Create
build files for libarchive and fakeroot (just copy and modify your PKGBUILD
for pacman) and build and reinstall these in order. Keep in mind that these
are not going to the tools directory now, and that you have shared libraries
available. Don't forget to check your makepkg.conf and pacman.conf files
again after the installs. If you want to get, install and build against the
full pacman dependency set, you can, now or later, but be aware that this
requires building and installing about a dozen more packages. Below are the
.configure lines I used in my PKGBUILDS for this stage.

-----

Libarchive:

./configure --prefix=/usr --without-xml2

Fakeroot:

./configure --prefix=/usr --libdir=/usr/lib/libfakeroot \
             --disable-static --with-ipc=sysv

Pacman:

./configure --prefix=/usr --sysconfdir=/etc
             --localstatedir=/var --disable-doc

-----

Note that some of the pacman tests require python to run, so you cannot
test pacman unless you install python first.

Finish chapter six manually.


Stage six: Finishing the book

In my opinion it is easiest to do chapters 7 and 8 manually first. Then,
once your system is up and running correctly, you can make packages if
you want; see the tips section below. If LFS is the only Linux installed
to your system, you will probably have to install grub. Otherwise, most
likely grub is already installed. If it is, find out which partition is
active, look in /boot/grub, and make appropriate modifications to the
config files.


Tips, gotchas, further possibilities:

Arch build files assume that you are building/installing on an Arch system.
Specifically and at least: that you have installed the Arch "base" and
"base-devel" package groups, that the system is configured for Arch and
running, and sometimes that the specific package you are building has been
installed before. Often, these assumptions don't matter, but you may find
that a package has dependencies that are not listed, or that you need to
create a .install file or add a post_install clause to it. Sometimes you
can just copy and use the post_upgrade clause, perhaps with modifications.
And be aware of potential issues such as: I installed util-linux using the
book instructions, but used more or less the Arch ones for shadow. Well, it
turns out the the book instructions for util-linux don't build login, but
the Arch instructions for shadow assumed that util-linux had. So, when I
tried to boot into LFS, I got a username prompt but couldn't login. It
took me a while to figure that one out.

I thought it appropriate to get and install the licenses package from Arch;
this puts many of the common licenses in a handy place for reference. If
you want to do this, get the "binary" package, not the source. Install it
with pacman as the first thing you do in stage four, right before the
linux-api-headers.

In addition to the software packages, you can make other packages to do the
rest of the system setup. If a config file belongs to a specific package,
I put it in that package. For more general setup, I have a "filesystem"
package to make the initial directories and links, a "config files" package
to install basic config files, and a "bootscripts" package. Once you have
your packages, if you want to install from scratch, you probably should 
start
with the "filesystems" package, then your software packages, then the config
files and bootscripts packages. Experiment until you find an order that
works. Pacman can handle dependencies and install order for you or even
install everything with one command if you set things up properly; do some
research if you want to.

I found that the command   expect -c "spawn ls"  did not work for "bob" as a
non-root user; you need it to run some tests. If it doesn't work for bob,
first be sure that it does work for root. If it does work for root but not
bob, try this as root:

chmod 4755 /tools/libexec/pt_chown

If it doesn't work for root either, or this doesn't fix it, there are 
several
things it could be; google around for some things to try.


The instructions in the book assume that standard directories exist on the
system, such as /bin. However, these will not exist under $pkgdir unless
they are created. Sometimes the package "make install" step will do this,
but sometimes it won't. So a command such as

mv -v ${pkgdir}/usr/bin/foo ${pkgdir}/bin

in a PKGBUILD may appear to work, but what it actually does is rename
/usr/bin/foo to bin in the pkgdir / directory if the bin directory isn't
already there. If you then force the install in pacman with -f, you may
kill your /bin directory. The solution is to make the directory first
if needed:

install -dm755 ${pkgdir}/bin
mv -v ${pkgdir}/usr/bin/foo ${pkgdir}/bin

Things such as this are why you want to be cautious about feeding the -f
parameter to pacman. And it is always a good idea when developing package
builds to leave off the -c parameter to makepkg and look in the pkgdir
to see what actually got done before installing a package.


When creating symlinks in a PKGBUILD, usually the link should be created
relative to pkgdir, but point to where the real file will be when installed
to the system.


If makepkg says "Cannot find the gpg binary..." be sure you don't have .sig
or .asc files as part of your source lines in PKGBUILD. These are
signature/check files, and makepkg will try to check your source if these
exist.


The "mpc" package from the book is called "libmpc" by Arch, in case you are
looking for the build files. Some other package is called mpc there.


Be sure to give "bob" a password and further set him up if you want to keep
him; it is probably more secure to move your build directories elsewhere,
delete bob, and add a regular user of your choice to your system in a more
standard way. Be sure to chown your build files or anything else owned by
bob to the new user if you do this.


CHANGELOG:

Ver 1.0 - 3/15/2013
-- 
http://linuxfromscratch.org/mailman/listinfo/hints
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to