Installer et configurer automatiquement une debian sid via debootstrap sur une partition existante.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.5 KiB

#!/usr/bin/env bash
if [ "$(whoami)" != "sid" ]
then
echo "Please execute this script as user sid"
exit 1
fi
cat > "/tmp/$(whoami)_sudo" << EOF
$(whoami) ALL = NOPASSWD: /usr/bin/chown * /mnt/$(whoami)*
$(whoami) ALL = NOPASSWD: /usr/bin/mount * /mnt/$(whoami)*
$(whoami) ALL = NOPASSWD: /usr/sbin/chroot /mnt/$(whoami)*
$(whoami) ALL = NOPASSWD: /usr/sbin/debootstrap * /mnt/$(whoami)*
EOF
add_sid_user()
{
echo "# useradd $(whoami)"
echo "# passwd $(whoami)"
echo "And add sudo rights"
echo "# cp /tmp/$(whoami)_sudo /etc/sudoers.d/$(whoami)"
}
if [ "$(whoami)" != "sid" ]
then
echo "Please execute this script as user sid"
grep -q sid /etc/passwd || add_sid_user
exit 1
fi
target=/mnt/$(whoami)
if ! grep "${target} ext" /etc/mtab
then
echo "Error : mount partition under ${target}"
exit 1
fi
if ! dpkg -l | grep "^ii debootstrap"
then
echo "Error: install debootstrap"
fi
if [ ! -d "${target}/debootstrap" ]
then
if ! sudo /usr/sbin/debootstrap sid "${target}" http://deb.debian.org/debian/
then
echo "Error: please check"
fi
fi
if [ ! -e mount_chroot.sh ]
then
echo "mount_chroot.sh absent"
echo "link it here"
exit 1
fi
# shellcheck source=/dev/null
source mount_chroot.sh "${target}"
echo "--- Creating the ${target}/etc/ Files"
if ! grep " / " "${target}"/etc/fstab
then
echo "--- Creating /etc/fstab"
FSTAB=${target}/tmp/fstab
if [ -e "${FSTAB}" ]
then
rm "${FSTAB}"
fi
blockdevice="$(mount |grep "${target} type ext4" |cut --delimiter=' ' --fields=1 |cut --delimiter='/' --fields=3)"
for link in /dev/disk/by-uuid/*
do
case $(readlink "${link}") in
*${blockdevice})
blockuuid=$(basename "${link}")
if [[ ! "${blockuuid}" =~ ^[a-zA-Z0-9-]+$ ]]
then
echo "Can't init /etc/fstab"
echo "Fix it by ourself"
exit 1
else
cat > "${FSTAB}" << EOSF
UUID=${blockuuid} / ext4 defaults 0 0
EOSF
fi
;;
*)
continue
;;
esac
done
if [ ! -e "${FSTAB}" ]
then
echo "Can't init /etc/fstab"
echo "Fix it by ourself"
exit 1
fi
swapuuid=$(lsblk --output UUID,MOUNTPOINTS |grep SWAP |head -1 |cut --delimiter=' ' --fields=1)
if [[ "${blockuuid}" =~ ^[a-zA-Z0-9-]+$ ]]
then
cat >> "${FSTAB}" << EOSF
UUID=${swapuuid} none swap sw 0 0
EOSF
echo "The first swap partition found of the host was written in /etc/fstab"
echo "Fix it of you have/want another swap"
else
echo "Can't init swap /etc/fstab"
echo "Fix it by ourself"
exit 1
fi
echo "--- Creating /etc/adjtime"
cp --verbose /etc/adjtime "${target}"/tmp/adjtime
echo "--- Creating /etc/network/interfaces.d/lo.conf"
cat > "${target}"/tmp/lo.conf <<"EOSF"
# The loopback network interface
auto lo
iface lo inet loopback
EOSF
echo "--- Creating /etc/network/interfaces.d/eno1.conf"
cat > "${target}"/tmp/eno1.conf <<"EOSF"
auto eno1
iface eno1 inet dhcp
EOSF
echo "--- Creating /etc/resolv.conf"
cp --verbose /etc/resolv.conf "${target}"/tmp/resolv.conf
echo "--- Creating /etc/hostname"
cp --verbose /etc/hostname "${target}"/tmp/hostname
echo "--- Creating /etc/hosts"
cp --verbose /etc/hosts "${target}"/tmp/hosts
echo "--- Creating /etc/locale.conf"
cp --verbose /etc/locale.conf "${target}"/tmp/locale.conf
echo "--- Creating /etc/default/keyboard"
cp --verbose /etc/default/keyboard "${target}"/tmp/keyboard
echo "--- Creating sudofile executable"
sudofile=/tmp/mnt_$(whoami)_etc_files.sh
cat > "${sudofile}" << "EOSF"
cp --verbose /tmp/fstab /etc/fstab
cp --verbose /tmp/adjtime /etc/adjtime
cp --verbose /tmp/lo.conf /etc/network/interfaces.d/lo.conf
cp --verbose /tmp/eno1.conf /etc/network/interfaces.d/eno1.conf
cp --verbose /tmp/resolv.conf /etc/resolv.conf
cp --verbose /tmp/hostname /etc/hostname
cp --verbose /tmp/hosts /etc/hosts
cp --verbose /tmp/locale.conf /etc/locate.conf
cp --verbose /tmp/keyboard /etc/default/keyboard
apt update
echo "root:$(basename $(ls -1 /etc/s*w))" | chpasswd
apt --yes install linux-image-amd64 ssh
apt --yes install locales console-setup
EOSF
echo "bash mount_chroot.sh ${target} ${sudofile}"
if ! bash mount_chroot.sh "${target}" "${sudofile}"
then
echo "Configuration error"
exit 1
fi
fi