forked from pipaos/qt5-rpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsysroot.py
executable file
·1314 lines (1085 loc) · 51.9 KB
/
xsysroot.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#
# xsysroot
#
# A small tool that gives sysroot access to multiple ARM Linux images on a i686 host
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Albert Casals - [email protected]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
__version__ = 1.914
import os
import sys
import socket
import subprocess
import json
import re
from optparse import OptionParser
default_settings='''
{
"default" : {
"description": "pipaOS 3.5 Wheezy",
"nbdev" : "/dev/nbd5",
"nbdev_part" : "p2",
"sysroot" : "/tmp/pipaos",
"tmp" : "/tmp/pipaos-tmp",
"backing_image": "~/pipaos-3.5-wheezy-xgui.img.gz",
"qcow_image": "~/pipaos-3.5-wheezy-xgui.qcow",
"qcow_size": "4G",
"display": "1024x768x24 fluxbox"
}
}
'''
class XSysroot():
'''
A class which encapsulates a mount based access to a ARM sysroot image
'''
def __init__(self, profile=None, verbose=True):
self.verbose=verbose
self.settings_filename='xsysroot.conf'
self.vnc_start_port=5900
self.last_profile_cache=os.path.join(os.path.expanduser('~'), '.xsysroot')
# Filenames in the sysroot to avoid Qemu syscall traps, for cross-CPU native libraries
self.ld_so_preload='etc/ld.so.preload'
self.ld_so_preload_backup='{}-disabled'.format(self.ld_so_preload)
# In order to allow your sysroot access to private network data
# a custom DNS setting is specified here.
self.dns_server='8.8.8.8'
print "profile ", profile
# choose a settings profile, or set to last used
if not profile:
self.profile=self._get_active_profile()
else:
# print "profile is ", profile
self.profile=profile
self._load_profile()
def _get_settings_filename(self):
'''
Finds the system-wide or private user configuration file
'''
settings_system=os.path.join(os.path.expanduser('/etc'), self.settings_filename)
settings_user=os.path.join(os.path.expanduser('~'), self.settings_filename)
if os.path.isfile(settings_system):
# print "settings_system ", settings_system
return settings_system
if os.path.isfile(settings_user):
# print "settings_user", settings_user
return settings_user
return None
def _get_active_profile(self):
'''
Returns the profile name you are currently using
'''
try:
with open(self.last_profile_cache, 'r') as f:
profile,value=f.read().split(':')
if profile=='last_profile':
return value.strip()
except:
return 'default'
def _set_active_profile(self, profilename):
'''
Keeps track of the profile you are currently working on
'''
with open(self.last_profile_cache, 'w') as f:
f.write('last_profile: {}\n'.format(profilename))
def _get_add_mounts(self):
'''
Returns a list of dictionaries of additional mount points.
These optional mounts can be specified using the "add_mounts" key in the
configuration file, for example:
"add_mounts" : "p3:/tmp/third p4:/tmp/fourth"
Each dictionary comes in the form: { 'device': '/dev/nbdXpY', 'mount': '/wherever' }
If no additional mounts are specified, an empty list is returned.
'''
additional_mounts=[]
if self.settings.has_key('add_mounts'):
extra_mounts = self.settings['add_mounts'].split(' ')
for new_mount in extra_mounts:
new_device, new_mountpoint = new_mount.split(':')
additional_mounts.append(
{ 'device' : '{}{}'.format(self.settings['nbdev'], new_device),
'mount' : '{}'.format(new_mountpoint) })
return additional_mounts
def _get_virtual_display(self):
'''
Returns a display number, requested resolution and Window Manager to allocate a virtual screen.
'''
try:
# Display resolution and window manager to use (e.g. "1024x768x24 fluxbox")
resolution, win_manager = self.settings['display'].split(' ')
# The display number will be bound to the same NBDEV device number
display_number = re.search('(\d+)', self.settings['nbdev']).group()
return display_number, resolution, win_manager
except:
return None, None, None
def _load_profile(self):
'''
Loads your current working profile from the configuration file at /etc,
your home directory, or embedded in the xsysroot module.
'''
self.settings=None
try:
filename=self._get_settings_filename()
if filename:
with open(filename, 'r') as f:
self.settings=json.load(f)[self.profile]
print self.settings
else:
filename=__file__
self.profile='default'
self.settings=json.loads(default_settings)[self.profile]
print self.settings
except:
# FIXME: when no self.profile found it does raise exception
print 'could not load settings - please check Json syntax ({})'.format(filename)
raise
self._set_active_profile(self.profile)
# Expand shell macros, this allows to embed things like (date +%d) and ~ homedir.
self.settings['sysroot'] = os.popen(
'echo {}'.format(self.settings['sysroot'])).read().strip('\n')
self.settings['tmp'] = os.popen(
'echo {}'.format(self.settings['tmp'])).read().strip('\n')
self.settings['backing_image'] = os.popen(
'echo {}'.format(self.settings['backing_image'])).read().strip('\n')
self.settings['qcow_image'] = os.popen(
'echo {}'.format(self.settings['qcow_image'])).read().strip('\n')
def _uncompress_backing_image(self):
'''
Uncompress the backing image if necessary, returns the raw image filename
'''
if not os.path.isfile(self.settings['backing_image']):
return None
file_pathname, extension=os.path.splitext(self.settings['backing_image'])
if extension in ('.gz', '.zip', '.xz'):
uncompressed=self.settings['qcow_image'].replace('.qcow', '.img')
if os.path.isfile(uncompressed):
print 'Removing uncompressed backing image {}'.format(uncompressed)
rc=self._run_cmd('rm {}'.format(uncompressed))
if extension=='.gz':
uncompress='gunzip -c'
elif extension=='.zip':
uncompress='unzip -p'
elif extension=='xz':
uncompress='xz --decompress --stdout'
else:
return None
# Uncompress again to get the latest version of the backing image
print 'Uncompressing image {} into {}'.format(self.settings['backing_image'], uncompressed)
p=self._run_cmd('{} {} > {}'.format(uncompress, self.settings['backing_image'], uncompressed))
elif extension in ('.img'):
# backing file is in raw format, no need to uncompress
uncompressed=self.settings['backing_image']
else:
return None
return uncompressed
def _prepare_sysroot(self):
if not self.is_mounted():
print 'sysroot not mounted, please mount first with -m'
return False
# Copy ARM emulator, setup a default DNS, and remove libcofi.so preload
print 'Preparing sysroot for chroot to function correctly'
self._run_cmd('sudo mkdir -p {sysroot}/usr/bin'.format(**self.settings))
self._run_cmd('sudo cp $(which qemu-arm-static) ' \
'{sysroot}/usr/bin'.format(**self.settings))
self.edfile('/etc/resolv.conf', 'nameserver {}'.format(self.dns_server), verbose=True)
print 'Preparation done'
return True
def _run_cmd(self, command):
'''
Runs a command on the host system, returns its error level code
'''
rc=os.system(command)
return os.WEXITSTATUS(rc)
def _xrun_cmd(self, environment='LC_ALL=C', command='/bin/bash', as_user=None):
'''
Runs a command inside the sysroot.
If the sysroot has a virtual display attached to it,
the DISPLAY environment variable will be pointing to it.
'''
userspec=''
display, _, _=self._get_virtual_display()
if display:
environment='"{}" "{}"'.format(environment, 'DISPLAY=:{}'.format(display))
if as_user:
userspec='-c "su - {}"'.format(as_user)
cmdline='sudo {} chroot {} {} {}'.format(environment, self.settings['sysroot'], command, userspec)
return self._run_cmd(cmdline)
def list_profiles(self):
'''
Lists all profiles defined in your configuration file
'''
print 'Active profile: {} ({})'.format(self.profile, self.settings['description'])
try:
print 'Available profiles (* means mounted)'
filename=self._get_settings_filename()
if filename:
with open(filename, 'r') as f:
settings=json.load(f)
for profile in settings:
mounted=' '
if self.is_mounted(settings=settings[profile]):
mounted = '*'
print ' {} {} ({})'.format(mounted, profile, settings[profile]['description'])
else:
print 'could not find settings file: {}'.format(self.settings_filename)
except:
print 'could not load settings - please check Json syntax'
raise
def print_settings(self):
'''
Prints all variables defined in the current profile
'''
for k,v in self.settings.items():
if k in 'display':
display, resolution, wmgr=self._get_virtual_display()
vnc = 'n/a'
try:
# Find out if the VNC server is running on this virtual display
x11_vnc_pid = int(os.popen('pgrep -f "[x]11vnc -display :{}"'.format(display)).read())
if x11_vnc_pid > 1:
vnc='{}:{}'.format(socket.gethostname(), int(display)+self.vnc_start_port)
except:
pass
v='Display: {}, Resolution: {}, WMgr: {}, VNC: {}'.format(display, resolution, wmgr, vnc)
print '{:<25}: {}'.format(k,v)
def print_is_mounted(self):
'''
Displays a message to say if the sysroot is mounted
'''
ismounted=self.is_mounted()
print 'sysroot mounted?', ismounted
return ismounted
def is_mounted(self, settings=None):
'''
Returns True if the current profile sysroot is mounted
'''
if not settings:
settings=self.settings
rc=self._run_cmd('mountpoint {sysroot} > /dev/null 2>&1'.format(**settings))
return (rc == 0)
def status(self):
'''
Displays current profile status and variable information
'''
print 'Active profile: {} ({})'.format(self.profile, self.settings['description'])
self.print_is_mounted()
self.print_settings()
return True
def query(self, variable):
'''
Queries a variable from your current profile configuration
Useful for automated scripts - i.e. cat $(xsysroot -q tmp)
'''
return self.settings[variable]
def running(self):
'''
Finds and reports any processes currently running on the sysroot
Returns True if processes were found, False otherwise
'''
if not self.is_mounted():
print 'sysroot not mounted'
return False
rc=self._run_cmd('sudo lsof -l {sysroot}'.format(**self.settings))
return (rc == 0)
def mount(self):
'''
Mounts the sysroot image to get ready for use
'''
mounted=self.is_mounted()
if mounted:
print 'sysroot already mounted'
return mounted
elif not os.path.isfile(self.settings['qcow_image']):
print 'Qcow image not found {qcow_image} - please run "renew"'.format(**self.settings)
return mounted
else:
print 'binding qcow image:', self.settings['qcow_image']
p=self._run_cmd('mkdir -p {}'.format(self.settings['sysroot']))
p=self._run_cmd('mkdir -p {}'.format(self.settings['tmp']))
# Connect the image and mount the "nbdev" root partition
p=self._run_cmd('sudo qemu-nbd -c {nbdev} {qcow_image}; sync'.format(**self.settings))
print 'mounting root partition {nbdev}{nbdev_part} -> {sysroot}'.format(**self.settings)
p=self._run_cmd('sudo mount {nbdev}{nbdev_part} {sysroot}'.format(**self.settings))
# Disable ld.so.preload from dragging QEMU unsupported syscalls (restored on umount)
if os.path.isfile('{}/{}'.format(self.query('sysroot'), self.ld_so_preload)):
self._run_cmd('sudo mv -fv {}/{} {}/{}'.format(
self.query('sysroot'), self.ld_so_preload,
self.query('sysroot'), self.ld_so_preload_backup))
# Map linux virtual file systems into the host
p=self._run_cmd('sudo mount --bind /dev {sysroot}/dev'.format(**self.settings))
p=self._run_cmd('sudo mount --bind /proc {sysroot}/proc'.format(**self.settings))
p=self._run_cmd('sudo mount --bind /sys {sysroot}/sys'.format(**self.settings))
p=self._run_cmd('sudo mount --bind {tmp} {sysroot}/tmp'.format(**self.settings))
# try to mount the boot partition if specified
if self.settings.has_key('boot_part') and self.settings.has_key('sysboot'):
p=self._run_cmd('mkdir -p {}'.format(self.settings['sysboot']))
print 'mounting boot partition {nbdev}{boot_part} -> {sysboot}'.format(**self.settings)
rc=self._run_cmd('sudo mount {nbdev}{boot_part} {sysboot}'.format(**self.settings))
if not rc:
# Create a bind mount so the boot partition is accesible from the root
self._run_cmd('sudo mount --bind {sysboot} {sysroot}/boot'.format(**self.settings))
# mount additional partitions if specified
add_mounts = self._get_add_mounts()
if len(add_mounts):
print 'mounting additional partitions'
for extra_mount in add_mounts:
rc=self._run_cmd('mkdir {mount} ; sudo mount {device} {mount}'.format(**extra_mount))
print 'mounted {} => {} rc={}'.format(
extra_mount['device'], extra_mount['mount'], rc)
mounted=self.is_mounted()
print 'Mount done'
# Start a virtual display server, bound to a tcp endpoint so the sysroot can connect to it
display_number, resolution, win_manager=self._get_virtual_display()
if mounted and display_number and resolution and win_manager:
# Start a Frame Buffer headless X server.
cmdline='xvfb-run --server-args="-screen 0 {} -shmem" --xauth-protocol="127.0.0.1:{}" ' \
'--server-num {} {} > /dev/null 2>&1 &'.format(resolution, display_number, display_number, win_manager)
rc=self._run_cmd(cmdline)
if rc == 0:
print 'started display number :{} resolution {} window manager {}'.format(display_number, resolution, win_manager)
# Start a VNC server attached to the fake X server, on termination it will die when we terminate the headless.
rc=self._run_cmd('x11vnc -display :{} -forever -shared -bg -N ' \
'-no6 -noipv6 > /dev/null 2>&1'.format(display_number))
if rc == 0:
print 'attached a VNC server to display :{} network address {}:{}'.format(
display_number, socket.gethostname(), int(display_number)+self.vnc_start_port)
if not mounted:
# if problems mounting, tell Qemu to free the image
p=self._run_cmd('sudo qemu-nbd -d {nbdev}; sync'.format(**self.settings))
return mounted
def umount(self):
'''
Unmounts the sysroot image and releases associated resources
'''
mounted=self.is_mounted()
if not mounted:
print 'sysroot is already unmounted'
return True
else:
# sanity check
if self.running() == True:
print 'ERROR - there seem to be processes working on this sysroot, umount aborted'
return False
# Restore ld.so.preload to its original state (QEMU syscalls safeguard)
if os.path.isfile('{}/{}'.format(self.query('sysroot'), self.ld_so_preload_backup)):
self._run_cmd('sudo mv -fv {}/{} {}/{}'.format(
self.query('sysroot'), self.ld_so_preload_backup,
self.query('sysroot'), self.ld_so_preload))
print 'unbinding {qcow_image}'.format(**self.settings)
p=self._run_cmd('sudo umount {sysroot}/tmp'.format(**self.settings))
p=self._run_cmd('sudo umount {sysroot}/sys'.format(**self.settings))
p=self._run_cmd('sudo umount {sysroot}/proc'.format(**self.settings))
p=self._run_cmd('sudo umount {sysroot}/dev'.format(**self.settings))
# try to unmount the boot partition if mounted
if self.settings.has_key('boot_part') and self.settings.has_key('sysboot'):
p=self._run_cmd('sudo umount {sysboot}'.format(**self.settings))
p=self._run_cmd('rmdir {sysboot}'.format(**self.settings))
p=self._run_cmd('sudo umount {sysroot}/boot'.format(**self.settings))
p=self._run_cmd('sudo umount {sysroot} ; rmdir {sysroot}'.format(**self.settings))
# unmount any additional partitions specified
add_mounts = self._get_add_mounts()
if len(add_mounts):
for extra_mount in add_mounts:
rc=self._run_cmd('sudo umount {mount} ; rmdir {mount}'.format(**extra_mount))
p=self._run_cmd('sudo qemu-nbd -d {nbdev}; sync'.format(**self.settings))
mounted=self.is_mounted()
# Stop the virtual display
display_number, resolution, win_manager=self._get_virtual_display()
if not mounted and display_number:
print 'Disconnecting Display number {} and window manager {}'.format(display_number, win_manager)
self._run_cmd('pkill -f "Xvfb :{}"'.format(display_number))
print 'Unmount done'
return (mounted == False)
def renew(self):
'''
Recreates the sysroot from scratch unfolding the original backing image
'''
if self.is_mounted():
print 'sysroot is mounted, please unmount first'
return False
# stop now if the backing image is not available
if not os.path.isfile('{backing_image}'.format(**self.settings)):
print self.settings
print 'Could not find backing image - aborting'
return False
if os.path.isfile('{qcow_image}'.format(**self.settings)):
print 'Removing qcow image {qcow_image}'.format(**self.settings)
rc=self._run_cmd('rm {qcow_image}'.format(**self.settings))
# Get the original backing image, which means uncompress it if necessary
uncompressed=self._uncompress_backing_image()
if not uncompressed:
print 'Backing image not found or unsupported format: {}'.format(self.settings['backing_image'])
return False
# Qcow image size can match the original backing image,
# or be forced to be larger which allows to expand the filesystem (--expand option)
# A smaller value will not work and the mount will likely fail to proceed.
qcow_size=''
try:
qcow_size=self.settings['qcow_size']
print 'Creating qcow image {qcow_image} of new forced size {qcow_size}'.format(**self.settings)
except:
print 'Creating qcow image {qcow_image} of original size'.format(**self.settings)
p=self._run_cmd('qemu-img create -f qcow2 -b {} {} {}'.format(
uncompressed, self.settings['qcow_image'], qcow_size))
if self.mount():
# Prepare image settings to chroot and access network
self._prepare_sysroot()
print 'Renew done'
return True
else:
print 'Error renewing sysroot'
return False
def expand(self):
'''
Expands the last ext2/ext4/ext4 partition to fit the image size
as specified by the "qcow_size" setting. The image has to be unmounted.
Use this function with care, i.e. assume "qcow_image" is volatile.
'''
expanded=False
modified=False
if self.is_mounted():
print 'sysroot is mounted, please unmount before expanding'
return False
# connect the image to a disk device
disk_device='{nbdev}'.format(**self.settings)
part_device='{nbdev}{nbdev_part}'.format(**self.settings)
print 'Connecting image {qcow_image} to find and expand last partition'.format(**self.settings)
rc=self._run_cmd('sudo qemu-nbd -c {nbdev} {qcow_image}; sync'.format(**self.settings))
qcow_image = self.settings['qcow_image']
if qcow_image[-1]=='G':
qcow_image = int(qcow_image[0:-1])*1024
elif qcow_image[-1]=='K':
qcow_image = int(qcow_image[0:-1]) / 1024.0
if rc:
print 'error connecting image rc={}'.format(rc)
return False
def print_partition_info():
try:
# parse details reported by "parted"
info_partition=os.popen('sudo parted --script --machine {nbdev} print'.format(**self.settings)).readlines()
part_num, part_start, part_end, part_size, part_type, unknown1, unknown2=info_partition[len(info_partition)-1].strip().split(':')
print 'Partition number: {} start: {} end: {} size: {} type: {}'.format(part_num, part_start, part_end, part_size, part_type)
return part_num, part_start, part_end, part_size, part_type
except:
return None, None, None, None, None
# find last partition details and recreate it to fit image size extension
part_num, part_start, part_end, part_size, part_type=print_partition_info()
if not part_num:
print 'Could not find partition details, aborting'
else:
if part_type not in ('ext2', 'ext3', 'ext4'):
print 'Could not expand partition number {} of type {}'.format(part_num, part_type)
else:
part_size = int(part_size[0:-1])
if abs(part_size - qcow_image) < 1:
expanded = True
print "OK, got here expanded."
return expanded
rc=self._run_cmd('sudo parted --script {} rm {}'.format(disk_device, part_num))
modified=True
if rc:
print 'Error removing partition number {}'.format(part_num)
else:
rc=self._run_cmd('sudo parted --script {} mkpart primary {} {} 100%'.format(disk_device, part_type, part_start))
if rc:
print 'Error creating new partition at offset {}'.format(part_start)
else:
rc=self._run_cmd('sudo e2fsck -p -f {}; sudo resize2fs {}; sync'.format(part_device, part_device))
if rc:
print 'Error checking and resizing new partition {}'.format(part_device)
else:
print 'Image partition expanded successfully, new layout:'
print_partition_info()
expanded=True
# report results and disconnect image from disk device
if not expanded:
print 'Errors were found and the partition was not expanded'
if modified:
print 'The image integrity might be compromised - you should run "renew"'
rc=self._run_cmd('sudo qemu-nbd -d {nbdev}; sync'.format(**self.settings))
return expanded
def zerofree(self, partition='all', verbose=True):
'''
Fills up a partition free space with zeroes. Increases final image compression ratio.
partition is the NBDEV device name, by default it will zero sysroot partition (nbdev_part variable).
The image needs to be unmounted for this function to work.
'''
success=False
if self.is_mounted():
print 'sysroot is mounted - aborting'
elif partition=='all':
rc=self.zerofree(self.query('nbdev_part'))
success = (rc==0)
elif not re.match('(p\d+)$', partition):
print 'Partition name not recognized, must be p1, p2, .. {}'.format(partition)
else:
print 'connecting image {} to zerofree partition {}'.format(self.query('qcow_image'), partition)
rc_connect=self._run_cmd('sudo qemu-nbd -c {nbdev} {qcow_image}; sync'.format(**self.settings))
if rc_connect:
print 'error connecting image'
else:
zero_device='{}{}'.format(self.query('nbdev'), partition)
print 'zero free', zero_device
cmdline='sudo zerofree {}'.format(zero_device)
if verbose:
cmdline += ' -v'
rc=self._run_cmd(cmdline)
print 'disconnecting image'
rc_disconnect=self._run_cmd('sudo qemu-nbd -d {nbdev}; sync'.format(**self.settings))
success = (rc_connect==0)
return success
def execute(self, command, verbose=True, pipes=False, as_user=None):
'''
Executes a command inside the sysroot.
Returns the program errorlevel, -1 if failure.
Set pipes to True if you are chaining commands through a pipe,
or passing environment variables as in "MYVAR=1; do-whatever"
as_user will execute the command as a different account in the sysroot via su.
'''
if not self.is_mounted():
print 'sysroot not mounted - aborting'
return -1
else:
# If the command uses pipes, embrace it inside bash
if pipes:
command = "/bin/bash -c '{}'".format(command)
if as_user:
command = 'su -l {} -c "{}"'.format(as_user, command)
if verbose:
print 'sysroot executing: {}'.format(command)
return self._xrun_cmd(command=command)
def edfile(self, filename, literal, append=False, verbose=True):
'''
Dumps a literal into a file, appending at the end if specified.
If you need to pass quotation marks, make sure to escape them.
'''
redirection = '>'
if append:
redirection += '>'
return self.execute('/bin/bash -c "echo \'{}\' {} {}"'.format(
literal, redirection, filename), verbose)
def screenshot(self, filename='screenshot.png'):
'''
Takes a screnshot of the current sysroot virtual display
'''
success=False
if not self.is_mounted():
print 'sysroot not mounted - aborting'
return success
display, _, _=self._get_virtual_display()
if display:
rc=self._run_cmd('import -display :{} -window root {}'.format(display, filename))
if rc:
print 'Error taking a screenshot from display {}'.format(display)
else:
success=True
else:
print 'This sysroot does not have a virtual display'
return success
def jail(self):
'''
Protects harmful commands in the sysroot (reboot, shutdown)
and provides a blind sudo without password prompts.
You may find this option useful for testing software.
'''
if not self.is_mounted():
print 'sysroot not mounted - please mount first with -m'
return False
# disable reboot tools
self.execute('ln -sfv $(which true) $(which reboot)', pipes=True, verbose=False)
self.execute('ln -sfv $(which true) $(which poweroff)', pipes=True, verbose=False)
self.execute('ln -sfv $(which true) $(which shutdown)', pipes=True, verbose=False)
self.execute('ln -sfv $(which true) $(which halt)', pipes=True, verbose=False)
# blind sudo - make sure your user belongs to "sudo" group
self.edfile('/etc/sudoers', '%sudo ALL=NOPASSWD: ALL', append=True)
# fake hostname to match the host system
import socket
host_hostname=socket.gethostname()
self.edfile('/etc/hosts', '127.0.0.1 {}'.format(host_hostname), append=True)
return True
def chroot(self, username=None):
'''
Starts an interactive shell into the ARM sysroot as the superuser.
Returns chrooted command termination error level.
'''
if not self.is_mounted():
print 'sysroot not mounted - please mount first with -m'
return -1
else:
print 'Starting sysroot shell into: {sysroot}'.format(**self.settings),
if username:
print 'as user', username
else:
print 'as the superuser'
return self._xrun_cmd(as_user=username)
def depends(self, repo_dir='.'):
'''
Parses a Debian package control file, and install build-depends in the sysroot
'''
success=False
control_file=os.path.join(repo_dir, 'debian/control')
if not self.is_mounted():
print 'sysroot not mounted - aborting'
else:
pkgs_install=''
print 'Checking Build-Dependencies at {}'.format(control_file)
output = subprocess.Popen(['dpkg-checkbuilddeps', control_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
rc=output.wait()
if rc == 1:
# Dependencies need to be installed:
# Parsing the output of dpkg-checkbuilddeps
for item in output.stderr.read().split()[4:]:
if item.find('(') == -1 and item.find(')') == -1:
pkgs_install += '{} '.format(item)
print 'Installing packages in the sysroot:', pkgs_install
rc=self._xrun_cmd(command='apt-get --no-install-recommends -y install {}'.format(pkgs_install))
print 'apt-get completed with rc={}'.format(rc)
success=(rc==0)
elif rc == 2:
print 'dpkg-checkbuilddeps returned with rc={} - no debian control file found'.format(rc)
else:
print 'dpkg-checkbuilddeps failed with unknown rc={}'.format(rc)
return success
def build(self, repo_dir='.', debuild_cmd='echo "y" | debuild --preserve-envvar PATH -us -uc -d -aarmhf'):
debian_dir=os.path.abspath(os.path.join(repo_dir, 'debian'))
pkg_dir=os.path.abspath(repo_dir + '/../')
if not os.path.exists(debian_dir):
print 'cannot find debian package directory at: {}'.format(debian_dir)
return False
# Resolve build dependencies
self.depends(repo_dir=repo_dir)
print 'building package for repo', repo_dir
rc=os.system('cd {} && {}'.format(repo_dir, debuild_cmd))
if rc:
print 'ERROR - failure building package for repo {}'.format(repo_dir)
return False
else:
print 'OK - Debian package built successfuly'
return True
def is_os_platform_supported():
'''
Returns True if your local system processor and
operating system is supported by xsysroot.
'''
local_os = os.popen('uname -s').read().strip()
if not local_os == 'Linux':
return False
local_machine = os.popen('uname -m').read().strip()
if local_machine not in ('i686', 'armv7l', 'armv6', 'armv6l', 'x86_64'):
return False
return True
def check_system_tools():
'''
Checks wether you have the necessary tools to fully utilize xsysroot
Returns True if you can rock & roll, False otherwise
'''
core_tools_ready = 0
min_core_tools = 3
if not is_os_platform_supported():
print 'Sorry, your local system is not supported,'
print 'Xsysroot runs on i386 and ARM based Linux systems.'
return False
# check if the NBD kernel modules is loaded
rc=os.system('lsmod | grep nbd > /dev/null 2>&1')
if (rc):
print 'The NBD kernel module is not loaded'
print ' please add to /etc/modules: "nbd nbds_max=64 max_part=4"'
else:
core_tools_ready += 1
# check for password-less sudo
rc = os.system('sudo -n -k whoami > /dev/null 2>&1')
if rc:
print 'Error: You do not seem to have sudo password-less permissions, please run visudo to fix'
else:
core_tools_ready += 1
# check for qemu tools
rc = os.system('which qemu-arm-static qemu-img qemu-nbd > /dev/null 2>&1')
if rc:
print 'Error: please install qemu-user-static and qemu-utils packages'
else:
core_tools_ready += 1
# check for bin format support
rc = os.system('sudo -n -k which update-binfmts > /dev/null 2>&1')
if rc:
print 'Error: please install binfmt-support package'
# The debian packge build-depends tool
rc = os.system('which dpkg-checkbuilddeps > /dev/null 2>&1')
if rc:
print 'Warning: dpkg-checkbuilddeps cannot be found, --depends will not work'
# The debian packge build-depends tool
rc = os.system('which dpkg-checkbuilddeps > /dev/null 2>&1')
if rc:
print 'Warning: debuild not found, please install devscripts package if you want to --build'
# The fake X server to provide a virtual screen to sysroots
rc = os.system('which Xvfb > /dev/null 2>&1')
if rc:
print 'Warning: Xvfb is not available, virtual displays will not be available'
# The VNC server will allow to connect to the xsysroot virtual display remotely
rc = os.system('which x11vnc > /dev/null 2>&1')
if rc:
print 'Warning: X11VNC is not available, install it to connect remotely to virtual displays'
# Imagemagick import for taking screenshots
rc = os.system('which import > /dev/null 2>&1')
if rc:
print 'Warning: tool "import" not found, screenshots will not be available (you need ImageMagick)'
# Parted tool to generate raw images
rc1 = os.system('sudo -n -k which parted > /dev/null 2>&1')
rc2 = os.system('sudo -n -k which resize2fs > /dev/null 2>&1')
rc3 = os.system('sudo -n -k which e2fsck > /dev/null 2>&1')
if rc1 or rc2 or rc3:
print 'Warning: tools "parted", "resize2fs" or "e2fsck" not found, --geometry option will not be available'
# If minimal core tools are available, xsysroot is ready to work
if core_tools_ready >= min_core_tools:
return True
return False
def create_debian_skeleton(prj_directory):
'''
Creates an empty Debian skeleton folder to generate a package with "debuild"
'''
changelog='''xsysroot (1.0-1) unstable; urgency=low
* xsysroot initial version
-- Albert Casals <[email protected]> Fri, 20 Jun 2015 00:00:00 +0100
'''
control='''Source: xsysroot
Maintainer: Albert Casals <[email protected]>
Section: shells
Priority: optional
Standards-Version: 3.9.4
Build-Depends: debhelper (>=9.0.0)
Package: xsysroot
Architecture: all
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: A small tool that gives sysroot access to multiple ARM Linux images on a i686 host
'''
rules='''
#!/usr/bin/make -f
%:
\tdh $@
'''
if not os.path.exists(prj_directory):
print 'error - cannot access directory: {}'.format(prj_directory)
return False
debian_directory=os.path.join(prj_directory, 'debian')
if os.path.exists(debian_directory):
print 'error - debian directory already exists: {}'.format(debian_directory)
return False
rc=os.system('mkdir -p {}'.format(debian_directory))
if rc:
print 'error - could not create directory: {}'.format(debian_directory)
with open(os.path.join(debian_directory, 'changelog'), 'w') as f:
f.writelines(changelog)
rc=os.system('echo "9" > {}'.format(os.path.join(debian_directory, 'compat')))
rc=os.system('echo "./xsysroot /usr/bin" > {}'.format(os.path.join(debian_directory, 'files')))
with open(os.path.join(debian_directory, 'control'), 'w') as f:
f.writelines(control)
with open(os.path.join(debian_directory, 'rules'), 'w') as f:
f.writelines(rules)
print 'Debian skeleton package created at: {}'.format(debian_directory)
return True
def create_image(geometry, nbdev='/dev/nbd0'):
'''
Builds an empty image file with partitions of given size and file system types
Geometry specifies the image layout in the form "imagefile.img fstype1:size_mb fstype2:size_mb"
Currently supported file system types are "fat" and "ext2" to "ext4".
'''
image_size=0
part_offset=0
partitions=[]
def safe_exec(cmdline, silent=True):
'''
Executes a command making sure it succeds
'''
try:
if silent:
cmdline=cmdline + ' >/dev/null 2>&1'
rc=os.system(cmdline)
assert(rc==0)
except Exception as err:
raise IOError('Error executing step: {}'.format(cmdline))
# collect geometry details
details=geometry.split(' ')
filename=details[0]
if os.path.isfile(filename):
print 'image file already exists: {}'.format(filename)
return False
for partnum, partition in enumerate(details[1:]):
partype, partsize=partition.split(':')
partitions.append({ 'partnum' : partnum, 'partype' : partype, 'partsize': partsize })
image_size += int(partsize)
print 'creating {}MB image file {}...'.format(image_size, filename)
rc=safe_exec('dd if=/dev/zero of={} bs=1MB count={}'.format(filename, image_size))
rc=safe_exec('sudo parted --script {} mklabel msdos'.format(filename))
for part in partitions:
print ' partition {} type {} size {}MB'.format(part['partnum'], part['partype'], part['partsize'])
if part['partnum'] == 0:
# this is the first partition
rc=safe_exec('sudo parted --script {} mkpart primary {} -- 1 {}MB'.format(filename, part['partype'], part['partsize']))
elif part['partnum'] < len(partitions) -1:
# intermediate partition