-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy_os.sh
executable file
·146 lines (133 loc) · 3.63 KB
/
deploy_os.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh -e
IP1=192.168.122.2
NET1=255.255.255.0
GW1=192.168.122.1
ETH1=eth0
HOST=test
DHCP=off
DNS=8.8.8.8
PASSWORD=passw0rd
for arg in $(cat /proc/cmdline); do
case "$arg" in
ip=*)
# ip=${IP}::${GW}:${NET}:${HOST}:${DEV}:off
ip="${arg#ip=}"
echo "Using IP address passed as kernel parameter: ip=${ip}"
IP1=$(echo $ip | cut -d: -f1)
NET1=$(echo $ip | cut -d: -f4)
GW1=$(echo $ip | cut -d: -f3)
ETH1=$(echo $ip | cut -d: -f6)
HOST=$(echo $ip | cut -d: -f5)
DHCP=$(echo $ip | cut -d: -f7)
;;
--)
break
;;
*)
;;
esac
done
# Get the device name (sda, vda, xda, etc) or use default first block device
DEV=${1:-$(ls /dev/?da)}
if [ ! -b "${DEV}" ]; then
echo "Block device doesn't exist: ${DEV}"
exit 1
fi
# Get the public key
PUB_KEY=$(cat /root-*/.ssh/authorized_keys 2>/dev/null || cat /root/.ssh/authorized_keys)
echo "The following parameters will be used:
IP=${IP1}
NET=${NET1}
GW=${GW1}
ETH=${ETH1}
HOST=${HOST}
DHCP=${DHCP}
PUB_KEY=${PUB_KEY}
DEV=${DEV}"
read -p "Are you sure you want to proceed? " -r REPLY
echo
if [ "$REPLY" != "y" ]; then
echo "You didn't type \"y\", exiting..."
exit 1
fi
# Download and write a VMWare Ubuntu Xenial image
if [ ! -f xenial-server-cloudimg-amd64-disk1.vmdk ]; then
curl -O https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.vmdk
fi
qemu-img convert -f vmdk -O raw xenial-server-cloudimg-amd64-disk1.vmdk ${DEV}
# inform the OS of partition table changes
sync
udevadm settle
# Create mount point and mount brand new FS onto /mnt
mkdir -p /mnt
mount ${DEV}1 /mnt
# Define initial configuration and set default user credentials
mkdir -p /mnt/var/lib/cloud/seed/nocloud
cat > /mnt/var/lib/cloud/seed/nocloud/user-data <<EOF
#cloud-config
package_upgrade: true
hostname: ${HOST}
chpasswd: { expire: False }
ssh_pwauth: True
password: ${PASSWORD}
packages:
- htop
- screen
- mc
- iftop
users:
- default:
ssh-authorized-keys:
- '${PUB_KEY}'
runcmd:
- service networking restart
- ifdown ${ETH1}
- ifup ${ETH1}
EOF
cat > /mnt/var/lib/cloud/seed/nocloud/meta-data <<-EOF
instance-id: iid-${HOST}
hostname: ${HOST}
dsmode: local
EOF
if [ "${DHCP}" = "off" ]; then
cat >> /mnt/var/lib/cloud/seed/nocloud/meta-data <<-EOF
network-interfaces: |
auto lo
iface lo inet loopback
auto ${ETH1}
iface ${ETH1} inet static
address ${IP1}
netmask ${NET1}
gateway ${GW1}
dns-nameservers ${DNS}
EOF
else
cat >> /mnt/var/lib/cloud/seed/nocloud/meta-data <<-EOF
network-interfaces: |
EOF
for iface in $(ip addr | awk -F: '/^[0-9]+/ {print $2}'); do
cat >> /mnt/var/lib/cloud/seed/nocloud/meta-data <<-EOF
auto ${iface}
iface ${iface} inet dhcp
EOF
done
fi
# Permanently define old-school ethX names and set "console=tty1" as a primary console
echo "GRUB_CMDLINE_LINUX_DEFAULT=\"console=ttyS0 console=tty1 net.ifnames=0 biosdevname=0\"" > /mnt/etc/default/grub.d/98-default_ifname.cfg
# Define old-school ethX names for the next boot and set "console=tty1" as a primary console
sed -i 's#\(console=ttyS0\)$#\1 console=tty1 net.ifnames=0 biosdevname=0#g' /mnt/boot/grub/grub.cfg
if lsmod | grep -q r816*; then
# Build DKMS r8169 ethernet module for Hetzner
for i in dev proc sys; do mount --bind /$i /mnt/$i ; done
mv /mnt/etc/resolv.conf /mnt/etc/resolv.conf_
echo nameserver 8.8.8.8 >> /mnt/etc/resolv.conf
chroot /mnt apt-get update
chroot /mnt apt-get install -y r8168-dkms
mv /mnt/etc/resolv.conf_ /mnt/etc/resolv.conf
for i in dev proc sys; do umount /mnt/$i ; done
fi
umount /mnt
# boot
echo "Ubuntu Xenial has been installed"
echo "To reboot please enter the command below:"
echo "echo b > /proc/sysrq-trigger"