MBR(bios)からもESP(uefi)からも起動できるUSBメモリの作り方

MBR(bios)からもESP(uefi)からも起動できるUSBメモリの作り方

その1 バックアップとパーティション変更、フォーマット、2タイプのgrubのインストール

1 概要

  • UbuntuでブータブルUSBメモリを作成する手順
  • 2019/10/23現在だと、Ubuntu18.04を利用して作成してください。
  • 今後Ubuntu 19系でもgrubのバージョンアップにより不具合なしのものが同じ手順で作成できるようになるかも

2 注意

  • 2019/10/23現在だと、Ubuntu18.04を利用して作成してください。19.10ではgrubが2.04でisoイメージを起動するときに利用するloopbackのところでフリーズしています。

3 手順

**以下の操作を行っている動画




3.1 必要パッケージのインストール

sudo apt install dosfstools e2fsprogs gdisk util-linux

3.2 USBメモリ接続

接続したら、自動マウントされるので、取り出しではなくアンマウント

sudo umount マウントポイント(ディレクトリ)
sudo umount /media/user/xxxx-xxx-xxx

3.3 現状のバックアップ

3.3.1 現状のディスクイメージのパーティション情報保存

  • /dev/sdc はマシンや環境で変化するので、実際に使いたいUSBメモリのデバイスを割り当てたものを使ってください。(/dev/sdb,/dev/sddとかの場合もあり)
sudo gdisk -l /dev/sdc
Number  Start (sector)    End (sector)  Size       Code  Name
   1              34            2047   1007.0 KiB  EF02  BIOS boot partition
   2            2048         1024000   499.0 MiB   EF00  EFI System
   3         1026048        10485726   4.5 GiB     8300  Linux filesystem

3.3.2 初期状態のパーティション情報のバックアップ

  • 以下のコマンドで usborggdisk-l.txt に情報が保存される
  • /dev/sdc はマシンや環境で変化するので、実際に使いたいUSBメモリのデバイスを割り当てたものを使ってください。(/dev/sdb,/dev/sddとかの場合もあり)
sudo gdsik -l /dev/sdc > usb_org_gdisk-l.txt

3.3.3 初期状態のフルバックアップ(USBメモリ)

  • 以下のコマンドで usborg.img.gz っていうファイル名でgzip圧縮したイメージをバックアップできます。
  • 私は32GのUSBメモリで以下のコマンドを実行した時数時間必要になりました。相当時間かかります。
  • パーティション情報から空のディスクを生成可能なので、このバックアップ作業をスキップしても良い
sudo cat /dev/sdc | gzip -9c > usb_org.img.gz

3.4 ディスクパーティション変更

  • 以下のコマンドでパーティションを変更するコマンドを実行
  • /dev/sdc はマシンや環境で変化するので、実際に使いたいUSBメモリのデバイスを割り当てたものを使ってください。(/dev/sdb,/dev/sddとかの場合もあり)
sudo gdisk /dev/sdc

3.4.1 gdisk 起動して、初期のパーティション確認、その後初期パーティション削除

$ sudo  gdisk /dev/sdc
GPT fdisk (gdisk) version 1.0.3

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


***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************

Warning! Main partition table overlaps the first partition by 2 blocks!
Try reducing the partition table size by 8 entries.
(Use the 's' item on the experts' menu.)

Warning! Secondary partition table overlaps the last partition by
33 blocks!
You will need to delete this partition or resize it in another utility.

Command (? for help): p
Disk /dev/sdc: 61741056 sectors, 29.4 GiB
Model: USB DISK        
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CC3D509A-4DB7-47F6-B6BC-8A51B37DADEC
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 61741022
Partitions will be aligned on 32-sector boundaries
Total free space is 0 sectors (0 bytes)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              32        61741055   29.4 GiB    0700  Microsoft basic data

Command (? for help): d
Using 1

Command (? for help): p
Disk /dev/sdc: 61741056 sectors, 29.4 GiB
Model: USB DISK        
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CC3D509A-4DB7-47F6-B6BC-8A51B37DADEC
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 61741022
Partitions will be aligned on 32-sector boundaries
Total free space is 61740989 sectors (29.4 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

3.4.2 MBRパーティション作成

  • 設定変更
Expert command (? for help): l
Enter the sector alignment value (1-65536, default = 2048): 1

Expert command (? for help): m
  • パーティション追加
Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-61741022, default = 34) or {+-}size{KMGTP}: 
Last sector (34-61741022, default = 61741022) or {+-}size{KMGTP}: 2047
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): ef02
Changed type of partition to 'BIOS boot partition'

  • パーティション確認
Command (? for help): p
Disk /dev/sdc: 61741056 sectors, 29.4 GiB
Model: USB DISK        
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CC3D509A-4DB7-47F6-B6BC-8A51B37DADEC
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 61741022
Partitions will be aligned on 1-sector boundaries
Total free space is 61738975 sectors (29.4 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              34            2047   1007.0 KiB  EF02  BIOS boot partition

  • 設定を元に戻す
Command (? for help): x

Expert command (? for help): l
Enter the sector alignment value (1-65536, default = 2048): 

Expert command (? for help): m

3.4.3 ESP用パーティション作成

Command (? for help): n
Partition number (2-128, default 2): 
First sector (2048-61741022, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-61741022, default = 61741022) or {+-}size{KMGTP}: +500M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): ef00
Changed type of partition to 'EFI System'

  • パーティション確認
