Skip to content

Instantly share code, notes, and snippets.

@superboum
Last active March 3, 2026 17:07
Show Gist options
  • Select an option

  • Save superboum/1c7adcd967d3e15dfbd30d04b9ae6144 to your computer and use it in GitHub Desktop.

Select an option

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 fat32 1MiB 128MiB # was before fat16, suggested by @rgaufman
parted ${DEVICE} --script set 1 msftdata on
echo "Create OS partition"
parted ${DEVICE} --script mkpart LINUX btrfs 128MiB 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 trixie ${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.
@LoganBlizna
Copy link

@superboum there are mistakes in the script. is it gonna be arm64 or amd64?

@superboum
Copy link
Author

superboum commented Mar 3, 2026

@superboum there are mistakes in the script. is it gonna be arm64 or amd64?

It's an amd64 script. It has been useful to many people before you: don't assume it's broken without argument. If you are convinced there is a mistake, please mention where, why and if possible how to fix it. Thx in advance.

@superboum
Copy link
Author

superboum commented Mar 3, 2026

@rgaufman

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

Don't remember why I chose FAT16. I've exclusively used FAT32 since the last 8 years.
I've updated the script with your line.

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

I don't remember why, but it's a Microsoft flag, it might help if you want to add Windows to your EFI partition. I will try to read more about it and discard it if I'm convinced that no broken firmware depends on it.

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

I don't remember why but IIRC it was linked with grub-efi-amd64 grub2/update_nvram boolean false.
I wanted to configure grub to not update nvram when I run grub-install and/or running update-grub and/or update the system, and that's the solution I found.
But I am not a debian packaging expert and it might not be required / we may have easier/more straightforward solutions.

@LoganBlizna
Copy link

@superboum
....
apt-get install -y linux-image-amd64 linux-headers-amd64...?

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