Create usb pendrive with Arch Linux to boot in several systems

Material

  • 128GB USB3 pendrive

Features

  • Run Arch Linux in my Udoo system and eventually laptop and other systems.
  • Has a data partition to be oponed in all Operating Systems (Formatted in NTFS)

Disk partitions

Changing from msdos partition table to gpt one

To check if the disk drive is in gpt or dos you should run:

1
sudo fdisk -l /dev/sdb

You will see something like that:

1
2
3
4
5
6
Disk /dev/sdb: 931,5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0x4f8ae775

If the Disklabel type is dos you should change to gpt, if is marked as gpt you can continue to next point.

WARNING Doing this will remove all data from disk!

If your partition table is msdos you should change it to gpt running the next command:

1
sudo fdisk /dev/sdb

Then press g to create the GPT partition table and w to persist the changes.

Creating schema

We want to create the next schema:

1
2
3
4
5
6
7
8
9
10
11
Disk /dev/sdb: 119,2 GiB, 128035323904 bytes, 250068992 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 74461979-09F8-4B29-B5B4-9C3B96F01190

Dispositiu Start Final Sectors Size Tipus
/dev/sdb1 2048 125036543 125034496 59,6G Microsoft basic data
/dev/sdb2 125036544 126265343 1228800 600M EFI System
/dev/sdb3 126265344 250066943 123801600 59G Linux filesystem

Just use fdisk or parted to create the partitions you want.

Format EFI partition

1
mkfs.fat -F32 /dev/sdb2

Disabling journal in ext4 / partition

1
sudo mkfs.ext4 -O "^has_journal" /dev/sdb3

Prepare Arch Linux system

1
2
3
cd /tmp
curl -O http://ftp.rediris.es/mirror/archlinux/iso/latest/archlinux-bootstrap-2018.01.01-x86_64.tar.gz
tar xzf archlinux-bootstrap-2018.01.01-x86_64.tar.gz

Enable pacman mirrors:

1
nvim etc/pacman.d/mirrorlist

Chroot to new system:

1
/tmp/root.x86_64/bin/arch-chroot /tmp/root.x86_64/

Update sources and install base system:

1
2
3
4
5
# Initialize packman keys
pacman-key --init
pacman-key --populate archlinux
pacman -Sy archlinux-keyring
pacman -Syyu

Install the prepared system to the USB stick

1
2
3
4
5
6
7
8
9
10
11
12
# Mount system partition to /tmp folder
mount /dev/sdb3 /mnt
cd /tmp/root.x86_64/
# Copy all files from prepared system to its final partition
rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} ./ /mnt/
# Generate fstab
genfstab -U /mnt >> /mnt/etc/fstab
# Installing base system
arch-chroot /mnt/
pacman -S base parted linux
# Installing boot loader
pacman -S grub syslinux

Follow regular installation instructions

Setup bootloader

Before creating the initial RAM disk # mkinitcpio -p linux, in /etc/mkinitcpio.conf add the block hook to the hooks array right after udev. This is necessary for appropriate module loading in early userspace.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Mount filesystem
mount /dev/sdb2 /mnt/boot
# Chroot new system
arch-chroot /mnt/
grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=grub --removable --recheck
grub-mkconfig -o /boot/grub/grub.cfg

# Setting root password
passwd


# Syslinux
# Enable EFI boot
pacman -S syslinux
mkdir -p /boot/EFI/syslinux
cp -r /usr/lib/syslinux/efi64/* /boot/EFI/syslinux/
cd /boot/EFI
mv syslinux boot
cd boot
mv syslinux.efi bootx64.efi
vi /boot/syslinux/syslinux.cfg # update root partition with UUID=...
# also check the paths for the images
# they will be relative from the config file

dd bs=440 count=1 conv=notrunc if=/usr/lib/syslinux/bios/gptmbr.bin of=/dev/sdb
extlinux --install /boot
mkdir /syslinux
cp -r /usr/lib/syslinux/bios/* /syslinux/
vim /syslinux/syslinux.cfg # See discussion below

# Reboot to boot with your new brand usb stick
reboot

Comments

⬆︎TOP