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