Debootstrap

From campisano.org
Jump to navigation Jump to search

Debian chrooted

Debian 9 "Stretch"

# stretch amd64 example
#### set host and guest options
export TARGET=stretch       # debian guest version
export ARCH=amd64           # the guest target must be the same of the host
export VARIANT=minbase      # minbase is the minimal debian environment
export MIRROR=http://ftp.debian.org/debian/     # debian mirror to use
#### start
apt-get update
apt-get install debootstrap
export DEST_DIR=DEBIAN_"$TARGET"_"$ARCH"
export LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:/lib/i386-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/i386-linux-gnu
debootstrap --arch "$ARCH" --variant="$VARIANT" "$TARGET" /srv/"$DEST_DIR" $MIRROR
cat /etc/hostname > ${DEST_DIR}/etc/hostname
echo 127.0.0.1       localhost `cat /etc/hostname` > ${DEST_DIR}/etc/hosts
cat /etc/resolv.conf > ${DEST_DIR}/etc/resolv.conf
mkdir -p ${DEST_DIR}/dev/pts
mount -o bind /proc ${DEST_DIR}/proc
mount -o bind /sys ${DEST_DIR}/sys
mount -o bind /dev/pts ${DEST_DIR}/dev/pts
chroot ${DEST_DIR} env -i LC_ALL=C TERM="$TERM" HOME=/root TARGET=$TARGET MIRROR=$MIRROR bash --login --posix
################################################################################
#### inside chroot env
echo "test `id -u` -eq 0 && PS1='\u@CHROOT:\w# ' || PS1='\u@CHROOT:\w$ '" >> /etc/profile
echo "source /etc/profile" >> ~/.bashrc
source /etc/profile
passwd --delete root
echo none / auto rw 0 0 > /etc/mtab
#### OPTIONAL: prevent services startup on install (it depends of the use of chrooted system)
echo '#!/bin/sh' > /usr/sbin/policy-rc.d
echo 'exit 101' >> /usr/sbin/policy-rc.d
chmod 0755 /usr/sbin/policy-rc.d
#### fix ischroot command to be able to detect chroot environment
dpkg-divert --divert /usr/bin/ischroot.debianutils --rename /usr/bin/ischroot
ln -s /bin/true /usr/bin/ischroot
#### configuring apt sources
echo '#'" $TARGET"  > /etc/apt/sources.list
echo "deb $MIRROR $TARGET main contrib non-free" >> /etc/apt/sources.list
echo "deb $MIRROR ${TARGET}-updates main contrib non-free" >> /etc/apt/sources.list
echo "" >> /etc/apt/sources.list
echo '#'" security" >> /etc/apt/sources.list
echo "deb http://security.debian.org/ ${TARGET}/updates main contrib non-free" >> /etc/apt/sources.list
apt-get update
#### install dialog console interface
apt-get install dialog
#### configuring locales and time
apt-get install locales tzdata
dpkg-reconfigure locales        # choose en_US.UTF8, None default
dpkg-reconfigure tzdata         # choose Etc -> UTC
echo "export LANG=en_US.UTF-8 LANGUAGE=en_US.UTF-8" >> /etc/profile
source /etc/profile
#### update the system
apt-get dist-upgrade
#### install basic system tools
apt-get install binutils nano psmisc lsof findutils grep less tar gzip bzip2 procps iptables kmod wget
# optionally1: apt-get install cron exim4
# optionally2: apt-get install dnsutils mutt man nmap
apt-get clean
cat /dev/null > /root/.bash_history && history -c && exit       # clear history and exit chroot
################################################################################
#### outside chroot env
umount ${DEST_DIR}/dev/pts
umount ${DEST_DIR}/sys
umount ${DEST_DIR}/proc
#### OPTIONAL: make a backup of the resulting system
tar -czf "bkp_${DEST_DIR}_`date +%Y-%m-%d`.tgz" $DEST_DIR       # make a backup ~80MB

Previous versions

Debian 8 "Jessie"

# JESSIE amd64 example
### setting up
export TARGET=jessie
export ARCH=amd64
export VARIANT=minbase
export DEST_DIR=DEBIAN_"$TARGET"_"$ARCH"
### start
apt-get update
apt-get install debootstrap
LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:/lib/i386-linux-gnu debootstrap --arch "$ARCH" --variant="$VARIANT" "$TARGET" /srv/"$DEST_DIR" http://ftp.debian.org/debian/
ln -s "$DEST_DIR" /srv/CHROOT
cat /etc/resolv.conf > /srv/CHROOT/etc/resolv.conf
cat /etc/hostname > /srv/CHROOT/etc/hostname
echo 127.0.0.1       localhost `cat /etc/hostname` > /srv/CHROOT/etc/hosts
mount -o bind /proc /srv/CHROOT/proc
mount -o bind /sys /srv/CHROOT/sys
mount -o bind /dev/pts /srv/CHROOT/dev/pts
chroot /srv/CHROOT
### inside chroot env
passwd --delete root
echo none / auto rw 0 0 > /etc/mtab
export LC_ALL=C LANGUAGE=C LANG=C
### prevent services startup on install # NO MORE NEEDED
#echo -e \#\!/bin/sh > /usr/sbin/policy-rc.d
#echo -e exit 101 >> /usr/sbin/policy-rc.d
#chmod a+x ./usr/sbin/policy-rc.d
### configuring apt sources
echo -e \# "$TARGET"  > /etc/apt/sources.list
echo deb http://ftp.debian.org/debian/ "$TARGET" main contrib non-free >> /etc/apt/sources.list
echo deb http://ftp.debian.org/debian/ "$TARGET"-updates main contrib non-free >> /etc/apt/sources.list
echo -e \\n\# security >> /etc/apt/sources.list
echo deb http://security.debian.org/ "$TARGET"/updates main contrib non-free >> /etc/apt/sources.list
apt-get update
apt-get install dialog
apt-get dist-upgrade
### configuring locales and time
apt-get install locales
dpkg-reconfigure locales # choose en_US.UTF8, None default
dpkg-reconfigure tzdata # choose None of the above -> UTC
echo "export LC_ALL=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8" >> /etc/profile
echo "test `id -u` -eq 1 && PS1='\u@CHROOT:\w\# ' || PS1='\u@CHROOT:\w\$ '" >> /etc/profile
echo "source /etc/profile" >> ~/.bashrc
### install basic system tools
bash # to apply locales
apt-get install binutils nano psmisc lsof findutils grep less tar gzip bzip2 module-init-tools iptables procps cron
# optionally: apt-get install wget dnsutils mutt man nmap
apt-get clean
exit # bash
history -c
exit # chroot
### outside chroot env
umount /srv/CHROOT/dev/pts
umount /srv/CHROOT/sys
umount /srv/CHROOT/proc

Debian 7 "Wheezy"

# WHEEZY amd64 example
apt-get install debootstrap
LD_LIBRARY_PATH=/lib/x86_64-linux-gnu:/lib/i386-linux-gnu debootstrap --arch amd64 --variant=minbase wheezy /srv/DEBIAN_wheezy_amd64 http://http.debian.net/debian/
ln -s DEBIAN_wheezy_amd64 /srv/CHROOT
cp -a /etc/resolv.conf /srv/CHROOT/etc
echo -n MY_HOSTNAME > /srv/CHROOT/etc/hostname
mount -o bind /proc /srv/CHROOT/proc
mount -o bind /sys /srv/CHROOT/sys
mount -o bind /dev/pts /srv/CHROOT/dev/pts
chroot /srv/CHROOT
### inside chroot env
passwd --delete root
echo none / auto rw 0 0 > /etc/mtab
export LC_ALL=C LANGUAGE=C LANG=C
### prevent services startup on install
echo -e \#\!/bin/sh > /usr/sbin/policy-rc.d
echo -e exit 101 >> /usr/sbin/policy-rc.d
chmod a+x ./usr/sbin/policy-rc.d
### configuring apt sources
echo -e \# wheezy  > /etc/apt/sources.list
echo deb http://http.debian.net/debian/ wheezy main contrib non-free >> /etc/apt/sources.list
echo deb http://http.debian.net/debian/ wheezy-updates main contrib non-free >> /etc/apt/sources.list
echo -e \\n\# security >> /etc/apt/sources.list
echo deb http://security.debian.org/ wheezy/updates main contrib non-free >> /etc/apt/sources.list
apt-get update
apt-get dist-upgrade
### configuring locales and time
apt-get install dialog
apt-get install locales
dpkg-reconfigure locales # choose en_US.UTF8
dpkg-reconfigure tzdata # choose None of the above -> UTC
echo "export LC_ALL=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8" >> /etc/profile
export LC_ALL=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8
### install basic system tools
apt-get install binutils nano psmisc lsof findutils grep less tar gzip bzip2 wget dnsutils module-init-tools iptables procps mutt
apt-get clean
exit
### outside chroot env
umount /srv/CHROOT/dev/pts
umount /srv/CHROOT/sys
umount /srv/CHROOT/proc

Debian 6 "Squeeze"

root@host:~# apt-get install debootstrap
root@host:~# debootstrap --arch i386 squeeze /srv/DEBIAN_squeeze_i386 http://ftp.us.debian.org/debian
root@host:~# ln -s DEBIAN_squeeze_i386 /srv/CHROOT
root@host:~# cp -a /etc/resolv.conf /srv/CHROOT/etc
root@host:~# echo -n stablei386 > /srv/CHROOT/etc/hostname
root@host:~# mount -o bind /proc /srv/CHROOT/proc
root@host:~# mount -o bind /sys /srv/CHROOT/sys
root@host:~# mount -o bind /dev/pts /srv/CHROOT/dev/pts
root@host:~# chroot /srv/CHROOT
stablei386:/# echo none / auto rw 0 0 > /etc/mtab
stablei386:/# export LC_ALL=C LANGUAGE=C LANG=C
stablei386:/# apt-get install locales
stablei386:/# dpkg-reconfigure locales # choose en_US.UTF-8
stablei386:/# echo "export LC_ALL=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8" >> /etc/profile
stablei386:/# export LC_ALL=en_US.UTF-8 LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8
stablei386:/# apt-get install binutils nano nmap psmisc lsof findutils grep less tar gzip bzip2 wget dnsutils
stablei386:/# apt-get clean
stablei386:/# exit
root@host:~# umount /srv/CHROOT/dev/pts
root@host:~# umount /srv/CHROOT/sys
root@host:~# umount /srv/CHROOT/proc

References