Creating USB installed Ubuntu bootable in both UEFI and legacy BIOS mode

Resouces

My notes on the steps

Step A: Format the USB stick as required.

Boot GPartd. Select the USB stick. If there is a key icon next to any partitions of the stick, unmount the USB stick from either the Linux system or GPartd.

Select Partition, then Create new partition. Use the default DOS-favor of partition table.

Create the first FAT32 partition of size 1-2GB, with label usb-live at the front. Confirm and make the change before you can right click, select "Manage flag" and make it bootable.

Use the remaining space to create a extended partition. From within create the ext4 partition with label usb-installed, and then linux-swap. Note the order is important. The ext4 partition will then move to partition 6 rather than partition 5 if you reverse the order.

Step B

Straightforward

Step C

Make sure you detach all the other disks of your PC before booting using the USB. If you come across error when using the live usb, use a live CD to boot into Ubuntu instal.
 
Changes on ../usb-live/syslinux/txt.cfg
Addition of the following line to the end:
# ------------------------------------------------------------------
label ubu-installed
  menu label ^Installed Ubuntu in partition 5 with grub in PBR
  com32 chain.c32
  append boot 5

boot 5 means it will go to the first extended partition of the first harddrive for the MBR.

Changes on ../usb-live/boot/grub/grub.cfg
 Addition of the following lines to the end:
menuentry 'Installed Ubuntu, with Linux 3.16.0-34-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-34-generic-advanced-9ad38740-453e-4cc4-81ec-2f9385b90a82' {
    insmod part_msdos
    insmod ext2
    set root='hd0,msdos5'
    if [ x$feature_platform_search_hint = xy ]; then
      search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos5 --hint-efi=hd0,msdos5 --hint-baremetal=ahci0,msdos5  9ad38740-453e-4cc4-81ec-2f9385b90a82
    else
      search --no-floppy --fs-uuid --set=root 9ad38740-453e-4cc4-81ec-2f9385b90a82
    fi
    echo    'Loading Linux 3.16.0-34-generic ...'
    linux    /boot/vmlinuz-3.16.0-34-generic.efi.signed root=UUID=9ad38740-453e-4cc4-81ec-2f9385b90a82 ro  quiet splash $vt_handoff
    echo    'Loading initial ramdisk ...'
    initrd    /boot/initrd.img-3.16.0-34-generic
}

Find the Linux version on your own install from the file names in ../usb_installed/boot/ and do the substitution appropriately. In the above, the Linux version is 3.16.0-34.
Find the uuid of your own install in usb-installed/etc/fstab and do the substitution appropriately. In the above, the uuid is 9ad38740-453e-4cc4-81ec-2f9385b90a82.

Changes on ../usb_installed/boot/grub/grub.cfg (may need to be root to edit it)

Additional of the following menu entries:

    menuentry 'Ubuntu, with Linux 3.19.0-25-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.19.0-25-generic-advanced-cb5efa00-cd30-4f62-b02d-e55534410cc6' {
        recordfail
        load_video
        gfxmode $linux_gfx_mode
        insmod gzio
        insmod part_msdos
        insmod ext2
        set root='hd2,msdos6'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd2,msdos6 --hint-efi=hd2,msdos6 --hint-baremetal=ahci0,msdos5  cb5efa00-cd30-4f62-b02d-e55534410cc6
        else
          search --no-floppy --fs-uuid --set=root cb5efa00-cd30-4f62-b02d-e55534410cc6
        fi
        echo    'Loading Linux 3.19.0-25-generic ...'
        linux    /boot/vmlinuz-3.19.0-25-generic root=UUID=cb5efa00-cd30-4f62-b02d-e55534410cc6 ro  quiet splash $vt_handoff
        echo    'Loading initial ramdisk ...'
        initrd    /boot/initrd.img-3.19.0-25-generic
    }
    menuentry 'Ubuntu, with Linux 3.19.0-25-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.19.0-25-generic-recovery-cb5efa00-cd30-4f62-b02d-e55534410cc6' {
        recordfail
        load_video
        insmod gzio
        insmod part_msdos
        insmod ext2
        set root='hd2,msdos6'
        if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root --hint-bios=hd2,msdos6 --hint-efi=hd2,msdos6 --hint-baremetal=ahci0,msdos5  cb5efa00-cd30-4f62-b02d-e55534410cc6
        else
          search --no-floppy --fs-uuid --set=root cb5efa00-cd30-4f62-b02d-e55534410cc6
        fi
        echo    'Loading Linux 3.19.0-25-generic ...'
        linux    /boot/vmlinuz-3.19.0-25-generic root=UUID=cb5efa00-cd30-4f62-b02d-e55534410cc6 ro recovery nomodeset
        echo    'Loading initial ramdisk ...'
        initrd    /boot/initrd.img-3.19.0-25-generic
    }

Similar to the previous file, replace Linux version and UUID.

uefi-firmware line
Change the following lines
### BEGIN /etc/grub.d/30_uefi-firmware ###
### END /etc/grub.d/30_uefi-firmware ###
to the following:
### BEGIN /etc/grub.d/30_uefi-firmware ###
menuentry 'System setup' $menuentry_id_option 'uefi-firmware' {
    fwsetup
}
### END /etc/grub.d/30_uefi-firmware ###

Post install steps

Back up and recovery

Mount the partitions.
Run the following command to write zeros to the filesystem. This helps achieve better compression.
dd if=/dev/zero of=zero.file bs=1024

Open Gpartd, make sure no partition in the USB drive is not mounted. Unmount any if so.
Then in a root shell, do something similar to the following. /dev/sdb is the device id of the usb device.
dd if=/dev/sdb conv=sync,noerror bs=64K | gzip -c > MobileUbuntu.img.gz

The .img.gz can then be consumed by mkusb or simply dd to reimage a usb stick.
gunzip -c MobileUbuntu.img.gz | dd of=/dev/sdb

Use it as a virtual machine on Virtual Box

See here.

First insert your mobile Ubuntu USB drive to your PC. Then run diskmgmt.msc and figure out the PhysicalDrive number that your USB drive is with (e.g. Disk 1).

After that, with an administrator account, create a raw disk representing the mounted USB drive.

cd %programfiles%\Oracle\VirtualBox

VBoxManage internalcommands createrawvmdk -filename C:\usb.vmdk -rawdisk \\.\PhysicalDrive#
(Replace # with the disk number, e.g. PhysicalDrive1 for Disk 1)

Finally, create a virtual machine using your admin-run. The machine should mount that raw disk which actually refers to your USB mobile Ubuntu. Start the machine and that's it.

NOTE: Do not install guest addition on your USB Ubuntu. Otherwise, your USB Ubuntu will lose the mouse and touchpad driver when it is used as typical physical machine. I once lost the driver and had to do the following to reinstall the driver.

apt-get install xserver-xorg-input-synaptics-lts-trusty
gnome-session-quit

Kernel upgrade

After a kernel upgrade, one needs to update the appearance of kernel version string (e.g. 3.19.0-25) in ../usb_live/boot/grub/grub.cfg with the new one. Then one may use tools such as ubuntu-tweak to clean up the old kernel files.

Comments

Popular posts from this blog

Using PlayStation 1 Densha de Go controller on emulators

Using PS1/PC Densha de Go! controller SLPH-00051/TCPP-20001/DGOC-44U on PS4/Switch Densha de Go!!

Play octoshape stream on windows with VLC player