# Installing/fixing Gentoo with livedvd & chroot

## bedtime

Why another tutorial on chroot? There are so many out there!

Well, there are many tutorials out there but none of them, at least through my searching, seemed to have all of the information in the one post. Many were missing critical information that would have saved a lot of hassle, such as turning swap on, adding the ability to use the network in chroot, or unmounting...

When should I use chroot?

1. You want to have a full suit of easy to use gui tools at your disposal when installing Gentoo, ie; internet browswer, xchat, partition manager...

2. You only have one computer and you don't want it tied down for a day or two whilst packages are being emerged.

3. You've messed something up badly and cannot boot the system.

Does this really work?

I've had success with doing a full Gentoo installation plus installing X, a window manager, all drivers, and several other packages... Only once did I reboot into my new Gentoo install to check if the initial install was okay, which it was. I will not promise this will work for you. You may end up destroying your partitions or ruining your install or just wasting your time. I'll not be responsible! You do this at your own risk!

Note: If you just want to chroot into an already installed system then jump down to the section: I want to chroot into my installed system - How do I do it?.

I'm in! Where do I start?

I'm going to assume you're installing using the Gentoo x86 manual and that you are 'going by the book'. If you've decided to install on different partitions then specified in the book then you'll have to change some steps accordingly to fit your situation. I did the install using the Gentoo 12.1 (at this time) livedvd installation medium which can be found here:

http://mirrors.us.kernel.org/gentoo//releases/x86/

Burn the dvd and boot it up. You'll be starting the install at chapter 4. So open up a browswer and go to this link:

http://www.gentoo.org/doc/en/handbook/handbook-x86.xml?full=1#book_part1_chap4

Open a terminal and follow the handbooks instructions until you get to Chapter 5b: Code Listing 2.5: Unpacking the stage. Enter this command instead:

```
tar --numeric-owner -xvjpf stage3-*.tar.bz2
```

Continue on until you get to Chapter 6a: Copy DNS Info and then enter the following commands instead:

Mount the partitions:

```
mount /dev/sda3 /mnt/gentoo

mount /dev/sda1 /mnt/gentoo/boot

mount -t proc none /mnt/gentoo/proc

mount -o bind /dev /mnt/gentoo/dev
```

Copy DNS info to allow us to use the net within chroot:

```
cp -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf
```

Chroot into the system:

```
chroot /mnt/gentoo /bin/env -i TERM=$TERM /bin/bash

env-update

source /etc/profile

export PS1="(chroot) $PS1"
```

.. resume the install at Chapter 6.b. Configuring Portage and finish the install.

Reboot into the new Gentoo install, that is, without the livedvd, and check to see if all is fine. If something has messed up or you just want to keep using chroot then follow the section below:

I want to chroot into my installed system - How do I do it?

Boot the Gentoo livedvd and open a terminal and browser to this link.

Copy and paste this into the terminal:

```
# Create mounting directories

mkdir /mnt/gentoo

cd /mnt/gentoo

 

# Enter root

sudo -i

 

# Activate swap

swapon /dev/sda2

 

# Mount partitions

mount /dev/sda3 /mnt/gentoo

mount /dev/sda1 /mnt/gentoo/boot

mount -t proc none /mnt/gentoo/proc

mount -o bind /dev /mnt/gentoo/dev

 

# Copy netowrk information so we can use the net inside chroot

cp -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf

 

# Chroot into system

chroot /mnt/gentoo /bin/bash

env-update

source /etc/profile

export PS1="(chroot) $PS1"

 

# Do stuff here:
```

Do whatever you want to do in choot. You will have net access and activated swap, so you can easily merge stuff, replace the kernel, edit files... like you would on an ordinary non-chrooted system. I should note that this chroot guide does not have the ability to run x, but that's not really necessary in most cases anyways.

When you're done simply paste the following into terminal:

```
# Exit chroot:

exit

 

# Unmount

umount /mnt/gentoo/boot /mnt/gentoo/dev /mnt/gentoo/proc /mnt/gentoo

 

# Deactivate swap

swapoff /dev/sda2

 

# Return to user prompt

exit
```

You're done!

If anyone has any suggestions or information to add please post and I'll add it and credit you. I'd love to get X working in chroot but have only got so far as to have X run the Gentoo livedvd's X, and not the X in the chroot environment.

----------

## KeyZerZooZee

Hi thank you for the tutorial.

[My first ever contribution on any Linux/Gentoo forum - what a beautiful day   :Very Happy:  ]

Maybe things have changed somewhat since 2013 regarding the cp command.

When I tried to chroot into my already installed Gentoo, doing the 

 *Quote:*   

> # Copy netowrk information so we can use the net inside chroot
> 
> cp -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf 

  I got:

```
livecd gentoo # cp -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf 

cp: not writing through dangling symlink '/mnt/gentoo/etc/resolv.conf'
```

The solution I got from here: https://askubuntu.com/questions/551537/force-cp-to-copy-on-dangling-symlinks

Witch as a solution suggests to add '--remove-destination' to the cp command:

```
livecd gentoo # cp --remove-destination -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf

```

Have a very nice day   :Cool: 

----------

## KeyZerZooZee

Another small fix for the tutorial:

After chroot into installed system (trying to fix a boot failure resulting in my / mounted ro (as in read only)) I got this error:

```
openpty failed: 'out of pty devices'
```

When I did:

```
emerge -auNDv --with-bdeps=y @world
```

Solution:

I changed:

```
# Mount partitions 

mount -o bind /dev /mnt/gentoo/dev
```

To:

```
mount --rbind /dev mnt/gentoo/dev
```

The solution I found in this post https://forums.gentoo.org/viewtopic-t-725408-start-0.html and waebbl's reply https://forums.gentoo.org/viewtopic-p-6781798.html#6781798:

 *Quote:*   

> I know this post is quite old, but today I ran into the same problem, so there's a chance the post might still be interesting for some people. 
> 
> It happens, although /dev was bind-mounted inside the chroot. I never noticed this before inside a chrooted environment. 
> 
> The key here is to use --rbind instead of --bind, which also rebinds the /dev/pts and /dev/shm filesystems. By exiting the chroot, unmount ${CHROOT}/dev and mount it again using the rbind option, it worked. I was used to use the rbind option, because the handbook uses that option before changing into the chroot, but today I just used the bind option, just because I saw it on a lot of articles on the net.

 

----------

