Debianインストール用USBメモリーの作成(ISOファイルコピー版)

# 目的
Debianインストール用USBメモリーの作成作業について残します

:::note info
ISOファイルをそのままコピーする方法
:::

# 開発環境

```bash:/etc/os-release
master@sv-server:~$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux bookworm/sid"
NAME="Debian GNU/Linux"
VERSION_CODENAME=bookworm
ID=debian
～
```

# USBメモリーの例

```bash:USBメモリーの確認(lsusb、lsblk、fdisk)
master@sv-server:~$ # /dev/sdbの場合
master@sv-server:~$ lsusb
～
Bus 003 Device 007: ID 125f:c81a A-DATA Technology Co., Ltd. Flash drive
～
master@sv-server:~$ lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
～
sdb             8:16   1  7.5G  0 disk
～
master@sv-server:~$ sudo fdisk -l /dev/sdb
Disk /dev/sdb: 7.53 GiB, 8086609920 bytes, 15794160 sectors
Disk model: USB Flash Drive
～
Device     Boot Start      End  Sectors  Size Id Type
/dev/sdb1        2048 15794159 15792112  7.5G  c W95 FAT32 (LBA)
```

# 作業例

:::note alert
<strong>全消去</strong>しても問題の無いUSBメモリーで作業する事
:::

:::note info
前提条件
* USBメモリー：/dev/sdb
* DVDメディア：debian testing(bookworm) netinst版ISOファイル
:::

## 作業準備

```bash:作業準備
sudo rm -rf ./cfg/ \
            ./iso/ \
            ./mnt/ \
            ./wrk/ \
            ./usb/
mkdir -p    ./cfg/ \
            ./iso/ \
            ./mnt/ \
            ./wrk/ \
            ./usb/
```

## 設定ファイル・ダウンロード

```bash:設定ファイル・ダウンロード
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/debian"                              "https://raw.githubusercontent.com/office-itou/Linux/master/installer/source/cfg/debian/preseed.cfg"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/debian"                              "https://raw.githubusercontent.com/office-itou/Linux/master/installer/source/cfg/debian/sub_late_command.sh"
```

## Debianファイル・ダウンロード

### oldoldstable

```bash:oldoldstable
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldoldstable"     "http://deb.debian.org/debian/dists/oldoldstable/main/installer-amd64/current/images/hd-media/boot.img.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldoldstable"     "http://deb.debian.org/debian/dists/oldoldstable/main/installer-amd64/current/images/hd-media/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldoldstable"     "http://deb.debian.org/debian/dists/oldoldstable/main/installer-amd64/current/images/hd-media/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldoldstable/gtk" "http://deb.debian.org/debian/dists/oldoldstable/main/installer-amd64/current/images/hd-media/gtk/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldoldstable/gtk" "http://deb.debian.org/debian/dists/oldoldstable/main/installer-amd64/current/images/hd-media/gtk/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./iso"                                     "https://cdimage.debian.org/cdimage/archive/latest-oldoldstable/amd64/iso-cd/debian-9.13.0-amd64-netinst.iso"
```

### oldstable
```bash:oldstable
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldstable"        "http://deb.debian.org/debian/dists/oldstable/main/installer-amd64/current/images/hd-media/boot.img.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldstable"        "http://deb.debian.org/debian/dists/oldstable/main/installer-amd64/current/images/hd-media/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldstable"        "http://deb.debian.org/debian/dists/oldstable/main/installer-amd64/current/images/hd-media/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldstable/gtk"    "http://deb.debian.org/debian/dists/oldstable/main/installer-amd64/current/images/hd-media/gtk/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/oldstable/gtk"    "http://deb.debian.org/debian/dists/oldstable/main/installer-amd64/current/images/hd-media/gtk/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./iso"                                     "https://cdimage.debian.org/cdimage/archive/latest-oldstable/amd64/iso-cd/debian-10.13.0-amd64-netinst.iso"
```

### stable

```bash:stable
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/stable"           "http://deb.debian.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/boot.img.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/stable"           "http://deb.debian.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/stable"           "http://deb.debian.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/stable/gtk"       "http://deb.debian.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/gtk/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/stable/gtk"       "http://deb.debian.org/debian/dists/stable/main/installer-amd64/current/images/hd-media/gtk/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./iso"                                     "https://cdimage.debian.org/cdimage/release/current/amd64/iso-cd/debian-11.6.0-amd64-netinst.iso"
```

### testing

```bash:testing
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/testing"          "https://d-i.debian.org/daily-images/amd64/daily/hd-media/boot.img.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/testing"          "https://d-i.debian.org/daily-images/amd64/daily/hd-media/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/testing"          "https://d-i.debian.org/daily-images/amd64/daily/hd-media/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/testing/gtk"      "https://d-i.debian.org/daily-images/amd64/daily/hd-media/gtk/initrd.gz"
curl -L -# -O -R -S --create-dirs --output-dir "./cfg/installer-hd-media/testing/gtk"      "https://d-i.debian.org/daily-images/amd64/daily/hd-media/gtk/vmlinuz"
curl -L -# -O -R -S --create-dirs --output-dir "./iso"                                     "https://cdimage.debian.org/cdimage/daily-builds/daily/arch-latest/amd64/iso-cd/debian-testing-amd64-netinst.iso"
```

## 作成作業

### フォーマット関係

```bash:フォーマット関係
sudo sfdisk /dev/sdb << _EOT_
label: dos
,,c,*
_EOT_
sudo bash -c 'cat /usr/lib/syslinux/mbr/mbr.bin > /dev/sdb'
sudo mkdosfs -F32 /dev/sdb1
```

## ファイルコピー

```bash:ファイルコピー
sudo syslinux /dev/sdb1
sudo mount /dev/sdb1 ./usb/
sudo grub-install --target=x86_64-efi --efi-directory=./usb/ --boot-directory=./usb/boot/ --no-floppy --removable
sudo mkdir -p ./usb/.disk/id/   \
              ./usb/install.amd \
              ./usb/syslinux    \
              ./usb/preseed
sudo touch ./usb/.disk/info
# ::: oldoldstable :::
# sudo cp --preserve=timestamps    ./iso/debian-9.13.0-amd64-netinst.iso                ./usb/
# sudo cp --preserve=timestamps -r ./cfg/installer-hd-media/oldoldstable/!(boot.img.gz) ./usb/install.amd/
# sudo gzip -dk ./cfg/installer-hd-media/oldoldstable/boot.img.gz
# sudo mv ./cfg/installer-hd-media/oldoldstable/boot.img ./wrk/
# ::: oldstable :::
# sudo cp --preserve=timestamps    ./iso/debian-10.13.0-amd64-netinst.iso               ./usb/
# sudo cp --preserve=timestamps -r ./cfg/installer-hd-media/oldstable/!(boot.img.gz)    ./usb/install.amd/
# sudo gzip -dk ./cfg/installer-hd-media/oldstable/boot.img.gz
# sudo mv ./cfg/installer-hd-media/oldstable/boot.img ./wrk/
# ::: stable :::
# sudo cp --preserve=timestamps    ./iso/debian-11.6.0-amd64-netinst.iso                ./usb/
# sudo cp --preserve=timestamps -r ./cfg/installer-hd-media/stable/!(boot.img.gz)       ./usb/install.amd/
# sudo gzip -dk ./cfg/installer-hd-media/stable/boot.img.gz
# sudo mv ./cfg/installer-hd-media/stable/boot.img ./wrk/
# ::: testing :::
sudo cp --preserve=timestamps    ./iso/debian-testing-amd64-netinst.iso               ./usb/
sudo cp --preserve=timestamps -r ./cfg/installer-hd-media/testing/!(boot.img.gz)      ./usb/install.amd/
sudo gzip -dk ./cfg/installer-hd-media/testing/boot.img.gz
sudo mv ./cfg/installer-hd-media/testing/boot.img ./wrk/
#
sudo mount -r -o loop ./wrk/boot.img ./mnt/
sudo cp --preserve=timestamps ./mnt/*.c32 ./usb/syslinux/
sudo umount ./mnt
sudo rm ./wrk/boot.img
#
sudo cp --preserve=timestamps    ./cfg/debian/preseed.cfg                ./usb/preseed/
sudo cp --preserve=timestamps    ./cfg/debian/sub_late_command.sh        ./usb/preseed/
sudo sed -e 's~ /cdrom/~ /media/~g' -i ./usb/preseed/preseed.cfg
```

