1

I am trying to achieve something somehow challenging. I have a fairly fast and large USB stick (SanDisk Ultra USB 3.0 16GB) which I'd like to use in an unorthodox way:

  1. I need a partition with a size around 1 GB, which can be readed by both Windows and Linux (as you may know, previous versions of Windows do not get along with multi-partitioned USB sticks very well. Or should I say, at all? They read only the first partition and ignore the rest)
  2. I need a Linux distribution (preferably Ubuntu) installed in the stick
  3. I need this stick to be bootable both in EFI enabled and legacy machines.

Installing Ubuntu to the disk was simple enough, and speed of the stick is better than I expected. But I was not able to boot the stick from a non-UEFI machine. Machine simply does not read the stick and keeps hanging on a full-black screen.

So, my question is:

  • Is what I try to achieve even possible?

  • If so, do I need a special partitioning scheme? I'm ok with doing a fresh installation.

  • Can I install grub in both UEFI and legacy mods? If so, how?

  • Just using a live USB might be easier, especially if you use persistence or can customize it. MX-Linux, AntiX, and I think Puppy has great customization tools, like "set up the live system then create an ISO or bootable USB with a click" easy & they should boot EFI – Xen2050 Jan 2 '18 at 10:38
  • I thought that, but I'll modify system files, install software etc. So a live usb with persistence won't cut. – SercioSoydanov Jan 2 '18 at 10:43
  • Also, I'd like to learn about the grub in the process, so I'l keep trying to do it :) – SercioSoydanov Jan 2 '18 at 10:44
  • The live USB's boot with syslinux (or isolinux?) or grub depending on what's supported. And you do know that persistence is what lets a live USB (or DVD, any ro ISO in some cases) install programs & change files? – Xen2050 Jan 2 '18 at 12:50
  • I thought that persistence only allows to store files in /home, it is good to know. But I also need a seperate partition on the drive, which is something I cannot have with a live USB w/ persistence created by automated installers. – SercioSoydanov Jan 2 '18 at 12:54
1

Is what I try to achieve even possible?

Yes!

If so, do I need a special partitioning scheme? I'm ok with doing a fresh installation.

You will need a special partitioning scheme. It may be possible to modify your existing one to fit (all you need to add is a tiny 1MiB BIOS boot partition), but I'll outline the necessary partitioning scheme below.

Can I install grub in both UEFI and legacy mods? If so, how?

You just need to install GRUB for each mode. Below are instructions for Ubuntu and Debian.


Setup

Here are from-scratch instructions on installing Ubuntu in both UEFI and legacy boot modes. These instructions work on any disk, not just USB ones.

This guide assumes that you want to start from scratch and your disk is /dev/sdX. Replace /dev/sdX with your disk block device.

  1. Remove all partitions to get a clean slate:

    sgdisk --zap-all /dev/sdX
    
  2. Create a GPT partition table on your disk:

    sgdisk --mbrtogpt /dev/sdX
    
  3. You have a requirement of a 1GB partition at the beginning of your flash drive, so here's the command to make a 1GiB partition (note: GiB instead of GB):

    sgdisk --new 3::+1G --typecode 3:0700 /dev/sdX
    
  4. Create a BIOS boot partition. This is necessary to add EFI support on legacy boot.

    sgdisk --new 1::+1M --typecode 1:ef02 /dev/sdX
    
  5. Create an EFI system partition:

    sgdisk --new 2::+50M --typecode 2:ef00 /dev/sdX
    
  6. Create the data partition where you'll install Ubuntu:

    sgdisk --new 4::+ --typecode 4:8300 /dev/sdX
    
  7. Install Ubuntu on the flash drive, using the partitioning scheme already set above.

    Proceed to the next step now only if you do not want to use the graphical Ubuntu installer.

    If you do want to use the graphical Ubuntu installer, do so now using the partitioning scheme already created, then come back here and run these commands:

    mount /dev/sdX4 /mnt
    mount --rbind /dev  /mnt/dev
    mount --rbind /proc /mnt/proc
    mount --rbind /sys  /mnt/sys
    chroot /mnt /bin/bash --login
    

    Since you used the graphical installer, skip to step 17.

  8. Create a file system for Ubuntu on /dev/sdX4, the data partition we just created:

    mkfs.ext4 /dev/sdX4
    
  9. Mount the new file system to a temporary path:

    mount /dev/sdX4 /mnt
    
  10. Install barebones Ubuntu (replacing bionic with the codename of the Ubuntu version you want to install):

    debootstrap bionic /mnt
    
  11. Set the hostname of your new Ubuntu installation (replacing HOSTNAME with your desired hostname):

    echo HOSTNAME > /mnt/etc/hostname
    
  12. Configure package sources by writing the following content into /mnt/etc/apt/sources.list (replacing bionic with the codename of the Ubuntu version you installed):

    deb http://archive.ubuntu.com/ubuntu bionic main universe
    deb-src http://archive.ubuntu.com/ubuntu bionic main universe
    
    deb http://security.ubuntu.com/ubuntu bionic-security main universe
    deb-src http://security.ubuntu.com/ubuntu bionic-security main universe
    
    deb http://archive.ubuntu.com/ubuntu bionic-updates main universe
    deb-src http://archive.ubuntu.com/ubuntu bionic-updates main universe
    
  13. Prepare to log in to the new Ubuntu system:

    mount --rbind /dev  /mnt/dev
    mount --rbind /proc /mnt/proc
    mount --rbind /sys  /mnt/sys
    
  14. Log in to your new Ubuntu system:

    chroot /mnt /bin/bash --login
    
  15. Configure the locale (modify as desired):

    locale-gen en_US.UTF-8
    echo LANG=en_US.UTF-8 > /etc/default/locale
    
  16. Install the rest of Ubuntu Desktop (replace ubuntu-desktop with the flavor you want, if you don't want Ubuntu Desktop):

    dpkg-reconfigure tzdata
    ln -s /proc/self/mounts /etc/mtab
    apt update
    apt install -y ubuntu-desktop
    
  17. Install GRUB for legacy boot:

    apt install -y grub-pc
    
  18. Set up the EFI file system and install GRUB for EFI boot:

    apt install -y dosfstools
    mkdosfs -F 32 -n EFI /dev/sdX2
    mkdir /boot/efi
    echo PARTUUID=$(blkid -s PARTUUID -o value /dev/sdX2) /boot/efi vfat nofail,x-systemd.device-timeout=1 0 1 >> /etc/fstab
    mount /boot/efi
    apt install -y grub-efi-amd64
    
  19. Configure GRUB for legacy boot:

    grub-install --target=i386-pc /dev/sdX
    
  20. Configure GRUB for EFI boot:

    grub-install --target=x86_64-efi /dev/sdX
    
  21. Update the initrd files:

    update-initramfs -ckall
    
  22. Update the GRUB boot configuration:

    update-grub
    
  23. Clean up by exiting the chroot and unmounting:

    exit
    umount -R /mnt
    

You should now have Ubuntu installed on your disk with the ability to boot in EFI mode or in legacy mode.

/dev/sdX3 is the data partition you requested. You can format this with a Windows-compatible file system as desired.


Acknowledgements

I pieced together this guide from two different sources:

  • Thanks a lot! With a few small modifications, it worked like a charm! – SercioSoydanov Jan 3 '18 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.