Thursday, June 1, 2023

Install grub boot loader + Fix Windows instalation

GRUB (GRand Unified Bootloader)

BIOS will read the first sector of a storage device for a boot partition table.
MBR( master boot record) - 4 partitions
To boot mbr you need UEFI set to boot mode:  Legacy BIOS.

For UEFI - you need a GPT partition table and a fat16/fat32 - mkfs.vfat partition anywhere on the disk  with an EFI folder - 100mb for windows 200mb linux (has all drivers here).
Kernel 10mb + initramfs 75mb ~ 100mb + 30mb windows bcd.
Dont store kernel and initrd here, store them in /boot on the partition you install the linux os.
GPT (Globally Unique Identifier Partition Table) - 128 partitions
EFI partition - FAT16, FAT32 or VFAT

UEFI will look into
<EFI_SYSTEM_PARTITION>\EFI\
Will read folders from here - and display them in UEFI boot menu
Everyfolder must have a xxx.efi boot file

FIX Windows BOOT

The System Reserved partition is optional and is created by the windows setup for the BitLocker only as it stores un-encrypted data in order to decrypt and boot main partition
It also has the BCD (Boot Configuration Data) folder.

To avoid it press SHIFT+F10 at windows setup it opens a cmd :
diskpart
select disk 0
create partition primary
and continue with graphical setup.

Install a boot manager - boot windows install media, press SHIFT+F10 to open a CMD promt

diskpart
select disk 0
list volume
select volume 2 (pick the fat32 one)
assing letter k:
exit

Create a BCD store. - required to boot windows.
bcdboot c:\Windows /l en-GB /s K: /f ALL
Bcdboot.exe c:\windows /s c:
 
/s k: = install to k:

Grub doesnt see ext4 unknown filesystem --- remove metadata_csum
sudo dump2fs /dev/sda1 | head -20

install package e2fsprogs
sudo tune2fs -O ^metadata_csum /dev/sda1  -- remove
sudo tune2fs -O metadata_csum /dev/sda1  -- add
sudo e2fsck -f /dev/sda1

Edit grub on windows

open diskpark - type ar run or cmd
list volume
select volume 1
assign letter=z

You will get access denied efi partition windows when you try to access the partition.
Right click on start menu - and select PowerSheel (admin)
Type z:  then use cd and ls to navigate
To edit grub.cfg type notepad grub.cfg at the proper path

remove letter=z in diskpart again to remove it from explorer

Install GRUB

For UEFI you need NVRAM access

ls /sys/firmware/efi/efivars   -- NVRAM You dont ussually have it in chroot, so rut it from live media where you have efivars populated. After it createa /EFI/folder/GRUB64.EFI it has to tell NVRAM where to look. Alternative default is /EFI/BOOT/


I like to run grub-install from the live, then after it boots you run it again from your live system.

Most distros will want to install grub in /boot/grub - if you change it, it will break on updates.


mount /dev/xxx /mnt  --- efi partition
--boot-directory=/mnt -- it will install under /mnt/grub - grub files
--efi-directory=/mnt -- it will install under /mnt/EFI/os_name (Microsoft/Debian/Ubuntu etc)
grub-install --boot-directory=/mnt/linux/boot --efi-directory=/mnt/linux/boot/efi --target=x86_64-efi
grub-mkconfig > /mnt/grub/grub.cfg
grub-update is equivalent to grub-mkconfig
grub-mkconfig -o /boot/efi/EFI/grub/grub.cfg


ls 
grub> ls (hd1,gpt3)/
grub> set root=(hd0,1) grub> linux /boot/vmlinuz-3.13.0-29-generic root=/dev/sda1 grub> initrd /boot/initrd.img-3.13.0-29-generic grub> boot

optional (insmod part_gpt, insmod chain)
set root=(hd0,gpt1)
use tab after / or after each folder to explore contents - bootmgr is for bios, bootmgfw.efi for uefi.
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
boot

Grub: no suitable video mode found boot in blind mode
insmod all_video

Edit grub.cfg :

insmod all_video
insmod part_gpt
insmod ext2
insmod gzio
insmod gfxterm
insmod gettext

font=unicode

