I will publish a shell script created to reduce the trouble of creating LiveUSB on Linux

The source code is described at the bottom of the article, so please save it in a file and use it.

Execution method

Execute the following command.

   sudo sh "createLiveMedia.sh path" "iso file path"

It is an iso file, but any other file is possible.

Method of operation

(1) A list of connected devices will be displayed as shown below. Enter the index of the device to write to (the displayed device will change depending on your environment).

INDEX TRAN     SIZE MODEL            NAME
    1 sata   465.8G HGST HTS545050A7 /dev/sda
    2 sata   119.2G SSM128GPTC0-S91  /dev/sdb
    3 usb      2.7T HDCZ-UT          /dev/sdc
    4 usb     60.4G Multi-Card       /dev/sdd
    5 usb      3.8G Cruzer Colors+   /dev/sde
    6 sata    1024M DVD-RAM UJ8E2    /dev/sr0
Specify the index of the device to write to:

(2) A list of mounted partitions and a confirmation message asking if you want to unmount are displayed as shown below (if the mounted partition does not exist, it will be skipped).

/dev/sde1
There is a mounted partition. Are you sure you want to unmount it?[y/n]:

(3) A confirmation message will be displayed asking if you want to write.

All data on the specified device will be deleted. Is it OK?[y/n]:

(4) When writing is completed, "Writing is completed." Is displayed.

Applicable license

This shell script is published under a textbook license (original license).

By using this product, it is considered that you have accepted the textbook license (see the article below for details).

(Terms of use) There are no conditions that must be followed in order to use this product.
(Disclaimer 1) The right holder of this product shall not be liable for any damage caused by using this product.
(Disclaimer 2) We do not guarantee that this product can be used in any case.

(Assumed question and answer)
Q: What kind of action corresponds to "using this product"?
A: All actions related to this product are applicable.

Source code

createLiveMedia.sh


#!/bin/sh
#Since this shell script contains operations that require administrator privileges (umount command and writing to device files), it requires administrator privileges at runtime.
#The umount command is not required, but if it is not unmounted, the corresponding device of Linux after processing is not recognized (it will be treated as mounting a partition that does not exist).
#The sync command is not required, but if delayed writing has been performed, it may be displayed as completed even though writing is in progress.
#Writing to optical media is possible, but limited (DVD can be written normally)+RW, DVD-RAM, BD-Only 3 types of RE. However, all of them cannot be written in the unformatted state).

println(){
    printf "%s" "$1"
    echo ""
}

throwException(){
    printf "%s" "$1"
    echo ""
    exit 1
}

