-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel.py
201 lines (165 loc) · 7.12 KB
/
kernel.py
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
#!/usr/bin/env python3
"""
clipi:
Emulate, organize, burn, manage a variety of sbc distributions for Raspberry Pi
Written by Jess Sullivan
@ https://github.com/Jesssullivan/clipi
@ https://transscendsurvival.org/
"""
import platform
import subprocess
from common import *
from names import names
from sources import sources
import os
import toml
import re
"""
kernel.py:
ramdisk & kernel related utilities
- controls fdisk -l utility to inspect partitions & sectors from image.
- install, prepare, make cross compile stuff for making 64 bit kernel-
- gcc- only really pertains to building a new kernel from source- for most purposes just use any of the existing binaries)
"""
class kernel(object):
def __init__(self):
if platform == 'darwin':
print("environment: detected osx- aborting. feel free to contribute osx methods...")
quit()
if platform == "linux":
print("environment: detected Linux, continuing with apt-get...")
if platform == "linux2":
print("environment: detected Linux, continuing with apt-get.....\n" +
"please feel free to contribute methods for alternative package managers :)")
if platform == "linux":
print("environment: detected Linux, continuing with apt-get...")
@staticmethod
def depends():
print('\npreparing kernel depends...\n')
subprocess.Popen("sudo chmod u+x kernel_sh/kernel_depends.sh", shell=True).wait()
print('\n preparing & installing kernel depends...\n')
subprocess.Popen("sudo ./kernel_sh/kernel_depends.sh", shell=True).wait()
@classmethod
def build_binutils(cls):
print('\n preparing preconf binutils...\n')
subprocess.Popen("sudo chmod u+x kernel_sh/preconf_binutils.sh", shell=True).wait()
subprocess.Popen("./kernel_sh/preconf_binutils.sh", shell=True).wait()
sleep(.1)
print('\n preparing binutils...\n')
sleep(.1)
common.ensure_dir(dirname='binutils-obj')
subprocess.Popen("cp kernel_sh/make_binutils.sh binutils-obj/make_binutils.sh", shell=True).wait()
subprocess.Popen("sudo chmod u+x binutils-obj/make_binutils.sh", shell=True).wait()
sleep(.1)
print('\nmaking binutils @ binutils-obj...\n')
sleep(.1)
subprocess.Popen("./binutils-obj/make_binutils.sh", shell=True).wait()
@classmethod
def build_gcc(cls):
print('\n preparing preconf gcc...\n')
subprocess.Popen("sudo chmod u+x kernel_sh/preconf_gcc.sh", shell=True).wait()
subprocess.Popen("./kernel_sh/preconf_gcc.sh", shell=True).wait()
sleep(.1)
print('\n preparing gcc config...\n')
sleep(.1)
common.ensure_dir(dirname='gcc-out')
subprocess.Popen("cp kernel_sh/make_gcc.sh gcc-out/make_gcc.sh", shell=True).wait()
subprocess.Popen("sudo chmod u+x gcc-out/make_gcc.sh", shell=True).wait()
sleep(.1)
print('\n making gcc @ gcc-out...\n')
sleep(.1)
subprocess.Popen("./gcc-out/make_gcc.sh", shell=True).wait()
@classmethod
def check_build_dirs(cls, image):
# `image` currently must the path of a *.img file
if not os.path.isdir(names.src_dir(image)):
os.mkdir(names.src_dir(image))
if not os.path.isdir(names.src_build(image)):
os.mkdir(names.src_build(image))
if not os.path.isdir(names.src_mnt(image)):
os.mkdir(names.src_mnt(image))
"""
@classmethod
def get_kernel(cls):
cmd = 'git clone --depth=1 -b rpi-4.19.y https://github.com/raspberrypi/linux.git'
"""
@classmethod
def replace_fstab(cls, image):
# `image` currently must the path of a *.img file (not the source.toml name)
kernel.check_build_dirs(image=image)
# mount must use short path names due to file name encryption silliness
# https://bugs.launchpad.net/ecryptfs/+bug/344878 lol
if not os.path.isdir('.pi'):
os.mkdir('.pi')
if not os.path.isdir('.pi/mnt'):
os.mkdir('.pi/mnt')
try:
disk = kernel.fdisk_read(names.src_img(image))['.img2']
fblock = int(disk['Start']) * 512
cmd_cp_in = str('sudo cp -rf ' + names.src_img(image) + ' ' + '.pi/pi.img')
subprocess.Popen(cmd_cp_in, shell=True, stdout=subprocess.PIPE).wait()
print('completed copy in attempt....')
sleep(.5)
cmd_mnt = str('sudo mount -o offset=' +
str(fblock) + ' ' +
'.pi/pi.img .pi/mnt')
subprocess.Popen(cmd_mnt, shell=True, stdout=subprocess.PIPE)
print('completed mount attempt....')
sleep(.5)
cmd_fstab = 'sudo cp -f kernel_sh/fstab .pi/mnt/etc/fstab'
subprocess.Popen(cmd_fstab, shell=True, stdout=subprocess.PIPE)
sleep(.1)
print('completed replace fstab attempt....')
cmd_umnt = str('sudo umount .pi/mnt')
subprocess.Popen(cmd_umnt, shell=True, stdout=subprocess.PIPE).wait()
print('completed unmount.')
cmd_cp_out = str('sudo cp -rf .pi/pi.img ' + names.src_img(image))
subprocess.Popen(cmd_cp_out, shell=True, stdout=subprocess.PIPE)
print('completed copy attempt....')
except TypeError:
sleep(.2)
print('moving on from fdisk....')
pass
@staticmethod
def fdisk_setup():
if sys.platform == 'darwin':
print('detected osx, checking fdisk in gptfdisk via brew...')
if sys.platform == "linux" or sys.platform == "linux2":
print('checking fdisk...')
common.dep_install(dep='fdisk', brew_dep='gptfdisk')
sleep(.1)
@staticmethod
def fdisk_read(image):
# ensure we've got fdisk- not sure yet if this works from osx via gptfdisk
cmd = str('fdisk -l ' + image)
# read fdisk -l output:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
result = proc.stdout.read().__str__()
# figure out what type we should iterate with when looking via file / part contained within image-
# (plz only use .img for now)
if '.iso' in result:
iter = '.iso'
# not sure it this one will work......
if '.qcow2' in result:
iter = '.qcow2'
else:
iter = '.img'
# chop up fdisk results by file / partition:
parts = re.findall(r'' + iter + '\d', result)
disk = {}
for p in parts:
# sub dict 'part' will contain fdisk -l output values:
part = {}
# get just the number words:
line = result.split(p)[1]
words = re.split(r'\s+', line)
# place each word into 'part':
part['Start'] = words[1]
part['End'] = words[2]
part['Sectors'] = words[3]
part['Size'] = words[4]
part['Id'] = words[5]
part['Format'] = words[6].split('\\n')[0]
# stick this part into 'disk', move onto next disk part:
disk[p] = part
return disk