Using a chroot to manipulate another installation.

Share your own howto's etc. Not for support questions!

Using a chroot to manipulate another installation.

Postby edbarx » 2013-09-21 13:46

Using a chroot in Debian is not as difficult as it sounds, and the power inherent in chroot is amazing to the point of restoring bootability to an otherwise unbootable installation. One such instance can arise if the kernel is uninstalled leaving an installation without it.

The Procedure:

  1. For conformity, create a directory under /mnt say sda2.
    Code: Select all
    mkdir /mnt/sda2
  2. Mount the root partition holding the installation to be modified. I will assume it is: /dev/sda2
    Code: Select all
    mount /dev/sda2 /mnt/sda2
  3. I found that for the chroot to work properly I had to bind the /sys, /proc and /dev directories as follows:
    Code: Select all
    mount -o bind /sys /mnt/sda2/sys
    mount -o bind /proc /mnt/sda2/proc
    mount -o bind /dev /mnt/sda2/dev
  4. Do the actual chroot as follows:
    Code: Select all
    chroot /mnt/sda2 /bin/sh
    If you are going to install packages containing daemons, you may need to prevent them from starting. The Debian Chroot Wiki contains this information.
  5. After finishing with the chroot, exit.
    Code: Select all
  6. Unmount the bound file systems.
    Code: Select all
    umount /mnt/sda2/dev
    umount /mnt/sda2/proc
    umount /mnt/sda2/sys
  7. Finally unmount the partition. If applicable to your case, unmount the other partitions that make part of the repaired installation. Finally, unmount the partition (in our case, sda2).
    Code: Select all
    umount /mnt/sda2

I use this same procedure to configure a new installation after installing it with debootstrap.

Using chroot to repair grub-pc from within a LiveCD session or from within another running session.
The first thing I found about repairing an otherwise badly behaving grub-pc, is by purging it completely together with its configuration files. This means, when I was asked to remove the grub files I replied in the affirmative.

The Procedure

  1. Purge grub-pc together with all its files under /boot.
    Code: Select all
    # apt-get remove --purge grub-pc

    Needless to repeat, answer that you want to delete all traces of grub-pc.
  2. Reinstall grub-pc.
    Code: Select all
    # apt-get install grub-pc

    When asked where to install the first stage select the MBR or the protective MBR in case of GPT.
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.
User avatar
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E

Re: Using a chroot to manipulate another installation.

Postby zilleplus » 2014-03-07 18:23

nice guide, for those who want to do this in a image from a qemu/kvm use this line:

add module:
Code: Select all
modprobe nbd max_part=16

link image to network block device:
Code: Select all
kvm-nbd -c /dev/nbd1 debian.raw

all the rest is the same use /dev/nbd1p1 instead of /dev/sda1.
Posts: 10
Joined: 2013-09-14 13:50

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 3 guests