I was working on a project that required a minimal debian installation. i didn't had the iso at the time, so i came up with an idea.
Debootstrap
Debian has a tool called debootstrap, which installs Debian base systems in a subdirectory of another, already installed system.
Debian wiki:
debootstrap doesn't require an installation CD, just access to a Debian repository. It can also be installed and run from another operating system - for example, you can use debootstrap to install Debian onto an unused partition from a running Gentoo system. It can also be used to create a rootfs for a machine of a different architecture, which is known as "cross-debootstrapping".
I'm Running Parch Linux which is a distribution based on Arch.
For start, i installed the qemu, debootstrap and other tools using pacman:
sudo pacman -S qemu-system-x86 qemu-img qemu-ui-gtk arch-install-scripts debootstrap
Disk image
now we need to create the disk image that we want to install the debian on, we use the qemu-img
command to create a new 60GB virtual disk for installation.
qemu-img create -f qcow2 debian.qcow2 60G
after this, we would load the nbd
module into the kernel for mounting the disk via qemu-nbd
command.
qemu-nbd: QEMU Disk Network Block Device Server which allows us to expose disk image files such as
.qcow2
,.vmdk
,.vdi
and etc as a Network Block Device (nbd) on linux.for short: It enables a virtual disk image to be treated like a real block device.
Using nbd
for start, we need to load the module. for that we should run this command:
sudo modprobe nbd
then after that, we would use the qemu-nbd
command to mount the disk as a network block.
sudo qemu-nbd --connect=/dev/nbd0 debian.qcow2
this would mount the virtual disk as /dev/nbd0
now we should partition the disk, you can use your preferd tool, i would use the parted
itself.
sudo parted /dev/nbd0 -- mklabel msdos
sudo parted /dev/nbd0 -- mkpart primary ext4 1MiB 100%
now we need to format the disk:
sudo mkfs.ext4 /dev/nbd0p1
Installation
Now after partitioning we need to start installing the debian itself.
we would first mount the disk at /mnt:
sudo mount /dev/nbd0p1 /mnt
Now we run this command to install the base system at /mnt which is our mounted disk:
sudo debootstrap --arch=amd64 trixie /mnt http://deb.debian.org/debian
notes: I'm using Trixie, because it is now stable enough to be used.
You can change the mirror and arch to your preferred one.
the installation would start after running that command.
Post installation
now we need to chroot in our newly bootstrapped debian and configure it.
we have two options
For Arch users
if you are running Arch Linux or it forks, there is a handy tool called arch-chroot
.
we need to simply run this command to chroot into it, then you can jump right to the next section.
sudo arch-chroot /mnt
For other distros:
sudo mount --bind /dev /mnt/dev
sudo mount --bind /sys /mnt/sys
sudo mount --bind /proc /mnt/proc
sudo chroot /mnt
configuration:
We need to set our hostname and hosts file, for that you can run this command:
echo debian > /etc/hostname
echo "127.0.0.1 localhost" > /etc/hosts
You can change the debian with whatever you like.
Now we need to configure the local time:
ln -sf /usr/share/zoneinfo/UTC /etc/localtime
/usr/sbin/dpkg-reconfigure tzdata
Now we set the root user password:
passwd
Now we need to install other system components to make it work properly:
apt update
apt install linux-image-amd64 grub-pc systemd-sysv sudo nano net-tools network-manager
now we install the bootloader:
grub-install --target=i386-pc --recheck /dev/nbd0
/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg
now we need to exit from chroot with exit
command and generate the fstab file.
sudo su
genfstab -U /mnt >> /mnt/etc/fstab
note:
genfstab
is a command from arch install scripts.
Booting into it
after all of this, we need to unmount our disk.
sudo umount /mnt
sudo qemu-nbd --disconnect /dev/nbd0
after that, you need to run this command to boot it.
qemu-system-x86_64 -m 1024 -hda debian.qcow2 -enable-kvm
Note: You can change the memory if you want or add more cpu cores, for that read the qemu docs.
Thanks for reading this, i might create a video from this later :)
Top comments (1)
Thanks Sohrab. wasn't aware of this tool. keep it up.