Skip to content

Instantly share code, notes, and snippets.

@superboum
Last active May 14, 2024 12:11
Show Gist options
  • Save superboum/1c7adcd967d3e15dfbd30d04b9ae6144 to your computer and use it in GitHub Desktop.
Save superboum/1c7adcd967d3e15dfbd30d04b9ae6144 to your computer and use it in GitHub Desktop.
Install Debian with Debootstrap + Grub EFI
#!/bin/bash
# SPDX-License-Identifier: MIT
set -e # Exit on error
DEVICE=$1
[ -z "${DEVICE}" ] && echo "Usage $0 /dev/sdX" && exit 1
udevadm info -n ${DEVICE} -q property
echo "Selected device is ${DEVICE}"
read -p "[Press enter to continue or CTRL+C to stop]"
echo "Umount ${DEVICE}"
umount ${DEVICE}* || true
echo "Set partition table to GPT (UEFI)"
parted ${DEVICE} --script mktable gpt
echo "Create EFI partition"
parted ${DEVICE} --script mkpart EFI fat16 1MiB 10MiB
parted ${DEVICE} --script set 1 msftdata on
echo "Create OS partition"
parted ${DEVICE} --script mkpart LINUX btrfs 10MiB 100%
echo "Format partitions"
mkfs.vfat -n EFI ${DEVICE}1
mkfs.btrfs -f -L LINUX ${DEVICE}2
echo "Mount OS partition"
ROOTFS="/tmp/installing-rootfs"
mkdir -p ${ROOTFS}
mount ${DEVICE}2 ${ROOTFS}
echo "Debootstrap system"
debootstrap --variant=minbase --arch amd64 buster ${ROOTFS} http://deb.debian.org/debian/
echo "Mount EFI partition"
mkdir -p ${ROOTFS}/boot/efi
mount ${DEVICE}1 ${ROOTFS}/boot/efi
echo "Get ready for chroot"
mount --bind /dev ${ROOTFS}/dev
mount -t devpts /dev/pts ${ROOTFS}/dev/pts
mount -t proc proc ${ROOTFS}/proc
mount -t sysfs sysfs ${ROOTFS}/sys
mount -t tmpfs tmpfs ${ROOTFS}/tmp
echo "Entering chroot, installing Linux kernel and Grub"
cat << EOF | chroot ${ROOTFS}
set -e
export HOME=/root
export DEBIAN_FRONTEND=noninteractive
debconf-set-selections <<< "grub-efi-amd64 grub2/update_nvram boolean false"
apt-get remove -y grub-efi grub-efi-amd64
apt-get update
apt-get install -y linux-image-amd64 linux-headers-amd64 grub-efi
grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck --no-nvram --removable
update-grub
EOF
echo "Unmounting filesystems"
umount ${ROOTFS}/dev/pts
umount ${ROOTFS}/dev
umount ${ROOTFS}/proc
umount ${ROOTFS}/sys
umount ${ROOTFS}/tmp
umount ${ROOTFS}/boot/efi
umount ${ROOTFS}
echo "Done"
MIT LICENSE
Copyright 2018 Quentin Dufour
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@vazhnov
Copy link

vazhnov commented Oct 24, 2023

@superboum , Could you please add information about the license of the script — is it allowed to copy, change and use the code in any purpose?
It would be great to see any popular open source license.

@superboum
Copy link
Author

I've added a MIT license, hope it will help.

@rgaufman
Copy link

rgaufman commented May 14, 2024

Why FAT16 instead of FAT32? - I've been creating my EFI partition like this:

parted #{drive_path} -- mkpart ESP fat32 1MiB 128MiB

And I did not need to do set 1 msftdata on, not sure what that is for.

Also, what is the logic behind removing grub-efi grub-efi-amd64 before installing it again?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment