The source code is described at the bottom of the article, so please save it in a file and use it.
Execute the following command.
sudo sh "createLiveMedia.sh path" "iso file path"
It is an iso file, but any other file is possible.
(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.
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.
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."
--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