diff --git a/baleine.sh b/baleine.sh old mode 100755 new mode 100644 index 6981026..91b3aef --- a/baleine.sh +++ b/baleine.sh @@ -27,18 +27,18 @@ case "$1" in #Si on a apelé ./baleine.sh container create par exemple, on va apeller le script container.sh qui va traiter les #opérations sur les containers, et $@ permet de lui passer tous les arguments qu'on a passé à baleine.sh #autrement dit, ./baleine.sh container create apellera le script container.sh avec les arguments $1=container et $2=create - bash container.sh "$@" + bash container.sh "${@:2}" ;; "image") bash image.sh "${@:2}" ;; "bridge") - bash bridge.sh "$@" + bash bridge.sh "${@:2}" ;; "help") - bash help.sh "$@" + bash help.sh ;; "test") - bash test.sh "$@" + bash test.sh "${@:2}" ;; esac \ No newline at end of file diff --git a/bridge.sh b/bridge.sh index b538a31..7b117e3 100644 --- a/bridge.sh +++ b/bridge.sh @@ -1,21 +1,21 @@ #!/bin/bash -case $2 in +case $1 in "create") #Si on veut creer un bridge (qu'on a apellé le script ./balaine.sh bridge create) - bash create_bridge.sh "$@" + bash create_bridge.sh "${@:2}" ;; "list") #Si on veut lister les bridges (qu'on a apellé le script ./balaine.sh bridge list) - bash list_bridges.sh "$@" + bash list_bridges.sh "${@:2}" ;; "remove") - bash remove_bridge.sh "$@" + bash remove_bridge.sh "${@:2}" ;; "up") - bash up_bridge.sh "$@" + bash up_bridge.sh "${@:2}" ;; "down") - bash down_bridge.sh "$@" + bash down_bridge.sh "${@:2}" esac \ No newline at end of file diff --git a/container.sh b/container.sh index a2ad275..3a401d4 100644 --- a/container.sh +++ b/container.sh @@ -1,26 +1,26 @@ #!/bin/bash -case $2 in +case $1 in "create") #Si on veut creer un container (qu'on a apellé le script ./baleine.sh container create) - bash create_container.sh "$@" + bash create_container.sh "${@:2}" ;; "list") #Si on veut lister les containers qui tournent (qu'on a apellé le script ./baleine.sh container list) - bash list_container.sh "$@" + bash list_container.sh "${@:2}" ;; "stop") - bash stop_container.sh "$@" + bash stop_container.sh "${@:2}" ;; "remove") - bash remove_container.sh "$@" + bash remove_container.sh "${@:2}" ;; "restart") - bash restart_container.sh "$@" + bash restart_container.sh "${@:2}" ;; "exec") - bash exec_container.sh "$@" + bash exec_container.sh "${@:2}" ;; esac \ No newline at end of file diff --git a/create_bridge.sh b/create_bridge.sh index dfa29d8..05208ec 100644 --- a/create_bridge.sh +++ b/create_bridge.sh @@ -1,7 +1,11 @@ #!/bin/bash -NOM_BRIDGE=$3; -ADDR_IPV4=$4; - +while getopts b:a: o; do + case $o in + (b) NOM_BRIDGE=$OPTARG;; + (a) ADDR_IPV4=$OPTARG;; + + esac +done #condition sur le nbre d'arguments @@ -11,15 +15,20 @@ if [[ -z "$NOM_BRIDGE" ]]; then fi #création du bridge ip link add $NOM_BRIDGE type bridge + +#attribution d'une adresse ip au bridge ip a add dev $NOM_BRIDGE $ADDR_IPV4 + +#demarrage du bridge ip link set $NOM_BRIDGE down ip link set $NOM_BRIDGE up -FILE= $NOM_BRIDGE.manifest -touch FILE # On crée un fichier contenant les infos du bridge -echo "$NOM_BRIDGE" >> FILE #nom du bridge + +MANIFEST=$NOM_BRIDGE.manifest +echo "nom_bridge:$NOM_BRIDGE" >> $MANIFEST #nom du bridge #condition de bordure pour le dossier Bridges if [[ ! -d "$PATH_MANIFEST/bridges" ]]; then mkdir -p $PATH_MANIFEST/bridges #&& mv FILE $PATH_MANIFEST/bridges fi -mv FILE $PATH_MANIFEST/bridges \ No newline at end of file + +mv $MANIFEST $PATH_MANIFEST/bridges \ No newline at end of file diff --git a/create_container.sh b/create_container.sh index 89e6a2b..9c586e5 100644 --- a/create_container.sh +++ b/create_container.sh @@ -1,5 +1,6 @@ #!/bin/bash -set -x + +set -x while getopts i:c:b:r:a:p: o; do case $o in (i) NOM_IMAGE=$OPTARG;; @@ -8,43 +9,56 @@ while getopts i:c:b:r:a:p: o; do (r) REPERTOIRE=$OPTARG;; (a) ADDRS_IPV4=$OPTARG;; (p) PROGRAM=$OPTARG;; - esac done +echo "Creation du container $NOM_CONTAINER basé sur l'image $NOM_IMAGE lancant le programme $PROGRAM connecté au(x) bridge(s) $BRIDGES avec les adresses $ADDRS_IPV4" +#/usr/local/apache2/bin + #Check existence image, bridge -if [[ -z "$PATH_MANIFEST/images/$NOM_IMAGE.manifest" ]] || [ -z $NOM_IMAGE ]]; then +if [ -z "$PATH_MANIFEST/images/$NOM_IMAGE.manifest" ] || [ -z $NOM_IMAGE ]; then echo "Image non existante." exit fi -if [[ ! -z $NOM_BRIDGE ]] [[ -z "$PATH_MANIFEST/bridges/$NOM_BRIDGE.manifest" ]]; then +if [ ! -z $NOM_BRIDGE ] || [ -z "$PATH_MANIFEST/bridges/$NOM_BRIDGE.manifest" ]; then echo "Bridge non existant." exit fi #On veut récupérer le chemin de l'image -PATH_IMAGE= ${grep chemin $PATH_MANIFEST/images/$NOM_IMAGE.manifest | cut -d':' -f2} +PATH_IMAGE="$(grep chemin $PATH_MANIFEST/images/$NOM_IMAGE.manifest | cut -d':' -f2)" +echo "PATH_IMAGE: $PATH_IMAGE" #on fait une copie dans /var/baleine/images pour mount depuis le conteneur if [[ ! -d "$PATH_BALEINE/containers/$NOM_CONTAINER" ]]; then mkdir -p $PATH_BALEINE/containers/$NOM_CONTAINER fi -cp $PATH_IMAGE $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE + +#Copie de l'image physique dans le container +cp -r $PATH_IMAGE/$NOM_IMAGE $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE +#création du du dossier de montage de l'image du container + +if [[ ! -d "/mnt/baleine/$NOM_CONTAINER" ]]; then + mkdir -p /mnt/baleine/$NOM_CONTAINER +fi #on monte le système de fichiers echo "Montage de l'image" mount -t ext4 -o loop $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE /mnt/baleine/$NOM_CONTAINER #On renseigne le fichier fstab du conteneur -echo "proc /proc proc defaults 0 0" >> /mnt/$NOM_IMAGE/etc/fstab -echo "$PROGRAM" >> /mnt/$NOM_IMAGE/etc/rc.local +echo "proc /proc proc defaults 0 0" >> /mnt/baleine/$NOM_CONTAINER/etc/fstab +echo "$PROGRAM" >> /mnt/baleine/$NOM_CONTAINER/etc/rc.local #On fait le unshare sur l'image passée en paramètre, -nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER $PROGRAM -c "mount /proc" & +#nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER "/bin/bash" -c "mount /proc" & + + + PID=$! echo "j'ai finis le unshare-nohup" @@ -57,7 +71,7 @@ date=${date} echo "nom_container:$NOM_CONTAINER" >> $FILE #nom de son image echo "nom_image:$NOM_IMAGE" >> $FILE #nom de son image -echo "pid:$PID" >> $FILE #Son PID +echo "pid:$PID" >> $FILE #Son PID echo "nom_bridge:$NOM_BRIDGE" >> $FILE #SON BRIDGE echo "starting_time: $date" >> $FILE #starting time @@ -74,21 +88,24 @@ mv $NOM_CONTAINER.manifest $PATH_MANIFEST/containers/$NOM_CONTAINER.manifest #AWK pour récupèrer le nombre d'addresses IPV4 données en arguments #(https://unix.stackexchange.com/questions/144217/counting-comma-separated-characters-in-a-row) -NOMBRE_INTERFACES=echo $ADDRS_IPV4 | awk -F '[,]' '{print NF}' +NOMBRE_INTERFACES="$(echo $ADDRS_IPV4 | awk -F '[,]' '{print NF}')" -NOMBRE_BRIDGES=echo $BRIDGES | awk -F '[,]' '{print NF}' +NOMBRE_BRIDGES="$(echo $BRIDGES | awk -F '[,]' '{print NF}')" -if [ NOMBRE_INTERFACES -ne NOMBRE_BRIDGES ]; do +if [ $NOMBRE_INTERFACES -ne $NOMBRE_BRIDGES ]; then echo "Erreur: Il n'y a pas autant de bridges que d'interfaces réseaux" exit 1 -done +fi for i in $(seq 1 $NOMBRE_INTERFACES); do - ip link add $NOM_CONTAINER_$i type veth peer name eth$i + echo "Nom de l'interface : $NOM_CONTAINER$i" + ip link add $NOM_CONTAINER$i type veth peer name eth$i done echo "J'ai fini de créer les interfaces chef" -NOMBRE_BRIDGES=echo $BRIDGES | awk -F '[,]' '{print NF}' + +#Compte le nombre de bridges présents +NOMBRE_BRIDGES=$(echo $BRIDGES | awk -F '[,]' '{print NF}') IFS="," @@ -103,13 +120,21 @@ for a in $ADDRS_IPV4; do ARRAY_IPV4+=(a) done -for (( i=0 ; i < ${#ARRAY_BRIDGES[*]} ; i++ )); do - ip link set $NOM_CONTAINER_$i master ${ARRAY_BRIDGES[i]} -done +for (( i=0 ; i < ${#ARRAY_IPV4[*]} ; i++ )); do + #Creation de la veth + #ip link add dev $NOM_CONTAINER$i type veth peer name eth$i@$NOM_CONTAINER$i + ip link add dev vif1 type veth peer name eth0@vif1 + #Ajout d'une des extrémités de la veth dans le namespace du container + #ip link set eth$i@$NOM_CONTAINER$i netns proc/$PID/ns/net name eth$i + ip link set eth0@vif1 netns proc/$PID/ns/net name eth0 + #Attribution de l'adresse ip à l'interface du container + #ip netns exec proc/$PID/ns/net ip addr add ${ARRAY_IPV4[i]} dev eth$i + ip netns exec proc/$PID/ns/net ip addr add ${ARRAY_IPV4[i]} dev eth0 + ip netns exec proc/$PID/ns/net ip link set dev eth0 down + ip netns exec proc/$PID/ns/net ip link set dev eth0 up -#création de son interface réseau +done -ip link set $NOM_CONTAINER master $NOM_BRIDGE -ip link set $NOM_CONTAINER up -ip link set eth0@$NOM_CONTAINER netns /proc/$PID/ns/net name eth0 -ip netns exec /proc/$PID/ns/net ip addr add $ADDR_IPV4 dev eth0 +for (( i=0 ; i < ${#ARRAY_BRIDGES[*]} ; i++ )); do + ip link set $NOM_CONTAINER$i master ${ARRAY_BRIDGES[i]} +done \ No newline at end of file diff --git a/create_image.sh b/create_image.sh index 485a3cd..dd0496b 100644 --- a/create_image.sh +++ b/create_image.sh @@ -1,4 +1,4 @@ -#!/bin/bash + #!/bin/bash while getopts i:s:r: o; do @@ -15,7 +15,7 @@ if [ "$SIZE" == "" ] || [ "$SIZE" -lt 0 ] || [ "$SIZE" -gt 10240 ]; then SIZE=10240 fi -echo "La size est de $SIZE" +echo "Le size est de $SIZE" mkdir -p $PATH_BALEINE/images if [[ -z $REPERTOIRE ]]; then @@ -33,9 +33,7 @@ MANIFEST=$NOM_IMAGE.manifest dd if=/dev/zero of=$REPERTOIRE/$NOM_IMAGE bs=1024k count=$SIZE -touch $MANIFEST - -echo "nom image:$NOM_IMAGE" >> $MANIFEST +echo "nom_image:$NOM_IMAGE" >> $MANIFEST echo "taille:$SIZE">> $MANIFEST echo "chemin:$REPERTOIRE">> $MANIFEST @@ -61,4 +59,8 @@ fi #Création de l'arborescence Debian avec debootstrap echo "Je vais faire le debootstrap" -debootstrap --include=apache2,vim,nano stable /mnt/baleine/$NOM_IMAGE \ No newline at end of file +mkdir -p /mnt/baleine/$NOM_IMAGE +mount -t ext4 -o loop $REPERTOIRE/$NOM_IMAGE /mnt/baleine/$NOM_IMAGE +debootstrap --include=apache2,vim,nano stable /mnt/baleine/$NOM_IMAGE +umount /mnt/baleine/$NOM_IMAGE +rm -rf /mnt/baleine/$NOM_IMAGE \ No newline at end of file diff --git a/down_bridge.sh b/down_bridge.sh index d14f1e1..0f0eb03 100644 --- a/down_bridge.sh +++ b/down_bridge.sh @@ -1,5 +1,9 @@ #!/bin/bash -NOM_BRIDGE=$3; +while getopts b: o; do + case $o in + (b) NOM_BRIDGE=$OPTARG;; + esac +done #vérification que l'utilisateur donne bien un nom à l'image if [[ -z "$NOM_BRIDGE" ]]; then echo "Il faut donner le nom du bridge Relancez la commande avec les bons arguments." diff --git a/image.sh b/image.sh index 9e36f18..4211287 100644 --- a/image.sh +++ b/image.sh @@ -7,14 +7,14 @@ case $1 in ;; "list") #Si on veut lister les images (qu'on a apellé le script ./balaine.sh image list) - bash list_images.sh "$@" + bash list_images.sh "${@:2}" ;; "import") - bash import_container.sh "$@" + bash import_container.sh "${@:2}" ;; "export") - bash export_container.sh "$@" + bash export_container.sh "${@:2}" ;; "remove") - bash remove_image.sh "$@" + bash remove_image.sh "${@:2}" esac \ No newline at end of file diff --git a/list_bridges.sh b/list_bridges.sh index c4a2ca4..088524d 100644 --- a/list_bridges.sh +++ b/list_bridges.sh @@ -1,9 +1,8 @@ #!/bin/bash -cd $PATH_MANIFEST/bridges - -for eachfile in ./*.manifest +for eachfile in $PATH_MANIFEST/bridges/*.manifest do - echo $eachfile - val=$(cat "$eachfile") - echo $val -done \ No newline at end of file + NOM_BRIDGE=$(grep nom_bridge $eachfile | cut -d ':' -f2) + echo "----------------------" + brctl show $NOM_BRIDGE + echo "----------------------" +done diff --git a/list_images.sh b/list_images.sh index a5c51cc..6f57805 100644 --- a/list_images.sh +++ b/list_images.sh @@ -1,10 +1,13 @@ #!/bin/bash -cd $PATH_MANIFEST/images - -for eachfile in ./*.manifest +for eachfile in $PATH_MANIFEST/images/*.manifest do - echo $eachfile - val=$(cat "$eachfile") - echo $val + NOM_IMAGE=$(grep nom_image $eachfile | cut -d ':' -f2) + TAILLE=$(grep taille $eachfile | cut -d ':' -f2) + CHEMIN=$(grep chemin $eachfile | cut -d ':' -f2) + echo "----------------------" + echo "Nom image: $NOM_IMAGE" + echo "Taille: $TAILLE" + echo "Chemin: $CHEMIN" + echo "----------------------" done diff --git a/remove_bridge.sh b/remove_bridge.sh index 6dc3aac..021e831 100644 --- a/remove_bridge.sh +++ b/remove_bridge.sh @@ -1,13 +1,12 @@ #!/bin/bash -NOM_BRIDGE_TO_REMOVE=$3; - +NAME_BRIDGE_TO_REMOVE=$1; #vérification que l'utilisateur donne bien un nom à l'image -if [[ -z "$NOM_BRIDGE_TO_REMOVE" ]]; then +if [[ -z "$NAME_BRIDGE_TO_REMOVE" ]]; then echo "Il faut donner le nom du bridge à supprimer ! Relancez la commande avec les bons arguments." exit fi -chmod -R 755 $PATH_MANIFEST -rm $PATH_MANIFEST/bridges/$NOM_BRIDGE_TO_REMOVE.* -ip link delete $NOM_BRIDGE_TO_REMOVE type bridge -#delbr + +rm $PATH_MANIFEST/bridges/$NAME_BRIDGE_TO_REMOVE.manifest +ip link set $NAME_BRIDGE_TO_REMOVE down +brctl delbr $NAME_BRIDGE_TO_REMOVE \ No newline at end of file diff --git a/remove_container.sh b/remove_container.sh index 17afe88..3517524 100644 --- a/remove_container.sh +++ b/remove_container.sh @@ -1,13 +1,17 @@ #!/bin/bash -NOM_CONTAINER_TO_REMOVE=$3; +NAME_CONTAINER_TO_REMOVE=$1; #vérification que l'utilisateur donne bien un nom à l'CONTAINER -if [[ -z "$NOM_CONTAINER_TO_REMOVE" ]]; then - echo "Il faut donner le nom du conteneur! Relancez la commande avec les bons arguments." +if [[ -z "$NAME_CONTAINER_TO_REMOVE" ]]; then + echo "Il faut donner le NAME du conteneur! Relancez la commande avec les bons arguments." exit fi -chmod -R 755 $PATH_MANIFEST -rm $PATH_MANIFEST/containers/$NOM_CONTAINER_TO_REMOVE.manifest -rm $PATH_BALEINE/containers/$NOM_CONTAINER_TO_REMOVE +rm -rf $PATH_MANIFEST/containers/$NAME_CONTAINER_TO_REMOVE.manifest +rm -rf $PATH_BALEINE/containers/$NAME_CONTAINER_TO_REMOVE -#il doit remove aussi l'image, les manifessts les bridges supprime la copie? +#umount /mnt/container +umount /mnt/baleine/$NAME_CONTAINER_TO_REMOVE +#supprime mnt/container +rm -rf /mnt/baleine/$NAME_CONTAINER_TO_REMOVE +#supprime l'image associé au container (manifest du container) +rm -rf /var/lib/baleine/containers/$NAME_CONTAINER_TO_REMOVE diff --git a/remove_image.sh b/remove_image.sh index af2b1e3..77dee9c 100644 --- a/remove_image.sh +++ b/remove_image.sh @@ -1,11 +1,11 @@ #!/bin/bash -NOM_IMAGE_TO_REMOVE=$3; +NAME_IMAGE_TO_REMOVE=$1; #vérification que l'utilisateur donne bien un nom à l'image -if [[ -z "$NOM_IMAGE_TO_REMOVE" ]]; then +if [[ -z "$NAME_IMAGE_TO_REMOVE" ]]; then echo "Il faut donner le nom de l'image ! Relancez la commande avec les bons arguments." exit fi -chmod -R 755 $PATH_MANIFEST -rm $PATH_MANIFEST/images/$NOM_IMAGE_TO_REMOVE.manifest -rm $PATH_BALEINE/images/$NOM_IMAGE_TO_REMOVE \ No newline at end of file +rm $PATH_MANIFEST/images/$NAME_IMAGE_TO_REMOVE.manifest +rm $PATH_BALEINE/images/$NAME_IMAGE_TO_REMOVE +#TODO : umount \ No newline at end of file diff --git a/restart_container.sh b/restart_container.sh index e3b8260..1b12481 100644 --- a/restart_container.sh +++ b/restart_container.sh @@ -1,7 +1,10 @@ #!/bin/bash -NOM_CONTAINER=$3; -NOM_IMAGE=$4; - +while getopts c:i: o; do + case $o in + (c) NOM_CONTAINER=$OPTARG;; + (i) NOM_IMAGE=$OPTARG;; + esac +done mount -t ext4 -o loop $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE /mnt/baleine/$NOM_CONTAINER nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER $PROGRAM -c "mount /proc" & diff --git a/stop_container.sh b/stop_container.sh index 5786865..e686e5d 100644 --- a/stop_container.sh +++ b/stop_container.sh @@ -1,5 +1,9 @@ #!/bin/bash -NOM_CONTAINER=$3; +while getopts c: o; do + case $o in + (c) NOM_CONTAINER=$OPTARG;; + esac +done kill $PID umount /mnt/baleine/$NOM_CONTAINER \ No newline at end of file diff --git a/test.sh b/test.sh index e6c21de..9c99a97 100644 --- a/test.sh +++ b/test.sh @@ -1,22 +1,9 @@ -#!/bin/bash +#bash baleine.sh bridge create mybridge 192.168.42.1 +#bash baleine.sh container create -i TEST -c mycontainer -b mybridge -a 192.168.42.2 -p /usr/sbin/apache2 -BRIDGES="pont,petitpont" -ADDRS_IPV4="lala,lulu" +A="HELLO" +B=" WORLD" -ARRAY_BRIDGES=() -ARRAY_IPV4=() +C="$A$B" -IFS=',' -for b in $BRIDGES; do - ARRAY_BRIDGES+=($b) -done - -for a in $ADDRS_IPV4; do - ARRAY_IPV4+=($a) -done - - -for ((i = 0, j = 0 ; i < ${#ARRAY_BRIDGES[*]} && j < ${#ARRAY_BRIDGES[*]} ; i++, j++ )); do - echo "i= ${ARRAY_BRIDGES[i]}" - echo "j= ${ARRAY_IPV4[j]}" -done \ No newline at end of file +echo $C \ No newline at end of file diff --git a/up_bridge.sh b/up_bridge.sh index 66dcc61..4fb0ccd 100644 --- a/up_bridge.sh +++ b/up_bridge.sh @@ -1,5 +1,10 @@ #!/bin/bash -NOM_BRIDGE=$3; + +while getopts b: o; do + case $o in + (b) NOM_BRIDGE=$OPTARG;; + esac +done #vérification que l'utilisateur donne bien un nom à l'image if [[ -z "$NOM_BRIDGE" ]]; then echo "Il faut donner le nom du bridge Relancez la commande avec les bons arguments." -- libgit2 0.21.2