Ubuntu Pastebin

Paste from Tartan Spartan at Thu, 14 Dec 2017 14:43:43 +0000

Download as text
  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
with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import psutil
import shlex
import shutil
import subprocess

from .Libertine import BaseContainer
from . import utils


def chown_recursive_dirs(path):
    uid = 0
    gid = 0

    if 'SUDO_UID' in os.environ:
        uid = int(os.environ['SUDO_UID'])
    if 'SUDO_GID' in os.environ:
        gid = int(os.environ['SUDO_GID'])

    if uid != 0 and gid != 0:
        for root, dirs, files in os.walk(path):
            for d in dirs:
                os.chown(os.path.join(root, d), uid, gid)
            for f in files:
                os.chown(os.path.join(root, f), uid, gid)

        os.chown(path, uid, gid)


class LibertineChroot(BaseContainer):
    """ 
    A concrete container type implemented using a plain old chroot.
    """ 


    def __init__(self, container_id):
        super().__init__(container_id)

  gid = 0

    if 'SUDO_UID' in os.environ:
        uid = int(os.environ['SUDO_UID'])
    if 'SUDO_GID' in os.environ:
        gid = int(os.environ['SUDO_GID'])

    if uid != 0 and gid != 0:
        for root, dirs, files in os.walk(path):
            for d in dirs:
                os.chown(os.path.join(root, d), uid, gid)
            for f in files:
                os.chown(os.path.join(root, f), uid, gid)

        os.chown(path, uid, gid)


class LibertineChroot(BaseContainer):
    """
    A concrete container type implemented using a plain old chroot.
    """

    def __init__(self, container_id):
        super().__init__(container_id)
        self.container_type = "chroot"
        os.environ['FAKECHROOT_CMD_SUBST'] = '$FAKECHROOT_CMD_SUBST:/usr/bin/chfn=/bin/true'
        os.environ['DEBIAN_FRONTEND'] = 'noninteractive'

    def run_in_container(self, command_string):
        cmd_args = shlex.split(command_string)
        command_prefix = "fakechroot fakeroot chroot " + self.root_path
        args = shlex.split(command_prefix + ' ' + command_string)
        cmd = subprocess.Popen(args)
        return cmd.wait()

    def destroy_libertine_container(self):
        container_root = os.path.join(utils.get_libertine_containers_dir_path(), self.container_id)
        try:
            shutil.rmtree(container_root)
        except Exception as e:
            print("%s" % e)

    def create_libertine_container(self, password=None, multiarch=False, verbosity=1):
        # Create the actual chroot
        command_line = "fakechroot fakeroot debootstrap --verbose --variant=fakechroot {} {}".format(
                    self.installed_release, self.root_path)
        args = shlex.split(command_line)
        cmd = subprocess.Popen(args)
        cmd.wait()

   os.environ['DEBIAN_FRONTEND'] = 'noninteractive'

    def run_in_container(self, command_string):
        cmd_args = shlex.split(command_string)
        command_prefix = "fakechroot fakeroot chroot " + self.root_path
        args = shlex.split(command_prefix + ' ' + command_string)
        cmd = subprocess.Popen(args)
        return cmd.wait()

    def destroy_libertine_container(self):
        container_root = os.path.join(utils.get_libertine_containers_dir_path(), self.container_id)
        try:
            shutil.rmtree(container_root)
        except Exception as e:
            print("%s" % e)

    def create_libertine_container(self, password=None, multiarch=False, verbosity=1):
        # Create the actual chroot
        command_line = "fakechroot fakeroot debootstrap --verbose --variant=fakechroot {} {}".format(
                    self.installed_release, self.root_path)
        args = shlex.split(command_line)
        cmd = subprocess.Popen(args)
        cmd.wait()

        if cmd.returncode != 0:
            print("Failed to create container")
            self.destroy_libertine_container()
            return False

        # Remove symlinks as they can cause ill-behaved recursive behavior in the chroot
        print("Fixing chroot symlinks...")
        os.remove(os.path.join(self.root_path, 'dev'))
        os.remove(os.path.join(self.root_path, 'proc'))

        with open(os.path.join(self.root_path, 'usr', 'sbin', 'policy-rc.d'), 'w+') as fd:
            fd.write("#!/bin/sh\n\n")
            fd.write("while true; do\n")
            fd.write("case \"$1\" in\n")
            fd.write("  -*) shift ;;\n")
            fd.write("  makedev) exit 0;;\n")
            fd.write("  *)  exit 101;;\n")
            fd.write("esac\n")
            fd.write("done\n")
            os.fchmod(fd.fileno(), 0o755)

        # Add universe, multiverse, and -updates to the chroot's sources.list
        if (self.architecture == 'armhf'):
            archive = "deb http://ports.ubuntu.com/ubuntu-ports "
        else:
