Development/Howto/Chroot

From Mandriva

Jump to: navigation, search
Chroot environment creation with Mandriva Linux

Contents

[hide]


[edit]

Physical Location

We'll use $mychroot to be simple. So, first:

# export mychroot=/home/phazeman/chroots
# mkdir $mychroot
[edit]

Create It

You can create a clean chroot environment in one of two ways: by copying an existing clean install, or by using urpmi.

[edit]

Copy a clean install

For this example we use the local system, but you can install a clean system on another partition or computer and copy that instead.

# rsync -av --one-file-system / $mychroot/

If you have separate /usr and /var partitions, redo it for each mount point, except network and special entry. You want only the system as separate:

You have /usr and /var:

for i in "var usr"; do
  mkdir ${mychroot}/${i}
  rsync -av --one-file-system /${i} $mychroot/${i}
done

You'll need to recreate /dev without devfs by remounting / with -o bind and using rsync, or using makedev.

Outside the chroot: mkdir /mnt/disk

mount -o bind / /mnt/disk
rsync -av /mnt/disk/dev/ $mychroot/dev/
umount /mnt/disk
[edit]

Install with urpmi

The problem with urpmi is that it uses the hdlists on the real system, so if you're using X version, you'll create chroot X version. Use only a clean rpm medium and main is enough. First we must create the rpmdb:

# mkdir -p $mychroot/var/lib/rpm
# rpm --initdb --root $mychroot

Now we can install the basesystem and urpmi:

# urpmi --root $mychroot basesystem urpmi

If you are installing by this method, you will also need sshd:

# urpmi --root $mychroot openssh-server

You can set up all of the config files from the real system, but all servers should be started with the chroot command

[edit]

Configure it

[edit]

DNS Setup

Set up a DNS entry if it is needed in $mychroot/etc/resolv.conf

[edit]

Create missing files

On 2005LE, I had to create the following empty files for urpmi to be able to setup new sources:

/etc/fstab
/etc/mtab
/etc/urpmi/urpmi.cfg
/var/lib/urpmi/MD5SUM

I used the touch command:

touch $mychroot/etc/fstab $mychroot/etc/mtab $mychroot/etc/urpmi/urpmi.cfg $mychroot/var/lib/urpmi/MD5SUM

(on a January 2007 Cooker the /etc/fstab and /etc/mtab files are already present)

[edit]

Setup ssh daemon

Change sshd port=22 to some other port, 24 for example, so it doesn't conflict with the real ssh server that might be running on the host machine.

# vi $mychroot/etc/ssh/sshd_config
[edit]

Cleanup

Now, we have to clean the *.pid files out of the chroot's /var/run directory, or else the automatic script will kill some processes

Note: this is fixed in the initscript package as of 9.2

# rm -f $mychroot/var/run/*.pid
[edit]

setup urpmi

Now we can finally enter our new chroot environment and remove all of the urpmi media that were set up for us automatically:

# chroot $mychroot
# urpmi.removemedia -a => zoop, no media

(? this does not work, rather only # urpmi.removemedia -a ??)

Set up cooker urpmi sources using your favorite cooker mirror. The website http://easyurpmi.zarb.org/ has a nice tool for selecting mirrors and easily setting up urpmi sources.

Remove all the scripts for services restart inside the chroot (why do we need to do this? Needs to be clarified):

find /etc/rc.d/rc.* -type l -exec rm -f {} \; and rm -f /var/run/*.pid
[edit]

Start sshd

Now you can start sshd inside the chroot:

# /etc/rc.d/init.d/sshd start

Run pwconv to unshadow password.

Add yourself a user with useradd

Exit the chroot. Now you should be able to login from your main install:

# ssh root@localhost -p N

(where N is the port you set up)

[edit]

Accessing your data

Ah yes.... our home

# mount -o bind /home $mychroot/home
# mount -o bind /proc $mychroot/proc
[edit]

Creating chroot for old versions

Add a new media to urpmi, let's say main-92.

# mkdir -p $mychroot/var/lib/rpm
# rpm --initdb --root $mychroot
# urpmi --root $mychroot --media main-92 basesystem urpmi

You then need to create /dev inside chroot, configure DNS and ssh, create a user,... as explained before.

However, due to a change in the rpm db format, there is an additional step if you are running Mandriva Linux > 9.1 and installing <= 9.1 : you have to convert the database. I do this in an ugly way that is usable only with local mirror or good internet connection as it will download everything once again :

# rpm --root $mychroot -qa --queryformat '%{name}-%{version}-%{release}.%{arch}.rpm\n' > $mychroot/tmp/rpms.list
# chroot $mychroot
# rm -f /var/lib/rpm/*
# rpm --initdb
    do rpm --justdb --noscripts --notriggers -i PATHTOYOURMIRROR(must be local, http or ftp)/$i
  done

Another solution is to use a previously created chroot of Mandrakelinux <= 9.1 to create the new one, I keep a mdk91.tar.bz2 for this purpose.

Created by Nanar (Nanar @ #mandriva on irc.freenode.net). Posted by Phazeman (Phazeman @ #mandriva on irc.freenode.net)

[edit]

Using X Applications in chroot

You can run a cooker application from the chroot in the stable environment of your official Mandriva release when logged in to X locally. To do this, you must perform the following command outside the chroot:

$ xhost +localhost

Then enter the chroot and load your application as in the following example:

$  DISPLAY=localhost:0 /usr/bin/mozilla

That's it. You can run a cooker version of the mozilla web browser inside a rock solid official Mandriva Linux release. A great way to test new software.

Personal tools