Skip to content

Commit da34de5

Browse files
committed
Cleaned up khack code, added comments.
1 parent e0f6a3c commit da34de5

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

khack/khack-kernel

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,52 +29,76 @@ linux_config = '/home/vagrant/linux-config'
2929
linux_source = '/home/vagrant/linux-source'
3030
libc_source = '/home/vagrant/glibc-source'
3131

32+
def call_or_die(command, shell=True):
33+
result = call(command, shell=shell)
34+
if result != 0:
35+
exit(result)
3236

33-
def make_in_linux_source(command, sudo=False):
34-
return call('{}make -C {} {}'.format('sudo ' if sudo else '', linux_source, command), shell=True)
37+
def check_output_or_die(command, shell=True):
38+
result = check_output(command, shell=shell)
39+
if not result:
40+
exit(result)
41+
42+
return result.strip()
43+
44+
45+
def make_in_linux_source_or_die(command, sudo=False):
46+
call_or_die('{}make -C {} {}'.format('sudo ' if sudo else '', linux_source, command))
3547

3648

3749
args = docopt(__doc__)
3850
if args['get']:
39-
call('sudo apt-get install -y linux-source && mkdir -p {} && tar -x -f /usr/src/linux-source-* --strip 1 -C {}'.format(
40-
linux_source, linux_source), shell=True)
51+
# Get kernel source from Debian repository
52+
call_or_die('sudo apt-get install -y linux-source')
53+
call_or_die('mkdir -p ' + linux_source)
54+
# Extract to ~/linux-source
55+
call_or_die('tar -x -f /usr/src/linux-source-* --strip 1 -C ' + linux_source, shell=True)
4156
elif args['config']:
4257
config_name = args['<name>']
43-
result = call('cp {}/{}.config {}/.config && khack kernel clean'.format(linux_config, config_name, linux_source),
44-
shell=True)
45-
if result == 0:
46-
make_in_linux_source('olddefconfig')
58+
call_or_die('cp {}/{}.config {}/.config'.format(linux_config, config_name, linux_source))
59+
call_or_die('khack kernel clean')
60+
61+
# Answer potential new config questions with yes. If you see this happening
62+
# you might want to update khack or modify .config yourself
63+
make_in_linux_source_or_die('olddefconfig')
4764
elif args['make']:
48-
result = make_in_linux_source('-j8')
49-
if result == 0:
50-
kernel_version = check_output('cd {} && make kernelversion'.format(linux_source), shell=True).strip()
51-
if kernel_version:
52-
call('sudo mkinitramfs -v -o {}/arch/x86/boot/initrd {}-khack'.format(linux_source, kernel_version),
53-
shell=True)
65+
# Showtime, build kernel. Yeah, that's really it
66+
make_in_linux_source_or_die('-j8')
67+
68+
# Build initrd/initramfs too so we can boot this kernel. We need to know the
69+
# exact kernel version for this
70+
kernel_version = check_output_or_die('cd {} && make kernelversion'.format(linux_source))
71+
if kernel_version:
72+
call('sudo mkinitramfs -v -o {}/arch/x86/boot/initrd {}-khack'.format(linux_source, kernel_version))
5473
elif args['run']:
55-
call('sudo /sbin/kexec -l {}/arch/x86/boot/bzImage --initrd={}/arch/x86/boot/initrd --reuse-cmdline -f'.format(
56-
linux_source, linux_source), shell=True)
74+
# Run the built kernel using kexec, skipping a full reboot
75+
call_or_die('sudo /sbin/kexec -l {}/arch/x86/boot/bzImage --initrd={}/arch/x86/boot/initrd --reuse-cmdline -f'.format(
76+
linux_source, linux_source))
5777
elif args['clean']:
58-
make_in_linux_source('clean')
78+
make_in_linux_source_or_die('clean')
5979
elif args['tag']:
60-
make_in_linux_source('tags')
80+
make_in_linux_source_or_die('tags')
6181
elif args['install']:
62-
result = make_in_linux_source('modules_install install', sudo=True)
63-
if result == 0:
64-
call('sudo KERN_DIR={} /usr/lib/x86_64-linux-gnu/VBoxGuestAdditions/vboxadd setup'.format(linux_source),
65-
shell=True)
82+
# Build the module directories in /lib/modules, then install the kernel image
83+
# and boot filesystem, etc to /boot
84+
make_in_linux_source_or_die('modules_install install', sudo=True)
85+
# Compile the VBox Guest Additions against the new kernel and install to /lib/modules,
86+
# otherwise we won't be able to access VBox shares with the new kernel
87+
call_or_die('sudo KERN_DIR={} /usr/lib/x86_64-linux-gnu/VBoxGuestAdditions/vboxadd setup'.format(linux_source))
6688
elif args['uninstall']:
67-
call('sudo rm -r /lib/modules/*-khack && sudo rm /boot/*-khack* && sudo update-grub', shell=True)
89+
call_or_die('sudo rm -r /lib/modules/*-khack')
90+
call_or_die('sudo rm /boot/*-khack*')
91+
call_or_die('sudo update-grub')
6892
elif args['running?']:
6993
if not exists('{}/.version'.format(linux_source)):
7094
print u'\033[91m\u2716 There is no .version file.'
7195
exit(1)
7296

73-
proc_version = check_output('cat /proc/version', shell=True)
97+
proc_version = check_output_or_die('cat /proc/version')
7498
proc_version_email = re.search('\((.+?@.+?)\)', proc_version).group(1)
7599
proc_version_number = re.search('#([0-9]+)', proc_version).group(1)
76100

77-
compiled_version_number = check_output('cat {}/.version'.format(linux_source), shell=True).strip()
101+
compiled_version_number = check_output_or_die('cat {}/.version'.format(linux_source))
78102

79103
if 'vagrant' in proc_version_email:
80104
if compiled_version_number != proc_version_number:

khack/khack-libc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ libc_source = '/home/vagrant/glibc-source'
2020

2121
args = docopt(__doc__)
2222
if args['get']:
23-
call('sudo apt-get install -y gawk glibc-source && mkdir -p {} && tar -x -f /usr/src/glibc/glibc-* --strip 1 -C {}'.format(libc_source, libc_source), shell=True)
23+
exit(call('sudo apt-get install -y gawk glibc-source && mkdir -p {} && tar -x -f /usr/src/glibc/glibc-* --strip 1 -C {}'.format(libc_source, libc_source), shell=True))

khack/khack-module

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ libc_source = '/home/vagrant/glibc-source'
2222

2323
args = docopt(__doc__)
2424
if args['make']:
25-
call('make M=../module -C {} -j8'.format(linux_source), shell=True)
25+
exit(call('make M=../module -C {} -j8'.format(linux_source)))
2626
elif args['clean']:
27-
call('make M=../module -C {} clean'.format(linux_source), shell=True)
27+
exit(call('make M=../module -C {} clean'.format(linux_source)))
2828
elif args['install']:
2929
pass

0 commit comments

Comments
 (0)