You are not logged in.

#1 2020-01-17 03:34:23

awbs
Member
Registered: 2013-01-18
Posts: 27

Using Arch in a loopfile file ... then taking it apart on shutdown

I have a USB flash drive formatted as so:

Disk /dev/sda: 3 GiB, 3221225472 bytes, 6291456 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: 05C53391-CEEC-A14B-A9CA-181B0CBE0E04

Device         Start     End Sectors  Size Type
/dev/sda1    2048 6029311 6027264  2.9G Microsoft basic data
/dev/sda2 6029312 6031359    2048    1M BIOS boot
/dev/sda3 6031360 6291422  260063  127M EFI System

where sda1 is an exfat partition. sda1 contains:

/
/ArchLinux.img

Where ArchLinux.img is just a BTRFS filesystem with a complete and working Arch Linux install ( i.e. sudo losetup /dev/loop0 ArchLinux.img ; sudo mount -t btrfs -o compress-force=zstd /dev/loop0 /mnt/ArchLinuxRoot )

I have a custom mkinitcpio hook "loopback" where mkinitcpio.conf looks like:

MODULES=(loop fuse)
BINARIES=(mount.exfat fsck.exfat)
HOOKS=(base udev loopback_shutdown loopback modconf block filesystems keyboard fsck)

Where this custom "loopback" hook is inspired by what is used by archiso, but heavily simplified ( https://git.archlinux.org/archiso.git/t … ks/archiso ):

run_hook ()
{
        mount_handler="loopback_mount_handler"
}

loopback_mount_handler() {

    # Mount exFat Partition
    mkdir /host
    mount -t exfat-fuse /dev/sda1 /host
 
    # And the loop filesystem
    losetup /dev/loop0 /host/ArchLinux.img
    mount -t btrfs -o rw,defaults,compress-force=zstd /dev/loop0 /new_root
    mount --move /host /new_root/home/helpdesk/Data // so I can access the rest of the exFat partition when booted-up
}

This actually boots up and works great. However, the problem is on shutdown.

I have a custom hook that tries to undo this "loopback_shutdown" that is taken from Archiso, apparently it replaces the shutdown executable that tears down the overlayfs/squashfs that archiso sets up, that I modified to tear down the loopback file:

/etc/initcpio/install/loopback_shutdown (unchanged from archiso_shutdown install file)

#!/bin/bash

build() {
    add_binary cp

    add_runscript

    add_file /etc/initcpio/loopback_shutdown /shutdown
}

/etc/initcpio/hooks/loopback_shutdown (unchanged from archiso's hooks file)

run_cleanuphook() {
    rm -rf /usr/lib/modules
    cp -ax / /run/initramfs
}

/etc/initcpio/loopback_shutdown

#!/bin/ash

mkdir /oldrun
mount -n --move /oldroot/host /oldrun

# Unmount all mounts now.
umount $(mount | awk '$3 ~/^\/oldroot/ {print $3}' | sort -r)

# Remove all dm-snapshot devices.
dmsetup remove_all

# Remove all loopback devices.
umount /dev/loop0

# reboot / poweroff / halt, depending on the argument passed by init
# if something invalid is passed, we halt
case "$1" in
  reboot|poweroff|halt) "$1" -f ;;
  *) halt -f;;
esac

Note: This is just the "archiso_shutdown" hook where the only modifications are on the third line (the mount command after mkdir /oldrun) to unmount the exFat partition, the rest of the lines are unchanged.

I get the following on shutdown

https://imgur.com/264dLk2

Note that it complains that it could not unmount the /dev/sda1 exFat partition I mounted after setting up root, and a bunch of BTRFS errors show up like root is not being unmounted correctly. Although on a restart, 'btrfs dev stat /' doesn't show any device errors and scrub returns no issues (maybe because a sync is called before trying to unmount everything?).

Can anyone with more experience as what initcpio does on shutdown shed light on why it's not unmounting correctly?

Last edited by awbs (2020-01-17 03:48:46)

Offline

Board footer

Powered by FluxBB