## /boot/grub/grub.cfg作成

```bash:/boot/grub/grub.cfg作成
cat << _EOT_ | sudo tee ./usb/boot/grub/grub.cfg > /dev/null
set timeout=5
set default=0

insmod part_acorn
insmod part_amiga
insmod part_apple
insmod part_bsd
insmod part_dfly
insmod part_dvh
insmod part_gpt
insmod part_msdos
insmod part_plan
insmod part_sun
insmod part_sunpc

insmod font

if loadfont /boot/grub/fonts/unicode.pf2 ; then
  set gfxmode=auto
  set gfxpayload=keep
  insmod efi_gop
  insmod efi_uga
  insmod video_bochs
  insmod video_cirrus
  insmod gfxterm
  insmod png
  terminal_output gfxterm
fi

if background_image /isolinux/splash.png; then
  set color_normal=light-gray/black
  set color_highlight=white/black
elif background_image /splash.png; then
  set color_normal=light-gray/black
  set color_highlight=white/black
else
  set menu_color_normal=cyan/blue
  set menu_color_highlight=white/blue
fi

insmod play
play 960 440 1 0 4 440 1
set background_color=black

menuentry --hotkey=i 'Install' {
    linux   /install.amd/vmlinuz root=/dev/sda1 ro vga=788 --- quiet
    initrd  /install.amd/initrd.gz
}
menuentry --hotkey=g 'Graphical install' {
    linux   /install.amd/gtk/vmlinuz root=/dev/sda1 ro vga=788 --- quiet
    initrd  /install.amd/gtk/initrd.gz
}
menuentry --hotkey=u 'Unattended installation' {
    linux   /install.amd/vmlinuz root=/dev/sda1 ro auto=true file=/hd-media/preseed/preseed.cfg vga=788 --- quiet
    initrd  /install.amd/initrd.gz
}
menuentry 'Graphical Unattended installation' {
    linux   /install.amd/gtk/vmlinuz root=/dev/sda1 ro auto=true file=/hd-media/preseed/preseed.cfg vga=788 --- quiet
    initrd  /install.amd/gtk/initrd.gz
}
_EOT_
```

## /syslinux/syslinux.cfg作成

```bash:/syslinux/syslinux.cfg作成
cat << _EOT_ | sudo tee ./usb/syslinux/syslinux.cfg > /dev/null
timeout 50
default install
ui vesamenu.c32
prompt 0

menu resolution 800x600

menu color screen       * #00000000 #00000000 none
menu color title        * #FFFFFFFF *
menu color border       * #00000000 #00000000 none
menu color sel          * #ffffffff #76a1d0ff *
menu color hotsel       1;7;37;40 #ffffffff #76a1d0ff *
menu color tabmsg       * #ffffffff #00000000 *
menu color help         37;40 #ffdddd00 #00000000 none

menu tabmsg Press ENTER to boot or TAB to edit a menu entry

menu title _Debian GNU/Linux installer menu (BIOS mode)
label install
	menu label ^Install
	kernel /install.amd/vmlinuz
	append initrd=/install.amd/initrd.gz vga=788 --- quiet
label installgui
	menu label ^Graphical install
	menu default
	kernel /install.amd/gtk/vmlinuz
	append initrd=/install.amd/gtk/initrd.gz vga=788 --- quiet
label unattended
	menu label ^Unattended installation
	kernel /install.amd/vmlinuz
	append initrd=/install.amd/initrd.gz vga=788 auto=true file=/hd-media/preseed/preseed.cfg --- quiet
label unattendedgui
	menu label ^Graphical Unattended installation
	menu default
	kernel /install.amd/gtk/vmlinuz
	append initrd=/install.amd/gtk/initrd.gz vga=788 auto=true file=/hd-media/preseed/preseed.cfg --- quiet

_EOT_
```

## 終了処理

```bash:終了処理
sudo umount ./usb
```

# 作業結果

## USBメモリーの内容

```bash:ls -R ./usb/
master@sv-server:~/work$ ls -R ./usb/
./usb/:
EFI   debian-testing-amd64-netinst.iso  ldlinux.c32  preseed
boot  install.amd                       ldlinux.sys  syslinux

./usb/EFI:
BOOT

./usb/EFI/BOOT:
BOOTX64.CSV  BOOTX64.EFI  grub.cfg  grubx64.efi  mmx64.efi

./usb/boot:
grub

./usb/boot/grub:
fonts  grub.cfg  grubenv  locale  x86_64-efi

./usb/boot/grub/fonts:
unicode.pf2

./usb/boot/grub/locale:
ast.mo        de_CH.mo        en@piglatin.mo  fr.mo  it.mo  nb.mo  pt_BR.mo  sv.mo     zh_TW.mo
ca.mo         en@arabic.mo    en@quot.mo      gl.mo  ja.mo  nl.mo  ro.mo     tr.mo
da.mo         en@cyrillic.mo  eo.mo           hr.mo  ko.mo  pa.mo  ru.mo     uk.mo
de.mo         en@greek.mo     es.mo           hu.mo  lg.mo  pl.mo  sl.mo     vi.mo
de@hebrew.mo  en@hebrew.mo    fi.mo           id.mo  lt.mo  pt.mo  sr.mo     zh_CN.mo

./usb/boot/grub/x86_64-efi:
acpi.mod              fshelp.mod                lvm.mod              relocator.mod
adler32.mod           functional_test.mod       lzopio.mod           romfs.mod
affs.mod              gcry_arcfour.mod          macbless.mod         scsi.mod
afs.mod               gcry_blowfish.mod         macho.mod            search.mod
afsplitter.mod        gcry_camellia.mod         mdraid09.mod         search_fs_file.mod
ahci.mod              gcry_cast5.mod            mdraid09_be.mod      search_fs_uuid.mod
all_video.mod         gcry_crc.mod              mdraid1x.mod         search_label.mod
aout.mod              gcry_des.mod              memdisk.mod          serial.mod
appleldr.mod          gcry_dsa.mod              memrw.mod            setjmp.mod
archelp.mod           gcry_idea.mod             minicmd.mod          setjmp_test.mod
at_keyboard.mod       gcry_md4.mod              minix.mod            setpci.mod
ata.mod               gcry_md5.mod              minix2.mod           sfs.mod
backtrace.mod         gcry_rfc2268.mod          minix2_be.mod        shift_test.mod
bfs.mod               gcry_rijndael.mod         minix3.mod           signature_test.mod
bitmap.mod            gcry_rmd160.mod           minix3_be.mod        sleep.mod
bitmap_scale.mod      gcry_rsa.mod              minix_be.mod         sleep_test.mod
blocklist.mod         gcry_seed.mod             mmap.mod             smbios.mod
boot.mod              gcry_serpent.mod          moddep.lst           spkmodem.mod
bsd.mod               gcry_sha1.mod             modinfo.sh           squash4.mod
bswap_test.mod        gcry_sha256.mod           morse.mod            strtoull_test.mod
btrfs.mod             gcry_sha512.mod           mpi.mod              syslinuxcfg.mod
bufio.mod             gcry_tiger.mod            msdospart.mod        tar.mod
cat.mod               gcry_twofish.mod          mul_test.mod         terminal.lst
cbfs.mod              gcry_whirlpool.mod        multiboot.mod        terminal.mod
cbls.mod              geli.mod                  multiboot2.mod       terminfo.mod
cbmemc.mod            gettext.mod               nativedisk.mod       test.mod
cbtable.mod           gfxmenu.mod               net.mod              test_blockarg.mod
cbtime.mod            gfxterm.mod               newc.mod             testload.mod
chain.mod             gfxterm_background.mod    nilfs2.mod           testspeed.mod
cmdline_cat_test.mod  gfxterm_menu.mod          normal.mod           tftp.mod
cmp.mod               gptsync.mod               ntfs.mod             tga.mod
cmp_test.mod          grub.efi                  ntfscomp.mod         time.mod
command.lst           gzio.mod                  odc.mod              tpm.mod
configfile.mod        halt.mod                  offsetio.mod         tr.mod
core.efi              hashsum.mod               ohci.mod             trig.mod
cpio.mod              hdparm.mod                part_acorn.mod       true.mod
cpio_be.mod           hello.mod                 part_amiga.mod       udf.mod
cpuid.mod             help.mod                  part_apple.mod       ufs1.mod
crc64.mod             hexdump.mod               part_bsd.mod         ufs1_be.mod
crypto.lst            hfs.mod                   part_dfly.mod        ufs2.mod
crypto.mod            hfsplus.mod               part_dvh.mod         uhci.mod
cryptodisk.mod        hfspluscomp.mod           part_gpt.mod         usb.mod
cs5536.mod            http.mod                  part_msdos.mod       usb_keyboard.mod
ctz_test.mod          iorw.mod                  part_plan.mod        usbms.mod
date.mod              iso9660.mod               part_sun.mod         usbserial_common.mod
datehook.mod          jfs.mod                   part_sunpc.mod       usbserial_ftdi.mod
datetime.mod          jpeg.mod                  partmap.lst          usbserial_pl2303.mod
disk.mod              json.mod                  parttool.lst         usbserial_usbdebug.mod
diskfilter.mod        keylayouts.mod            parttool.mod         usbtest.mod
div.mod               keystatus.mod             password.mod         video.lst
div_test.mod          ldm.mod                   password_pbkdf2.mod  video.mod
dm_nv.mod             legacy_password_test.mod  pata.mod             video_bochs.mod
echo.mod              legacycfg.mod             pbkdf2.mod           video_cirrus.mod
efi_gop.mod           linux.mod                 pbkdf2_test.mod      video_colors.mod
efi_uga.mod           linux16.mod               pcidump.mod          video_fb.mod
efifwsetup.mod        linuxefi.mod              pgp.mod              videoinfo.mod
efinet.mod            load.cfg                  play.mod             videotest.mod
ehci.mod              loadbios.mod              png.mod              videotest_checksum.mod
elf.mod               loadenv.mod               priority_queue.mod   wrmsr.mod
eval.mod              loopback.mod              probe.mod            xfs.mod
exfat.mod             ls.mod                    procfs.mod           xnu.mod
exfctest.mod          lsacpi.mod                progress.mod         xnu_uuid.mod
ext2.mod              lsefi.mod                 raid5rec.mod         xnu_uuid_test.mod
extcmd.mod            lsefimmap.mod             raid6rec.mod         xzio.mod
f2fs.mod              lsefisystab.mod           random.mod           zfs.mod
fat.mod               lsmmap.mod                rdmsr.mod            zfscrypt.mod
file.mod              lspci.mod                 read.mod             zfsinfo.mod
fixvideo.mod          lssal.mod                 reboot.mod           zstd.mod
font.mod              luks.mod                  regexp.mod
fs.lst                luks2.mod                 reiserfs.mod

./usb/install.amd:
gtk  initrd.gz  vmlinuz

./usb/install.amd/gtk:
initrd.gz  vmlinuz

./usb/preseed:
preseed.cfg  sub_late_command.sh

./usb/syslinux:
ldlinux.c32  libcom32.c32  libutil.c32  syslinux.cfg  vesamenu.c32
```