Blame view

create_container.sh 4.21 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
16
17
  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
  
3e470988   hmalti   First commit
18
  #Check existence image, bridge
5721892d   hmalti   Avancées
19
  if [ -z "$PATH_MANIFEST/images/$NOM_IMAGE.manifest" ] || [ -z $NOM_IMAGE ]; then
5cf93464   hmalti   Ajout interfaces ...
20
21
22
      echo "Image non existante."
      exit
  fi
3e470988   hmalti   First commit
23
  
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
  
5721892d   hmalti   Avancées
32
  echo "PATH_IMAGE: $PATH_IMAGE"
5cf93464   hmalti   Ajout interfaces ...
33
  #on fait une copie dans /var/baleine/images pour mount depuis le conteneur
3e470988   hmalti   First commit
34
  if [[ ! -d "$PATH_BALEINE/containers/$NOM_CONTAINER" ]]; then
5cf93464   hmalti   Ajout interfaces ...
35
      mkdir -p $PATH_BALEINE/containers/$NOM_CONTAINER
3e470988   hmalti   First commit
36
37
  fi
  
5721892d   hmalti   Avancées
38
39
40
  
  #Copie de l'image physique dans le container
  cp -r $PATH_IMAGE/$NOM_IMAGE $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE
3e470988   hmalti   First commit
41
  
5cf93464   hmalti   Ajout interfaces ...
42
  
5721892d   hmalti   Avancées
43
44
45
46
47
  #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
3e470988   hmalti   First commit
48
49
  
  #on monte le système de fichiers
5cf93464   hmalti   Ajout interfaces ...
50
  echo "Montage de l'image"
3e470988   hmalti   First commit
51
52
  mount -t ext4 -o loop $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE /mnt/baleine/$NOM_CONTAINER
  
5161dfdb   hmalti   Améliorations scr...
53
  #On renseigne le fichier fstab du conteneur
5721892d   hmalti   Avancées
54
55
  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...
56
  
5cf93464   hmalti   Ajout interfaces ...
57
  #On fait le unshare sur l'image passée en paramètre,
5721892d   hmalti   Avancées
58
59
60
61
  #nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER "/bin/bash" -c "mount /proc" &
  
  
  
3e470988   hmalti   First commit
62
63
64
65
66
67
  PID=$!
  echo "j'ai finis le unshare-nohup"
  
  ps axo ppid,pid | grep "^ *$PID" | sed -e 's/.* //'
  echo $PID
  
3e470988   hmalti   First commit
68
69
  FILE=$NOM_CONTAINER.manifest
  touch $FILE #création du fichier
5cf93464   hmalti   Ajout interfaces ...
70
71
  date=${date}
  
3e470988   hmalti   First commit
72
73
  echo "nom_container:$NOM_CONTAINER" >> $FILE #nom de son image
  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
90
  #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
91
  NOMBRE_INTERFACES="$(echo $ADDRS_IPV4 | awk -F '[,]' '{print NF}')"
5cf93464   hmalti   Ajout interfaces ...
92
  
5721892d   hmalti   Avancées
93
  NOMBRE_BRIDGES="$(echo $BRIDGES | awk -F '[,]' '{print NF}')"
5cf93464   hmalti   Ajout interfaces ...
94
  
5721892d   hmalti   Avancées
95
  if [ $NOMBRE_INTERFACES -ne $NOMBRE_BRIDGES ]; then
5cf93464   hmalti   Ajout interfaces ...
96
97
      echo "Erreur: Il n'y a pas autant de bridges que d'interfaces réseaux"
      exit 1
5721892d   hmalti   Avancées
98
  fi
5cf93464   hmalti   Ajout interfaces ...
99
100
  
  for i in $(seq 1 $NOMBRE_INTERFACES); do
5721892d   hmalti   Avancées
101
102
      echo "Nom de l'interface : $NOM_CONTAINER$i"
      ip link add $NOM_CONTAINER$i type veth peer name eth$i
5cf93464   hmalti   Ajout interfaces ...
103
104
105
  done
  
  echo "J'ai fini de créer les interfaces chef"
5721892d   hmalti   Avancées
106
107
108
  
  #Compte le nombre de bridges présents
  NOMBRE_BRIDGES=$(echo $BRIDGES | awk -F '[,]' '{print NF}')
5cf93464   hmalti   Ajout interfaces ...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  
  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
123
124
125
126
127
128
129
130
131
132
133
134
  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
3e470988   hmalti   First commit
135
  
5721892d   hmalti   Avancées
136
  done
3e470988   hmalti   First commit
137
  
5721892d   hmalti   Avancées
138
139
140
  for (( i=0 ; i < ${#ARRAY_BRIDGES[*]} ; i++ )); do
      ip link set $NOM_CONTAINER$i master ${ARRAY_BRIDGES[i]}
  done