[ "$(uname)" != "Linux" ] && throwException "This shell script contains commands that can only be used on Linux and cannot be executed on your OS."
[ -z "$(type lsblk 2> /dev/null)" ] && throwException "The lsblk command is not installed in your environment."
[ -z "$(type umount 2> /dev/null)" ] && throwException "The umount command is not installed in your environment."
[ -z "$(type sync 2> /dev/null)" ] && throwException "The sync command is not installed in your environment."
[ $# -ne 1 ] && throwException "The number of arguments is invalid. The number of arguments must always be 1."

isoFile=$1
[ ! -e "${isoFile}" ] && throwException "The specified file was not found."
[ "$(id -u)" -ne 0 ] && throwException "This shell script contains operations that require administrator privileges and cannot be executed by general users."

view="$(lsblk -d -l -p -o TRAN,SIZE,MODEL,NAME)"
view="$(println "${view}" | nl -v 0 -w 5 -s " " | sed -e "1 s/^ *0/INDEX/")"
deviceList="$(println "${view}" | sed -e "1d" | sed -e "s/^.* \{1,\}//")"
[ -z "${deviceList}" ] && throwException "There were no devices to choose from."

println "${view}"
printf "%s" "Specify the index of the device to write to:"
read -r index
index="$(println "${index}" | grep "^[1-9][0-9]*$")"
[ -z "${index}" ] && throwException "Enter the number displayed in the INDEX column."
[ "${index}" -gt "$(println "${deviceList}" | wc -l)" ] && throwException "Enter the number displayed in the INDEX column."

deviceFile="$(println "${deviceList}" | sed -n "${index}p")"
partitionList="$(lsblk "${deviceFile}" -l -n -p -o NAME)"
partitionCount="$(println "${partitionList}" | wc -l)"
while [ "${partitionCount}" -ge 1 ]; do
    partitionFile="$(println "${partitionList}" | sed -n "${partitionCount}p")"
    mountDir="$(lsblk "${partitionFile}" -d -l -n -o MOUNTPOINT 2> /dev/null)"
    if [ -z "${mountDir}" ]; then
        partitionList="$(println "${partitionList}" | sed -e "${partitionCount}d")"
    fi
    partitionCount="$((partitionCount - 1))"
done
if [ -n "${partitionList}" ]; then
    println "${partitionList}"
    printf "%s" "There is a mounted partition. Are you sure you want to unmount it?[y/n]:"
    read -r canUnmount
    canUnmount="$(println "${canUnmount}" | grep "^[Yy]$")"
    [ -z "${canUnmount}" ] && throwException "Processing was canceled because something other than y was entered."

    partitionCount="$(println "${partitionList}" | wc -l)"
    while [ "${partitionCount}" -ge 1 ]; do
        partitionFile="$(println "${partitionList}" | sed -n "${partitionCount}p")"
        [ -n "$(umount "${partitionFile}" 2>&1)" ] && throwException "Processing was canceled because an error occurred during unmounting."

        partitionCount="$((partitionCount - 1))"
    done
fi

printf "%s" "All data on the specified device will be deleted. Is it OK?[y/n]:"
read -r canExecute
canExecute="$(println "${canExecute}" | grep "^[Yy]$")"
[ -z "${canExecute}" ] && throwException "Processing was canceled because something other than y was entered."

dd bs=64k if="${isoFile}" of="${deviceFile}" && sync && println "Writing is complete."

Postscript

--You may find it strange to write command options and regular expressions, but this is because we are POSIX compliant as much as possible when creating this shell script. Specifically, it is as follows. --The syntax used in this shell script is POSIX compliant. --Regarding the commands used in this shell script and their options, the commands specified in POSIX are POSIX compliant, including options. --The regular expressions used in this shell script conform to POSIX's BRE (Basic Regular Expression). --You are always welcome to point out or question this article and this shell script. However, we do not always reply.

Recommended Posts

I will publish a shell script created to reduce the trouble of creating LiveUSB on Linux
[Shell startup] I tried to display the shell on the TV with a cheap Linux board G-cluster
Conditional branch due to the existence of a shell script file
Creating a shell script to write a diary
I measured the run queue wait time of a process on Linux
Verification of how to periodically execute a script on a Linux server on Windows
How to access the contents of a Linux disk on a Mac (but read-only)
I wrote a script to revive the gulp watch that will die soon
I made a script to record the active window using win32gui of Python
I created a script to check if English is entered in the specified position of the JSON file in Python.
The story of Linux that I want to teach myself half a year ago
I want to take a screenshot of the site on Docker using any font
"Stop committing Japanese files to git on Mac> <" For the time being, I wrote a script to search for incompatible Japanese files on Mac and Linux.
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
I tried to create a Python script to get the value of a cell in Microsoft Excel
[Linux] Copy data from Linux to Windows with a shell script
I made a function to check the model of DCGAN
I wrote a script to combine the divided ts files
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
Process the contents of the file in order with a shell script
I tried to create an environment of MkDocs on Amazon Linux
[Linux] I tried to summarize the command of resource confirmation system
Shell script (Linux, macOS) that outputs the date of the last week
I created a Python library to call the LINE WORKS API
Create a shell script to run the python file multiple times
On Linux, the time stamp of a file is a little past.
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
A note I was addicted to when making a beep on Linux
I just wrote a script to build Android on another machine
How to output the output result of the Linux man command to a file
I tried to register a station on the IoT platform "Rimotte"
A command to easily check the speed of the network on the console
How to update the python version of Cloud Shell on GCP
I found out by making a python script to record radiko while reading the code of the predecessors
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
I made a POST script to create an issue on Github and register it in the Project
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
I refactored "I tried to make a script that saves posted images at once by going back to the tweets of a specific user on Twitter".
A story I was addicted to trying to install LightFM on Amazon Linux
I made a kitchen timer to be displayed on the status bar!
[Linux] Command to get a list of commands executed in the past
Semi-automatically generate a description of the package to be registered on PyPI
AtCoder writer I wrote a script to aggregate the contests for each writer
I want to sort a list in the order of other lists
Hook to Shared Library on Linux to interrupt the behavior of existing binaries
A shell script to make sure you don't forget the pipenv shell again
I want to leave an arbitrary command in the command history of Shell
[Linux] I tried to verify the secure confirmation method of FQDN (CentOS7)
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
I tried to verify the result of A / B test by chi-square test
Python: I want to measure the processing time of a function neatly
Folding @ Home on Linux Mint to contribute to the analysis of the new coronavirus
I made a function to see the movement of a two-dimensional array (Python)
The story of creating Botonyan that returns the contents of Google Docs in response to a specific keyword on Slack
I created an SFTP-only user on Linux.
Script to change the description of fasta
A quick overview of the Linux kernel
I want to use Linux on mac
[Ubuntu] How to execute a shell script