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