Blame view

create_container.sh 5.3 KB
3e470988   hmalti   First commit
1
  #!/bin/bash
5721892d   hmalti   Avancées
2
3
  
  set -x 
5cf93464   hmalti   Ajout interfaces ...
4
5
6
7
8
9
10
11
  while getopts i:c:b:r:a:p: o; do
      case $o in
          (i) NOM_IMAGE=$OPTARG;;
          (c) NOM_CONTAINER=$OPTARG;;
          (b) BRIDGES=$OPTARG;;
          (r) REPERTOIRE=$OPTARG;;
          (a) ADDRS_IPV4=$OPTARG;;
          (p) PROGRAM=$OPTARG;;
5cf93464   hmalti   Ajout interfaces ...
12
13
      esac
  done
3e470988   hmalti   First commit
14
  
5721892d   hmalti   Avancées
15
  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"
5721892d   hmalti   Avancées
16
  
055618a3   hmalti   Ajout hash interf...
17
  #Check existence image
5721892d   hmalti   Avancées
18
  if [ -z "$PATH_MANIFEST/images/$NOM_IMAGE.manifest" ] || [ -z $NOM_IMAGE ]; then
5cf93464   hmalti   Ajout interfaces ...
19
20
21
      echo "Image non existante."
      exit
  fi
3e470988   hmalti   First commit
22
  
055618a3   hmalti   Ajout hash interf...
23
  #Check existence bridge
5721892d   hmalti   Avancées
24
  if [ ! -z $NOM_BRIDGE ] || [ -z "$PATH_MANIFEST/bridges/$NOM_BRIDGE.manifest" ]; then
5cf93464   hmalti   Ajout interfaces ...
25
26
27
      echo "Bridge non existant."
      exit
  fi
3e470988   hmalti   First commit
28
29
  
  #On veut récupérer le chemin de l'image
5721892d   hmalti   Avancées
30
  PATH_IMAGE="$(grep chemin $PATH_MANIFEST/images/$NOM_IMAGE.manifest | cut -d':' -f2)"
3e470988   hmalti   First commit
31
  
055618a3   hmalti   Ajout hash interf...
32
  #On fait une copie dans /var/baleine/images pour mount depuis le conteneur
3e470988   hmalti   First commit
33
  if [[ ! -d "$PATH_BALEINE/containers/$NOM_CONTAINER" ]]; then
5cf93464   hmalti   Ajout interfaces ...
34
      mkdir -p $PATH_BALEINE/containers/$NOM_CONTAINER
3e470988   hmalti   First commit
35
36
  fi
  
055618a3   hmalti   Ajout hash interf...
37
  cp $PATH_IMAGE/$NOM_IMAGE $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE
5721892d   hmalti   Avancées
38
  
3e470988   hmalti   First commit
39
  
055618a3   hmalti   Ajout hash interf...
40
  #Création du du dossier de montage de l'image du container
5721892d   hmalti   Avancées
41
42
43
44
  
  if [[ ! -d "/mnt/baleine/$NOM_CONTAINER" ]]; then
      mkdir -p /mnt/baleine/$NOM_CONTAINER
  fi
3e470988   hmalti   First commit
45
46
  
  #on monte le système de fichiers
5cf93464   hmalti   Ajout interfaces ...
47
  echo "Montage de l'image"
3e470988   hmalti   First commit
48
49
  mount -t ext4 -o loop $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE /mnt/baleine/$NOM_CONTAINER
  
5161dfdb   hmalti   Améliorations scr...
50
  #On renseigne le fichier fstab du conteneur
5721892d   hmalti   Avancées
51
52
  echo "proc /proc proc defaults 0 0" >> /mnt/baleine/$NOM_CONTAINER/etc/fstab
  echo "$PROGRAM" >> /mnt/baleine/$NOM_CONTAINER/etc/rc.local
5161dfdb   hmalti   Améliorations scr...
53
  
5cf93464   hmalti   Ajout interfaces ...
54
  #On fait le unshare sur l'image passée en paramètre,
5721892d   hmalti   Avancées
55
56
  #nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER "/bin/bash" -c "mount /proc" &
  
055618a3   hmalti   Ajout hash interf...
57
  nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER /bin/sh -c "mount /proc ; $PROGRAM ; while true ; do sleep 10 ; done" &
5721892d   hmalti   Avancées
58
  
3e470988   hmalti   First commit
59
  PID=$!
3e470988   hmalti   First commit
60
  
055618a3   hmalti   Ajout hash interf...
61
62
63
64
65
66
67
  #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 
3e470988   hmalti   First commit
68
  
3e470988   hmalti   First commit
69
  FILE=$NOM_CONTAINER.manifest
055618a3   hmalti   Ajout hash interf...
70
  date=$(date)
