Zhizhou Yang | 81d651f | 2020-02-10 16:51:20 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # Copyright 2010 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 7 | """Script to enter the ChromeOS chroot with mounted sources. |
| 8 | |
| 9 | This script enters the chroot with mounted sources. |
| 10 | """ |
| 11 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 12 | from __future__ import print_function |
| 13 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 14 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 15 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 16 | import argparse |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 17 | import getpass |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 18 | import os |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 19 | import pwd |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 20 | import sys |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 21 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 22 | from cros_utils import command_executer |
| 23 | from cros_utils import logger |
| 24 | from cros_utils import misc |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 25 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 26 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 27 | class MountPoint(object): |
| 28 | """Mount point class""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 29 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 30 | def __init__(self, external_dir, mount_dir, owner, options=None): |
asharif | 6c61913 | 2013-02-15 21:55:28 +0000 | [diff] [blame] | 31 | self.external_dir = os.path.realpath(external_dir) |
| 32 | self.mount_dir = os.path.realpath(mount_dir) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 33 | self.owner = owner |
| 34 | self.options = options |
| 35 | |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 36 | def CreateAndOwnDir(self, dir_name): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 37 | retv = 0 |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 38 | if not os.path.exists(dir_name): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 39 | command = 'mkdir -p ' + dir_name |
| 40 | command += ' || sudo mkdir -p ' + dir_name |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 41 | retv = command_executer.GetCommandExecuter().RunCommand(command) |
| 42 | if retv != 0: |
| 43 | return retv |
asharif | 556f4ff | 2013-02-15 04:50:35 +0000 | [diff] [blame] | 44 | pw = pwd.getpwnam(self.owner) |
| 45 | if os.stat(dir_name).st_uid != pw.pw_uid: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 46 | command = 'sudo chown -f ' + self.owner + ' ' + dir_name |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 47 | retv = command_executer.GetCommandExecuter().RunCommand(command) |
| 48 | return retv |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 49 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 50 | def DoMount(self): |
asharif | 6c61913 | 2013-02-15 21:55:28 +0000 | [diff] [blame] | 51 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 52 | mount_signature = '%s on %s' % (self.external_dir, self.mount_dir) |
| 53 | command = 'mount' |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 54 | retv, out, _ = ce.RunCommandWOutput(command) |
asharif | 6c61913 | 2013-02-15 21:55:28 +0000 | [diff] [blame] | 55 | if mount_signature not in out: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 56 | retv = self.CreateAndOwnDir(self.mount_dir) |
| 57 | logger.GetLogger().LogFatalIf(retv, 'Cannot create mount_dir!') |
| 58 | retv = self.CreateAndOwnDir(self.external_dir) |
| 59 | logger.GetLogger().LogFatalIf(retv, 'Cannot create external_dir!') |
| 60 | retv = self.MountDir() |
| 61 | logger.GetLogger().LogFatalIf(retv, 'Cannot mount!') |
| 62 | return retv |
asharif | 6c61913 | 2013-02-15 21:55:28 +0000 | [diff] [blame] | 63 | else: |
| 64 | return 0 |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 65 | |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 66 | def UnMount(self): |
| 67 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 68 | return ce.RunCommand('sudo umount %s' % self.mount_dir) |
asharif | c97199a | 2013-02-15 22:48:45 +0000 | [diff] [blame] | 69 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 70 | def MountDir(self): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 71 | command = 'sudo mount --bind ' + self.external_dir + ' ' + self.mount_dir |
| 72 | if self.options == 'ro': |
| 73 | command += ' && sudo mount --bind -oremount,ro ' + self.mount_dir |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 74 | retv = command_executer.GetCommandExecuter().RunCommand(command) |
| 75 | return retv |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 76 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 77 | def __str__(self): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 78 | ret = '' |
| 79 | ret += self.external_dir + '\n' |
| 80 | ret += self.mount_dir + '\n' |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 81 | if self.owner: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 82 | ret += self.owner + '\n' |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 83 | if self.options: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 84 | ret += self.options + '\n' |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 85 | return ret |
| 86 | |
| 87 | |
| 88 | def Main(argv, return_output=False): |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 89 | """The main function.""" |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 90 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 91 | parser = argparse.ArgumentParser() |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 92 | parser.add_argument( |
| 93 | '-c', |
| 94 | '--chromeos_root', |
| 95 | dest='chromeos_root', |
| 96 | default='../..', |
| 97 | help='ChromeOS root checkout directory.') |
| 98 | parser.add_argument( |
| 99 | '-t', |
| 100 | '--toolchain_root', |
| 101 | dest='toolchain_root', |
| 102 | help='Toolchain root directory.') |
| 103 | parser.add_argument( |
| 104 | '-o', '--output', dest='output', help='Toolchain output directory') |
| 105 | parser.add_argument( |
| 106 | '--sudo', |
| 107 | dest='sudo', |
| 108 | action='store_true', |
| 109 | default=False, |
| 110 | help='Run the command with sudo.') |
| 111 | parser.add_argument( |
| 112 | '-r', |
| 113 | '--third_party', |
| 114 | dest='third_party', |
| 115 | help='The third_party directory to mount.') |
| 116 | parser.add_argument( |
| 117 | '-m', |
| 118 | '--other_mounts', |
| 119 | dest='other_mounts', |
| 120 | help='Other mount points in the form: ' |
| 121 | 'dir:mounted_dir:options') |
| 122 | parser.add_argument( |
| 123 | '-s', |
| 124 | '--mount-scripts-only', |
| 125 | dest='mount_scripts_only', |
| 126 | action='store_true', |
| 127 | default=False, |
| 128 | help='Mount only the scripts dir, and not the sources.') |
| 129 | parser.add_argument( |
| 130 | 'passthrough_argv', |
| 131 | nargs='*', |
| 132 | help='Command to be executed inside the chroot.') |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 133 | |
| 134 | options = parser.parse_args(argv) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 135 | |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 136 | chromeos_root = options.chromeos_root |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 137 | |
| 138 | chromeos_root = os.path.expanduser(chromeos_root) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 139 | if options.toolchain_root: |
| 140 | options.toolchain_root = os.path.expanduser(options.toolchain_root) |
asharif | 1762130 | 2013-02-15 04:46:35 +0000 | [diff] [blame] | 141 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 142 | chromeos_root = os.path.abspath(chromeos_root) |
| 143 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 144 | tc_dirs = [] |
asharif | 642509c | 2013-02-15 09:19:32 +0000 | [diff] [blame] | 145 | if options.toolchain_root is None or options.mount_scripts_only: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 146 | m = 'toolchain_root not specified. Will not mount toolchain dirs.' |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 147 | logger.GetLogger().LogWarning(m) |
| 148 | else: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 149 | tc_dirs = [ |
| 150 | options.toolchain_root + '/google_vendor_src_branch/gcc', |
| 151 | options.toolchain_root + '/google_vendor_src_branch/binutils' |
| 152 | ] |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 153 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 154 | for tc_dir in tc_dirs: |
| 155 | if not os.path.exists(tc_dir): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 156 | logger.GetLogger().LogError('toolchain path ' + tc_dir + |
| 157 | ' does not exist!') |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 158 | parser.print_help() |
| 159 | sys.exit(1) |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 160 | |
| 161 | if not os.path.exists(chromeos_root): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 162 | logger.GetLogger().LogError('chromeos_root ' + options.chromeos_root + |
| 163 | ' does not exist!') |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 164 | parser.print_help() |
| 165 | sys.exit(1) |
| 166 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 167 | if not os.path.exists(chromeos_root + '/src/scripts/build_packages'): |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 168 | logger.GetLogger().LogError(options.chromeos_root + |
| 169 | '/src/scripts/build_packages' |
| 170 | ' not found!') |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 171 | parser.print_help() |
| 172 | sys.exit(1) |
| 173 | |
asharif | b225e79 | 2013-02-15 21:20:11 +0000 | [diff] [blame] | 174 | version_dir = os.path.realpath(os.path.expanduser(os.path.dirname(__file__))) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 175 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 176 | mounted_tc_root = '/usr/local/toolchain_root' |
| 177 | full_mounted_tc_root = chromeos_root + '/chroot/' + mounted_tc_root |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 178 | full_mounted_tc_root = os.path.abspath(full_mounted_tc_root) |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 179 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 180 | mount_points = [] |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 181 | for tc_dir in tc_dirs: |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 182 | last_dir = misc.GetRoot(tc_dir)[1] |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 183 | mount_point = MountPoint(tc_dir, full_mounted_tc_root + '/' + last_dir, |
| 184 | getpass.getuser(), 'ro') |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 185 | mount_points.append(mount_point) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 186 | |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 187 | # Add the third_party mount point if it exists |
| 188 | if options.third_party: |
| 189 | third_party_dir = options.third_party |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 190 | logger.GetLogger().LogFatalIf(not os.path.isdir(third_party_dir), |
| 191 | '--third_party option is not a valid dir.') |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 192 | else: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 193 | third_party_dir = os.path.abspath( |
| 194 | '%s/../../../third_party' % os.path.dirname(__file__)) |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 195 | |
| 196 | if os.path.isdir(third_party_dir): |
Zhizhou Yang | 81d651f | 2020-02-10 16:51:20 -0800 | [diff] [blame] | 197 | mount_point = MountPoint( |
| 198 | third_party_dir, |
| 199 | ('%s/%s' % (full_mounted_tc_root, os.path.basename(third_party_dir))), |
| 200 | getpass.getuser()) |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 201 | mount_points.append(mount_point) |
kbaclawski | 6999ada | 2013-02-15 19:57:09 +0000 | [diff] [blame] | 202 | |
asharif | 541b639 | 2013-02-15 04:50:38 +0000 | [diff] [blame] | 203 | output = options.output |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 204 | if output is None and options.toolchain_root: |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 205 | # Mount the output directory at /usr/local/toolchain_root/output |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 206 | output = options.toolchain_root + '/output' |
asharif | 01410cc | 2013-02-15 09:19:31 +0000 | [diff] [blame] | 207 | |
| 208 | if output: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 209 | mount_points.append( |
| 210 | MountPoint(output, full_mounted_tc_root + '/output', getpass.getuser())) |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 211 | |
| 212 | # Mount the other mount points |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 213 | mount_points += CreateMountPointsFromString(options.other_mounts, |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 214 | chromeos_root + '/chroot/') |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 215 | |
kbaclawski | 20082a0 | 2013-02-16 02:12:57 +0000 | [diff] [blame] | 216 | last_dir = misc.GetRoot(version_dir)[1] |
asharif | 8697d4e | 2013-02-15 09:18:09 +0000 | [diff] [blame] | 217 | |
| 218 | # Mount the version dir (v14) at /usr/local/toolchain_root/v14 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 219 | mount_point = MountPoint(version_dir, full_mounted_tc_root + '/' + last_dir, |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 220 | getpass.getuser()) |
| 221 | mount_points.append(mount_point) |
| 222 | |
| 223 | for mount_point in mount_points: |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 224 | retv = mount_point.DoMount() |
| 225 | if retv != 0: |
| 226 | return retv |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 227 | |
| 228 | # Finally, create the symlink to build-gcc. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 229 | command = 'sudo chown ' + getpass.getuser() + ' ' + full_mounted_tc_root |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 230 | retv = command_executer.GetCommandExecuter().RunCommand(command) |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 231 | |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 232 | try: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 233 | CreateSymlink(last_dir + '/build-gcc', full_mounted_tc_root + '/build-gcc') |
| 234 | CreateSymlink(last_dir + '/build-binutils', |
| 235 | full_mounted_tc_root + '/build-binutils') |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 236 | except Exception as e: |
asharif | 0b2f040 | 2013-02-15 04:50:25 +0000 | [diff] [blame] | 237 | logger.GetLogger().LogError(str(e)) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 238 | |
asharif | 3666653 | 2013-02-15 21:08:14 +0000 | [diff] [blame] | 239 | # Now call cros_sdk --enter with the rest of the arguments. |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 240 | command = 'cd %s/src/scripts && cros_sdk --enter' % chromeos_root |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 241 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 242 | if len(options.passthrough_argv) > 1: |
| 243 | inner_command = ' '.join(options.passthrough_argv[1:]) |
asharif | 51516da | 2013-02-15 04:56:12 +0000 | [diff] [blame] | 244 | inner_command = inner_command.strip() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 245 | if inner_command.startswith('-- '): |
asharif | 51516da | 2013-02-15 04:56:12 +0000 | [diff] [blame] | 246 | inner_command = inner_command[3:] |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 247 | command_file = 'tc_enter_chroot.cmd' |
| 248 | command_file_path = chromeos_root + '/src/scripts/' + command_file |
Zhizhou Yang | 81d651f | 2020-02-10 16:51:20 -0800 | [diff] [blame] | 249 | retv = command_executer.GetCommandExecuter().RunCommand('sudo rm -f ' + |
| 250 | command_file_path) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 251 | if retv != 0: |
| 252 | return retv |
Zhizhou Yang | 81d651f | 2020-02-10 16:51:20 -0800 | [diff] [blame] | 253 | with open(command_file_path, 'w', encoding='utf-8') as f: |
| 254 | f.write(inner_command) |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 255 | logger.GetLogger().LogCmd(inner_command) |
Zhizhou Yang | 81d651f | 2020-02-10 16:51:20 -0800 | [diff] [blame] | 256 | retv = command_executer.GetCommandExecuter().RunCommand('chmod +x ' + |
| 257 | command_file_path) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 258 | if retv != 0: |
| 259 | return retv |
asharif | 52284c6 | 2013-02-15 19:59:08 +0000 | [diff] [blame] | 260 | |
| 261 | if options.sudo: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 262 | command += ' sudo ./' + command_file |
asharif | 52284c6 | 2013-02-15 19:59:08 +0000 | [diff] [blame] | 263 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 264 | command += ' ./' + command_file |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 265 | retv = command_executer.GetCommandExecuter().RunCommandGeneric( |
Luis Lozano | 036c923 | 2015-12-10 10:47:01 -0800 | [diff] [blame] | 266 | command, return_output) |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 267 | return retv |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 268 | else: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 269 | os.chdir('%s/src/scripts' % chromeos_root) |
asharif | 3666653 | 2013-02-15 21:08:14 +0000 | [diff] [blame] | 270 | ce = command_executer.GetCommandExecuter() |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 271 | _, out, _ = ce.RunCommandWOutput('which cros_sdk') |
asharif | 3666653 | 2013-02-15 21:08:14 +0000 | [diff] [blame] | 272 | cros_sdk_binary = out.split()[0] |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 273 | return os.execv(cros_sdk_binary, ['', '--enter']) |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 274 | |
| 275 | |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 276 | def CreateMountPointsFromString(mount_strings, chroot_dir): |
| 277 | # String has options in the form dir:mount:options |
| 278 | mount_points = [] |
| 279 | if not mount_strings: |
| 280 | return mount_points |
| 281 | mount_list = mount_strings.split() |
| 282 | for mount_string in mount_list: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 283 | mount_values = mount_string.split(':') |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 284 | external_dir = mount_values[0] |
| 285 | mount_dir = mount_values[1] |
raymes | a7d219c | 2013-02-15 04:56:23 +0000 | [diff] [blame] | 286 | if len(mount_values) > 2: |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 287 | options = mount_values[2] |
| 288 | else: |
| 289 | options = None |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 290 | mount_point = MountPoint(external_dir, chroot_dir + '/' + mount_dir, |
asharif | da9ac65 | 2013-02-15 04:50:09 +0000 | [diff] [blame] | 291 | getpass.getuser(), options) |
| 292 | mount_points.append(mount_point) |
| 293 | return mount_points |
asharif | 252df0f | 2013-02-15 04:46:28 +0000 | [diff] [blame] | 294 | |
| 295 | |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 296 | def CreateSymlink(target, link_name): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 297 | logger.GetLogger().LogFatalIf( |
| 298 | target.startswith('/'), "Can't create symlink to absolute path!") |
| 299 | real_from_file = misc.GetRoot(link_name)[0] + '/' + target |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 300 | if os.path.realpath(real_from_file) != os.path.realpath(link_name): |
| 301 | if os.path.exists(link_name): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 302 | command = 'rm -rf ' + link_name |
asharif | 8a87387 | 2013-02-15 04:56:52 +0000 | [diff] [blame] | 303 | command_executer.GetCommandExecuter().RunCommand(command) |
| 304 | os.symlink(target, link_name) |
| 305 | |
| 306 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 307 | if __name__ == '__main__': |
asharif | 2198c51 | 2013-02-15 09:21:35 +0000 | [diff] [blame] | 308 | retval = Main(sys.argv) |
| 309 | sys.exit(retval) |