Skip to content

Instantly share code, notes, and snippets.

@mistificator
Created July 31, 2019 11:07
Show Gist options
  • Save mistificator/5dfccc415c881b0afac3b5d71cfd918a to your computer and use it in GitHub Desktop.
Save mistificator/5dfccc415c881b0afac3b5d71cfd918a to your computer and use it in GitHub Desktop.
#!/bin/sh
if [ -x "$(command -v tput)" ]; then
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
fi
echo Welcome to ${green}Clean Debian${reset} - one-script Debian installer from recovery console.
echo Version 1.0
echo "(c) 2019 Mist Poryvaev"
distro_name=$1
mount_point=/mnt/clean_debian
distro_path=/cdrom
destination_device=/dev/sda
if [ -z "$1" ]; then
echo ${green}Usage:${reset} clean_debian.sh distro_name [distro_path=$distro_path] [destination_device=$destination_device]
echo ${red}No distro name specified${reset}
echo Available distro:
ls $distro_path/dists
exit 1
fi
umount $mount_point > /dev/null 2>&1
rm -R $mount_point > /dev/null 2>&1
mkdir -p $mount_point
if [ -n "$2" ]; then
distro_path=$2
fi
if [ -n "$3" ]; then
destination_device=$3
fi
echo ${green}Ready to install system${reset}
echo Distro $distro_name, path $distro_path, mount point $mount_point, destination device $destination_device
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Prepare disk${reset}
# parted $destination_device rm 4
# parted $destination_device rm 3
# parted $destination_device rm 2
# parted $destination_device rm 1
parted $destination_device mklabel gpt
parted $destination_device mkpart primary 1MiB 3MiB
parted $destination_device name 1 grub
parted $destination_device set 1 bios_grub on
parted $destination_device mkpart primary 3MiB 131MiB
parted $destination_device name 2 boot
parted $destination_device mkpart primary 131MiB 643MiB
parted $destination_device name 3 swap
parted $destination_device mkpart primary 643MiB 100%
parted $destination_device name 4 rootfs
parted $destination_device set 2 boot on
echo Ready to format disk
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Format disk${reset}
mkfs.ext2 ${destination_device}2
mkfs.ext4 ${destination_device}4
mkswap ${destination_device}3
swapon ${destination_device}3
parted $destination_device print
echo Ready to install base system
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Install base system${reset}
ls $mount_point
mount ${destination_device}4 $mount_point
debootstrap --arch=amd64 --variant=minbase --include=linux-image-$(uname -r),grub-pc,grub2-common $distro_name $mount_point file://$distro_path
#debootstrap --arch=amd64 --variant=minbase $distro_name $mount_point file://$distro_path
echo Ready to mount system folders
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Mount system folders${reset}
mount -t proc none $mount_point/proc
mount -t sysfs sys $mount_point/sys
mount --bind /dev $mount_point/dev
mount --bind /dev/pts $mount_point/dev/pts
mkdir -p $mount_point/media/cdrom
mount --bind $distro_path $mount_point/media/cdrom
chroot $mount_point du -sh $(ls -d /*/ | while read line; do echo "$line"; done)
chroot $mount_point apt-cdrom add -m
echo Ready to install utils and GRUB
read -p "Press Enter to continue, press Ctrl+C to quit"
aptget_install="apt-get --yes --force-yes install --no-install-recommends --no-install-suggests"
# aptget_install="apt-get --yes --force-yes install"
echo ${green}Install utils and GRUB${reset}
cat > $mount_point/etc/mtab <<EOF
rootfs / rootfs rw 0 0
EOF
#chroot $mount_point $aptget_install grub-pc
#chroot $mount_point $aptget_install grub2-common
chroot $mount_point grub-install --recheck --no-floppy ${destination_device}
chroot $mount_point update-grub
#chroot $mount_point $aptget_install linux-image-generic
chroot $mount_point $aptget_install apt-utils
chroot $mount_point $aptget_install pciutils
chroot $mount_point $aptget_install usbutils
chroot $mount_point $aptget_install sudo
chroot $mount_point $aptget_install nano
cat > $mount_point/etc/fstab <<EOF
/dev/sr0 /media/cdrom iso9660 ro,user,auto 0 0
EOF
echo Ready to install network
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Install network${reset}
chroot $mount_point $aptget_install dhcp-client
chroot $mount_point $aptget_install netbase
chroot $mount_point $aptget_install net-tools
chroot $mount_point $aptget_install ifupdown
chroot $mount_point $aptget_install iputils-ping
cat > $mount_point/etc/network/interfaces <<EOF
auto lo eth0
iface lo inet loopback
iface eth0 inet dhcp
EOF
echo Ready to create user:password
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Create user:password${reset}
chroot $mount_point useradd -m -G sudo -s /bin/bash user
chroot $mount_point echo user:password | chroot $mount_point chpasswd
echo "127.0.0.1 $(hostname)" >> $mount_point/etc/hosts
echo Ready to repair non-root X
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Repair non-root X${reset}
chroot $mount_point gpasswd -a user video
mkdir -p $mount_point/etc/init.d/
cat > $mount_point/etc/init.d/prepare_startx.sh <<EOF
#!/bin/sh
chown user:user /dev/input/event*
chown user:user /dev/tty*
EOF
chroot $mount_point chmod 777 /etc/init.d/prepare_startx.sh
chroot $mount_point update-rc.d prepare_startx.sh defaults
touch $mount_point/home/user/.bash_profile
cat > $mount_point/home/user/.bash_profile <<EOF
if [ -z "$DISPLAY" ] && [ $(tty) == /dev/tty1 ] && [ -x "$(command -v startx)" ]; then
update-menus
startx
fi
EOF
echo Ready to repair non-root USB mount
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Repair non-root USB mount${reset}
usbstorage_path=/lib/modules/$(uname -r)/kernel/drivers/usb/storage
mkdir -p $mount_point$usbstorage_path
cp $usbstorage_path/* $mount_point$usbstorage_path
#chroot $mount_point insmod $usbstorage_path/usb-storage.ko
mkdir -p $mount_point/etc/init.d/
cat > $mount_point/etc/init.d/insmod_usb_runonce.sh <<EOF
#!/bin/sh
rm $0
insmod $usbstorage_path/usb-storage.ko
EOF
chroot $mount_point chmod 777 /etc/init.d/insmod_usb_runonce.sh
chroot $mount_point update-rc.d insmod_usb_runonce.sh defaults
chroot $mount_point gpasswd -a user plugdev
mkdir -p $mount_point/etc/polkit-1/localauthority/50-local.d/
cat > $mount_point/etc/polkit-1/localauthority/50-local.d/55-storage.pkla <<EOF
[Storage Permissions]
Identity=unix-group:plugdev
Action=org.freedesktop.udisks.filesystem-mount;org.freedesktop.udisks.drive-eject;org.freedesktop.udisks.drive-detach;org.freedesktop.udisks.luks-unlock;org.freedesktop.udisks.inhibit-polling;org.freedesktop.udisks.drive-set-spindown
ResultAny=yes
ResultActive=yes
ResultInactive=no
EOF
echo Ready to clean APT cache
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Clean APT cache${reset}
chroot $mount_point apt-get clean
chroot $mount_point apt-get autoclean
chroot $mount_point apt-get autoremove
chroot $mount_point du -sh $(ls -d /*/ | while read line; do echo "$line"; done)
echo Ready to unmount system folders
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Unmount system folders${reset}
umount $mount_point/media/cdrom
umount $mount_point/proc
umount $mount_point/sys
umount $mount_point/dev/pts
umount $mount_point/dev
umount $mount_point
echo Ready to reboot
read -p "Press Enter to continue, press Ctrl+C to quit"
echo ${green}Finish install, reboot${reset}
reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment