Difference between revisions of "Change root"

From ArchWiki
Jump to: navigation, search
m (Requirements)
m (Changing Root)
Line 46: Line 46:
  
 
  chroot /mnt/arch /bin/bash
 
  chroot /mnt/arch /bin/bash
 +
 +
{{Note|If you see the error, "<tt>chroot: cannot run command '/bin/bash': Exec format error</tt>" it is likely the two archtectures do not match.}}
  
 
If using a separate {{Filename|/boot}} partition, you must mount this partition now within the chroot environment.  This is necessary for actions like reinstalling GRUB, performing a kernel update, or rebuilding the initramfs image:
 
If using a separate {{Filename|/boot}} partition, you must mount this partition now within the chroot environment.  This is necessary for actions like reinstalling GRUB, performing a kernel update, or rebuilding the initramfs image:
Line 51: Line 53:
 
  mount /dev/<device-or-partition-name> /boot
 
  mount /dev/<device-or-partition-name> /boot
  
To be sure of which environment you are in you can change your bash prompt to show it:
+
To be sure of which environment you are in, you can change your bash prompt to show it:
  
 
  source /etc/profile
 
  source /etc/profile

Revision as of 06:37, 20 March 2010

Changing Root involves the process of changing of the apparent disk root directory (and the current running process and its children) to another root directory. When you change root to another directory you cannot access files and commands outside that directory. This directory is called a chroot jail. Changing root is commonly done for system maintenance tasks for such things as reinstalling GRUB or to reset a forgotten password. Changing root is often done from from a LiveCD or LiveUSB to enter the installation root partition.

Requirements

  • You'll need to boot to another working Linux environment (for example, to a LiveCD or USB flash disk). For Arch install mediums there is both the [LiveCD and USB Flash disk].
  • Root privleges are required before you can chroot.
  • Be sure that the architectures of the Linux environment you have booted into matches the architecture of the root directory you wish to enter (i.e. i686, x86_64).

Mounting the device

First, the device or partition with the Linux system on it will need to be mounted. To discover the kernel name of the storage device name, type:

fdisk -l

Create a directory where you would like to mount the partition, then mount the device or partition:

mkdir /mnt/arch
mount /dev/<device-or-partition-name> /mnt/arch

Changing Root

Before you can chroot be sure that the Linux environment architectures of both the current environment and the environment you wish to enter match (e.g. 32bit LiveCD and 32bit system install). You can find the architecture of your current environment by:

uname -m

If they do, then you will need to mount the temporary filesystems:

mount -t proc none /mnt/arch/proc
mount -t sysfs none /mnt/arch/sys
mount -o bind /dev /mnt/arch/dev

Mount other parts of your filesystem (e.g. Template:Filename, Template:Filename... except Template:Filename) that reside on separate partitions but which you need access to. Generally when you're chrooting to fix something you won't need access to Template:Filename, so you don't need to bother with that:

mount /dev/<device-or-partition-name> /mnt/arch/var

It's possible to mount filesystems after you've chrooted, but it's safer to do so beforehand. The reason is that when you do it after, the outside/kernel environment will not know about the mounted filesystems and could possibly cause damage to the filesystem if not unmounted cleanly.

If you need your static filesystems updated (if you'll be doing anything with GRUB...), you'll need to be sure your {{Filename}/etc/mtab}} is up-to-date:

grep -v rootfs /proc/mounts > /etc/mtab

Now chroot to your installed device or partition and define your shell:

chroot /mnt/arch /bin/bash
Note: If you see the error, "chroot: cannot run command '/bin/bash': Exec format error" it is likely the two archtectures do not match.

If using a separate Template:Filename partition, you must mount this partition now within the chroot environment. This is necessary for actions like reinstalling GRUB, performing a kernel update, or rebuilding the initramfs image:

mount /dev/<device-or-partition-name> /boot

To be sure of which environment you are in, you can change your bash prompt to show it:

source /etc/profile
export PS1="(chroot) $PS1"

Exiting chroot

First the chroot shell must be exited:

exit

Then unmount the temporary filesystems and mounted devices then reboot:

umount /mnt/arch/{proc,sys,dev}

Finally attempt to unmount your hard drive:

   umount /mnt

If you get an error saying that Template:Filename (or any other partition) is busy, this can mean one of two things:

  • A program was left running inside of the chroot.

In the latter case, simply unmount the offending mount point first. To get a reminder of all the current mount points, run Template:Codeline with no parameters. If you still are unable to mount a partition, use the force option:

umount -f /mnt

Finally:

reboot

Resources