Blame view

create_container.sh 3.16 KB
3e470988   hmalti   First commit
1
2
  #!/bin/bash
  set -x
5cf93464   hmalti   Ajout interfaces ...
3
4
5
6
7
8
9
10
11
12
13
  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;;
  
      esac
  done
3e470988   hmalti   First commit
14
15
  
  #Check existence image, bridge
5cf93464   hmalti   Ajout interfaces ...
16
17
18
19
  if [[ -z "$PATH_MANIFEST/images/$NOM_IMAGE.manifest" ]] || [ -z $NOM_IMAGE ]]; then
      echo "Image non existante."
      exit
  fi
3e470988   hmalti   First commit
20
  
5cf93464   hmalti   Ajout interfaces ...
21
22
23
24
  if [[ ! -z $NOM_BRIDGE ]] [[ -z "$PATH_MANIFEST/bridges/$NOM_BRIDGE.manifest" ]]; then
      echo "Bridge non existant."
      exit
  fi
3e470988   hmalti   First commit
25
26
  
  #On veut récupérer le chemin de l'image
5cf93464   hmalti   Ajout interfaces ...
27
  PATH_IMAGE= ${grep chemin $PATH_MANIFEST/images/$NOM_IMAGE.manifest | cut -d':' -f2}
3e470988   hmalti   First commit
28
  
5cf93464   hmalti   Ajout interfaces ...
29
  #on fait une copie dans /var/baleine/images pour mount depuis le conteneur
3e470988   hmalti   First commit
30
  if [[ ! -d "$PATH_BALEINE/containers/$NOM_CONTAINER" ]]; then
5cf93464   hmalti   Ajout interfaces ...
31
      mkdir -p $PATH_BALEINE/containers/$NOM_CONTAINER
3e470988   hmalti   First commit
32
33
34
35
  fi
  
  cp $PATH_IMAGE $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE
  
5cf93464   hmalti   Ajout interfaces ...
36
  
3e470988   hmalti   First commit
37
38
  
  #on monte le système de fichiers
5cf93464   hmalti   Ajout interfaces ...
39
  echo "Montage de l'image"
3e470988   hmalti   First commit
40
41
  mount -t ext4 -o loop $PATH_BALEINE/containers/$NOM_CONTAINER/$NOM_IMAGE /mnt/baleine/$NOM_CONTAINER
  
5161dfdb   hmalti   Améliorations scr...
42
43
44
45
  #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
  
5cf93464   hmalti   Ajout interfaces ...
46
  #On fait le unshare sur l'image passée en paramètre,
3e470988   hmalti   First commit
47
48
49
50
51
52
53
  nohup unshare -p -f -m -n -u chroot /mnt/baleine/$NOM_CONTAINER $PROGRAM -c "mount /proc" &
  PID=$!
  echo "j'ai finis le unshare-nohup"
  
  ps axo ppid,pid | grep "^ *$PID" | sed -e 's/.* //'
  echo $PID
  
3e470988   hmalti   First commit
54
55
  FILE=$NOM_CONTAINER.manifest
  touch $FILE #création du fichier
5cf93464   hmalti   Ajout interfaces ...
56
57
  date=${date}
  
3e470988   hmalti   First commit
58
59
60
61
  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 "nom_bridge:$NOM_BRIDGE" >> $FILE #SON BRIDGE
5cf93464   hmalti   Ajout interfaces ...
62
  echo "starting_time: $date" >> $FILE #starting time
3e470988   hmalti   First commit
63
  
b1087939   hmalti   Ajout commandes
64
  
3e470988   hmalti   First commit
65
66
67
68
69
  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...
70
71
  
  
5cf93464   hmalti   Ajout interfaces ...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  #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)
  
  NOMBRE_INTERFACES=echo $ADDRS_IPV4 | awk -F '[,]' '{print NF}'
  
  NOMBRE_BRIDGES=echo $BRIDGES | awk -F '[,]' '{print NF}'
  
  if [ NOMBRE_INTERFACES -ne NOMBRE_BRIDGES ]; do
      echo "Erreur: Il n'y a pas autant de bridges que d'interfaces réseaux"
      exit 1
  done
  
  for i in $(seq 1 $NOMBRE_INTERFACES); do
      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}'
  
  IFS=","
  
  ARRAY_BRIDGES=()
  ARRAY_IPV4=()
  
  for b in $BRIDGES; do
      ARRAY_BRIDGES+=(b)    
  done
  
  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
3e470988   hmalti   First commit
109
110
  
  #création de son interface réseau
3e470988   hmalti   First commit
111
  
3e470988   hmalti   First commit
112
113
114
115
  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