@@ -29,52 +29,76 @@ linux_config = '/home/vagrant/linux-config'
29
29
linux_source = '/home/vagrant/linux-source'
30
30
libc_source = '/home/vagrant/glibc-source'
31
31
32
+ def call_or_die (command , shell = True ):
33
+ result = call (command , shell = shell )
34
+ if result != 0 :
35
+ exit (result )
32
36
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 ))
35
47
36
48
37
49
args = docopt (__doc__ )
38
50
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 )
41
56
elif args ['config' ]:
42
57
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' )
47
64
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 ))
54
73
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 ))
57
77
elif args ['clean' ]:
58
- make_in_linux_source ('clean' )
78
+ make_in_linux_source_or_die ('clean' )
59
79
elif args ['tag' ]:
60
- make_in_linux_source ('tags' )
80
+ make_in_linux_source_or_die ('tags' )
61
81
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 ))
66
88
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' )
68
92
elif args ['running?' ]:
69
93
if not exists ('{}/.version' .format (linux_source )):
70
94
print u'\033 [91m\u2716 There is no .version file.'
71
95
exit (1 )
72
96
73
- proc_version = check_output ('cat /proc/version' , shell = True )
97
+ proc_version = check_output_or_die ('cat /proc/version' )
74
98
proc_version_email = re .search ('\((.+?@.+?)\)' , proc_version ).group (1 )
75
99
proc_version_number = re .search ('#([0-9]+)' , proc_version ).group (1 )
76
100
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 ))
78
102
79
103
if 'vagrant' in proc_version_email :
80
104
if compiled_version_number != proc_version_number :
0 commit comments