Command (? for help): p
Disk /dev/sdc: 61741056 sectors, 29.4 GiB
Model: USB DISK        
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CC3D509A-4DB7-47F6-B6BC-8A51B37DADEC
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 61741022
Partitions will be aligned on 2048-sector boundaries
Total free space is 60714975 sectors (29.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              34            2047   1007.0 KiB  EF02  BIOS boot partition
   2            2048         1026047   500.0 MiB   EF00  EFI System

3.4.4 grub関係用パーティション作成

  • 動画ではパーティションサイズの前に+をいれてませんが、入れたらそのサイズのパーティションを割当可能です
Command (? for help): n
Partition number (3-128, default 3): 
First sector (1026048-61741022, default = 1026048) or {+-}size{KMGTP}: 
Last sector (1026048-61741022, default = 61741022) or {+-}size{KMGTP}: +600M
Current type is '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/sdc: 61741056 sectors, 29.4 GiB
Model: USB DISK        
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CC3D509A-4DB7-47F6-B6BC-8A51B37DADEC
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 61741022
Partitions will be aligned on 2048-sector boundaries
Total free space is 59486175 sectors (28.4 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              34            2047   1007.0 KiB  EF02  BIOS boot partition
   2            2048         1026047   500.0 MiB   EF00  EFI System
   3         1026048         2254847   600.0 MiB   8300  Linux filesystem

3.4.5 データ用パーティション作成

  • 動画ではパーティションサイズの前に+をいれてませんが、入れたらそのサイズのパーティションを割当可能です
Command (? for help): n
Partition number (4-128, default 4): 
First sector (2254848-61741022, default = 2254848) or {+-}size{KMGTP}: 
Last sector (2254848-61741022, default = 61741022) or {+-}size{KMGTP}: 
Current type is '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/sdc: 61741056 sectors, 29.4 GiB
Model: USB DISK        
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): CC3D509A-4DB7-47F6-B6BC-8A51B37DADEC
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 61741022
Partitions will be aligned on 2048-sector boundaries
Total free space is 0 sectors (0 bytes)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              34            2047   1007.0 KiB  EF02  BIOS boot partition
   2            2048         1026047   500.0 MiB   EF00  EFI System
   3         1026048         2254847   600.0 MiB   8300  Linux filesystem
   4         2254848        61741022   28.4 GiB    8300  Linux filesystem

3.4.6 変更書き込み

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.

3.5 作成した後のディスクパーティション確認

gdisk -l /dev/sdc

3.6 変更したパーティションのフォーマット

sudo /sbin/mkfs.vfat -F32 /dev/sdc2
sudo /sbin/mkfs.ext4  /dev/sdc3
sudo /sbin/mkfs.ext4  /dev/sdc4

3.7 grubのインストールMBR用

sudo mount /dev/sdc3 /mnt
sudo grub-install --target=i386-pc --boot-directory=/mnt/boot --force /dev/sdc

3.8 grubのインストールESP用

sudo mkdir /mnt/boot/efi
sudo mount /dev/sdc2 /mnt/boot/efi
sudo grub-install --target=x86_64-efi --efi-directory=/mnt/boot/efi --boot-directory=/mnt/boot

3.9 アンマウント

sudo umount /mnt/boot/efi
sudo umount /mnt

wget -m -l 1 http://releases.ubuntu.com/19.10/MD5SUMS http://releases.ubuntu.com/19.10/ubuntu-19.10-desktop-amd64.iso

cd releases.ubuntu.com/19.10/ cat MD5SUMS cat ubuntu-19.10-desktop-amd64.iso.md5sum

sudo mount /dev/sdc4 /mnt
sudo cp ubuntu-19.10-desktop-amd64.iso* /mnt/ISO/
sudo touch /mnt/ISO/usbMemoryBoot.txt 
sudo tree /mnt
sudo umount /mnt

3.10 grub.cfg を作成し適切な場所にコピー

insmod iso9660
insmod part_gpt
insmod fat
insmod ext2
insmod ext4

set dafault=0
set timeout=20

menuentry 'ubuntu-19.10-desktop-amd64.iso' {
    search --file --set isopart /ISO/usbMemoryBoot.txt 
    set isofile="/ISO/ubuntu-19.10-desktop-amd64.iso"
    loopback loop ($isopart)$isofile
    linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=$isofile debian-installer/language=ja keyboard-configuration/layoutcode?=jp keyboard-configuration/modelcode?=jp106 noeject quiet splash --
    initrd (loop)/casper/initrd.lz
}
  • コピーは以下のコマンドで
sudo mount /dev/sdc3 /mnt
sudo cp -i grub.cfg /mnt/boot/grub/
sudo umount /mnt

3.11 qemu でテスト

  • qemuをインストール設定済みでkvmを利用した場合は以下のコマンドで実行可能
  • /dev/sdc はマシンや環境で変化するので、実際に使いたいUSBメモリのデバイスを割り当てたものを使ってください。(/dev/sdb,/dev/sddとかの場合もあり)
  • qemuでなく実機で確認する方がよいかも
sudo kvm -m 1G -hda /dev/sdc

4 続きを追記してきます。

  • 初版 2019/10/23

著者: NM Max

Created: 2019-10-23 水 19:47

Validate

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です