e78484411a76145ff3951914ed50059f83519711
[catalyst.git] / catalyst / targets / livecd_stage2_target.py
1 """
2 LiveCD stage2 target, builds upon previous LiveCD stage1 tarball
3 """
4 # NOTE: That^^ docstring has influence catalyst-spec(5) man page generation.
5
6 import os,string,types,stat,shutil
7 from catalyst.support import *
8 from generic_stage_target import *
9
10 class livecd_stage2_target(generic_stage_target):
11         """
12         Builder class for a LiveCD stage2 build.
13         """
14         def __init__(self,spec,addlargs):
15                 self.required_values=["boot/kernel"]
16
17                 self.valid_values=[]
18
19                 self.valid_values.extend(self.required_values)
20                 self.valid_values.extend(["livecd/cdtar","livecd/empty","livecd/rm",\
21                         "livecd/unmerge","livecd/iso","livecd/gk_mainargs","livecd/type",\
22                         "livecd/readme","livecd/motd","livecd/overlay",\
23                         "livecd/modblacklist","livecd/splash_theme","livecd/rcadd",\
24                         "livecd/rcdel","livecd/fsscript","livecd/xinitrc",\
25                         "livecd/root_overlay","livecd/users","portage_overlay",\
26                         "livecd/fstype","livecd/fsops","livecd/linuxrc","livecd/bootargs",\
27                         "gamecd/conf","livecd/xdm","livecd/xsession","livecd/volid"])
28
29                 generic_stage_target.__init__(self,spec,addlargs)
30                 if "livecd/type" not in self.settings:
31                         self.settings["livecd/type"] = "generic-livecd"
32
33                 file_locate(self.settings, ["cdtar","controller_file"])
34
35         def set_source_path(self):
36                 self.settings["source_path"]=normpath(self.settings["storedir"]+"/builds/"+self.settings["source_subpath"]+".tar.bz2")
37                 if os.path.isfile(self.settings["source_path"]):
38                         self.settings["source_path_hash"] = \
39                                 self.settings["hash_map"].generate_hash(
40                                         self.settings["source_path"])
41                 else:
42                         self.settings["source_path"]=normpath(self.settings["storedir"]+"/tmp/"+self.settings["source_subpath"]+"/")
43                 if not os.path.exists(self.settings["source_path"]):
44                         raise CatalystError,"Source Path: "+self.settings["source_path"]+" does not exist."
45
46         def set_spec_prefix(self):
47             self.settings["spec_prefix"]="livecd"
48
49         def set_target_path(self):
50                 self.settings["target_path"]=normpath(self.settings["storedir"]+"/builds/"+self.settings["target_subpath"]+"/")
51                 if "AUTORESUME" in self.settings \
52                         and os.path.exists(self.settings["autoresume_path"]+"setup_target_path"):
53                                 print "Resume point detected, skipping target path setup operation..."
54                 else:
55                         # first clean up any existing target stuff
56                         if os.path.isdir(self.settings["target_path"]):
57                                 cmd("rm -rf "+self.settings["target_path"],
58                                 "Could not remove existing directory: "+self.settings["target_path"],env=self.env)
59                                 touch(self.settings["autoresume_path"]+"setup_target_path")
60                         if not os.path.exists(self.settings["target_path"]):
61                                 os.makedirs(self.settings["target_path"])
62
63         def run_local(self):
64                 # what modules do we want to blacklist?
65                 if "livecd/modblacklist" in self.settings:
66                         try:
67                                 myf=open(self.settings["chroot_path"]+"/etc/modprobe.d/blacklist.conf","a")
68                         except:
69                                 self.unbind()
70                                 raise CatalystError,"Couldn't open "+self.settings["chroot_path"]+"/etc/modprobe.d/blacklist.conf."
71
72                         myf.write("\n#Added by Catalyst:")
73                         # workaround until config.py is using configparser
74                         if isinstance(self.settings["livecd/modblacklist"], str):
75                                 self.settings["livecd/modblacklist"] = self.settings["livecd/modblacklist"].split()
76                         for x in self.settings["livecd/modblacklist"]:
77                                 myf.write("\nblacklist "+x)
78                         myf.close()
79
80         def unpack(self):
81                 unpack=True
82                 display_msg=None
83
84                 clst_unpack_hash=read_from_clst(self.settings["autoresume_path"]+"unpack")
85
86                 if os.path.isdir(self.settings["source_path"]):
87                         unpack_cmd="rsync -a --delete "+self.settings["source_path"]+" "+self.settings["chroot_path"]
88                         display_msg="\nStarting rsync from "+self.settings["source_path"]+"\nto "+\
89                                 self.settings["chroot_path"]+" (This may take some time) ...\n"
90                         error_msg="Rsync of "+self.settings["source_path"]+" to "+self.settings["chroot_path"]+" failed."
91                         invalid_snapshot=False
92
93                 if "AUTORESUME" in self.settings:
94                         if os.path.isdir(self.settings["source_path"]) and \
95                                 os.path.exists(self.settings["autoresume_path"]+"unpack"):
96                                 print "Resume point detected, skipping unpack operation..."
97                                 unpack=False
98                         elif "source_path_hash" in self.settings:
99                                 if self.settings["source_path_hash"] != clst_unpack_hash:
100                                         invalid_snapshot=True
101
102                 if unpack:
103                         self.mount_safety_check()
104                         if invalid_snapshot:
105                                 print "No Valid Resume point detected, cleaning up  ..."
106                                 #os.remove(self.settings["autoresume_path"]+"dir_setup")
107                                 self.clear_autoresume()
108                                 self.clear_chroot()
109                                 #self.dir_setup()
110
111                         if not os.path.exists(self.settings["chroot_path"]):
112                                 os.makedirs(self.settings["chroot_path"])
113
114                         if not os.path.exists(self.settings["chroot_path"]+"/tmp"):
115                                 os.makedirs(self.settings["chroot_path"]+"/tmp",1777)
116
117                         if "PKGCACHE" in self.settings:
118                                 if not os.path.exists(self.settings["pkgcache_path"]):
119                                         os.makedirs(self.settings["pkgcache_path"],0755)
120
121                         if not display_msg:
122                                 raise CatalystError,"Could not find appropriate source. Please check the 'source_subpath' setting in the spec file."
123
124                         print display_msg
125                         cmd(unpack_cmd,error_msg,env=self.env)
126
127                         if "source_path_hash" in self.settings:
128                                 myf=open(self.settings["autoresume_path"]+"unpack","w")
129                                 myf.write(self.settings["source_path_hash"])
130                                 myf.close()
131                         else:
132                                 touch(self.settings["autoresume_path"]+"unpack")
133
134         def set_action_sequence(self):
135                 self.settings["action_sequence"]=["unpack","unpack_snapshot",\
136                                 "config_profile_link","setup_confdir","portage_overlay",\
137                                 "bind","chroot_setup","setup_environment","run_local",\
138                                 "build_kernel"]
139                 if "FETCH" not in self.settings:
140                         self.settings["action_sequence"] += ["bootloader","preclean",\
141                                 "livecd_update","root_overlay","fsscript","rcupdate","unmerge",\
142                                 "unbind","remove","empty","target_setup",\
143                                 "setup_overlay","create_iso"]
144                 self.settings["action_sequence"].append("clear_autoresume")
145
146 def register(foo):
147         foo.update({"livecd-stage2":livecd_stage2_target})
148         return foo