Quantcast
Channel: blog.opperschaap.net » chroot
Viewing all articles
Browse latest Browse all 2

chroot with upstart

$
0
0

Upstart

You can find a better version of the chroot script here

According to the following thread on the Ubuntu Forums chrooting into a system which includes upstart will/could cause problems when upgrading packages when they need to be restarted. The logic behind this is extracted from the Karmic release notes:

Upstart jobs cannot be run in a chroot

Upstart jobs cannot be started in a chroot because upstart acts as a service
supervisor, and processes within the chroot are unable to communicate with the
upstart running outside of the chroot (430224). This will cause some packages
that have been converted to use upstart jobs instead of init scripts to fail to
upgrade within a chroot. Users are advised to configure their chroots with
/sbin/initctl pointing to /bin/true, with the following commands run within the
chroot:

dpkg-divert --local --rename --add /sbin/initctl
ln -s /bin/true /sbin/initctl

If you want to undo the action (if you want to boot to the chrooted OS after):

rm /sbin/initctl
dpkg-divert --local --rename --remove /sbin/initctl

I’ve created a script to help me chroot into any OS running on my machines. I’ve added some steps to (un)do the dpkg-divert action:

#!/bin/bash                                                  

mounted=$(mount)
mnt_devices_start="proc dev dev/pts sys var/log home opt"
mnt_devices_stop="opt home var/log dev/pts dev sys proc" 

# Ubuntu
root_disk=/dev/sda2
mnt=/chroot/ubuntu 

# Debian
#root_disk=/dev/sda3
#mnt=/chroot/debian 

resolv="$mnt"/etc/resolv.conf
resolv_o="$resolv.orig"      

start() {
    sudo mkdir -p "$mnt"

    echo -e "$mounted" | grep -q "$mnt"

    if [ $? -ne 0 ] ; then
        sudo mount $root_disk "$mnt"
    fi                              

    local i
    for i in $mnt_devices_start ; do

        echo -e "$mounted" | grep -q "$mnt/$i"
        [ $? -eq 0 ] && continue              

        sudo mount -o bind /$i "$mnt"/$i

    done

    [ ! -e "$resolv_o" ] && sudo mv "$resolv" "$resolv_o"
    sudo cp /etc/resolv.conf "$resolv"

    if [ ! -e "$mnt/sbin/initctl.distrib" ] ; then
        sudo chroot "$mnt" dpkg-divert --local --rename --add /sbin/initctl
        sudo chroot "$mnt" ln -s /bin/true /sbin/initctl
    fi
    sudo chroot "$mnt" bash
}

stop() {

    if [ -e "$mnt/sbin/initctl.distrib" ] ; then
        sudo chroot "$mnt" rm /sbin/initctl
        sudo chroot "$mnt" dpkg-divert --local --rename --remove /sbin/initctl
    fi

    if [ -e "$resolv_o" ] ; then
        sudo rm "$resolv"
        sudo mv "$resolv_o" "$resolv"
    fi

    local i
    for i in $mnt_devices_stop; do
        echo -e "$mounted" | grep -q "$mnt/$i"
        [ $? -ne 0 ] && continue
        sudo umount "$mnt/$i"
    done

    echo -e "$mounted" | grep -q "$mnt"

    if [ $? -eq 0 ] ; then
        sudo umount "$mnt"
        sudo rmdir "$mnt"
    fi
}

case $1 in
    start|stop) $1;;
    *) echo "Usage $(basename $0) " ;;
esac

Viewing all articles
Browse latest Browse all 2

Trending Articles