set gfxmode=auto
set lang=en_US
set timeout=5
set menu_color_normal=cyan/blue
set menu_color_highlight=white/blue

# For booting Microsoft Windows
menuentry "Microsoft Windows 7" {
    set root=(hd0,2)
    chainloader +1
}

menuentry 'Debian' {
set root='hd0,gpt2'
echo 'Loading Linux ...'
linux /boot/vmlinuz-5.10.0-11-amd64 root=/dev/sda2 ro
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-5.10.0-11-amd64
}

menuentry 'UEFI Firmware Settings' $menuentry_id_option 'uefi-firmware' {
fwsetup
}

Manual boot from GRUB menu

chainloader /efi/Microsoft/Boot/bootmgfw.efi
boot

iso images:
loopback lo (hd0,4)/image.iso
linux (lo)/artix/vmlinuz boot=casper iso-scan/filename=${isofile}
initrd (lo)/artix.initrd.gz
boot

ls - shows disks
ls (hd0,1)/    +tab = browse partitions

If you get to grub boot menu grub 0.x
root (hd0,0) --- this will mount the partition
kernel /boot/vmlinuz... root=/dev/sda1
initrd /boot/initrd....
boot

Grub 2
ls
set root=(hd0,msdos1)
linux /boot/vmlinuz... root=/dev/sda1
initrd /boot/initrd...
boot

Grub custom font

# Generate a GRUB-compatible font with specified size
# from a TTF (type-type font)

sudo grub-mkfont --output=/boot/grub/fonts/DejaVuSansMono36.pf2 --size=36 /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf


Slow loading of linux from USB on old laptops

Grub calls the BIOS to read from the USB.
Some usb sticks are not compatible with bios drivers for high speed usb. Other sticks might work at faster speeds.
 
 Grub uses bios driver to load vmlinuz and initramfs from usb and it is using USB 1.0 - it has a speed of around 1.5Mbps = 0.2MB/s. 75mb/0.2 = 375 = 5-10 min.

For old laptops / pcs:

The Plop Boot Manager is a small program to boot different operating systems.
The boot manager has a built-in ide cdrom and usb driver to access that hardware without the help/need of a bios.

https://www.plop.at/en/bootmanager/intro.html

Connect to Wifi internet linux

echo 127.0.0.1    laptop > /etc/hosts
echo pcname > /etc/hostname


Connect to WIFI
sudo systemctl enable iwd

Enable dhcp client
/etc/iwd/main.conf
[General]
EnableNetworkConfiguration=true
[Rank]
BandModifier5Ghz=100.0

DNS
Either use resolv.conf for a static dns or enable systemd-resolved
/etc/resolv.conf
nameserver 1.1.1.1
#cloudflare
nameserver 4.4.4.4  
nameserver 8.8.8.8
# google public dns

/etc/runit/sv/iwd/run
#!/bin/sh
exec /lib/iwd/iwd 2>/dev/null
ln -s /etc/runit/sv/iwd /run/runit/iwd


Connect to AP
rfkill 
rfkill unblock all
* if it is powered off - press FN+Wifi or Fn+Airplane Mode twice

iwctl device list
iwctl station wlan0 scan
iwctl station wlan0 get-networks
iwctl station wlan0 connect NETWORK_NAME
iwctl station wlan0 disconnect
iwctl station wlan0 show
iwctl known-networks list
iwctl known-networks NETWORK_NAME forget

Install iwgtk. On debian it must be compiled.

ifconfig - temporary settings in kernel, not persistent at reboot
ifup/down - config /etc/network/interfaces

Don't install networkmanager 
If you do, sudo systemctl disable NetworkManager-wait-online
Systemd - waits for - multi-user environments - for the network to connect and then continues to load services and you get a long wait at boot.

===========================

ip a
dhclient -v eth0
route
ping
netstat -tapn
namp
iptables -L (INPUT - FORWARD - OUTPUT)

wpa_supplicant
wpa_password wifi_network password > wifi
ip a
rfkill -- check it is enabled
wpa_supplicant -B -i wlan0 -c ./wifi
sudo dhclient wlan0

wpa_supplicant -B -i wlan0 -c <(wpa_passphrase "Your_SSID" Your_passphrase) && dhclient wlan0

