You are not logged in.

#1 2020-08-05 13:36:23

stri
Member
Registered: 2014-06-09
Posts: 72

Installing into image file attached to loop device?

Hi, I'm trying to build Arch raw images for qemu/kvm by pacstrapping to a loop device, but when launching qemu, the initram doesn't find the root device.

Is this actually possible to do? If so, what am I doing wrong? Is there a problem with the bootloader installation?

The error when booting the .img in qemu is:

 BdsDxe: failed to load Boot0001 "UEFI QEMU DVD-ROM QM00003 " from PciRoot(0x0)/Pci(0x1,0x1)/Ata(Secondary,Master,0x0): Not Found
BdsDxe: loading Boot0002 "UEFI Misc Device" from PciRoot(0x0)/Pci(0x4,0x0)
BdsDxe: starting Boot0002 "UEFI Misc Device" from PciRoot(0x0)/Pci(0x4,0x0)
:: running early hook [udev]
Starting version 245.7-1-arch
:: running hook [udev]
:: Triggering uevents...
Waiting 10 seconds for device /dev/disk/by-uuid/2502edd9-28c7-4848-8b89-b6a8a9e65d8e ...
ERROR: device 'UUID=2502edd9-28c7-4848-8b89-b6a8a9e65d8e' not found. Skipping fsck.
:: mounting 'UUID=2502edd9-28c7-4848-8b89-b6a8a9e65d8e' on real root
mount: /new_root: can't find UUID=2502edd9-28c7-4848-8b89-b6a8a9e65d8e.
You are now being dropped into an emergency shell.
sh: can't access tty; job control turned off
[rootfs ]# 

I don't know why it can't find the root disk by uuid. In fact it can't find any disk/partition:

[rootfs ]# ls /dev/disk                                                                                               
ls: /dev/disk: No such file or directory

But attaching the image on a loop device works fine. I don't see anything strange here:

[xxx]$ sudo losetup  --find --show --partscan test.img
/dev/loop0

[xxx]$ lsblk
NAME          MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
loop0           7:0    0     2G  0 loop  
├─loop0p1     259:0    0   550M  0 part  
└─loop0p2     259:1    0   1.5G  0 part  
...

[xxx]$ sudo fdisk -l /dev/loop0
Disk /dev/loop0: 2 GiB, 2147483648 bytes, 4194304 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: DDE936E7-9715-4D2E-A383-051034D72073

Device         Start     End Sectors  Size Type
/dev/loop0p1    2048 1128447 1126400  550M EFI System
/dev/loop0p2 1128448 4192255 3063808  1.5G Linux filesystem

Also, the UUID of the root device looks fine:

[xxx]$ sudo blkid /dev/loop0p2
/dev/loop0p2: UUID="2502edd9-28c7-4848-8b89-b6a8a9e65d8e" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="primary" PARTUUID="39ea0869-b3d8-48b0-97d7-fb109827b2f1"

The filesystem looks normal too

[xxx]$ sudo mount /dev/loop0p2 /mnt/test
[xxx]$ sudo mount /dev/loop0p1 /mnt/test/boot
[xxx]$ ls /mnt/test/
bin  boot  dev  etc  home  lib  lib64  lost+found  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[xxxt=]$ ls /mnt/test/boot/
0067dfcde01e4a71bb1484a973ad6b0e  EFI  initramfs-linux-fallback.img  initramfs-linux.img  loader  vmlinuz-linux

This is the installation script:

#!/bin/bash -eu

# image creation
IMG="/tmp/test.img"
qemu-img create -f raw $IMG 2G

# loop device
LOOP=$(sudo losetup -f)
sudo losetup $LOOP $IMG

# partition
sudo parted -s -a optimal $LOOP mklabel gpt mkpart primary fat32 1MiB 551MiB set 1 esp on
sudo parted -s -a optimal $LOOP mkpart primary ext4 551MiB 100%

BOOTDEV="$LOOP"p1
ROOTDEV="$LOOP"p2

ROOTMNT=/mnt/test
BOOTMNT=$ROOTMNT/boot

# file system
sudo mkfs.vfat -F 32 $BOOTDEV
sudo mkfs.ext4 $ROOTDEV

sudo mkdir $ROOTMNT ||:
sudo mount $ROOTDEV $ROOTMNT
sudo mkdir $BOOTMNT
sudo mount $BOOTDEV $BOOTMNT

# pacstrap
sudo pacstrap -c $ROOTMNT/ base linux linux-firmware sudo

# fstab
sudo bash -c 'genfstab -U '$ROOTMNT' >> '$ROOTMNT'/etc/fstab'

# bootloader
sudo bootctl --esp-path=$BOOTMNT install

UUID=$(sudo blkid -s UUID -o value $ROOTDEV)
sudo tee $BOOTMNT/loader/entries/arch.conf << EOF
title   Arch Linux
linux   /vmlinuz-linux
initrd  /initramfs-linux.img
options root=UUID=$UUID rw 
EOF

# clean up
sudo umount -R $ROOTMNT
sudo losetup -d $LOOP

So I'm not chrooting explicitly (pacstrap does it if I understand correctly), I'm using bootctl from my host system. But that should work, shouldn't it?

Thanks for your help!

Offline

#2 2020-08-06 11:54:37

stri
Member
Registered: 2014-06-09
Posts: 72

Re: Installing into image file attached to loop device?

Hmm, I tried the following and it works:

1) pacstrap 'base' to a plain directory
2) create a raw image (IMG) with boot+root partition and make the filesystems
3) rsync the plain directory content to the root partition of IMG
4) start the arch install iso in qemu and attach IMG as a qemu drive
5) mount the IMG partitions in qemu and pacstrap linux, linux-firmware
6) install the bootloader as in the script above (no chroot)

The image boots normally.

So the problem must be the loop devices?

In qemu the image is "virtualized" to "true" block device, and it works. How would I achieve this without using qemu, but with loop devices?

Not that using qemu is a large problem, but it would be much more convenient/quick to not having to boot into the live system to make the image bootable in qemu or bare metal.

Offline

Board footer

Powered by FluxBB