You are not logged in.

#1 2021-11-22 03:49:54

lfitzgerald
Member
Registered: 2021-07-16
Posts: 162

[SOLVED] How can I add a persistent data partition to the live USB?

I have an 8 GB live USB. I write the ISO to this with

cat archlinux-2021.11.01-x86_64.iso > /dev/sdb

However since the ISO only takes up about 1 GB, I want to create a data partition in the remaining 7 GB so I can keep using the USB for storage while I still have the arch iso on it.

I had done this successfully earlier by simply creating a new partition. The data partition would show up as /dev/sdb1 and I could mount it, even from the arch live OS (with copy to RAM so I could first unmount the drive). Unfortunately looks like I forgot to take notes about exactly how I did it, I only wrote that I used gdisk. I tried running "gdisk /dev/sdb" and here's an example session:

$ sudo gdisk /dev/sdb
GPT fdisk (gdisk) version 1.0.8

Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: present

Found valid MBR and GPT. Which do you want to use?
 1 - MBR
 2 - GPT
 3 - Create blank GPT

Your answer: 1

Command (? for help): p
Disk /dev/sdb: 15435776 sectors, 7.4 GiB
Model: Transcend 8GB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 3F1D52A4-228A-426D-9436-A82D3EE7E396
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 15435742
Partitions will be aligned on 2048-sector boundaries
Total free space is 15259581 sectors (7.3 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   2         1556480         1732607   86.0 MiB    EF00  EFI system partition

Command (? for help): n
Partition number (1-128, default 1): 3
First sector (34-15435742, default = 1732608) or {+-}size{KMGTP}:
Last sector (1732608-15435742, default = 15435742) or {+-}size{KMGTP}:
Current type is 8300 (Linux filesystem)
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sdb: 15435776 sectors, 7.4 GiB
Model: Transcend 8GB
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 3F1D52A4-228A-426D-9436-A82D3EE7E396
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 15435742
Partitions will be aligned on 2048-sector boundaries
Total free space is 1556446 sectors (760.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   2         1556480         1732607   86.0 MiB    EF00  EFI system partition
   3         1732608        15435742   6.5 GiB     8300  Linux filesystem

This seems like it's doing the right thing, but the resulting USB fails to boot. I have also tried to create the partition as 1 instead, which didn't work either. When I choose GPT in the beginning, it doesn't let me create a partition at all as the drive shows up as having no remaining space.

What am I missing?

Last edited by lfitzgerald (2021-11-22 06:05:53)

Offline

#2 2021-11-22 04:59:31

mpan
Member
Registered: 2012-08-01
Posts: 1,207
Website

Re: [SOLVED] How can I add a persistent data partition to the live USB?

I am doing that by not copying the ISO to a pendrive directly, but instead letting GRUB run an ISO file. This way you may have a normal data partitions, including the one used by GRUB. And you may have multiple ISOs. Just act as if you were installing GRUB onto the pendrive, but do not generate initramfs etc. Instead, copy ISOs to some directory accessible to GRUB and manually craft the “grub.cfg”. As a reference I have a “/boot/grub/” directory on my pendrive partition, with “/boot/grub/iso” containing ISOs, and “/boot/grub/grub.cfg” being:

set imgdevpath="/dev/disk/by-uuid/2f2ec7ab-1cc0-4f9e-8d6b-4e0ca64592ae"

menuentry 'ArchISO 2021-06-01' {
    set isofile='/boot/grub/iso/archlinux-2021.06.01-x86_64.iso'
    loopback loop $isofile
    search --fs-uuid --set=root 2f2ec7ab-1cc0-4f9e-8d6b-4e0ca64592ae
    echo 'Loading Linux...'
    linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
    echo 'Loading initial ramdisk...'
    initrd (loop)/arch/boot/x86_64/initramfs-linux.img
}

menuentry 'ArchISO 2021-07-01' {
    set isofile='/boot/grub/iso/archlinux-2021.07.01-x86_64.iso'
    loopback loop $isofile
    search --fs-uuid --set=root 2f2ec7ab-1cc0-4f9e-8d6b-4e0ca64592ae
    echo 'Loading Linux...'
    linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
    echo 'Loading initial ramdisk...'
    initrd (loop)/arch/boot/x86_64/initramfs-linux.img
}

menuentry 'ArchISO 2021-08-01' {
    set isofile='/boot/grub/iso/archlinux-2021.08.01-x86_64.iso'
    loopback loop $isofile
    search --fs-uuid --set=root 2f2ec7ab-1cc0-4f9e-8d6b-4e0ca64592ae
    echo 'Loading Linux...'
    linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
    echo 'Loading initial ramdisk...'
    initrd (loop)/arch/boot/x86_64/initramfs-linux.img
}

menuentry 'ArchISO 2021-09-01' {
    set isofile='/boot/grub/iso/archlinux-2021.09.01-x86_64.iso'
    loopback loop $isofile
    search --fs-uuid --set=root 2f2ec7ab-1cc0-4f9e-8d6b-4e0ca64592ae
    echo 'Loading Linux...'
    linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
    echo 'Loading initial ramdisk...'
    initrd (loop)/arch/boot/x86_64/initramfs-linux.img
}

menuentry 'ArchISO 2021-10-01' {
    set isofile='/boot/grub/iso/archlinux-2021.10.01-x86_64.iso'
    loopback loop $isofile
    search --fs-uuid --set=root 2f2ec7ab-1cc0-4f9e-8d6b-4e0ca64592ae
    echo 'Loading Linux...'
    linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
    echo 'Loading initial ramdisk...'
    initrd (loop)/arch/boot/x86_64/initramfs-linux.img
}

menuentry 'ArchISO 2021-11-01' {
    set isofile='/boot/grub/iso/archlinux-2021.11.01-x86_64.iso'
    loopback loop $isofile
    search --fs-uuid --set=root 2f2ec7ab-1cc0-4f9e-8d6b-4e0ca64592ae
    echo 'Loading Linux...'
    linux (loop)/arch/boot/x86_64/vmlinuz-linux img_dev=$imgdevpath img_loop=$isofile earlymodules=loop
    echo 'Loading initial ramdisk...'
    initrd (loop)/arch/boot/x86_64/initramfs-linux.img
}

The imgdevpath set in the begining is the UUID of the partition, which is needed for the linux command that boots the image from loop-back mounted ISO file (variable isofile). You must set that to the UUID of your partition.

Unfortunately I only tested that on BIOS and UEFI working in legacy mode, so I can’t answer if it works on any UEFI system.

Relevant documentation: loopback, search, linux, initrd.

Last edited by mpan (2021-11-22 05:03:19)


Sometimes I seem a bit harsh — don’t get offended too easily!

Offline

#3 2021-11-22 06:00:22

lfitzgerald
Member
Registered: 2021-07-16
Posts: 162

Re: [SOLVED] How can I add a persistent data partition to the live USB?

mpan wrote:

I am doing that by not copying the ISO to a pendrive directly, but instead letting GRUB run an ISO file.

Interesting, I didn't know you could do that!

For future readers, here are the steps I did:

  1. Create a new MBR (DOS) partition table to get rid of my crappy old one

  2. Create a new primary partition (type 07 which is exFAT), set bootable flag to on

  3. Format as FAT: sudo mkfs.fat /dev/sdb1

  4. Mount it: pmount /dev/sdb1 && cd /media/sdb1

  5. Prepare boot dir: mkdir -p boot/grub

  6. Install grub: sudo grub-install --debug --boot-directory=/media/sdb1/boot /dev/sdb

  7. Copy your grub.cfg filet to /media/sdb1/boot/grub/grub.cfg

  8. Find my drive's UUID from sudo blkid and then search & replace your old one in the grub.cfg

  9. Copy my Arch ISO to /media/sdb1/boot/grub/iso (I only had one)

I then tested it with qemu (based on https://mbusb.aguslr.com/install.html#t … -with-qemu, but they wrongly assume everyone's cpu supports kvm):

sudo qemu-system-x86_64 -rtc base=localtime -m 2G -vga std -drive file=/dev/sdb,readonly=on,cache=none,format=raw,if=virtio

The GRUB menu showed up with the list of ISOs. Since I only provided the one ISO, most of them resulted in errors, but the one I provided booted into Arch. I didn't get the usual Arch boot menu offering to boot from RAM etc, but that was fine for me. I did lsblk and saw that the drive itself was available under /run/archiso/img_dev. I'll clean up the grub config file later to get rid of the entries I don't need.

I'll give this a try and see if it boots my laptop as well. I'll post here if it doesn't work. It booted, yay!

Last edited by lfitzgerald (2021-11-22 06:04:28)

Offline

Board footer

Powered by FluxBB