Files
plct/plct.sh
2026-01-24 21:40:53 +03:00

236 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
printf "welcome to portable linux creation tool v1.0p1\n"
printf "available distros: debian\n\n"
while true; do
read -rp "distro: " distro
distro=${distro,,}
if [ "$distro" = "debian" ] || \
[ "$distro" = "alpine" ]; then
break
fi
echo "pls choose distro from list"
done
read -rp "choose mountpoint (/mnt/install): " mount
if [ ! -n "$mount" ]; then
mount="/mnt/install"
fi
read -rp "disk (e.g. /dev/sdX) or skip to install to $mount: " disk
while true; do
read -rp "gpt/mbr: " table
table=${table,,}
if [ "$table" = "gpt" ] || [ "$table" = "mbr" ]; then
break
fi
echo "pls choose gpt or mbr"
done
while true; do
read -rp "efi? (yes/no): " efi
efi=${efi,,}
if [ "$efi" = "yes" ] || [ "$efi" = "no" ]; then
break
fi
echo "pls choose yes or no"
done
if [[ $disk =~ nvme[0-9]+n[0-9]+$ ]] || [[ $disk =~ mmcblk[0-9]+$ ]] || [[ $disk =~ loop[0-9]+$ ]]; then
prefix="p"
else
prefix=""
fi
prepare_debian() {
declare -A need=(
[debootstrap --help]=debootstrap
[parted --help]=parted
[mkfs.fat --help]=e2fsprogs
[which mkfs.ext4]=mkfs.ext4
[blkid]=blkid
)
missing=()
read -rp "choose debian mirror (https://deb.debian.org/debian): " mirror
if [ ! -n "$mirror" ]; then
mirror="https://deb.debian.org/debian"
fi
# prompt for debian version
read -rp "choose debian version (bookworm): " choice
if [ ! -n "$choice" ]; then
choice="bookworm"
fi
# checking if version of debian exists
while true; do
if curl -sS -o /dev/null -w "%{http_code}" -L "$mirror/dists/$choice" | grep -q "200"; then
break
fi
echo "debian $choice was not found at $mirror/dists/$choice"
read -rp "choose debian version (bookworm): " choice
done
if [ ! -n "$choice" ]; then
choice="bookworm"
fi
echo "found debian $choice"
for cmd in "${!need[@]}"; do
if ! $cmd >/dev/null 2>&1; then
missing+=("${need[$cmd]:-$cmd}")
fi
done
if ((${#missing[@]})); then
echo "missing this things: ${missing[*]}" >&2
echo "pls install them or run the script as root" >&2
exit 1
fi
if [ ! -n $mount ]; then
while true; do
read -rp "everything is ok, type 'no' to cancel or 'yes' to preceed installation of debian $choice to $disk with $table table (DATA ON $disk WILL BE LOST): " proceeding
if [ "$proceeding" = "no" ]; then
echo "ok aborting"
exit
fi
if [ "$proceeding" = "yes" ]; then
echo "ok starting the installation"
break
fi
done
else
while true; do
read -rp "everything is ok, type 'no' to cancel or 'yes' to preceed installation of debian $choice to $mount with $table table: " proceeding
if [ "$proceeding" = "no" ]; then
echo "ok aborting"
exit
fi
if [ "$proceeding" = "yes" ]; then
echo "ok starting the installation"
break
fi
done
fi
install_debian
}
install_debian() {
if [ ! -n "$mount" ]; then
echo "unmounting $disk"
umount $disk*
umount -R $mount
rm -rf $mount
mkdir $mount
echo "making $table table"
if [ $table = "mbr" ]; then
parted $disk --script mklabel msdos
fi
if [ $table = "gpt" ]; then
parted $disk --script mklabel gpt
fi
echo "making partitions"
if [ $efi = "yes" ]; then
parted $disk --script \
mkpart primary fat32 1MiB 513MiB \
set 1 esp on \
mkpart primary ext4 513MiB 100%
mkfs.fat -F 32 ${disk}${prefix}1
mkfs.ext4 ${disk}${prefix}2
echo "mounting"
mount ${disk}${prefix}2 $mount
mkdir $mount/boot
mount ${disk}${prefix}1 $mount/boot
fi
if [ $efi = "no" ]; then
parted $disk --script \
mkpart primary ext4 1MiB 100%
mkfs.ext4 ${disk}${prefix}1
mount ${disk}${prefix}1 $mount
fi
fi
debootstrap $choice $mount $mirror
mount --bind /dev $mount/dev
mount --bind /dev/pts $mount/dev/pts
mount --bind /proc $mount/proc
mount --bind /sys $mount/sys
RUN="chroot $mount"
$RUN apt update && $RUN apt install -y locales tzdata network-manager linux-image-amd64
$RUN dpkg-reconfigure locales
$RUN dpkg-reconfigure tzdata
while true; do
read -rp "hostname: " hostname
if [ -n "$hostname" ]; then
break
fi
echo "pls set hostname"
done
echo $hostname > $mount/etc/hostname
echo "updating root password"
$RUN passwd root
if [ $efi = "yes" ]; then
$RUN apt install -y grub-efi-amd64
mkdir $mount/boot/efi
$RUN grub-install --target=x86_64-efi --efi-directory=/boot --removable
$RUN update-grub
if [ ! -n "$mount" ]; then
efi_uuid=$(blkid -s UUID -o value ${disk}${prefix}1)
root_uuid=$(blkid -s UUID -o value ${disk}${prefix}2)
echo "UUID=$efi_uuid /boot/ vfat umask=0077 0 1" >> $mount/etc/fstab
echo "UUID=$root_uuid / ext4 defaults,noatime 0 1" >> $mount/etc/fstab
fi
fi
if [ $efi = "no" ]; then
$RUN apt-get install -y grub-pc
$RUN grub-install $disk
$RUN update-grub
if [ ! -n "$mount" ]; then
root_uuid=$(blkid -s UUID -o value ${disk}${prefix}1)
echo "UUID=$root_uuid / ext4 defaults,noatime 0 1" >> $mount/etc/fstab
fi
fi
echo 'done! chrooted you into installed system. type "exit" to finish the installation'
if [ -n "$mount" ]; then
echo "pls generate fstab manually"
fi
$RUN /bin/bash
umount -R $mount
}
if [ $distro = "debian" ]; then
prepare_debian
exit
fi
if [ $distro = "alpine" ]; then
prepare_alpine
exit
fi
echo "bye!"