カテゴリー
Linux

LinuxでWindows10のインストールUSBを作るシェルスクリプトを作った

使い方

Windows10のインストールUSBを作る記事のコマンドをシェルスクリプトにまとめました。以下のコードを適当なファイル(mkwin10usb.shとか)に保存し、chmod +x で実行権限を付与した上で、管理者権限で実行します。引数なしで実行すると使い方が表示されます。第一引数にWindows10のインストールISOのパスを、第二引数にUSBのブロックデバイス(/dev/sdbとか)を指定します。第二引数で指定したUSBメモリのデータは完全に削除されるので気をつけてください。また、パーティション操作時にキー入力が必要ですので、数回入力してください。

コード本体

#!/bin/bash

# execute command with prompt
function run() {
    echo -e "\033[30;1m# $@\033[0m"
    $@
}

# confirm the required command exists
function confirm_command() {
    which $1 > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "error: command '$1' is required. Please install it before continue."
        exit 1
    fi
}

# argument check
if [ $# -ne 2 ]; then
    echo "usage: $0 /path/to/Windows10.iso /dev/sdX"
    exit 1
fi

# permission check
if [ "$USER" != "root" ]; then
    echo "error: root permission required"
    exit 1
fi

# map arguments to variables
ISO_IMAGE=$1
BLOCK_DEVICE=$2
WORKDIR_ROOT=/tmp/mkwin10usb

if [ ! -e "${ISO_IMAGE}" ]; then
    echo "error: no such file: $ISO_IMAGE"
    exit 1
fi

if [ ! -e "${BLOCK_DEVICE}" ]; then
    echo "error: no such file: ${BLOCK_DEVICE}"
    exit 1
fi

confirm_command parted
confirm_command mkfs.vfat
confirm_command mkfs.ntfs

# make confirmation before operation
echo "This script creates Windows10 installation USB memstick:"
echo -e "\033[32;1m*\033[0m windows10 iso image path: ${ISO_IMAGE}"
echo -e "\033[32;1m*\033[0m all data on ${BLOCK_DEVICE} will be deleted and create following partitions:"
echo "  - ${BLOCK_DEVICE}1 (FAT32): for uefi boot"
echo "  - ${BLOCK_DEVICE}2 (NTFS): for windows10 installation data"
echo -e "\033[32;1m*\033[0m working directory: ${WORKDIR_ROOT}"
echo
echo -e "\033[33;1mWARNING: all data on ${BLOCK_DEVICE} will be deleted and cannot be recovered.\033[0m"
echo -n "Are you sure to continue? (Yes/No): "
while [ true ]; do
    read ans
    case "$ans" in
        Yes) break ;;
        No) exit 1 ;;
        *) echo -n "Please type 'Yes' or 'No': " ;;
    esac
done

# prepare partitions
echo
echo -e "\033[32;1m*\033[0m (1/6) preparing partitions on ${BLOCK_DEVICE}..."
echo
echo -e "\033[34;1mNOTICE: Please type 'Ignore' or 'Yes' to continue, if asked.\033[0m"
echo
run parted $BLOCK_DEVICE mklabel gpt
run parted $BLOCK_DEVICE mkpart primary fat32 0G 1G
run parted $BLOCK_DEVICE name 1 UEFI_Boot
run parted $BLOCK_DEVICE mkpart primary ntfs 1G 100%
run parted $BLOCK_DEVICE name 2 Win10
run parted $BLOCK_DEVICE print
run mkfs.vfat ${BLOCK_DEVICE}1
run mkfs.ntfs -f ${BLOCK_DEVICE}2

# mount partitions and iso image
echo
echo -e "\033[32;1m*\033[0m (2/6) mount devices and iso image..."
echo "  - ${ISO_IMAGE} => ${WORKDIR_ROOT}/loop... "
echo "  - ${BLOCK_DEVICE}1 => ${WORKDIR_ROOT}/uefi_boot"
echo "  - ${BLOCK_DEVICE}2 => ${WORKDIR_ROOT}/win10"
run mkdir -p $WORKDIR_ROOT/{uefi_boot,win10,loop}
run mount -t udf -o ro,loop "$ISO_IMAGE" ${WORKDIR_ROOT}/loop
run mount -t vfat ${BLOCK_DEVICE}1 ${WORKDIR_ROOT}/uefi_boot
run mount -t ntfs ${BLOCK_DEVICE}2 ${WORKDIR_ROOT}/win10

# copy files
echo
echo -e "\033[32;1m*\033[0m (3/6) copying files to ${WORKDIR_ROOT}/uefi_boot..."
run cp -r \
  ${WORKDIR_ROOT}/loop/{autorun.inf,boot,bootmgr,bootmgr.efi,efi,setup.exe,support} \
  ${WORKDIR_ROOT}/uefi_boot
run mkdir ${WORKDIR_ROOT}/uefi_boot/sources
run cp -r ${WORKDIR_ROOT}/loop/sources/boot.wim \
  ${WORKDIR_ROOT}/uefi_boot/sources/boot.wim

echo
echo -e "\033[32;1m*\033[0m (4/6) copying files to ${WORKDIR_ROOT}/win10..."
run cp -r ${WORKDIR_ROOT}/loop/sources ${WORKDIR_ROOT}/win10

# wait for i/o compolete
echo
echo -e "\033[32;1m*\033[0m (5/6) waiting for I/O to be completed (this may take a while)..."
run sync

# umount and delete working directories
echo
echo -e "\033[32;1m*\033[0m (6/6) unmounting and clearning working directories..."
run umount ${WORKDIR_ROOT}/{uefi_boot,win10,loop}
run rmdir ${WORKDIR_ROOT}/{uefi_boot,win10,loop,}

echo
echo "Completed! Now you can unplug the USB stick."
echo "Have fun!"

コメントを残す

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

CAPTCHA