Today we will create a “live” USB drive with an Arch Linux installation image. “Live” in the sense that during use it will be possible to install and remove programs, change any files and save changes. I have prepared for you the most detailed instructions on the basic steps, but then you can configure everything the way you like.
Every Linux user has a favorite system. Some even have several, each for specific tasks. One such special case is a portable system that can be booted on someone else's machine or on yours if the main OS suddenly stops working properly.
There are many ready-made developments designed specifically to solve such problems. But we want to get a completely customized environment – familiar and with a set of necessary programs!
There are two options here: make a bootable ISO image or simply install everything on a flash drive. In the first case, the system will be much more compact, in the second we will get the important ability to install and remove programs and change settings while working.
Yes, the flash drive will wear out, but they are inexpensive now, and the ability to work with a full-fledged OS, in my opinion, is much more important.
Preparation
I will carry out all the work on Arch Linux for x86-64 and will take the original archiso, which is used to create official ISO images.
Therefore, the following packages must be installed on the system: archiso, arch-install-scripts, gptfdisk.
You can either immediately install it on a USB drive or create a disk image, which can then be written to a flash drive. Next, I will describe the process of creating a disk image.
Create an empty file to fit the size of the flash drive
Let's go to the directory where we will create our image.
Все дальнейшие действия будем делать с привилегиями суперпользователя.
Чтобы тебе было легче ориентироваться, буду показывать вывод некоторых команд.
Узнать точный размер устройства можно командой
lsblk -bo NAME,SIZE
NAME SIZE
sda4005527552
├─sda1 10485760
├─sda2 268435456
└─sda3 3725540864
mmcblk0 62226694144
├─mmcblk0p1 209715200
└─mmcblk0p2 62015930368
Она покажет все устройства, среди них тебе нужно найти свой USB-накопитель.
Узнав номер устройства, ты можешь составить команду и задать нужный размер файла. Делим размер на 4096 и получаем нужное количество блоков.
Примеры:
16 Гб: 16034824192 / 4096 = 3914752
4 Гб: 4005527552 / 4096 = 977912
4096 is the normal sector size for a file system.
Command to create file usb. 4 GB in size:
dd if=/dev/zero of=usb.img bs=4096 count=977912 status=progress
$ dd if=/dev/zero of=usb.img bs=4096 count=977912 status=progress
3651747840 bytes (3.7 GB, 3.4 GiB) copied, 8 s, 456 MB/s
977912+0 records received
977912+0 entries sent
4005527552 bytes (4.0 GB, 3.7 GiB) copied, 8.78378 s, 456 MB/s
We check:
$ ls -la usb.img
-rw-rw-r-- 1 root root 4005527552 апр 25 19:51 usb.img
Если файл получился больше, можно обрезать до нужной длины с помощью truncate. For example, up to 16 GB:
truncate -s 16034824192 usb.img
Create a disk partition
In order for our system to boot on computers with both BIOS and EFI, we need to partition the disk into three partitions:
- one - 10 MB, without file system, required for installing the GRUB boot loader in BIOS mode;
- the second - 256 MB, will be formatted in FAT32, we will create a partition here
bootand install GRUB in EFI mode; - We will give all the remaining space to the root file system. Let's format it in Ext4.
Create a GPT partition and divide the disk into three partitions:
sgdisk -o -n 1:0:+10M -t 1:EF02 -n 2:0:+256M -t 2:EF00 -n 3:0:0 -t 3:8300 usb.img
$ sgdisk -o -n 1:0:+10M -t 1:EF02 -n 2:0:+256M -t 2:EF00 -n 3:0:0 -t 3:8300 usb.img
Creating new GPT entries in memory.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
Mount the created image into the system
Create a link between the image file usb. and a loopback device. This allows you to work with the image file as if it were a physical device, such as a USB drive.
losetup --show -Pf usb.img
$ losetup --show -Pf usb.img
/dev/loop0
The command will show the name of the created device, in this case it is /.
Let's check.
$ ls /dev/loop0*
/dev/loop0
/dev/loop0p1
/dev/loop0p2
/dev/loop0p3
У нашего образа должно быть три раздела: loop0p1, loop0p2, loop0p3.
Formatting sections
According to the standard, the EFI partition must be formatted in FAT32.
mkfs.fat -F32 /dev/loop0p2
$ mkfs.fat -F32 /dev/loop0p2
mkfs.fat 4.2 (2021-01-31)
The third section will be the main one; for it you can choose any FS that will be supported by both the bootloader and the kernel.
Format to Ext4:
$ mkfs.ext4 /dev/loop0p3
mke2fs 1.46.5 (30-Dec-2021)
...
...
Создание журнала (16384 блоков): готово
Writing superblocks and filesystem accounting information: готово
Монтируем разделы в /mnt/usb
Сначала мы смонтируем раздел /, this will be our main partition on which we will install the system. Then on this section we will create a directory /into which we will mount the partition /.
mkdir -p /mnt/usb
mount /dev/loop0p3 /mnt/usb
mkdir /mnt/usb/boot
mount /dev/loop0p2 /mnt/usb/boot
Copy airrootfs
A few words about archiso. This is a tool that creates official Arch Linux images. It consists of two parts: script mkarchiso, which collects the image, and a directory with the settings of the future image. We will need a directory with configs. It contains:
- a list of packages that need to be installed, they are stored in a file
packages.;x86_64 - catalog
airootfs. Its structure is identical to the structure of Arch Linux; system configs are located here.
We will have to specify the path to the directory with archiso settings three times. To make it easier, let's put the path to it into a variable.
export ARCHISO=/usr/share/archiso/configs/releng