Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Bash by Askar Safin ( 15 years ago )
#!/bin/bash -e

[ $# != 0 ] && echo "Usage: $(basename "$0")" && exit 1

[ "$(id -u)" != 0 ] && echo "$(basename "$0"): You are not root" && exit 1

[ -e /target ] && echo "$(basename "$0"): /target exists" && exit 1

type mount-all > /dev/null 2> /dev/null || { echo "$(basename "$0"): mount-all: not found"; exit 1; }
type safe-mount > /dev/null 2> /dev/null || { echo "$(basename "$0"): safe-mount: not found"; exit 1; }
type umount-all > /dev/null 2> /dev/null || { echo "$(basename "$0"): umount-all: not found"; exit 1; }

if [ -f /tmp/install-debian-answers ]; then
        . /tmp/install-debian-answers
else
        while :; do
                read -ep 'install, copy or configure: ' WHAT
                { [ "$WHAT" = install ] || [ "$WHAT" = copy ] || [ "$WHAT" = configure ]; } && break
        done

        if [ $WHAT = install ]; then
                [ -f /usr/sbin/debootstrap ] || { echo "$(basename "$0"): /usr/sbin/debootstrap: not found"; exit 1; }
                ping -c 1 mirror.yandex.ru > /dev/null || exit 1
                type clean > /dev/null 2> /dev/null || { echo "$(basename "$0"): clean: not found"; exit 1; }
        fi

        if [ $WHAT = configure ]; then
                [ -f /usr/bin/apt-get ] || { echo "$(basename "$0"): /usr/bin/apt-get: not found"; exit 1; }
        fi
                                                                                                                                                                                                
                                                                                                                                                                                                
        [ $WHAT = copy ] && while :; do
                read -ep 'From: ' FROM
                if [ -f "$FROM/usr/sbin/dpkg-reconfigure" ]; then
                        break
                else
                        echo "$(basename "$0"): $FROM/usr/sbin/dpkg-reconfigure: not found"
                fi
        done

        [ $WHAT = install ] && while :; do
                read -ep 'Path to utils: ' UTILS
                if [ -d "$UTILS" ] && [ -f "$UTILS"/*akefile ]; then
                        break
                else
                        echo "$(basename "$0"): Bad utils directory"
                fi
        done

        while :; do
                read -ep 'hard or flash: ' DISK
                { [ "$DISK" = hard ] || [ "$DISK" = flash ]; } && break
        done

        while :; do
                read -ep 'Device: ' DEV
                DEVICE="$(readlink -f "$DEV")"
                if [[ "$DEVICE" =~ ^/dev/[a-z][a-z][a-z][0-9]$ ]]; then
                        break
                else
                        echo "$(basename "$0"): Device must be /dev/xxxx"
                fi
        done

        [ $DISK = hard ] && while :; do
                read -ep '/root device: ' DEV
                ROOT_DEVICE="$(readlink -f "$DEV")"
                if [[ "$ROOT_DEVICE" =~ ^/dev/[a-z][a-z][a-z][0-9]$ ]]; then
                        break
                else
                        echo "$(basename "$0"): Device must be /dev/xxxx"
                fi
        done

        [ $WHAT = install ] && while :; do
                read -ep 'i386 or amd64: ' ARCH
                { [ "$ARCH" = i386 ] || [ "$ARCH" = amd64 ]; } && break
        done

        while :; do
                read -ep 'debian or ubuntu: ' DISTRO
                { [ "$DISTRO" = debian ] || [ "$DISTRO" = ubuntu ]; } && break
        done

        read -ep 'Host: ' -i "$(if [ $WHAT = copy ]; then cat "$FROM/etc/hostname"; else cat /etc/hostname; fi)" NEW_HOST

        printf 'Color digit ('
        for((i = 1; i <= 6; ++i)){ printf "\033[1;3${i}m$i"; }
        printf '\033[0m): '
        read COLOR

        cat > /tmp/install-debian-answers <<- EOF
        WHAT='$WHAT'
        FROM='$FROM'
        UTILS='$UTILS'
        DISK='$DISK'
        DEVICE='$DEVICE'
        ROOT_DEVICE='$ROOT_DEVICE'
        ARCH='$ARCH'
        DISTRO='$DISTRO'
        NEW_HOST='$NEW_HOST'
        COLOR='$COLOR'
        EOF
fi

printf 'Ready. Press "Enter" to continue...'
read A
mkdir /target
safe-mount $DEVICE /target
rm -f /target/log

if [ $DISTRO = debian ]; then
        SUITE=squeeze
else
        SUITE=lucid
fi

if [ $DISK = hard ]; then
        mkdir -p /target/root
        echo '. ~/.bashrc' > /target/root/.profile
        echo "PS1='\[\033[1;41m\]/root not mounted\[\033[0m\] \[\033[1;3$COLOR""m\]\l:\w\\\$\[\033[0m\] '" > /target/root/.bashrc
        mount $ROOT_DEVICE /target/root
fi

if [ $WHAT = install ]; then
        if [ $DISTRO = debian ]; then
                LOCALES=locales
        else
                LOCALES=language-pack-en
        fi

        if [ $DISTRO = debian ]; then
                if [ $ARCH = i386 ]; then
                        LINUX_IMAGE=linux-image-686
                else
                        LINUX_IMAGE=linux-image-amd64
                fi
        else
                LINUX_IMAGE=linux-image-generic
        fi

        debootstrap --arch=$ARCH --include=$LOCALES,$LINUX_IMAGE,aptitude,g++,make,libreadline-dev,grub-pc,network-manager,pppoeconf,lynx $SUITE /target http://mirror.yandex.ru/$DISTRO
        echo debootstrap > /target/log
elif [ $WHAT = copy ]; then
        for FILE in "$FROM/bin" "$FROM/boot" "$FROM/etc" "$FROM/home" "$FROM/initrd.img" "$FROM"/lib* "$FROM/opt" "$FROM/root" "$FROM/sbin" "$FROM/selinux" "$FROM/srv" \
                "$FROM/usr" "$FROM/var" "$FROM/vmlinuz"; do
                printf "$(basename "$0"): $FILE... "
                cp -r "$FILE" /target
                echo OK
                echo "$FILE" >> /target/log
        done
        ln -s media/cdrom /target/cdrom
        mkdir /target/dev
        mkdir /target/media
        mkdir /target/media/cdrom
        mkdir /target/mnt
        mkdir /target/proc
        mkdir /target/sys
        mkdir /target/tmp
        chmod a+rwx /target/tmp
fi

mount-all /target
chroot /target /usr/bin/passwd -d root

cat > /target/etc/apt/apt.conf << "EOF"
Apt::AutoRemove::RecommendsImportant "false";
Apt::Install-Recommends "false";
EOF

cat > /target/etc/fstab << EOF
# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system>                            <mount point>            <type>  <options>            <dump>  <pass>
proc                                       /proc                    proc    nodev,noexec,nosuid  0       0
UUID=$(blkid -o value -s UUID $DEVICE)  /                        $(blkid -o value -s TYPE $DEVICE)    errors=remount-ro    0       1
tmpfs                                      /tmp                     tmpfs   defaults             0       0
EOF

[ $DISK = hard ] && cat >> /target/etc/fstab << EOF
UUID=$(blkid -o value -s UUID $ROOT_DEVICE)  /root                    $(blkid -o value -s TYPE $ROOT_DEVICE)    defaults             0       2
EOF

[ $DISK = flash ] && [ $DISTRO = debian ] && cat >> /target/etc/fstab << "EOF"
tmpfs                                      /var/cache/apt/archives  tmpfs   defaults             0       0
EOF

cat > /target/etc/default/locale << "EOF"
LANG="en_US.UTF-8"
LANGUAGE="en_US:en"
EOF

if [ $WHAT = install ]; then
        if [ $DISTRO = debian ]; then
                sed -i 's/\\h:\\w\\\$/\\\[\\033\[1;3'"$COLOR"'m\\\]\\l:\\w\\\$\\\[\\033\[00m\\\]/' /target/root/.bashrc
                sed -i 's/^# PS1=/PS1=/' /target/root/.bashrc
                sed -i "s/^# export LS_OPTIONS='--color=auto'\$/export LS_OPTIONS='--color=auto'/" /target/root/.bashrc
                sed -i 's/^# eval "`dircolors`"$/eval "`dircolors`"/' /target/root/.bashrc
                sed -i 's/^# alias ls=/alias ls=/' /target/root/.bashrc
        else
                sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/' /target/root/.bashrc
                sed -i 's/\\\[\\033\[01;32m\\\]\\u@\\h\\\[\\033\[00m\\\]:\\\[\\033\[01;34m\\\]\\w\\\[\\033\[00m\\\]\\\$/\\\[\\033\[1;3'"$COLOR"'m\\\]\\l:\\w\\\$\\\[\\033\[00m\\\]/' /target/root/.bashrc
                sed -i 's/\\u@\\h:\\w\\\$/\\l:\\w\\\$/' /target/root/.bashrc
        fi
else
        sed -i 's/\\\[\\033\[[0-9;]*m\\\]\\l:\\w\\\$\\\[\\033\[00m\\\]/\\\[\\033\[1;3'"$COLOR"'m\\\]\\l:\\w\\\$\\\[\\033\[00m\\\]/' /target/root/.bashrc
fi

grep -q safe-mount /target/root/.bashrc || echo alias mount=safe-mount >> /target/root/.bashrc

grep -q HOME/bin /target/root/.profile || cat >> /target/root/.profile << "EOF"
export PATH="$HOME/bin:$PATH"
EOF

cat > /target/etc/rc.local << "EOF"
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

EOF
[ $DISK = hard ] && echo 'HOME=/root /root/bin/daem dropbox start' >> /target/etc/rc.local

[ $DISTRO = ubuntu ] && {
        grep -q Desktop /target/etc/profile || {
                echo 'rm -f /root/Desktop/.directory' >> /target/etc/profile
                echo '[ -d /root/Desktop ] && rmdir /root/Desktop' >> /target/etc/profile
        }
}

if [ $DISTRO = debian ]; then
        for((i = 1; i <= 5; ++i)){
                sed -i "s~/sbin/getty.*tty$i\$~/bin/login root < /dev/tty$i > /dev/tty$i 2>\&1~" /target/etc/inittab
        }
else
        for((i = 1; i <= 5; ++i)){
                sed -i "s~/sbin/getty.*tty$i\$~/bin/login root < /dev/tty$i > /dev/tty$i 2>\&1~" /target/etc/init/tty$i.conf
        }
fi

cat > /target/usr/local/bin/g++ << "EOF"
#!/bin/sh
exec /usr/bin/g++ -W -Wall -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -fstack-protector-all -Wno-format -Wno-long-long -Wfloat-equal -Wpointer-arith -Wwrite-strings -Wcast-align -Woverloaded-virtual "$@"
EOF
chmod +x /target/usr/local/bin/g++

if [ -f /target/etc/hosts ]; then
        sed -i 's/^127\.0\.1\.1.*$/127.0.1.1       '"$NEW_HOST/" /target/etc/hosts
else
        echo "127.0.1.1 $NEW_HOST" > /target/etc/hosts
fi
echo "$NEW_HOST" > /target/etc/hostname

[ $DISTRO = debian ] && cat > /target/etc/init.d/rcS << EOF
#!/bin/bash
#
# rcS
#
# Call all S??* scripts in /etc/rcS.d/ in numerical/alphabetical order
#

fast_shell_eval&#40;&#41;{
        while read -ep \$'\033[1;34mfast-shell: '"\$1>"\$'\033[0m ' FAST_SHELL_COMMAND; do
                eval "\$FAST_SHELL_COMMAND";
        done
}

/bin/login root < /dev/tty12 > /dev/tty12 2>&1 &

if read -t 1 -p \$'\033[1;34mfast-shell: press "Enter" to enter login shell...\033[0m'; then
        /bin/mount -o remount,rw / || printf '\033[1;34mfast-shell: cannot remount / for read and write\033[0m\n'
        /bin/mount $ROOT_DEVICE /root || printf '\033[1;34mfast-shell: cannot mount /root\033[0m\n'
        if /bin/login root; then
                if /bin/umount /root; then
                        /bin/mount -o remount,ro / || fast_shell_eval 'cannot remount / for read only'
                else
                        fast_shell_eval 'cannot unmount /root'
                fi
        else
                fast_shell_eval 'cannot enter login shell'
        fi
else
        echo
fi

exec /etc/init.d/rc S
EOF

echo 'configuration files' >> /target/log

if [ $WHAT = install ]; then
        if [ $DISTRO = ubuntu ]; then
                echo "deb http://mirror.yandex.ru/ubuntu $SUITE main universe" > /target/etc/apt/sources.list
                chroot /target /usr/bin/apt-get update
        fi

        chroot /target /usr/bin/apt-get -y --force-yes install madwimax console-cyrillic mc gpm
        echo 'universe packages' >> /target/log
fi

if [ $DISTRO = debian ]; then
        cat > /target/etc/apt/sources.list <<- EOF
        deb     http://mirror.yandex.ru/debian           $SUITE           main contrib non-free
        deb-src http://mirror.yandex.ru/debian           $SUITE           main contrib non-free

        deb     http://mirror.yandex.ru/debian           $SUITE-updates   main contrib non-free
        deb-src http://mirror.yandex.ru/debian           $SUITE-updates   main contrib non-free

        deb     http://mirror.yandex.ru/debian-security  $SUITE/updates   main contrib non-free
        deb-src http://mirror.yandex.ru/debian-security  $SUITE/updates   main contrib non-free

        deb     http://mirror.yandex.ru/debian-backports $SUITE-backports main contrib non-free
        deb-src http://mirror.yandex.ru/debian-backports $SUITE-backports main contrib non-free
        EOF
else
        cat > /target/etc/apt/sources.list <<- EOF
        deb     http://mirror.yandex.ru/ubuntu           $SUITE           main restricted universe multiverse
        deb-src http://mirror.yandex.ru/ubuntu           $SUITE           main restricted universe multiverse

        deb     http://mirror.yandex.ru/ubuntu           $SUITE-updates   main restricted universe multiverse
        deb-src http://mirror.yandex.ru/ubuntu           $SUITE-updates   main restricted universe multiverse

        # deb     http://mirror.yandex.ru/ubuntu           $SUITE-backports main restricted universe multiverse
        # deb-src http://mirror.yandex.ru/ubuntu           $SUITE-backports main restricted universe multiverse

        # deb     http://archive.canonical.com/ubuntu      $SUITE           partner
        # deb-src http://archive.canonical.com/ubuntu      $SUITE           partner

        # deb     http://extras.ubuntu.com/ubuntu          $SUITE           main
        # deb-src http://extras.ubuntu.com/ubuntu          $SUITE           main

        deb     http://security.ubuntu.com/ubuntu        $SUITE-security  main restricted universe multiverse
        deb-src http://security.ubuntu.com/ubuntu        $SUITE-security  main restricted universe multiverse
        EOF
fi

[ $DISTRO = ubuntu ] && [ $DISK = hard ] && echo deb http://linux.dropbox.com/ubuntu $SUITE main > /target/etc/apt/sources.list.d/dropbox.list

if [ $WHAT = install ]; then
        chroot /target /usr/bin/apt-get update
        echo update >> /target/log
fi

if [ $WHAT = copy ]; then
        chroot /target /usr/sbin/dpkg-reconfigure grub-pc
        echo dpkg-reconfigure grub-pc >> /target/log
fi

if [ $WHAT = install ]; then
        chroot /target /usr/sbin/dpkg-reconfigure console-cyrillic
        echo dpkg-reconfigure console-cyrillic >> /target/log
fi

if [ $WHAT = install ]; then
        cp -rT "$UTILS" /target/utils
        cd /target/utils
        clean
        mkdir -p /target/root/bin
        PATH=/usr/bin:/bin HOME=/root chroot /target make -C /utils
        rm -r /target/utils
        echo utils >> /target/log
fi

rm /target/log
umount-all /target
umount /target
rmdir /target
echo "$(basename "$0"): OK"

 

Revise this Paste

Your Name: Code Language: