Debootstrap

From campisano.org
Jump to navigation Jump to search

Debian minimal distribution

Debian 10 "Buster"

  • A minimal Debian system root folder can be downloaded from any debian-based machine with same arch of the final host machine (for instance amd64)
########################################
#### FROM ANY DEBIAN-BASED MACHINE WITH SAME ARCH (e.g. amd64)
# debian base image creation
su -
apt-get install debootstrap systemd-container
#### define Debian flavors
export TARGET=buster                            # 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
export BASE_PATH=/srv
export DEST_DIR=DEBIAN_${TARGET}_${ARCH}
#### get
cd ${BASE_PATH}
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} --include=init,apt,nano,systemd-container ${TARGET} ${BASE_PATH}/${DEST_DIR} ${MIRROR}      # produce a folder of ~190MB
rm -f ${BASE_PATH}/${DEST_DIR}/var/cache/apt/archives/*.deb
systemd-nspawn --directory=${BASE_PATH}/${DEST_DIR} passwd --delete root
#### make a backup of the downloaded system, if you want to test changes
# REMEMBER: to restore, use tar --numeric-owner -xzf <FILENAME>
tar -czf "bkp_${DEST_DIR}_debootstrapped_`date +%Y-%m-%d`.tgz" ${DEST_DIR}      # make a backup of ~70MB
ls -lsh --color
du -hs ${BASE_PATH}/${DEST_DIR}
  • Such minimal Debian root folder can be ran and configured from any other host machine sharing the same arch (e.g. amd64):
########################################
#### FROM THE DESTINATION HOST WITH SAME ARCH (e.g. amd64)
# define guest Debian root folder destination in the host machine
export BASE_PATH=/srv
export DEST_DIR=DEBIAN_buster_amd64
## run the system in one terminal
cd ${BASE_PATH}
tar --numeric-owner -xzf bkp_"${DEST_DIR}"_debootstrapped_*.tgz
# apt-get -y install systemd-container || yum install systemd.x86_64            # command examples to install systemd-nspawn 
systemd-nspawn --boot --directory=${BASE_PATH}/${DEST_DIR}                      # use 'root' user to login
########################################
#### INSIDE SYSTEMD GUEST ENV
echo 'pts/0' >> /etc/securetty
echo 'pts/1' >> /etc/securetty
apt-get -y update
apt-get -y install dialog locales tzdata
dpkg-reconfigure locales        # choose en_US.UTF8 and set en_US.UTF8 as default
dpkg-reconfigure tzdata         # choose Etc -> UTC
cat >> ~/.profile << 'EOF'
test 0 -eq 1 && PS1='\u@SYSTEMD:\w\# ' || PS1='\u@SYSTEMD:\w$ '
umask 0027
export HISTTIMEFORMAT='%F %T '
export HISTSIZE=10000
export HISTFILESIZE=10000
export HISTCONTROL=ignorespace
alias cp="cp -i"
alias mv="mv -i"
alias rm="rm -i"
EOF
login root                              # login again to use the changes above
apt-mark auto `apt-mark showmanual`     # mark all packages as "not requested by the user"
apt-get -y install apt bash binutils bsdutils bzip2 coreutils debconf dialog dpkg findutils grep gzip init iptables kmod less libc-bin locales login lsof mount nano passwd procps psmisc readline-common rsyslog sed systemd sysvinit-utils tar util-linux
apt-get -y autoremove --purge           # remove all "not requested" packages
apt-get -y dist-upgrade
apt-get -y clean
rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
sync
exit
shutdown -h now
#### make a backup of the configured system, if you want to test changes
# REMEMBER: to restore, use tar --numeric-owner -xzf <FILENAME>
tar -czf "bkp_${DEST_DIR}_debootstrapped_configured_`date +%Y-%m-%d`.tgz" ${DEST_DIR}      # make a backup of ~80MB
ls -lsh --color
du -hs ${BASE_PATH}/${DEST_DIR}

References

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 curl
# 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