-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
270 lines (212 loc) · 8.56 KB
/
run.py
File metadata and controls
270 lines (212 loc) · 8.56 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python2
# coding: utf8
import subprocess
import socket
import time
import sys
import paramiko
device = 'sda'
print("1. Boot to ArchLinux.iso")
ip = raw_input("2. Get IP (run `ip address` of `ifconfig`): ")
print(" Checking ip")
proc = subprocess.Popen(["ping", "-c 1", "-W 100", ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.wait()
res = proc.communicate()
response = proc.returncode
if response != 0:
print(" Host is down =(")
exit(1)
print("3. Run `passwd` and set root's password `root`")
print("4. Run `systemctl start sshd`")
print(" Waiting for 22 port")
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, 22))
s.close()
break
except:
time.sleep(1)
print(" Connecting")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=ip, username='root', password='root')
### Work using ssh
def dbg_print(text):
print("--> " + text)
def ssh_exec(command, ignore_exit_code=False):
stdin, stdout, stderr = client.exec_command(command, get_pty=True)
while not stdout.closed:
line = stdout.readline()
if line == '':
break
sys.stdout.write('\033[92m>\033[0m ' + line)
# stdout_str = stdout.read()
# if stdout_str != '':
# stdout_lines = stdout_str.splitlines()
# for line in stdout_lines:
# print('\033[92m>\033[0m ' + line)
stderr_str = stderr.read()
if stderr_str != '':
stderr_lines = stderr_str.splitlines()
for line in stderr_lines:
print('\033[91m>\033[0m ' + line)
if not ignore_exit_code:
exit_status = stdout.channel.recv_exit_status()
if exit_status != 0:
print("\033[91m==> FAIL\033[0m {0}".format(exit_status))
exit(2)
dbg_print("Setting time")
ssh_exec('timedatectl set-ntp true')
dbg_print("Partitioning drive")
ssh_exec('parted /dev/{0} -s -a opt mklabel msdos mkpart primary ext2 0% 100MB set 1 boot on mkpart primary linux-swap 100MB 2048MB mkpart primary ext4 2048MB 100%'.format(device))
dbg_print("Creating filesystems")
ssh_exec('mkfs.ext2 /dev/{0}1 -F -L boot'.format(device))
ssh_exec('mkswap /dev/{0}2 -L swap'.format(device))
ssh_exec('mkfs.ext3 /dev/{0}3 -F -L root'.format(device))
dbg_print("Mounting filesystems")
ssh_exec('mount /dev/{0}3 /mnt'.format(device))
ssh_exec('mkdir /mnt/boot')
ssh_exec('mount /dev/{0}1 /mnt/boot'.format(device))
ssh_exec('swapon /dev/{0}2'.format(device))
dbg_print("Updating mirrors")
ssh_exec('mv /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.orig')
ssh_exec('echo \'Server = http://mirror.yandex.ru/archlinux/$repo/os/$arch\' > /etc/pacman.d/mirrorlist')
ssh_exec('cat /etc/pacman.d/mirrorlist.orig >> /etc/pacman.d/mirrorlist')
ssh_exec('rm /etc/pacman.d/mirrorlist.orig')
dbg_print("Killing SigLevel")
ssh_exec('pacman-key --init')
ssh_exec('sed -i \'s/^SigLevel\s*= Required DatabaseOptional/SigLevel=Never/g\' /etc/pacman.conf')
dbg_print("Installing packages")
ssh_exec('pacstrap /mnt --noconfirm base base-devel net-tools grub openssh')
dbg_print("Generating fstab")
ssh_exec('genfstab -p /mnt > /mnt/etc/fstab')
dbg_print("Localizing new system")
ssh_exec('echo "en_US.UTF-8 UTF-8" >> /mnt/etc/locale.gen')
ssh_exec('echo "ru_RU.UTF-8 UTF-8" >> /mnt/etc/locale.gen')
ssh_exec('arch-chroot /mnt locale-gen')
dbg_print("Adding hooks and modules to mkinitcpio.conf")
ssh_exec('sed -i \'s/^MODULES=""/MODULES="i915 radeon nouveau"/g\' /mnt/etc/mkinitcpio.conf')
ssh_exec('sed -i \'s/^HOOKS="base/HOOKS="base keymap/g\' /mnt/etc/mkinitcpio.conf')
dbg_print("Running mkinitcpio")
ssh_exec('arch-chroot /mnt mkinitcpio -p linux')
dbg_print("Installing grub")
ssh_exec('arch-chroot /mnt grub-install /dev/{0}'.format(device))
ssh_exec('arch-chroot /mnt grub-mkconfig -o /boot/grub/grub.cfg')
dbg_print("Enabling dhcpcd and sshd")
ssh_exec('arch-chroot /mnt systemctl enable dhcpcd')
ssh_exec('arch-chroot /mnt systemctl enable sshd')
ssh_exec('echo "PermitRootLogin yes" >> /mnt/etc/ssh/sshd_config')
dbg_print("Setting root password (`root`)")
stdin, stdout, stderr = client.exec_command('arch-chroot /mnt passwd')
stdin.write('root\n')
stdin.write('root\n')
dbg_print("Umounting drives")
ssh_exec('umount /mnt/boot')
time.sleep(2)
ssh_exec('umount /mnt')
time.sleep(2)
ssh_exec('swapoff /dev/{0}2'.format(device))
raw_input("5. Now remove installation media and press Enter to reboot")
dbg_print("Rebooting")
ssh_exec('reboot', ignore_exit_code=True)
client.close()
## Working in installed system
print("6. Login as root/root")
ip = raw_input("7. Get IP (run `ip address` of `ifconfig`): ")
print(" Checking ip")
proc = subprocess.Popen(["ping", "-c 1", "-W 100", ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.wait()
res = proc.communicate()
response = proc.returncode
if response != 0:
print(" Host is down =(")
exit(1)
hostname = raw_input("8. Enter hostname for new system: ")
username = raw_input("9. Enter username for new system: ")
print(" Password will be the same")
timezone = raw_input("10. Enter timezone: ")
packages = raw_input("11. Packages to install: ") # tmux ffmpeg git docker gpm nmon dstat ...
print(" Waiting for 22 port")
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, 22))
s.close()
break
except:
time.sleep(1)
print(" Connecting")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=ip, username='root', password='root')
### Work using ssh
dbg_print("Setting hostname")
ssh_exec('hostnamectl set-hostname ' + hostname)
ssh_exec('systemctl enable systemd-resolved')
dbg_print("Setting time")
ssh_exec('timedatectl set-timezone ' + timezone)
ssh_exec('timedatectl set-ntp true')
dbg_print("Localizing system")
ssh_exec('localectl set-keymap ru')
ssh_exec('setfont cyr-sun16')
ssh_exec('localectl set-locale LANG="ru_RU.UTF-8"')
ssh_exec('export LANG=ru_RU.UTF-8')
ssh_exec('echo "FONT=cyr-sun16" > /etc/vconsole.conf')
dbg_print("Updating mkinitcpio")
ssh_exec('mkinitcpio -p linux')
dbg_print("Updating grub")
ssh_exec('grub-mkconfig -o /boot/grub/grub.cfg')
dbg_print("Getting system architecture")
stdin, stdout, stderr = client.exec_command('grep -q "^flags.*\\blm\\b" /proc/cpuinfo && echo 64 || echo 32')
architecture = stdout.read().strip()
if architecture == "":
dbg_print("\033[91mERROR! Cannot get system architecture\033[0m")
if architecture == "64":
dbg_print("Adding multilib to pacman repos list (x64 only)")
ssh_exec('echo -e \'[multilib]\\nInclude = /etc/pacman.d/mirrorlist\' >> /etc/pacman.conf')
dbg_print("Creating user")
ssh_exec('useradd -m -g users -G audio,games,lp,optical,power,scanner,storage,video,wheel -s /bin/bash ' + username)
stdin, stdout, stderr = client.exec_command('passwd ' + username)
stdin.write(username + '\n')
stdin.write(username + '\n')
dbg_print("Updating keys")
ssh_exec('pacman-key --init')
ssh_exec('pacman-key --populate archlinux')
dbg_print("Updating system")
ssh_exec('pacman --noconfirm -Syyu')
dbg_print("Installing packages")
ssh_exec('pacman --noconfirm -S yajl bash-completion ' + packages)
packages = 'xorg-server xorg-xinit xorg-server-utils mesa-libgl ' + \
'xf86-video-intel xf86-video-ati xf86-video-nouveau xf86-video-vesa ' + \
'xfce4 xfce4-goodies ' + \
'ttf-liberation ttf-dejavu opendesktop-fonts ttf-bitstream-vera ttf-arphic-ukai ttf-arphic-uming ttf-hanazono '
if architecture == "64":
packages += 'lib32-mesa-libgl '
dbg_print("Installing X")
ssh_exec('pacman --noconfirm -S ' + packages)
dbg_print("Configuring sudo")
ssh_exec('sed -i \'s/^# %wheel ALL=(ALL) ALL/%wheel ALL=(ALL) ALL/g\' /etc/sudoers')
dbg_print("Installing yaourt")
ssh_exec('mkdir /build')
ssh_exec('echo \'cd /build\' > /build/build.sh')
ssh_exec('echo \'curl -O https://aur.archlinux.org/cgit/aur.git/snapshot/$1.tar.gz\' >> /build/build.sh')
ssh_exec('echo \'tar xzf $1.tar.gz\' >> /build/build.sh')
ssh_exec('echo \'cd $1\' >> /build/build.sh')
ssh_exec('echo \'makepkg\' >> /build/build.sh')
ssh_exec('chmod +x /build/build.sh')
ssh_exec('chown nobody:nobody /build')
dbg_print(" Installing package-query")
ssh_exec('sudo -u nobody /build/build.sh package-query')
ssh_exec('pacman --noconfirm -U /build/package-query/*.pkg.tar.xz')
dbg_print(" Installing package-query")
ssh_exec('sudo -u nobody /build/build.sh yaourt')
ssh_exec('pacman --noconfirm -U /build/yaourt/*.pkg.tar.xz')
dbg_print(" Cleaning up")
ssh_exec('cd /')
ssh_exec('rm -rf /build')
dbg_print("Removing PermitRootLogin from sshd_config and adding " + username)
ssh_exec('sed -i \'s/^PermitRootLogin yes/AllowUsers ' + username + '/g\' /etc/ssh/sshd_config')
print("12. System is installed and configured. Rebooting")
ssh_exec('reboot', ignore_exit_code=True)
client.close()