#!/bin/bash # Ikki_USB_Creator.sh - Créer une clé USB bootable avec une partition persistante depuis un fichier iso # Copyright (C) 2007-2023 Julien Billard # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # https://ikkiboot.tuxfamily.org # Version: 2.4 - 09/12/2022 export LANG=C set -e label="Données" script=$0 block_size=4 # Aide usage() { cat </dev/null ; then if [ $block_size -lt 1 ] || [ $block_size -gt 100 ]; then echo echo "La taille des blocks doit être comprise en 1 et 100" echo exit 1 fi else echo echo "La taille des blocks doit être un entier" echo exit 1 fi fi # Test si le fichier est un .iso len=${#iso} len=$((len-3)) if [ "${iso:len}" != "iso" ]; then usage exit 1 fi # Vérifie si le fichier ISO existe if [ ! -f $iso ]; then echo echo "Le fichier $iso n'existe pas"; echo exit 1 fi # Vérifie si le fichier ISO est dans le même répertoire que le script if [ $(dirname $iso) != $(dirname $script) ]; then echo echo "Le fichier ISO doit être dans le même répertoire que ce script" echo exit 1 fi if [ -z $target ]; then { # Liste les périphériques amovible de type disque list_key=$(lsblk -nbdpo NAME,SIZE,TYPE,HOTPLUG | grep -Ev " 0.disk" | grep -E .disk.*1$) } || { echo echo "Aucune clé USB n'a été détecté" echo exit 1 } # Nombre de disque amovible détecté nbr_word=$(echo $list_key | wc -w) nbr_key=$(echo $((nbr_word/4))) if [ $nbr_key -eq 1 ]; then target=$(echo $list_key | awk '{print $1}') target_space=$(echo $list_key | awk '{print $2}') echo echo "Une clé USB a été détectée :" lsblk -dpo MODEL,NAME,SIZE | grep -E $target echo while [ "$confirmation" != "o" ] && [ "$confirmation" != "n" ] ; do read -p "Confirmez-vous l'utilisation de cette clé o/n ? " confirmation done elif [ $nbr_key -gt 1 ]; then echo echo "Plusieurs clés USB ont été détectées :" echo lsblk -dpo MODEL,NAME,SIZE,TYPE,HOTPLUG | grep -Ev " 0.?.disk" | grep -E .disk.*1$ | rev | cut -d ' ' -f3- | rev | sed s/disk/\/g echo echo "Une seule clé USB doit être branchée" echo exit 1 fi else #T est $target { list_key=$(lsblk -nbdpo NAME,SIZE,TYPE,HOTPLUG | grep -E $target.*disk.*1$) } || { echo echo "Le périphérique de destination $target n'est pas amovible" echo exit 1 } echo echo "Périphérique de destination :" lsblk -dpo MODEL,NAME,SIZE | grep -E $target target_space=$(echo $list_key | awk '{print $2}') echo while [ "$confirmation" != "o" ] && [ "$confirmation" != "n" ] ; do read -p "Confirmez-vous l'utilisation de cette clé o/n ? " confirmation done fi if [ $confirmation = "n" ]; then exit 1 fi # Vérifie si la clé est en cours d'utilisation target_use=$(mount | grep $target | awk '{print $3}') if [ -n "$target_use" ]; then echo echo "La clé USB est actuellement utilisé dans :" echo "$target_use" echo echo "Démontez la clé USB et relancez le script" exit 1 fi iso_space=$(du -b $iso | awk '{print $1}') # Vérifie la taille de la clé par rapport au fichier ISO if [ $iso_space -gt $target_space ]; then echo echo "Une clé d'une plus grande capacité est nécessaire pour ce fichier ISO" echo exit 1 fi # On écrit l'image ISO sur la clé USB target_space=$(echo $(($target_space/1024/1024/1024))) while [ "$confirmation" != "OUI" ] && [ "$confirmation" != "NON" ] ; do echo read -p "Etes-vous sûr de vouloir effacer le disque $target de $target_space Go (OUI/NON) : " confirmation done if [ "$confirmation" == "OUI" ] ; then echo echo "1) Ecriture de l'image ISO sur la clé" echo echo "dd if=$iso of=$target bs="$block_size"M status=progress && sync" dd if=$iso of=$target bs="$block_size"M status=progress && sync else exit 1 fi sleep 5 echo echo "2) Création de la partition NTFS sur $target" # Test du nombre de partition count_part=$(lsblk $target | wc -l) list_count_part=$(echo $((count_part-2))) if [ $list_count_part -eq 3 ] ; then # On supprime la 3ème partition ( echo d # Del partition echo 3 # Partition number echo w # Write changes ) | fdisk $target fi # On créé la partition NTFS ( echo n # Add a new partition echo p # Primary partition echo 3 # Partition number echo # First sector (Accept default) echo # Last sector (Accept default) echo t # Type echo 3 # Partition number echo 7 # NTFS sleep 5 echo w # Write changes echo q # Quit ) | fdisk $target # On attend que la 3ème partition soit détectée until fdisk -l | grep "$target"3 1> /dev/null do sleep 1 done # Formatage de la partition NTFS mkfs.ntfs -L $label -Q -p 0 -H 0 -S 0 -c 4096 "$target"3 sync echo echo "La clé USB a été créé avec succès" echo