Another method of install tpk.
[platform/upstream/mic.git] / mic / cmd_chroot.py
1 #!/usr/bin/python -tt\r
2 # vim: ai ts=4 sts=4 et sw=4\r
3 #\r
4 # Copyright (c) 2012 Intel, Inc.\r
5 #\r
6 # This program is free software; you can redistribute it and/or modify it\r
7 # under the terms of the GNU General Public License as published by the Free\r
8 # Software Foundation; version 2 of the License\r
9 #\r
10 # This program is distributed in the hope that it will be useful, but\r
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13 # for more details.\r
14 #\r
15 # You should have received a copy of the GNU General Public License along\r
16 # with this program; if not, write to the Free Software Foundation, Inc., 59\r
17 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
18 \r
19 """Implementation of subcmd: chroot\r
20 """\r
21 \r
22 import os\r
23 import os, sys, re\r
24 import pwd\r
25 import argparse\r
26 \r
27 from mic import msger\r
28 from mic.utils import misc, errors\r
29 from mic.conf import configmgr\r
30 from mic.plugin import pluginmgr\r
31 \r
32 def _root_confirm():\r
33     """Make sure command is called by root\r
34     There are a lot of commands needed to be run during creating images,\r
35     some of them must be run with root privilege like mount, kpartx"""\r
36     if os.geteuid() != 0:\r
37         msger.error('Root permission is required to continue, abort')\r
38             \r
39 def main(parser, args, argv):\r
40     """mic choot entry point."""\r
41 \r
42     #args is argparser namespace, argv is the input cmd line\r
43     if args is None:\r
44         raise errors.Usage("Invalid arguments")\r
45 \r
46     targetimage = args.imagefile\r
47     if not os.path.exists(targetimage):\r
48         raise errors.CreatorError("Cannot find the image: %s"\r
49                                   % targetimage)\r
50 \r
51     _root_confirm()\r
52 \r
53     configmgr.chroot['saveto'] = args.saveto\r
54 \r
55     imagetype = misc.get_image_type(targetimage)\r
56     if imagetype in ("ext3fsimg", "ext4fsimg", "btrfsimg"):\r
57         imagetype = "loop"\r
58 \r
59     chrootclass = None\r
60     for pname, pcls in pluginmgr.get_plugins('imager').iteritems():\r
61         if pname == imagetype and hasattr(pcls, "do_chroot"):\r
62             chrootclass = pcls\r
63             break\r
64 \r
65     if not chrootclass:\r
66         raise errors.CreatorError("Cannot support image type: %s" \\r
67                                   % imagetype)\r
68 \r
69     chrootclass.do_chroot(targetimage, args.cmd)\r
70         \r
71     \r