Version 13.4.5
[livecd.git] / tools / liveimage-mount
blob76602a7d2aada1722ebae7a96d95e6602f05e90c
1 #!/usr/bin/python -tt
3 # livecd-mount: Mount a live CD at the specified point, and log
4 # into a shell.
6 # Copyright 2010, Red Hat Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; version 2 of the License.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Library General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 import os
22 import sys
23 import getopt
24 import tempfile
25 import subprocess
27 def usage(ecode):
28 print "Usage: %s ISO.iso MOUNTPOINT [COMMAND] [ARGS]"
29 sys.exit(ecode)
31 def main():
32 try:
33 opts,args = getopt.getopt(sys.argv[1:], 'h', ['help', 'chroot', 'mount-hacks'])
34 except getopt.GetoptError, e:
35 usage(1)
37 rw = False
38 chroot = False
39 mount_hacks = False
40 for o,a in opts:
41 if o in ('-h', '--help'):
42 usage(0)
43 elif o in ('--chroot', ):
44 chroot = True
45 elif o in ('--mount-hacks', ):
46 mount_hacks = True
48 if len(args) < 2:
49 usage(1)
51 isopath = args[0]
52 destmnt = args[1]
54 command = args[2:]
55 verbose = not command
57 isomnt = tempfile.mkdtemp(prefix='livemnt-iso')
58 squashmnt = tempfile.mkdtemp(prefix='livemnt-squash')
60 mountflags = ['loop', 'ro']
61 mountflags_str = ','.join(mountflags)
63 try:
64 subprocess.check_call(['mount', '-o', mountflags_str, isopath, isomnt], stderr=sys.stderr)
65 squash_img_path = os.path.join(isomnt, 'LiveOS', 'squashfs.img')
66 subprocess.check_call(['mount', '-o', mountflags_str, squash_img_path, squashmnt], stderr=sys.stderr)
67 ext3_img_path = os.path.join(squashmnt, 'LiveOS', 'ext3fs.img')
68 subprocess.check_call(['mount', '-o', mountflags_str, ext3_img_path, destmnt], stderr=sys.stderr)
70 if mount_hacks:
71 subprocess.check_call(['mount', '-t', 'proc', 'proc', os.path.join(destmnt, 'proc')], stderr=sys.stderr)
72 subprocess.check_call(['mount', '-t', 'tmpfs', 'tmpfs', os.path.join(destmnt, 'var', 'run')], stderr=sys.stderr)
74 if len(command) > 0:
75 args = ['chroot', destmnt]
76 args.extend(command)
77 ecode = subprocess.call(args, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
78 elif chroot:
79 print "Starting subshell in chroot, press Ctrl-D to exit..."
80 ecode = subprocess.call(['chroot', destmnt], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
81 else:
82 print "Starting subshell, press Ctrl-D to exit..."
83 ecode = subprocess.call([os.environ['SHELL']], cwd=destmnt, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
84 finally:
85 if verbose:
86 print "Cleaning up..."
87 if mount_hacks:
88 subprocess.call(['umount', os.path.join(destmnt, 'var', 'run')])
89 subprocess.call(['umount', os.path.join(destmnt, 'proc')])
90 subprocess.call(['umount', destmnt])
91 subprocess.call(['umount', squashmnt])
92 os.rmdir(squashmnt)
93 subprocess.call(['umount', isomnt])
94 os.rmdir(isomnt)
95 if verbose:
96 print "Cleanup complete"
98 sys.exit(ecode)
100 if __name__ == '__main__':
101 main()