#!/bin/bash export LC_ALL=C export LANG=C export LANGUAGE=C if [ "$(id -u)" != "0" ] then echo 'You must have root privileges to use this.' exit fi ping -c 1 8.8.8.8 > /dev/null 2>&1 if [ $? != 0 ] then echo 'No connection to Internet. Can not continue.' exit fi echo '' echo 'This is a script to install slimmed Ubuntu LTS.' ### CREATE ROOT PARTITION echo '' echo 'List of found disks:' for D in $(ls /dev/sd[a-z]) do fdisk -l $D | head -2 | sed 's/^Disk //' | sed -z 's/sectors\n/sectors, /' done echo '' echo 'Select a disk to install Ubuntu on, for example sda, sdb or sdc:' read -i sd -e ROOTDISK if [ ! -e /dev/$ROOTDISK ] then echo '' echo "Device $ROOTDISK does not exist." exit fi echo '' echo 'Here are existing partitions on this disk:' fdisk -l /dev/$ROOTDISK | grep --color=never -E '^Device|^\/dev' echo '' echo 'Would you like to use whole disk for root partition?' echo 'If so, print yes. This will create totally new partition table.' echo "To use existing partition, print it's number, for example 2 :" read -e PARTNUM; if [ "$PARTNUM" = "yes" ] then echo '' ROOTPART="$ROOTDISK"1 dd if=/dev/zero of=/dev/$ROOTDISK count=2048 (echo o; echo n; echo ""; echo ""; echo ""; echo ""; echo a; echo p; echo w) | fdisk /dev/$ROOTDISK elif [ ! -e /dev/$ROOTDISK$PARTNUM ] then echo '' echo 'Partition was not found.' echo 'Use cfdisk to manually create partition and rerun this installer.' exit else ROOTPART=$ROOTDISK$PARTNUM dd if=/dev/zero of=/dev/$ROOTDISK seek=1 count=2047 (echo a; echo $PARTNUM; echo p; echo w) | fdisk /dev/$ROOTDISK fi ### FORMAT ROOT yes | mkfs.ext4 "/dev/$ROOTPART" mount "/dev/$ROOTPART" /mnt ### DOWNLOAD AND UNPACK UBUNTU BASE cd /tmp wget https://cdimage.ubuntu.com/ubuntu-base/releases/24.04/release/SHA256SUMS TAR=$(cut -d '*' -f 2 SHA256SUMS | grep amd64 | tail -1) wget https://cdimage.ubuntu.com/ubuntu-base/releases/24.04/release/$TAR tar -xf $TAR -C /mnt if [ $? -ne 0 ] then echo 'Error: Can not download Ubuntu base archive.' umount "/dev/$ROOTPART" exit fi ### CREATE SWAP, FSTAB AND RESOLV.CONF echo "/dev/$ROOTPART / ext4 defaults 0 1" > /mnt/etc/fstab fallocate -l 2G /mnt/swapfile chmod 0600 /mnt/swapfile mkswap /mnt/swapfile echo '/swapfile none swap defaults 0 0' >> /mnt/etc/fstab echo 'nameserver 9.9.9.9' > /mnt/etc/resolv.conf ### PREPARE TO CHROOT mount --bind /dev /mnt/dev mount --bind /tmp /mnt/tmp mount --bind /run /mnt/run mount -t proc proc /mnt/proc mount -t sysfs none /mnt/sys mount -t devpts -o noexec,nosuid devpts /mnt/dev/pts ### WRITE CHROOT SCRIPT # What happens here: # unminimize base system # install minimal set of desktop needed packages # configure grub2 # set hostname # add user # add a couple of sane fixes # add nonsnap firefox repository # configure user session cat << 'EOF' > /mnt/continue_install rm /etc/dpkg/dpkg.cfg.d/excludes rm /etc/update-motd.d/60-unminimize rm /usr/local/sbin/unminimize rm /usr/bin/man dpkg-divert --remove --no-rename /usr/bin/man apt update apt install --reinstall -y $(dpkg-query -S /usr/share/man | tr -d ',' | sed 's/\: \/usr.*$//') apt upgrade -y DEBIAN_FRONTEND=noninteractive apt install -y \ apt-utils dialog locales man-db keyboard-configuration systemd init grub2 \ initramfs-tools linux-base linux-generic linux-image-generic zstd tzdata \ fdisk iproute2 inetutils-ping network-manager wget pulseaudio alsa-utils \ bc less nvi ncal sed pciutils xserver-xorg xinit x11-xserver-utils xterm fluxbox dpkg-reconfigure tzdata echo '' echo 'Would you like to set specific boot options for GRUB?' echo 'If so, print them here. Leave the line blank if no specific options needed.' read GRUBOPTS sed -i -e 's/^GRUB_CMDLINE_LINUX_DEFAULT=.*$/GRUB_CMDLINE_LINUX_DEFAULT="'"$GRUBOPTS"'"/' \ -e 's/^GRUB_TIMEOUT_STYLE=.*$/GRUB_TIMEOUT_STYLE=menu/' \ -e 's/^GRUB_TIMEOUT=0/GRUB_TIMEOUT=1/' \ -e 's/^\#GRUB_TERMINAL=.*$/GRUB_TERMINAL=console/' /etc/default/grub grub-mkconfig -o /boot/grub/grub.cfg grub-install $(head -1 /etc/fstab | cut -d ' ' -f 1 | rev | cut -c 2- | rev) echo '' echo 'Set hostname:' read -i "ubuntu" -e HOSTNAME echo "$HOSTNAME" > /etc/hostname echo '' echo 'Set locale for X11. Console locale will remain en_US:' read -i "ru_RU" -e LOCALE if [ "$LOCALE" = "en_US" ] then LOCALE='' fi locale-gen en_US.UTF-8 ${LOCALE:+$LOCALE.UTF-8} localectl set-locale en_US.UTF-8 echo '' echo 'Set root password:' passwd echo '' echo 'Choose name for regular user:' read USER if [ -z "$USER" ] then echo 'Empty name. User will not be created.' echo '' else useradd -g root -G operator,sudo -s /bin/bash -m $USER passwd -d $USER 1> /dev/null echo '' echo 'Add tty autologin for this user?' read -i "yes" -e ANS if [ "$ANS" = "yes" ] then mkdir /etc/systemd/system/getty\@.service.d (echo '[Service]' echo 'ExecStart=' grep '^ExecStart=' /usr/lib/systemd/system/getty\@.service | sed "s/--noclear/--noclear --autologin $USER/" ) \ > /etc/systemd/system/getty\@.service.d/autologin.conf fi fi rmdir /*.usr-is-merged echo 'APT::AutoRemove::SuggestsImportant "false";' > /etc/apt/apt.conf.d/99autoremove # fix autoremoving echo 'kernel.dmesg_restrict=0' >> /etc/sysctl.conf # allow user to read dmesg echo 'set ruler' >> /etc/vi.exrc echo 'set autoindent' >> /etc/vi.exrc echo 'set noflash' >> /etc/vi.exrc echo 'Section "InputClass" Identifier "libinput touchpad catchall" Option "Tapping" "on" EndSection' > /etc/X11/xorg.conf.d/20-touchpad.conf echo 'network: renderer: NetworkManager' > /etc/netplan/config.yaml wget -O /etc/apt/keyrings/packages.mozilla.org.asc https://packages.mozilla.org/apt/repo-signing-key.gpg echo "deb [signed-by=/etc/apt/keyrings/packages.mozilla.org.asc] https://packages.mozilla.org/apt mozilla main" \ > /etc/apt/sources.list.d/mozilla.list echo -e 'Package: *\nPin: origin packages.mozilla.org\nPin-Priority: 1000' > /etc/apt/preferences.d/mozilla apt update if [ "$LOCALE" ] then echo "export LANG=$LOCALE.UTF-8" >> /home/$USER/.xinitrc KB=$(echo $LOCALE | sed 's/_.*$//') echo "setxkbmap -layout 'us,$KB' -option 'grp:lwin_toggle,grp_led:scroll'" \ >> /home/$USER/.xinitrc fi echo 'xset r rate 250 34' >> /home/$USER/.xinitrc echo 'exec fluxbox' >> /home/$USER/.xinitrc chown $USER /home/$USER/.xinitrc EOF ### RUN INSTALLATION INSIDE CHROOT chroot /mnt /bin/bash -c "/bin/bash /continue_install" rm /mnt/continue_install echo '' echo 'Installation done.' echo 'Now you can reboot to installed system or continue by chroot /mnt .'