Link service to runit by 
# ln -s /etc/sv/wpa_supplicant /var/service
or systemctl enable wap_supplicant

Manual install devuan or from scratch

Boot  Arch linux image from USB. Use rufus to burn it windows,  or dd in linux.
dd if=~/Download/arch.iso of=/dev/sdb bs=4M status=progress oflag=direct

* Install base packages distro

Wifi - internet
iwctl station wlan0 connect SSDID_name

Partition
lsblk
cfdsik -> sda1 100-200MB efi , sda2 20GB
mkfs.ext4 /dev/sda2
mount /dev/sda2 /mnt/linux

Install:
- from arch: pacman -Sy debootstrap
- from debian: apt install debootstrap

Install debian: debootstrap --arch amd64 stable /mnt/linux
Install devuan: debootstrap --arch amd64 stable /mnt https://pkgmaster.devuan.org/merged

mount --bind /dev /mnt/dev

mount --bind /dev/pts /mnt/dev/pts

mount --bind /proc /mnt/proc

mount --bind /sys /mnt/sys

mount --bind /run /mnt/run

chroot /mnt /bin/bash --login  (for proper $PATH)

arch-chroot --- does auto mount/bind those but doesn provide the login paramer and $PATH will be wrong so dont use it.

Apt remove cron-daemon-common

Apt install systemd-standalone-sysusers devuan-keyring ca-certificates apt install cron-daemon-common

Dpkg --configure -a Apt install openrc


apt install nano apt-file

/etc/fstab
/dev/sda2 / ext4 rw,relatime 0 1


Set Time Zone:

ln -sf /usr/share/zoneinfo/Europe/Bucharest /etc/localtime
date

apt install debconf
dpkg-reconfigure tzdata
dpkg-reconfigure locales

apt install sudo
passwd   --- create password for root or you cant login after reboot

adduser username
usermod -aG sudo user

Kernel
apt install linux-image-amd64  (it install the latest kernel its meta pkg)
apt install grub-efi-amd64

or use your kernel if you have one
copy /boot/vmlinuz 
copy /usr/lib/modules/uname -r/ /mnt/linux/.... same path

Install GRUB -- I sugest to install it on the uefi partition so systems updates dont mess it
mount /dev/sda1 /boot/efi
grub-install --boot-directory=/boot/efi --efi-directory=/boot/efi --target=x86_64-efi
ln -s /boot/vmlinuz-xxxx vmlinuz
ln -s /boot/initrd-xxxx initrd
nano /boot/efi/grub/grub.cfg
menuentry 'Linux' {
    set root ='hd0,gpt2'
    linux    /boot/vmlinuz root=/dev/sda2 ro
    initrd    /boot/initrd
}

WIFI

nano /etc/apt/sources.list - add non-free-firmware
apt update
apt install iproute2 net-tools rfkill iwd ifupdown
#apt install firmware-misc-nonfree    --- othter wifi boards - realtek asus
apt install firmware-iwlwifi    --- mostly intel wifi boards
apt install pciutils lsutils

apt install iwd (alternative is wpa_supplicant) 
Enable dhcp client + dns
/etc/iwd/main.conf
[General]
EnableNetworkConfiguration=true
/etc/resolv.conf
nameserver 1.1.1.1

nano /etc/hostname
nano /etc/hosts    - 127.0.0.1 hostname

/etc/network/interfaces
auto lo
iface lo inet loopback


Reboot

* Video system
You can install here a desktop/login manager - gdm3, lightdm etc OR just use xinit without an GUI interface
apt install xfce4   --- it will install xorg as a requirement

Note on devual bare xinit doesnt work, it requires to many manual changes just install lightdm.

Change console FONT size

Go to directory /usr/share/kbd/consolefonts - and ls to see all available fonts

then setfont iso01-12x22.psfu.gz 
or any other fonts you like from there

dpkg-reconfigure console-setup

Change active Display Manager:
sudo dpkg-reconfigure gdm3
sudo dpkg-reconfigure lightdm


https://www.linuxfromscratch.org/

ArchLinux - https://wiki.archlinux.org/title/Installation_guide

https://wiki.debian.org/Debootstrap