~ubports-developers/portcraft/trunk

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
# This is the device flash part of portcraft
import os, sys
import subprocess as sub
from portcraft import adb, utils

tarballName = "preinstalled-touch-armhf.tar.gz"
homeDir = os.getenv("HOME")
portcraftDir = "%s/.cache/portcraft" % (homeDir)


def convertAndroidImg(androidImg):
    sub.check_output("%s/convertAndroidImg %s" % (os.path.dirname(os.path.realpath(__file__)), androidImg))


def prepare_ubuntu_system():
	adb.shell("rm -f /data/ubuntu.img")
	for data in ["system", "android"]:
		adb.shell("rm -rf /data/%s-data" % data)

	adb.shell("dd if=/dev/zero of=/data/ubuntu.img seek=500K bs=4096 count=0 >/dev/null 2>&1")
	adb.shell("mkfs.ext2 -F /data/ubuntu.img >/dev/null 2>&1")
	adb.shell("mkdir -p /cache/system")
	adb.shell("mount -o loop /data/ubuntu.img /cache/system/")

	# system.img is the deprecated name, but we make link to ubuntu.img
	adb.shell("ln /data/ubuntu.img /data/system.img")

def cleanup():
	cleanupDevice()

def cleanupDevice():
	adb.shell("rm -f /recovery/%s" % tarballName)

def enableAdb(isMounted=False, preMountedDir="/a"):
    if not adb.isDeviceOnline():
    	print("please make sure the device is attched via USB in recovery mode")
    	sys.exit()
    if not isMounted:
        adb.shell("mount /data")
        adb.shell("mkdir /a")
        adb.shell("mount /data/ubuntu.img /a")
    adbDir = "%s/adb" % portcraftDir
    if not os.path.exists(adbDir):
        os.makedirs(adbDir)
    adbd = utils.downloadFile("http://people.ubuntu.com/~mariogrip/Ubuntu-touch/fp2/adbd", adbDir, "adbd")
    adbConf = utils.downloadFile("http://people.ubuntu.com/~mariogrip/Ubuntu-touch/fp2/adbd.conf", adbDir, "adbd.conf")
    adb.push("%s/adbd.conf" % adbDir, "%s/etc/init/" % preMountedDir)
    adb.push("%s/adbd" % adbDir, "%s/sbin/" % preMountedDir)
    adb.shell("chmod +x %s/sbin/adbd" % preMountedDir)

def downloadPreinstalledTarball(force=False):
    if os.path.isfile("%s/vivid-preinstalled-touch-armhf.tar.gz" % tarballDir) and not force:
        return "%s/vivid-preinstalled-touch-armhf.tar.gz" % tarballDir
    print("donwloading rootfs tarball ... ")
    tarballDir = "%s/tarball" % portcraftDir
    if not os.path.exists(tarballDir):
        os.makedirs(tarballDir)
    tarballUrl="http://cdimage.ubuntu.com/ubuntu-touch/vivid/daily-preinstalled/current/vivid-preinstalled-touch-armhf.tar.gz"
    utils.downloadFile(tarballUrl, tarballDir, "vivid-preinstalled-touch-armhf.tar.gz")
    return "%s/vivid-preinstalled-touch-armhf.tar.gz" % tarballDir

def rootstockInstall(tarball, androidImg):
    if not isDeviceOnline():
    	print("please make sure the device is attched via USB in recovery mode")
    	sys.exit()

    print("mounting system ...")
    adb.shell("mount -a")

    print("transfering rootfs tarball ... ")
    adb.push(tarball, "/recovery/%s" % tarballName)

    print("preparing system-image on device ... ")
    prepareUbuntuSystem()

    print("unpacking rootfs tarball to system-image ... ")
    adb.shell("cd /cache/system && zcat /recovery/%s | tar xf -" % tarballName)
    adb.shell("mkdir -p /cache/system/android/firmware")
    adb.shell("mkdir -p /cache/system/android/persist")
    adb.shell("mkdir -p /cache/system/userdata")
    adb.shell("[ -e /cache/system/SWAP.swap ] && mv /cache/system/SWAP.swap /data/SWAP.img")
    for link in ["cache", "data", "factory", "firmware", "persist", "system"]:
	       adb.shell("cd /cache/system && ln -s /android/%s %s" % link)
    adb.shell("cd /cache/system/lib && ln -s /system/lib/modules modules")
    adb.shell("cd /cache/system && ln -s /android/system/vendor vendor")
    adb.shell("[ -e /cache/system/etc/mtab ] && rm /cache/system/etc/mtab")
    adb.shell("cd /cache/system/etc && ln -s /proc/mounts mtab")


    print("adding android system image to installation ... ")
    replaceAndroidImg(androidImg, False, "/cache/system")

    print("enabling Mir ... ")
    adb.shell("touch /cache/system/home/phablet/.display-mir")

    print("cleaning up on device ... ")
    cleanupDevice()

    print("rebooting device")
    adb.reboot()

def replaceAndroidImg(androidDir, mount=True, mountDir="/a"):
    convertAndroidImg()
    if (mount):
        adb.mount()
        adb.shell("mkdir %s" % mountDir)
        adb.shell("mount -o loop /data/system.img %s" % mountDir)
    androidImgPlaceDir="/var/lib/lxc/android/"
    adb.push(androidImg, "%s%s" % (mountDir, androidDir))


def tarballFromWeb(androidImg):
    if not isDeviceOnline():
    	print("please make sure the device is attched via USB in recovery mode")
    	sys.exit()
    tarball = downloadPreinstalledTarball()
    rootstockInstall(tarball, androidImg)