5cf93464   hmalti   Ajout interfaces ...
71
  
17bfc359   hmalti   Help Image+contai...
72
  echo "nom_container:$NOM_CONTAINER" >> $FILE #nom du conteneur
3e470988   hmalti   First commit
73
  echo "nom_image:$NOM_IMAGE" >> $FILE #nom de son image
5721892d   hmalti   Avancées
74
  echo "pid:$PID" >> $FILE #Son PID 
3e470988   hmalti   First commit
75
  echo "nom_bridge:$NOM_BRIDGE" >> $FILE #SON BRIDGE
5cf93464   hmalti   Ajout interfaces ...
76
  echo "starting_time: $date" >> $FILE #starting time
3e470988   hmalti   First commit
77
  
b1087939   hmalti   Ajout commandes
78
  
3e470988   hmalti   First commit
79
80
81
82
83
  if [[ ! -d "$PATH_MANIFEST/containers" ]]; then
      mkdir -p $PATH_MANIFEST/containers
  fi
  
  mv $NOM_CONTAINER.manifest $PATH_MANIFEST/containers/$NOM_CONTAINER.manifest
5161dfdb   hmalti   Améliorations scr...
84
85
  
  
5cf93464   hmalti   Ajout interfaces ...
86
87
88
89
  #TODO : taille mémoire / limite mémoire
  
  #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)
5721892d   hmalti   Avancées
90
  NOMBRE_INTERFACES="$(echo $ADDRS_IPV4 | awk -F '[,]' '{print NF}')"
5721892d   hmalti   Avancées
91
  NOMBRE_BRIDGES="$(echo $BRIDGES | awk -F '[,]' '{print NF}')"
5cf93464   hmalti   Ajout interfaces ...
92
  
5721892d   hmalti   Avancées
93
  if [ $NOMBRE_INTERFACES -ne $NOMBRE_BRIDGES ]; then
5cf93464   hmalti   Ajout interfaces ...
94
95
      echo "Erreur: Il n'y a pas autant de bridges que d'interfaces réseaux"
      exit 1
5721892d   hmalti   Avancées
96
  fi
5cf93464   hmalti   Ajout interfaces ...
97
  
055618a3   hmalti   Ajout hash interf...
98
99
100
101
102
103
104
  #Creation veth
  #Problématique : Sur Linux, impossible d'avoir des interfaces avec des noms de plus de 16 caractères
  #                Impossible donc de mettre des interfaces du type
  #Solution : On hash le nom du container, on le ramène aux trois premiers digits, puis on ajoute l'index
  
  ARRAY_INTERFACES=()
  
5cf93464   hmalti   Ajout interfaces ...
105
  for i in $(seq 1 $NOMBRE_INTERFACES); do
055618a3   hmalti   Ajout hash interf...
106
107
108
109
110
111
112
113
114
115
116
117
118
119
      #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"
      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)
      
      #On redemarre l'interface virtuelle
      ip link set dev $INTERFACE_NAME down
      ip link set dev $INTERFACE_NAME up
  
      #On place l'une des extrémité de la paire veth dans le namespace du container
      ip link set eth$i@$INTERFACE_NAME netns /proc/$PID/ns/net name eth$i
5cf93464   hmalti   Ajout interfaces ...
120
121
  done
  
5721892d   hmalti   Avancées
122
123
124
  
  #Compte le nombre de bridges présents
  NOMBRE_BRIDGES=$(echo $BRIDGES | awk -F '[,]' '{print NF}')
5cf93464   hmalti   Ajout interfaces ...
125
  
055618a3   hmalti   Ajout hash interf...
126
  #On met le séparateur à "," pour délimiter les addresses ip et bridges
5cf93464   hmalti   Ajout interfaces ...
127
128
129
130
131
132
133
134
135
136
137
138
139
  IFS=","
  
  ARRAY_BRIDGES=()
  ARRAY_IPV4=()
  
  for b in $BRIDGES; do
      ARRAY_BRIDGES+=(b)    
  done
  
  for a in $ADDRS_IPV4; do
      ARRAY_IPV4+=(a)
  done
  
5721892d   hmalti   Avancées
140
  for (( i=0 ; i < ${#ARRAY_IPV4[*]} ; i++ )); do
055618a3   hmalti   Ajout hash interf...
141
142
143
144
145
146
147
148
      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
  
      #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
5721892d   hmalti   Avancées
149
  done
3e470988   hmalti   First commit
150
  
5721892d   hmalti   Avancées
151
  for (( i=0 ; i < ${#ARRAY_BRIDGES[*]} ; i++ )); do
055618a3   hmalti   Ajout hash interf...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
      #Pour chaque interface, on la relie à son bridge associé
      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
      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/^.//')