#SEUMAS NOTE: I changed the above code to the complete URL of course, but leaving it as the original one so you see where I had to change it
            archive = "deb http://archive.ubuntu.com/ubuntu "

 if verbosity == 1:
            print("Updating chroot's sources.list entries...")
        with open(os.path.join(self.root_path, 'etc', 'apt', 'sources.list'), 'a') as fd:
            fd.write(archive + self.installed_release + "-updates main\n")
            fd.write(archive + self.installed_release + " universe\n")
            fd.write(archive + self.installed_release + "-updates universe\n")
            fd.write(archive + self.installed_release + " multiverse\n")
            fd.write(archive + self.installed_release + "-updates multiverse\n")

        utils.create_libertine_user_data_dir(self.container_id)

        if multiarch and self.architecture == 'amd64':
            if verbosity == 1:
                print("Adding i386 multiarch support...")
            self.run_in_container("dpkg --add-architecture i386")

        if verbosity == 1:
            print("Updating the contents of the container after creation...")
        self.update_packages(verbosity)

        for package in self.default_packages:
            if not self.install_package(package, verbosity, update_cache=False):
                print("Failure installing %s during container creation" % package)
                self.destroy_libertine_container()
                return False
 if self.installed_release == "vivid":
            if verbosity == 1:
                print("Installing the Vivid Stable Overlay PPA...")
            if not self.install_package("software-properties-common", verbosity, update_cache=False):
                print("Failure installing software-properties-common during container creation")
                self.destroy_libertine_container()
                return False

            self.run_in_container("add-apt-repository ppa:ci-train-ppa-service/stable-phone-overlay -y")
            self.update_packages(verbosity)

        # Check if the container was created as root and chown the user directories as necessary
        chown_recursive_dirs(utils.get_libertine_container_userdata_dir_path(self.container_id))

        return True

    def update_packages(self, verbosity=1):
        retcode = super().update_packages(verbosity)
        self._run_ldconfig(verbosity)
        return retcode

    def install_package(self, package_name, verbosity=1, no_dialog=False, update_cache=True):
        returncode = super().install_package(package_name, verbosity, no_dialog, update_cache)

        if returncode:

def _build_proot_command(self):
        proot_cmd = shutil.which('proot')
        if not proot_cmd:
            raise RuntimeError('executable proot not found')

        proot_cmd += " -R " + self.root_path

        # Bind-mount the host's locale(s)
        proot_cmd += " -b /usr/lib/locale"

        # Bind-mount extrausers on the phone
        if os.path.exists("/var/lib/extrausers"):
            proot_cmd += " -b /var/lib/extrausers"

        home_path = os.environ['HOME']

        # Bind-mount common XDG direcotries
        bind_mounts = (
            " -b %s:%s"
            % (utils.get_libertine_container_userdata_dir_path(self.container_id), home_path)
        )

        for user_dir in utils.get_common_xdg_user_directories():
            user_dir_path = os.path.join(home_path, user_dir[1])
            bind_mounts += " -b %s:%s" % (user_dir[0], user_dir_path)

        proot_cmd += bind_mounts

        user_dconf_path = os.path.join(home_path, '.config', 'dconf')
        if os.path.exists(user_dconf_path):
            proot_cmd += " -b %s" % user_dconf_path

        return proot_cmd

    def _build_privileged_proot_cmd(self):
        proot_cmd = shutil.which('proot')
        if not proot_cmd:
            raise RuntimeError('executable proot not found')

        proot_cmd += " -b /usr/lib/locale -S " + self.root_path

        return proot_cmd

    def launch_application(self, app_exec_line):
        # FIXME: Disabling seccomp is a temporary measure until we fully understand why we need
        #        it or figure out when we need it.
        os.environ['PROOT_NO_SECCOMP'] = '1'

 # Workaround issue where a custom dconf profile is on the machine
        if 'DCONF_PROFILE' in os.environ:
            del os.environ['DCONF_PROFILE']

        proot_cmd = self._build_proot_command()

        args = shlex.split(proot_cmd)
        args.extend(utils.setup_window_manager(self.container_id, enable_toolbars=True))
        window_manager = psutil.Popen(args)

        args = shlex.split(proot_cmd)
        args.extend(app_exec_line)
        psutil.Popen(args).wait()

        utils.terminate_window_manager(window_manager)

    def _run_ldconfig(self, verbosity=1):
        if verbosity == 1:
            print("Refreshing the container's dynamic linker run-time bindings...")

        command_line = self._build_privileged_proot_cmd() + " ldconfig.REAL"

        args = shlex.split(command_line)
        subprocess.Popen(args).wait()
Download as text