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 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.
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

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 --efi-directory=/mnt --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 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


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 debian or arch linux from scratch

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

* Install base packages distro

Wifi - internet
iwctl station wlan0 connect SSDID_name

Partition
lsblk
mkfs.ext4 /dev/sda3
mount /dev/sda3 /mnt

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

Install debian: debootstrap --arch amd64 stable /mnt

genfstab /mnt/linux >> /mnt/linux/etc/fstab
Chroot into the new install
arch-chroot /mnt/linux     --- this will auto mount /dev /proc etc from current system

Set Time Zone:
cd /etc
ls -l | grep localtime
date
sudo ln -s /usr/share/zoneinfo/Europe/Bucharest /etc/localtime
date

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

WIFI

nano /etc/apt/sources.list - add non-free-firmware
apt update
apt install firmware-misc-nonfree    --- othter wifi boards - realtek asus
apt install firmware-iwlwifi    --- mostly intel wifi boards

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
adduser username
usermode -aG sudo user

Reboot

sudo dpkg-reconfigure locales

* 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

Xwayland works with gdm3 and gnome only for now.
Xfce4 works with lightdm and xorg/x11.

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

To start video mode:
systemctl start lightdm 

sudo apt install wget
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -i google.....deb
sudo apt -f install  --- to fix dependencies error from above

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


https://www.linuxfromscratch.org/

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

https://gist.github.com/myTerminal/4f1d26eaf32cdc5bcc9fb19f594c03d2

https://www.debian.org/releases/stretch/amd64/apds03.html.en

https://wiki.debian.org/Debootstrap

On xfce4 time will not update after sustend on the gui - but time sync works fine