blob: 27514a1dd7cf71605379d0f373a4501736db8bf1 [file] [log] [blame]
asharif252df0f2013-02-15 04:46:28 +00001#!/usr/bin/python2.6
2#
3# Copyright 2010 Google Inc. All Rights Reserved.
4
5"""Script to enter the ChromeOS chroot with mounted sources.
6
7This script enters the chroot with mounted sources.
8"""
9
10__author__ = "asharif@google.com (Ahmad Sharif)"
11
12import getpass
13import optparse
14import os
15import sys
raymes01959ae2013-02-15 04:50:07 +000016from utils import command_executer
17from utils import logger
asharif252df0f2013-02-15 04:46:28 +000018from utils import utils
19
20# Common initializations
raymes01959ae2013-02-15 04:50:07 +000021cmd_executer = command_executer.GetCommandExecuter()
asharif252df0f2013-02-15 04:46:28 +000022
23
asharifda9ac652013-02-15 04:50:09 +000024class MountPoint:
25 def __init__(self, external_dir, mount_dir, owner, options=None):
26 self.external_dir = external_dir
27 self.mount_dir = mount_dir
28 self.owner = owner
29 self.options = options
30
31
32 def CreateMountPoint(self):
33 if not os.path.exists(self.mount_dir):
34 command = "mkdir -p " + self.mount_dir
35 command += " || sudo mkdir -p " + self.mount_dir
36 retval = cmd_executer.RunCommand(command)
37 if retval != 0:
38 return retval
39 command = "sudo chown " + self.owner + " " + self.mount_dir
40 retval = cmd_executer.RunCommand(command)
41 return retval
42
43
44 def DoMount(self):
45 self.CreateMountPoint()
46 self.MountDir()
47
48
49 def MountDir(self):
50 command = "sudo mount --bind " + self.external_dir + " " + self.mount_dir
51 if self.options == "ro":
52 command += " && sudo mount --bind -oremount,ro " + self.mount_dir
53 retval = cmd_executer.RunCommand(command)
54 return retval
55
56
57 def __str__(self):
58 ret = ""
59 ret += self.external_dir + "\n"
60 ret += self.mount_dir + "\n"
61 if self.owner:
62 ret += self.owner + "\n"
63 if self.options:
64 ret += self.options + "\n"
65 return ret
66
67
68def Main(argv, return_output=False):
asharif252df0f2013-02-15 04:46:28 +000069 """The main function."""
70 parser = optparse.OptionParser()
71 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
72 help="ChromeOS root checkout directory.")
73 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
74 help="Toolchain root directory.")
asharifda9ac652013-02-15 04:50:09 +000075 parser.add_option("-o", "--other_mounts", dest="other_mounts",
76 help="Other mount points in the form: " +
77 "dir:mounted_dir:options")
asharif252df0f2013-02-15 04:46:28 +000078
raymes01959ae2013-02-15 04:50:07 +000079 relevant_argv = []
80 passthrough_argv = []
asharif0269d462013-02-15 04:46:31 +000081 for i in xrange(len(argv)):
82 found = False
83 for option in parser.option_list:
84 for long_opt in option._long_opts:
85 if argv[i].startswith(long_opt):
86 found = True
87 break
88 for short_opt in option._short_opts:
89 if argv[i].startswith(short_opt):
90 found = True
91 break
92
93 if found == True:
94 break
95
96 if found == True:
97 relevant_argv.append(argv[i])
98 else:
99 passthrough_argv.append(argv[i])
100
101 options = parser.parse_args(relevant_argv)[0]
asharif252df0f2013-02-15 04:46:28 +0000102
103 if options.chromeos_root is None:
104 chromeos_root = "../.."
105 else:
106 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +0000107
108 chromeos_root = os.path.expanduser(chromeos_root)
asharifda9ac652013-02-15 04:50:09 +0000109 if options.toolchain_root:
110 options.toolchain_root = os.path.expanduser(options.toolchain_root)
asharif17621302013-02-15 04:46:35 +0000111
asharif252df0f2013-02-15 04:46:28 +0000112 chromeos_root = os.path.abspath(chromeos_root)
113
114 if (options.toolchain_root is None or
115 not os.path.exists(options.toolchain_root) or
116 not os.path.exists(chromeos_root)):
117 parser.print_help()
118 sys.exit(1)
119
120 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc"]
raymes01959ae2013-02-15 04:50:07 +0000121 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif252df0f2013-02-15 04:46:28 +0000122 version_dir = rootdir
123
asharif252df0f2013-02-15 04:46:28 +0000124 mounted_tc_root = "/usr/local/toolchain_root"
125 full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root
126 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
asharifda9ac652013-02-15 04:50:09 +0000127
128 mount_points = []
asharif252df0f2013-02-15 04:46:28 +0000129 for tc_dir in tc_dirs:
130 last_dir = utils.GetRoot(tc_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000131 mount_point = MountPoint(tc_dir, full_mounted_tc_root + "/" + last_dir,
132 getpass.getuser(), "ro")
133 mount_points.append(mount_point)
asharif252df0f2013-02-15 04:46:28 +0000134
asharifda9ac652013-02-15 04:50:09 +0000135 mount_points += CreateMountPointsFromString(options.other_mounts,
136 chromeos_root + "/chroot/")
137
asharif252df0f2013-02-15 04:46:28 +0000138 last_dir = utils.GetRoot(version_dir)[1]
asharifda9ac652013-02-15 04:50:09 +0000139 mount_point = MountPoint(version_dir, full_mounted_tc_root + "/" + last_dir,
140 getpass.getuser())
141 mount_points.append(mount_point)
142
143 for mount_point in mount_points:
144 mount_point.DoMount()
asharif252df0f2013-02-15 04:46:28 +0000145
146 # Finally, create the symlink to build-gcc.
asharifda9ac652013-02-15 04:50:09 +0000147 command = "sudo chown " + getpass.getuser() + " " + full_mounted_tc_root
148 retval = cmd_executer.RunCommand(command)
149
asharif252df0f2013-02-15 04:46:28 +0000150 try:
151 os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
152 except Exception as e:
raymes01959ae2013-02-15 04:50:07 +0000153 logger.GetLogger().LogOutput(str(e))
asharif252df0f2013-02-15 04:46:28 +0000154
155 # Now call enter_chroot with the rest of the arguments.
ashariffcf8cfc2013-02-15 04:49:21 +0000156 command = chromeos_root + "/src/scripts/enter_chroot.sh"
asharif252df0f2013-02-15 04:46:28 +0000157
asharif0269d462013-02-15 04:46:31 +0000158 if len(passthrough_argv) > 1:
159 command += " " + " ".join(passthrough_argv[1:])
asharifda9ac652013-02-15 04:50:09 +0000160 retval = cmd_executer.RunCommand(command, return_output)
asharif252df0f2013-02-15 04:46:28 +0000161 return retval
162 else:
163 os.execv(command, [""])
164
165
asharifda9ac652013-02-15 04:50:09 +0000166def CreateMountPointsFromString(mount_strings, chroot_dir):
167 # String has options in the form dir:mount:options
168 mount_points = []
169 if not mount_strings:
170 return mount_points
171 mount_list = mount_strings.split()
172 for mount_string in mount_list:
173 mount_values = mount_string.split(":")
174 external_dir = mount_values[0]
175 mount_dir = mount_values[1]
176 if len(mount_values)>2:
177 options = mount_values[2]
178 else:
179 options = None
180 mount_point = MountPoint(external_dir, chroot_dir + "/" + mount_dir,
181 getpass.getuser(), options)
182 mount_points.append(mount_point)
183 return mount_points
asharif252df0f2013-02-15 04:46:28 +0000184
185
186if __name__ == "__main__":
asharife3668f12013-02-15 04:46:29 +0000187 Main(sys.argv)
asharif252df0f2013-02-15 04:46:28 +0000188