virt-install-debian 3.31 KB
Newer Older
Server Team's avatar
Server Team committed
1
#!/bin/bash
Server Team's avatar
Server Team committed
2

Server Team's avatar
Server Team committed
3
4
MACHINE_CONF_FILE="/root/machine.conf"
if [ ! -f "$MACHINE_CONF_FILE" ]; then
Peter Niederlag's avatar
Peter Niederlag committed
5
	echo "please checke existance of $MACHINE_CONF_FILE"
Server Team's avatar
Server Team committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	exit 99
fi

source $MACHINE_CONF_FILE

# we should now have the following variables

echo "SERVER_NAME $SERVER_NAME"

echo "NET_IPV4_MAC $NET_IPV4_MAC"
echo "NET_IPV4_ADDRESS $NET_IPV4_ADDRESS"
echo "NET_IPV4_GATEWAY $NET_IPV4_GATEWAY"
echo "NET_IPV4_BROADCAST $NET_IPV4_BROADCAST"
echo "NET_IPV4_NETMASK $NET_IPV4_NETMASK"

Peter Niederlag's avatar
Peter Niederlag committed
21
read -p "Do you cant to create a machine with this config? " -n 1 -r
Server Team's avatar
Server Team committed
22
23
24
25
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi
Server Team's avatar
Server Team committed
26

Server Team's avatar
Server Team committed
27
28
29
30
31
32
33
34
###########################################
###########################################
###                                     ###
### no customisation below this point   ###
###                                     ###
###########################################
###########################################

35
36
37
38
39
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=740895
#NET_IPV6_ADDRESS="2a01:4f8:141:21a3::3"
#NET_IPV6_NETMASK="64"
#NET_IPV6_GATEWAY="fe80::1"

40
41
HOST_NAME=$SERVER_NAME

Server Team's avatar
Server Team committed
42
# create passwd
Server Team's avatar
Server Team committed
43
44
45
PASSWORD=$(makepasswd --minchars=10 --maxchars=12)
echo "Generated random password"
echo "ADD TO PASSWORD STORE/KEEP IN SAFE PLACE"
Server Team's avatar
Server Team committed
46
echo $PASSWORD
Server Team's avatar
Server Team committed
47
48
49
50
51
52
53
read -p "Are you reay to continue? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi


Steffen Gebert's avatar
Steffen Gebert committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# helper function to wait until the VM has shut down
wait_for_shutdown()
{
  echo "Waiting for shutdown of $1"
  while true; do
    virsh list | grep -c $1 > /dev/null
    if [ $? -eq 1 ]; then
      echo
      echo "VM stopped: $1"
      return 0
    fi
    sleep 1
    echo -n "."
  done
}

Server Team's avatar
Server Team committed
70
71
72
73
74
75
76
# note on preseeding
# file must be injected in the root/intrd filesysetm
# if file is not named preseed.cfg the file name must be passed to the kerne like preseed/file=/some/whatever.cfg
# see here for the full story:
# https://www.debian.org/releases/stable/amd64/apbs02.html.en#preseed-loading


Server Team's avatar
Server Team committed
77
78
#VOLUME=$SERVER_NAME
#--network bridge=br0 \
79
#--network bridge=br-ext,mac=$NET_IPV4_MAC,model=virtio \
Server Team's avatar
Server Team committed
80

Server Team's avatar
Server Team committed
81
virt-install \
Server Team's avatar
Server Team committed
82
--connect qemu:///system \
Server Team's avatar
Server Team committed
83
--name $SERVER_NAME \
Server Team's avatar
Server Team committed
84
--ram 2048 \
85
--disk pool=vgpool,size=50,bus=ide \
Server Team's avatar
Server Team committed
86
87
88
--vcpus 2 \
--os-type linux \
--os-variant generic \
89
--network bridge=br-int,model=virtio \
Server Team's avatar
Server Team committed
90
91
--graphics none \
--console pty,target_type=serial \
92
--location 'http://mirror.hetzner.de/debian/packages/dists/jessie/main/installer-amd64/' \
Peter Niederlag's avatar
Peter Niederlag committed
93
--initrd-inject '/usr/local/virtinstaller/preseed.cfg' \
94
--extra-args "auto=true netcfg/get_hostname=$HOST_NAME netcfg/hostname=$HOST_NAME passwd/root-password=$PASSWORD passwd/root-password-again=$PASSWORD netcfg/get_ipaddress=$NET_IPV4_ADDRESS netcfg/get_netmask=$NET_IPV4_NETMASK netcfg/get_gateway=$NET_IPV4_GATEWAY console=ttyS0,115200n8 serial"
Steffen Gebert's avatar
Steffen Gebert committed
95
96

echo "Finished with virt-install."
Steffen Gebert's avatar
Steffen Gebert committed
97
98
99
100
101
102
103

read -p "Did the installation finish and the VM shut down? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
    exit 1
fi

Steffen Gebert's avatar
Steffen Gebert committed
104
105
echo "Doing some aftermath..."
# shutdown
Steffen Gebert's avatar
Steffen Gebert committed
106
virsh destroy $SERVER_NAME
Steffen Gebert's avatar
Steffen Gebert committed
107

Steffen Gebert's avatar
Steffen Gebert committed
108
109
wait_for_shutdown $SERVER_NAME

Steffen Gebert's avatar
Steffen Gebert committed
110
111
112
113
114
115
116
117
118
119
120
# copy ssh-keys of admins into machine
# generate ssh-authorized keys so they can be uploaded into the root-account
SCRIPT_PATH=$(dirname $([ -L $0 ] && readlink -f $0 || echo $0))
$SCRIPT_PATH/gatherkeys.sh > $SCRIPT_PATH/authorized_keys
virt-copy-in -d $SERVER_NAME $SCRIPT_PATH/authorized_keys /root/.ssh/

# start again
virsh start $SERVER_NAME

# set domain to autostart
virsh autostart $SERVER_NAME