blob: b0f943d93ee9b85aafbb3dd2bbdd2483a252b1a3 [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
asharife3668f12013-02-15 04:46:29 +000024def Main(argv):
asharif252df0f2013-02-15 04:46:28 +000025 """The main function."""
26 parser = optparse.OptionParser()
27 parser.add_option("-c", "--chromeos_root", dest="chromeos_root",
28 help="ChromeOS root checkout directory.")
29 parser.add_option("-t", "--toolchain_root", dest="toolchain_root",
30 help="Toolchain root directory.")
31
raymes01959ae2013-02-15 04:50:07 +000032 relevant_argv = []
33 passthrough_argv = []
asharif0269d462013-02-15 04:46:31 +000034 for i in xrange(len(argv)):
35 found = False
36 for option in parser.option_list:
37 for long_opt in option._long_opts:
38 if argv[i].startswith(long_opt):
39 found = True
40 break
41 for short_opt in option._short_opts:
42 if argv[i].startswith(short_opt):
43 found = True
44 break
45
46 if found == True:
47 break
48
49 if found == True:
50 relevant_argv.append(argv[i])
51 else:
52 passthrough_argv.append(argv[i])
53
54 options = parser.parse_args(relevant_argv)[0]
asharif252df0f2013-02-15 04:46:28 +000055
56 if options.chromeos_root is None:
57 chromeos_root = "../.."
58 else:
59 chromeos_root = options.chromeos_root
asharif17621302013-02-15 04:46:35 +000060
61 chromeos_root = os.path.expanduser(chromeos_root)
62 options.toolchain_root = os.path.expanduser(options.toolchain_root)
63
asharif252df0f2013-02-15 04:46:28 +000064 chromeos_root = os.path.abspath(chromeos_root)
65
66 if (options.toolchain_root is None or
67 not os.path.exists(options.toolchain_root) or
68 not os.path.exists(chromeos_root)):
69 parser.print_help()
70 sys.exit(1)
71
72 tc_dirs = [options.toolchain_root + "/google_vendor_src_branch/gcc"]
raymes01959ae2013-02-15 04:50:07 +000073 rootdir = utils.GetRoot(sys.argv[0])[0]
asharif252df0f2013-02-15 04:46:28 +000074 version_dir = rootdir
75
76 all_dirs = tc_dirs[:]
77 all_dirs.append(rootdir)
78
79 mounted_tc_root = "/usr/local/toolchain_root"
80 full_mounted_tc_root = chromeos_root + "/chroot/" + mounted_tc_root
81 full_mounted_tc_root = os.path.abspath(full_mounted_tc_root)
82
83 # First create the mount points
84 CreateDir(full_mounted_tc_root, getpass.getuser())
85 for d in all_dirs:
86 last_dir = utils.GetRoot(d)[1]
87 mounted_dir = (full_mounted_tc_root + "/" + last_dir)
88 CreateDir(mounted_dir, getpass.getuser())
89
90 # Now mount the toolchain directories.
91 for tc_dir in tc_dirs:
92 last_dir = utils.GetRoot(tc_dir)[1]
93 MountDir(tc_dir, full_mounted_tc_root + "/" + last_dir, "ro")
94
95 # Next, mount the version directory.
96 last_dir = utils.GetRoot(version_dir)[1]
97 MountDir(version_dir, full_mounted_tc_root + "/" + last_dir)
98
99 # Finally, create the symlink to build-gcc.
100 try:
101 os.symlink(last_dir + "/build-gcc", full_mounted_tc_root + "/build-gcc")
102 except Exception as e:
raymes01959ae2013-02-15 04:50:07 +0000103 logger.GetLogger().LogOutput(str(e))
asharif252df0f2013-02-15 04:46:28 +0000104
105 # Now call enter_chroot with the rest of the arguments.
ashariffcf8cfc2013-02-15 04:49:21 +0000106 command = chromeos_root + "/src/scripts/enter_chroot.sh"
asharif252df0f2013-02-15 04:46:28 +0000107
asharif0269d462013-02-15 04:46:31 +0000108 if len(passthrough_argv) > 1:
109 command += " " + " ".join(passthrough_argv[1:])
raymes01959ae2013-02-15 04:50:07 +0000110 retval = cmd_executer.RunCommand(command)
asharif252df0f2013-02-15 04:46:28 +0000111 return retval
112 else:
113 os.execv(command, [""])
114
115
116def MountDir(dir_name, mount_point, options=None):
117 command = "sudo mount --bind " + dir_name + " " + mount_point
118 if options == "ro":
119 command += " && sudo mount --bind -oremount,ro " + mount_point
raymes01959ae2013-02-15 04:50:07 +0000120 retval = cmd_executer.RunCommand(command)
asharif252df0f2013-02-15 04:46:28 +0000121 return retval
122
123
124def CreateDir(dir_name, owner):
125 if not os.path.exists(dir_name):
126 command = "mkdir -p " + dir_name
127 command += " || sudo mkdir -p " + dir_name
raymes01959ae2013-02-15 04:50:07 +0000128 retval = cmd_executer.RunCommand(command)
asharif252df0f2013-02-15 04:46:28 +0000129 if retval != 0:
130 return retval
131 command = "sudo chown " + owner + " " + dir_name
raymes01959ae2013-02-15 04:50:07 +0000132 retval = cmd_executer.RunCommand(command)
asharif252df0f2013-02-15 04:46:28 +0000133 return retval
134
135
136if __name__ == "__main__":
asharife3668f12013-02-15 04:46:29 +0000137 Main(sys.argv)
asharif252df0f2013-02-15 04:46:28 +0000138