Bootable pendrive

From campisano.org
Jump to navigation Jump to search

Create a bootable usb pendrive

NOTE: In this example a 16GB pen-drive is used.

With BIOS partition table (MBR)

The first partition is to avoid problems, as the "world" expects the first partition to be a VFAT. You can still use it as a normal pendrive, to exchange files between different O.S. like Windows and Linux.

Additionally, according with the booting image that you will use, the ISO image file cannot be stored on the Windows or Persistence partitions, or they will be mounted only 'read-only' and cannot be used to store file. See Debian_live as an example.

To avoid this problem, 4 partitions will be created:

WARNING! the pendrive device name on your computer will be different, in this example /dev/sdX is used, change it accordingly with your one!

  • 1) /dev/sdX1 partition with a Windows compatible FAT32 filesystem of 1GB, to share data between different O.S.;
  • 2) /dev/sdX2 partition with a bootable Linux EXT2 filesystem of 128Mb containing the bootloader;
  • 2) /dev/sdX3 partition with a Linux EXT2 filesystem of 5GB containing the ISO image of your choice (it MUST be greater than your ISO image);
  • 3) /dev/sdX4 partition with a Linux EXT2 filesystem with the remaining space, used for Persistence, to keep your user data and to install custom programs.
root@host:~# fdisk -c=nondos -u=sectors /dev/sdX
o       ## create an empty DOS partition table
n       ## new
p       # primary
1       # partition 1
[enter] # default, start at initial disk
+1G     # 1GB of size
t       # type of the unique existent partition
c       # W95 FAT32 (LBA) partition up to 2Tb size
n       ## new
p       # primary
2       # partition 2
[enter] # default start at the end of previous partition
+128M   # 128MB size
t       # type of the partition
2       # select partition 2
17      # Hidden HPFS/NTFS
a       # enable boot
2       # select partition 2
n       ## new
p       # primary
3       # partition 3
[enter] # default start at the end of previous partition
+5GB    # 5GB size
t       # type of the partition
3       # select partition 3
83      # Linux
n       ## new
p       # primary
# auto-select partition 4
[enter] # default start at the end of previous partition
[enter] # all remaining space
t       # type of the partition
4       # select partition 4
83      # Linux
p       ## print new future partition, check if is equal to below description
w       ## WRITE CHANGES AND EXIT!!!
root@host:~# 
  • If all is ok, your configuration is something like:
root@host:~# fdisk -c=nondos -u=sectors -l /dev/sdX

Disk /dev/sdX: 14.44 GiB, 15502147584 bytes, 30277632 sectors
Disk model: DataTraveler 3.0
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: dos
Disk identifier: 0x4a00edc1

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdX1           2048  2099199  2097152    1G  c W95 FAT32 (LBA)
/dev/sdX2  *     2099200  2361343   262144  128M 17 Hidden HPFS/NTFS
/dev/sdX3        2361344 12847103 10485760    5G 83 Linux
/dev/sdX4       12847104 30277631 17430528  8.3G 83 Linux

Prepare the first partition for Windows use

root@host:~# mkfs.vfat -v -n USB -F 32 /dev/sdX1

Prepare the second partition for the bootloader

root@host:~# mkfs.ext2 -v -L bootloader /dev/sdX2

Prepare the third partition for the iso Image

root@host:~# mkfs.ext2 -v -L isoimages /dev/sdX3

Prepare the forth partition to be used as persistence

root@host:~# mkfs.ext2 -v -L persistence /dev/sdX4

With EFI partition under a MBR

According to Wikipedia article at https://en.wikipedia.org/wiki/EFI_system_partition , EFI specification does not need a GPT partition table to work. A MBR partition table can be used, and an EFI partition will work to boot EFI image on EFI complaint hardware. A test was made successfully on a Macbook pro late 2011.

To proceed with an EFI under MBR:

  • change the partition type of the secondary partition
root@host:~# fdisk -c=nondos -u=sectors /dev/sdX
t       # type of the partition
2       # select partition 2
ef      # EFI (FAT-12/16/32)
w       ## WRITE CHANGES AND EXIT!!!
  • format the partition as FAT
root@host:~# mkfs.vfat -v -n USB -F 32 /dev/sda2
  • add the EFI bootable image of your choice

With GUID partition table (GPT)

As writed in the MBR section, according with the booting image that you will use, the ISO image file cannot be stored on the Windows or Persistence partitions, or they will be mounted only 'read-only' and cannot be used to store file. See Debian_live as an example.

To avoid this problem, 3 partitions will be created:

WARNING! the pendrive device name on your computer will be different, in this example /dev/sdX is used, change it accordingly with your one!

  • 1) /dev/sdX1 partition with a EFI FAT32 filesystem of 128Mb containing the bootloader;
  • 2) /dev/sdX2 partition with a Linux EXT2 filesystem of 5GB containing the ISO image of your choice (it MUST be greater than your ISO image);
  • 3) /dev/sdX3 partition with a Linux EXT2 filesystem with the remaining space, used for Persistence, to keep your user data and to install custom programs.
root@host:~# fdisk -c=nondos -u=sectors /dev/sdX
g       ## create an empty GPT partition label
n       ## new
1       # partition 1
[enter] # default, start at initial disk
+128M   # 128MB size
t       # type of the unique existent partition
1       # EFI System
n       ## new
p       # primary
2       # partition 2
[enter] # default start at the end of previous partition
+5GB    # 5GB size
t       # type of the partition
2       # select partition 2
20      # Linux filesystem
n       ## new
3       # partition 3
[enter] # default start at the end of previous partition
[enter] # all remaining space
t       # type of the partition
3       # select partition 3
20      # Linux filesystem
p       ## print new future partition, check if is equal to below description
w       ## WRITE CHANGES AND EXIT!!!
root@host:~# 

Prepare the first partition for Windows use

root@host:~# mkfs.vfat -v -n EFI -F 32 /dev/sdX1

Prepare the third partition for the iso Image

root@host:~# mkfs.ext2 -v -L isoimages /dev/sdX2

Prepare the forth partition to be used as persistence

root@host:~# mkfs.ext2 -v -L persistence /dev/sdX3