Construie une clé USB bootable, pour déploier une debian.

From Tuxunix
Jump to: navigation, search
   1.#! /bin/bash
   2.      
   3.#
   4.#--@--Construction de la clé USB--@--
   5.#
   6.      
   7.#
   8.#Variables
   9.#
  10.defaultvar()
  11.{
  12.export EXT_TMP=`mktemp -d /tmp/selfextract.XXXXXX`
  13.PATH_INITRD=$EXT_TMP/PKGUSB/Plx-Usb/initrd/initrd
  14.PATH_KERNEL=$EXT_TMP/PKGUSB/Plx-Usb/kernel/bzImage
  15.PATH_PLXDEB=$EXT_TMP/PKGUSB/Plx-Sarge.tgz
  16.PATH_GRUB=$EXT_TMP/PKGUSB/Plx-Usb/grub
  17.mkdir /tmp/MP 2> /dev/null
  18.PATH_PART=/tmp/MP/
  19.#A initialiser
  20.LDCPLX_BOOT="ldcplxboot"
  21.PATH_SRC_LDCPLX_BOOT="$EXT_TMP/PKGUSB/Plx-Usb/grub/boot"
  22.LDCPLX_BOOT_VERSION=`cat $PATH_SRC_LDCPLX_BOOT/$LDCPLX_BOOT`
  23.}
  24.       
  25.#
  26.#Information peripherique USB
  27.#
  28.infodevice()
  29.{
  30.ERROR=1
  31.while [ ${ERROR}|> != "0" ]
  32.do
  33.    echo "Device USB key is recognized sda,sdb,or sdc... ?"
  34.    echo "1 - SDA"
  35.    echo "2 - SDB"
  36.    echo "3 - SDC"
  37.    echo "4 - SDD"
  38.    echo "--------"
  39.    echo "5 - Exit"
  40.    echo -n "Your choice :"
  41.    read CHOIX_DEVICE
  42.    case $CHOIX_DEVICE in
  43.        1) DEVICE=sda
  44.            etatdisk
  45.      
  46.        ;;
  47.        2) DEVICE=sdb
  48.            etatdisk
  49.      
  50.        ;;
  51.        3) DEVICE=sdc
  52.            etatdisk
  53.        ;;
  54.        4) DEVICE=sdd
  55.            etatdisk
  56.        ;;
  57.        5) exit 0
  58.        ;;
  59.        *)
  60.        ERROR=1
  61.        echo "Invalid Disk choice"
  62.        ;;
  63.     esac
  64.done
  65.}
  66.       
  67.#
  68.#Etat du disque
  69.#
  70.etatdisk()
  71.{
  72.      
  73.#verif key
  74.DISQUE_ETAT=`fdisk -l /dev/$DEVICE`
  75.if [ ! -n "$DISQUE_ETAT" ]
  76.then
  77.      
  78.    echo "WARNING!! device not found!"
  79.    ERROR=1
  80.else
  81.    #Test de la cle minimum 512Mo
  82.    #
  83.    DISQUE_SIZE=`fdisk -s /dev/$DEVICE`
  84.    #strictement inferieur
  85.    if [ $DISQUE_SIZE -lt "500000" ]
  86.    then
  87.       
  88.        echo "WARNING key very small (size minimum 512Mo)"
  89.        ERROR=1
  90.        exit
  91.    else
  92.        ERROR=0
  93.    fi
  94. fi
  95.       
  96.}
  97.       
  98.#
  99.#Construction usb key
 100.#
 101.construc_usbkey()
 102.{
 103.       
 104.    REP=""
 105.    while [ -z ${REP}|> ]
 106.    do
 107.        echo "you chose partition construction of usb key:"
 108.        echo "1 - USB-KEY (All construction - First use)"
 109.        echo "2 - USB-KEY-PART2 (Update partition 2 [Plx-DEB])"
 110.        #echo "3 - USB-KEY-PART1 (Update partition 1 [initrd/kernel/grub])"
 111.        echo "3 - Cancel"
 112.        echo -n "Your Choice : "
 113.        read CHOIX_PART
 114.        case $CHOIX_PART in
 115.                1) SD=/dev/${DEVICE}|>
 116.                    construcpartall
 117.                ;;
 118.       
 119.                2) SD=/dev/${DEVICE}|>
 120.                  PROCEDURE_CONSTRUC=construcpart2
 121.                  #Verifie la version sur la partition 1
 122.                  verif_version
 123.                ;;
 124.       
 125.                3) exit 0
 126.                ;;
 127.       
 128.                *)
 129.                ;;
 130.        esac
 131. done
 132.     
 133.}
 134.       
 135.       
 136.#
 137.#Construction part1
 138.#
 139.construcpart1()
 140.{
 141.    echo "WARNING !! this command will RAZ the partition 1 of USB KEY,
 142.    are you sure to continue ? (y/n)"
 143.    read REP
 144.    if [ "$REP" != "y" ]
 145.    then
 146.        exit 0
 147.    fi
 148.       
 149.    NUM_DEVICE_PART=/dev/${DEVICE}|>1
 150.    echo "$SP"
 151.       
 152.    echo "Create partition 1..."
 153.    create_partition1
 154.       
 155.    #format ext2
 156.    echo "format partition 1..."
 157.    mke2fs ${NUM_DEVICE_PART}|>
 158.    echo "${NUM_DEVICE_PART}"
 159.      
 160.    #monte la partition 1
 161.    echo "mount $PATH_PART"
 162.    mount ${NUM_DEVICE_PART}|> $PATH_PART
 163.    #install grub
 164.    echo "grub install..."
 165.    #copie stage1,stage2,menu.lst...
 166.    cp -r $PATH_GRUB/\* $PATH_PART
 167.    echo "cp initrd..."
 168.    #cp initrd
 169.    cp $PATH_INITRD $PATH_PART/boot/
 170.    echo "cp kernel..."
 171.    #cp kernel
 172.    cp $PATH_KERNEL $PATH_PART/boot/
 173.    echo "construction grub..."
 174.    #lance la commande grub
 175.    cd /
 176.    #
 177.    /tmp/MP/boot/grub-1 --batch << EOT
 178.    root (hd1,0)
 179.    setup (hd1)
 180.    quit
 181.    EOT
 182.       
 183.    cd /
 184.       
 185.    #umount de la cle
 186.    echo "umount $PATH_PART"
 187.    umount $PATH_PART
 188.    echo "Finish..."
 189.       
 190.}
 191.       
 192.       
 193.#
 194.#Construction part2
 195.#
 196.construcpart2()
 197.{
 198.       
 199.    echo "WARNING !! this command will RAZ the partition 2 of USB KEY,
 200.    are you sure to continue ? (y/n)"
 201.    read REP
 202.    if [ "$REP" != "y" ]
 203.    then
 204.        exit
 205.    fi
 206.       
 207.    NUM_DEVICE_PART=/dev/${DEVICE}|>2
 208.    echo "$NUM_DEVICE_PART"
 209.    #create partition 2
 210.    create_partition2
 211.      
 212.    echo "format partition2..."
 213.    mke2fs $NUM_DEVICE_PART
 214.       
 215.    #mount part 2
 216.    echo "mount $PATH_PART"
 217.    mount $NUM_DEVICE_PART $PATH_PART
 218.       
 219.    #copie des fichiers
 220.    echo "Copy file Plx-Sarge.tgz..."
 221.    cp $PATH_PLXDEB $PATH_PART
 222.       
 223.    #umount de la cle
 224.    echo "umount $PATH_PART"
 225.    umount $PATH_PART
 226.    echo "Finish..."
 227.      
 228.       
 229.}
 230.       
 231.#
 232.#Construction part all
 233.#
 234.construcpartall()
 235.{
 236.    echo "WARNING !! this command will RAZ the USB KEY, are you sure to continue? (y/n)"
 237.    read REP
 238.    if [ "$REP" != "y" ]
 239.    then
 240.        exit
 241.    fi
 242.       
 243.       
 244.       
 245.    #echo "RAZ du disk USB key"
 246.      
 247.    raz_disk
 248.       
 249.    echo "Construction ${DEVICE}1"
 250.    construcpart1
 251.       
 252.    echo "Construction ${DEVICE}2"
 253.    construcpart2
 254.      
 255.}
 256.       
 257.       
 258.raz_disk()
 259.{
 260.       
 261.       
 262.#SD=/dev/${DEVICE}
 263.      
 264.echo "Destroy disque $SD"
 265.dd if=/dev/zero of=$SD bs=1k count=10
 266.       
 267.}
 268.       
 269.create_partition1()
 270.{
 271.    #echo "Storage partitioning..."
 272.    echo "Create partition ${NUM_DEVICE_PART}"
 273.    fdisk $SD << EOF
 274.       
 275.    n
 276.    p
 277.    1
 278.    1
 279.    +50M
 280.      
 281.    a
 282.    1
 283.     
 284.    w
 285.      
 286.    EOF
 287.      
 288.}
 289.       
 290.       
 291.create_partition2()
 292.{
 293.    #echo "Storage partitioning..."
 294.    echo "Create partition ${NUM_DEVICE_PART}"
 295.    fdisk $SD << EOF
 296.    n
 297.    p
 298.    2
 299.       
 300.       
 301.    w
 302.       
 303.    EOF
 304.       
 305.}
 306.       
 307.raz_rep()
 308.{
 309.    #raze du repertoire MP
 310.    rm -r /tmp/MP 2> /dev/null
 311.    rm -r /tmp/selfextract* 2> /dev/null
 312.    echo "USB KEY READY"
 313.    #exit 0
 314.}
 315.       
 316.verif_version()
 317.{
 318.       
 319.#VERIF de la version de la cle
 320.#
 321.      
 322.mount /dev/${DEVICE}|>1 $PATH_PART 2> /dev/null
 323.PATH_FIC_USB=`cat /tmp/MP/boot/$LDCPLX_BOOT 2> /dev/null`
 324.      
 325.if test -e /tmp/MP/boot/$LDCPLX_BOOT
 326.then
 327.    if [ "$LDCPLX_BOOT_VERSION" != "$PATH_FIC_USB" ]
 328.    then
 329.        echo "Old version!"
 330.        echo "You must choice all construction!"
 331.        umount $PATH_PART
 332.        REP=""
 333.    else
 334.        umount $PATH_PART
 335.        $PROCEDURE_CONSTRUC
 336.    fi
 337.else
 338.    echo "No file version"
 339.    echo "You must choice all construction!"
 340.    umount $PATH_PART 2> /dev/null
 341.    REP=""
 342.fi
 343.       
 344.}
 345.       
 346.#
 347.#Main
 348.#
 349.     
 350.defaultvar
 351.      
 352.infodevice
 353.      
 354.#etatdisk
 355.      
 356.construc_usbkey
 357.      
 358.raz_rep