From b02be5245f09c95a2efbcb1f1b7857cf0ad83225 Mon Sep 17 00:00:00 2001 From: hmalti Date: Wed, 8 May 2019 22:56:12 +0200 Subject: [PATCH] Correctifs + gestion suppression interfaces réseaux --- create_container.sh | 33 +++++++++++++-------------------- create_image.sh | 3 ++- list_bridges.sh | 2 ++ list_container.sh | 19 +++++++++++++------ list_images.sh | 2 ++ remove_bridge.sh | 1 - remove_container.sh | 38 ++++++++++++++++++++++++++------------ restart_container.sh | 8 +++++--- stop_container.sh | 23 ++++++++++++++++++++--- 9 files changed, 83 insertions(+), 46 deletions(-) diff --git a/create_container.sh b/create_container.sh index e3406ab..9397d3f 100644 --- a/create_container.sh +++ b/create_container.sh @@ -1,6 +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;; @@ -52,27 +52,19 @@ 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 "/bin/bash" -c "mount /proc" & nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER /bin/sh -c "mount /proc ; $PROGRAM ; while true ; do sleep 10 ; done" & - PID=$! -#Récupère tous les processus fils du unshare (donc ici le programme qu'on lance) -#ps axo ppid,pid | grep "^ *$PID" | sed -e 's/.* //' - echo "PID Unshare :$PID" - -#TODO : Gerer les noms des interfaces < x caractères - FILE=$NOM_CONTAINER.manifest date=$(date) echo "nom_container:$NOM_CONTAINER" >> $FILE #nom du conteneur echo "nom_image:$NOM_IMAGE" >> $FILE #nom de son image echo "pid:$PID" >> $FILE #Son PID -echo "nom_bridge:$NOM_BRIDGE" >> $FILE #SON BRIDGE +echo "nom_bridges:$BRIDGES" >> $FILE #SON BRIDGE echo "starting_time: $date" >> $FILE #starting time @@ -102,11 +94,11 @@ fi ARRAY_INTERFACES=() -for i in $(seq 1 $NOMBRE_INTERFACES); do +for ((i = 0 ; i<$NOMBRE_INTERFACES ; i++)); do #Creation d'un hash du nom du container pour créer les interfaces HASH=$(sha1sum <<< $NOM_CONTAINER) #On prend les trois premiers caractères du hash, succédé de l'index de l'interface - INTERFACE_NAME="vif${HASH:0:3}_$i" + INTERFACE_NAME="vif${HASH:0:4}_$i" ip link add $INTERFACE_NAME type veth peer name eth$i@$INTERFACE_NAME #On rajoute l'interface à la liste des interfaces du container ARRAY_INTERFACES+=($INTERFACE_NAME) @@ -119,7 +111,6 @@ for i in $(seq 1 $NOMBRE_INTERFACES); do ip link set eth$i@$INTERFACE_NAME netns /proc/$PID/ns/net name eth$i done - #Compte le nombre de bridges présents NOMBRE_BRIDGES=$(echo $BRIDGES | awk -F '[,]' '{print NF}') @@ -130,19 +121,19 @@ ARRAY_BRIDGES=() ARRAY_IPV4=() for b in $BRIDGES; do - ARRAY_BRIDGES+=(b) + echo "Un bridge: $b" + ARRAY_BRIDGES+=($b) done for a in $ADDRS_IPV4; do - ARRAY_IPV4+=(a) + echo "Une addr: $a" + ARRAY_IPV4+=($a) done for (( i=0 ; i < ${#ARRAY_IPV4[*]} ; i++ )); do nsenter -t $PID -n ip a - #Attribution de la i-ème adresse ip à l'interface i - nsenter -t $PID -n ip add ${ARRAY_IPV4[i]} dev eth$i - + nsenter -t $PID -n ip a add ${ARRAY_IPV4[i]} dev eth$i #On redemarre l'interface i dans le container nsenter -t $PID -n ip link set dev eth$i down nsenter -t $PID -n ip link set dev eth$i up @@ -150,16 +141,18 @@ done for (( i=0 ; i < ${#ARRAY_BRIDGES[*]} ; i++ )); do #Pour chaque interface, on la relie à son bridge associé + echo "Nom bridge :${ARRAY_BRIDGES[i]}" ip link set ${ARRAY_INTERFACES[i]} master ${ARRAY_BRIDGES[i]} done #Chaine de caractère contenant toutes les interfaces du container, séparées par "," STRING_INTERFACE="" -for (( i=0 ; i < ${#ARRAY_INTERFACE[*]} ; i++)); do +for (( i=0 ; i < ${#ARRAY_INTERFACES[*]} ; i++)); do STRING_INTERFACE="$STRING_INTERFACE,${ARRAY_INTERFACES[i]}" #Exemple, à la fin on aura ",vif143,vif560" done #On supprime la première virgule exédentaire -STRING_INTERFACE=$(echo $STRING_INTERFACE | sed -e 's/^.//') \ No newline at end of file +STRING_INTERFACE=$(echo $STRING_INTERFACE | sed -e 's/^.//') +echo "interfaces:$STRING_INTERFACE" >> $FILE \ No newline at end of file diff --git a/create_image.sh b/create_image.sh index dd0496b..1996427 100644 --- a/create_image.sh +++ b/create_image.sh @@ -15,7 +15,8 @@ if [ "$SIZE" == "" ] || [ "$SIZE" -lt 0 ] || [ "$SIZE" -gt 10240 ]; then SIZE=10240 fi -echo "Le size est de $SIZE" +echo "Création d'une image de taille $SIZE" + mkdir -p $PATH_BALEINE/images if [[ -z $REPERTOIRE ]]; then diff --git a/list_bridges.sh b/list_bridges.sh index 088524d..d11ef6e 100644 --- a/list_bridges.sh +++ b/list_bridges.sh @@ -1,6 +1,8 @@ #!/bin/bash for eachfile in $PATH_MANIFEST/bridges/*.manifest do + #Test si des fichiers sont présents + test -f "$eachfile" || continue NOM_BRIDGE=$(grep nom_bridge $eachfile | cut -d ':' -f2) echo "----------------------" brctl show $NOM_BRIDGE diff --git a/list_container.sh b/list_container.sh index a41c15a..64cbf3b 100644 --- a/list_container.sh +++ b/list_container.sh @@ -1,10 +1,17 @@ #!/bin/bash -cd $PATH_MANIFEST/containers - -for eachfile in ./*.manifest +for eachfile in $PATH_MANIFEST/containers/*.manifest do - echo $eachfile - val=$(cat "$eachfile") - echo $val + #Test si des fichiers sont présents + test -f "$eachfile" || continue + NOM_CONTAINER=$(grep nom_container $eachfile | cut -d ':' -f2) + NOM_IMAGE=$(grep nom_image $eachfile | cut -d ':' -f2) + PID=$(grep pid $eachfile | cut -d ':' -f2) + TIME=$(grep starting_time $eachfile | cut -d':' -f2) + echo "----------------------" + echo "Nom container $NOM_CONTAINER" + echo "Nom image: $NOM_IMAGE" + echo "PID: $PID" + echo "Startint time: $TIME" + echo "----------------------" done diff --git a/list_images.sh b/list_images.sh index 6f57805..c9a83fc 100644 --- a/list_images.sh +++ b/list_images.sh @@ -2,6 +2,8 @@ for eachfile in $PATH_MANIFEST/images/*.manifest do + #Test si des fichiers sont présents + test -f "$eachfile" || continue NOM_IMAGE=$(grep nom_image $eachfile | cut -d ':' -f2) TAILLE=$(grep taille $eachfile | cut -d ':' -f2) CHEMIN=$(grep chemin $eachfile | cut -d ':' -f2) diff --git a/remove_bridge.sh b/remove_bridge.sh index 06868c3..78c8a51 100644 --- a/remove_bridge.sh +++ b/remove_bridge.sh @@ -1,5 +1,4 @@ #!/bin/bash -NAME_BRIDGE_TO_REMOVE=$1; while getopts b: o; do case $o in (b) NAME_BRIDGE_TO_REMOVE=$OPTARG;; diff --git a/remove_container.sh b/remove_container.sh index 5b5fe1e..e994283 100644 --- a/remove_container.sh +++ b/remove_container.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -x while getopts c: o; do case $o in (c) NAME_CONTAINER_TO_REMOVE=$OPTARG;; @@ -6,19 +7,32 @@ while getopts c: o; do done #vérification que l'utilisateur donne bien un nom à l'CONTAINER if [[ -z "$NAME_CONTAINER_TO_REMOVE" ]]; then - echo "Il faut donner le NAME du conteneur! Relancez la commande avec les bons arguments." + echo "Il faut donner le nom du conteneur! Relancez la commande avec les bons arguments." exit fi -rm -rf $PATH_MANIFEST/containers/$NAME_CONTAINER_TO_REMOVE.manifest -rm -rf $PATH_BALEINE/containers/$NAME_CONTAINER_TO_REMOVE -#umount /mnt/container +PID=$(grep "pid" $PATH_MANIFEST/containers/$NAME_CONTAINER_TO_REMOVE.manifest | cut -d":" -f2) + +echo "Kill du container" +kill $PID + +echo "Démontage de /mnt/baleine/$NAME_CONTAINER_TO_REMOVE" 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 - -#TODO : Supprimer interfaces réseaux -#TODO : Grep manifeste container, récupère les interfaces -#TODO : Boucle sur les interfaces, les supprime une à une \ No newline at end of file + +echo "Suppression de /mnt/baleine/$NAME_CONTAINER_TO_REMOVE" + +echo "Suppression de $PATH_BALEINE/containers/$NAME_CONTAINER_TO_REMOVE" +rm -rf $PATH_BALEINE/containers/$NAME_CONTAINER_TO_REMOVE + +INTERFACES=$(grep "interfaces" $PATH_MANIFEST/containers/$NAME_CONTAINER_TO_REMOVE.manifest | cut -d":" -f2) + +echo "Suppression des interfaces réseau :" +IFS="," +#On supprime les interfaces réseaux associées à ce container +for interface in $INTERFACES; do + echo "Suppression de $interface" + ip link del $interface +done + +echo "Suppression du manifeste du container" +rm -rf $PATH_MANIFEST/containers/$NAME_CONTAINER_TO_REMOVE.manifest \ No newline at end of file diff --git a/restart_container.sh b/restart_container.sh index 1b12481..1eabfc0 100644 --- a/restart_container.sh +++ b/restart_container.sh @@ -5,9 +5,11 @@ while getopts c:i: o; do (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" & +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 /bin/sh -c "mount /proc ; $PROGRAM ; while true ; do sleep 10 ; done" & PID=$! -ps axo ppid,pid | grep "^ *$PID" | sed -e 's/.* //' + +#TODO: *Rajouter les interfaces réseau au container +# * \ No newline at end of file diff --git a/stop_container.sh b/stop_container.sh index e686e5d..c5a73b8 100644 --- a/stop_container.sh +++ b/stop_container.sh @@ -1,9 +1,26 @@ #!/bin/bash - while getopts c: o; do case $o in - (c) NOM_CONTAINER=$OPTARG;; + (c) NAME_CONTAINER_TO_REMOVE=$OPTARG;; esac done +#vérification que l'utilisateur donne bien un nom à l'CONTAINER +if [[ -z "$NAME_CONTAINER_TO_REMOVE" ]]; then + echo "Il faut donner le nom du conteneur! Relancez la commande avec les bons arguments." + exit 1 +fi + +PID=$(grep "PID" $PATH_MANIFEST/containers/$NAME_CONTAINER_TO_REMOVE.manifest | cut -d":" -f2) + +echo "Kill du container" kill $PID -umount /mnt/baleine/$NOM_CONTAINER \ No newline at end of file + +rm -rf $PATH_MANIFEST/containers/$NAME_CONTAINER_TO_REMOVE.manifest +rm -rf $PATH_BALEINE/containers/$NAME_CONTAINER_TO_REMOVE + +echo "Démontage de /mnt/baleine/$NAME_CONTAINER_TO_REMOVE" +umount /mnt/baleine/$NAME_CONTAINER_TO_REMOVE +#supprime mnt/container + +echo "Suppression de /mnt/baleine/$NAME_CONTAINER_TO_REMOVE" +#supprime l'image associé au container (manifest du container) \ No newline at end of file -- libgit2 0.21.2