Files
arch-dotfiles/scripts/utilities/format.sh

148 lines
4.1 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
#
# usb-format.sh — formate proprement une clé USB / carte SD
# Usage: usb-format.sh /dev/sdX [fat32|exfat|ext4] [LABEL]
set -euo pipefail
DEVICE="${1:-}"
FSTYPE="${2:-fat32}"
LABEL="${3:-}"
if [[ -z "$DEVICE" ]]; then
echo "Usage: $0 /dev/sdX [fat32|exfat|ext4] [LABEL]"
echo
echo "Périphériques disponibles :"
lsblk -d -o NAME,SIZE,MODEL,TRAN
exit 1
fi
# Normaliser le chemin (accepte "sdb" ou "/dev/sdb")
[[ "$DEVICE" != /dev/* ]] && DEVICE="/dev/$DEVICE"
# --- Garde-fous ---
if [[ ! -b "$DEVICE" ]]; then
echo "Erreur : $DEVICE n'est pas un périphérique bloc valide."
exit 1
fi
if [[ "$DEVICE" == "/dev/nvme0n1" || "$DEVICE" =~ ^/dev/nvme0n1p[0-9]+$ ]]; then
echo "Erreur : $DEVICE correspond au disque interne (nvme0n1). Abandon."
exit 1
fi
ROOT_SRC=$(findmnt -n -o SOURCE / || true)
if [[ -n "$ROOT_SRC" && "$ROOT_SRC" == "$DEVICE"* ]]; then
echo "Erreur : $DEVICE semble contenir la partition racine. Abandon."
exit 1
fi
TRAN=$(lsblk -d -no TRAN "$DEVICE" 2>/dev/null || echo "?")
if [[ "$TRAN" != "usb" ]]; then
echo "Attention : $DEVICE n'est pas détecté comme périphérique USB (TRAN=$TRAN)."
fi
case "$FSTYPE" in
fat32|exfat|ext4) ;;
*)
echo "Erreur : type de système de fichiers non supporté : $FSTYPE"
echo "Choix possibles : fat32, exfat, ext4"
exit 1
;;
esac
if [[ -z "$LABEL" ]]; then
read -rp "Nom de la partition (label) : " LABEL
LABEL="${LABEL:-USB}"
fi
if [[ "$FSTYPE" == "fat32" && ${#LABEL} -gt 11 ]]; then
echo "Attention : FAT32 limite le label à 11 caractères, il sera tronqué."
fi
# --- Nouvelle demande : type de table de partition ---
echo
echo "Choisissez le type de table de partition :"
echo " [1] msdos (MBR) recommandé pour la compatibilité avec les vieux appareils (imprimantes, consoles)"
echo " [2] gpt pour les disques >2To ou l'UEFI moderne"
read -rp "Votre choix (1 ou 2, défaut=1) : " PT_CHOICE
PT_CHOICE="${PT_CHOICE:-1}"
case "$PT_CHOICE" in
1|msdos|mbr) PART_TABLE="msdos" ;;
2|gpt) PART_TABLE="gpt" ;;
*)
echo "Choix invalide, utilisation de msdos par défaut."
PART_TABLE="msdos"
;;
esac
echo "-> Table de partition choisie : $PART_TABLE"
echo "==================================================="
echo " Périphérique : $DEVICE"
echo " Transport : $TRAN"
echo " Système de fichiers: $FSTYPE"
echo " Label : $LABEL"
echo " Table de partition : $PART_TABLE"
echo "==================================================="
lsblk -f "$DEVICE"
echo
echo "TOUTES LES DONNÉES SUR $DEVICE VONT ÊTRE EFFACÉES."
read -rp "Tape le nom du périphérique ($DEVICE) pour confirmer : " CONFIRM
if [[ "$CONFIRM" != "$DEVICE" ]]; then
echo "Confirmation invalide. Abandon."
exit 1
fi
# --- Nettoyage et préparation ---
echo "--> Démontage des partitions existantes..."
for part in "${DEVICE}"?*; do
[[ -b "$part" ]] && sudo umount "$part" 2>/dev/null || true
done
echo "--> Suppression des signatures existantes..."
sudo wipefs -a "$DEVICE"
# --- CORRECTION : forcer le rechargement du périphérique avant parted ---
echo "--> Relecture de la table par le noyau..."
sudo partprobe "$DEVICE" 2>/dev/null || true
sudo udevadm settle
sleep 1
echo "--> Création d'une nouvelle table de partitions ($PART_TABLE)..."
sudo parted -s "$DEVICE" mklabel "$PART_TABLE"
# --- Création de la partition ---
echo "--> Création de la partition..."
sudo parted -s "$DEVICE" mkpart primary 1MiB 100%
sudo partprobe "$DEVICE"
sudo udevadm settle
sleep 1
# Déterminer le nom de la première partition
PART="${DEVICE}1"
[[ "$DEVICE" =~ nvme|mmcblk ]] && PART="${DEVICE}p1"
# Attendre que la partition soit créée
sleep 1
if [[ ! -b "$PART" ]]; then
echo "Erreur : la partition $PART n'a pas été créée."
exit 1
fi
echo "--> Formatage en $FSTYPE..."
case "$FSTYPE" in
fat32)
sudo mkfs.vfat -F32 -n "${LABEL^^}" "$PART"
;;
exfat)
sudo mkfs.exfat -n "$LABEL" "$PART"
;;
ext4)
sudo mkfs.ext4 -L "$LABEL" "$PART"
;;
esac
echo "--> Terminé."
lsblk -f "$DEVICE"