password generation utility was still python2
[bootcd.git] / prep.sh
1 #!/bin/bash
2 #
3 # Builds the BootCD reference image, the first of two
4 # initramfs cpio archives that are concatenated together by
5 # isolinux/syslinux to form a custom BootCD.
6 #
7 # Aaron Klingaman <alk@absarokasoft.com>
8 # Mark Huang <mlhuang@cs.princeton.edu>
9 # Copyright (C) 2004-2006 The Trustees of Princeton University
10 #
11
12 PATH=/sbin:/bin:/usr/sbin:/usr/bin
13
14 # In both a normal CVS environment and a PlanetLab RPM
15 # build environment, all of our dependencies are checked out into
16 # directories at the same level as us.
17 if [ -d ../build ] ; then
18     PATH=$PATH:../build
19     srcdir=..
20 else
21     echo "Error: Could not find sources in either . or .."
22     exit 1
23 fi
24
25 export PATH
26
27 . build.common
28
29 pldistro=$1 ; shift
30 nodefamily=$1; shift
31 rpmversion=$1; shift
32
33 # Packages to install, junk and precious : see build/<pldistro>/bootcd.pkgs
34
35 # Do not tolerate errors
36 set -e
37
38 # Root of the initramfs reference image
39 bootcd=$PWD/build/bootcd
40 install -d -m 755 $bootcd
41
42 # Write version number
43 echo ${rpmversion} > build/version.txt
44 echo ${nodefamily} > build/nodefamily
45
46 # Install base system
47 echo "* Creating fedora root image"
48 pl_root_makedevs $bootcd
49 pkgsfile=$(pl_locateDistroFile ../build/ $pldistro bootcd.pkgs) 
50 pl_root_mkfedora $bootcd $pldistro $pkgsfile
51 echo "** DBG1 - contents of /boot after mkfedora"
52 ls -R $bootcd/boot
53 pl_root_tune_image $bootcd
54 echo "** DBG2 - contents of /boot after tune_image"
55 ls -R $bootcd/boot
56
57 # Add site_admin console account to BootCD: with root priv, and self passwd
58 echo "* Creating site_admin account"
59 CRYPT_SA_PASSWORD=$(python3 -c "import crypt, random, string; salt = [random.choice(string.ascii_letters + string.digits + \"./\") for i in range(0,8)] ; print(crypt.crypt('site_admin', '\$1\$' + \"\".join(salt) + '\$'))")
60 chroot ${bootcd} /usr/sbin/useradd -p "$CRYPT_SA_PASSWORD" -o -g 0 -u 0 -m site_admin
61
62 # Install ipnmac (for SuperMicro machines with IPMI)
63 echo "* Installing IPMI utilities"
64 install -D -m 755 ipnmac/ipnmac.x86 $bootcd/usr/sbin/ipnmac
65
66 # Install initscripts
67 echo "* Installing initscripts"
68 for file in pl_functions pl_sysinit pl_hwinit pl_netinit pl_validateconf pl_boot ; do
69     sed -i -e "s,@PLDISTRO@,$pldistro,g" -e "s,@FCDISTRO@,$fcdistro,g" initscripts/$file
70     install -D -m 755 initscripts/$file $bootcd/etc/init.d/$file
71 done
72
73 # Install configuration files
74 echo "* Installing configuration files"
75 for file in fstab mtab modprobe.conf inittab hosts sysctl.conf ; do
76     install -D -m 644 etc/$file $bootcd/etc/$file
77 done
78 # connect our initscripts scripts for upstart
79 # fedora 9 comes with /sbin/init from upstart, that uses /etc/event.d instead of inittab
80 # (in fact inittab is read for determining the default runlevel)
81 if [ -d $bootcd/etc/event.d ] ; then
82     echo "* Tuning /etc/event.d/ for upstart"
83     pushd $bootcd/etc/event.d
84     # use our system initialisation script
85     sed -i -e 's,/etc/rc\.d/rc\.sysinit[a-z\.]*,/etc/init.d/pl_sysinit,g' rcS
86     # use our startup script in runlevel 2
87     sed -i -e 's,/etc/rc\.d/rc[ \t][ \t]*2,/etc/init.d/pl_boot,g' rc2
88     popd    
89 fi
90 # ditto for f14 and higher init style
91 if [ -d $bootcd/etc/init ] ; then
92     echo "* Tuning /etc/init/ for upstart"
93     pushd $bootcd/etc/init
94     # use our system initialisation script
95     sed -i -e 's,/etc/rc\.d/rc\.sysinit[a-z\.]*,/bin/bash -c /etc/init.d/pl_sysinit,g' rcS.conf
96     # use our startup script in runlevel 2
97     sed -i -e 's,/etc/rc.d/rc[a-z\.]*,/etc/init.d/pl_boot,g' rc.conf
98     popd    
99 fi
100 # Install systemd files for f16 and above
101 if [ -d $bootcd/etc/systemd/system ] ; then
102     echo "* Installing systemd files"
103     for file in pl_boot.service pl_boot.target ; do
104         install -D -m 644 systemd/$file $bootcd/etc/systemd/system
105     done
106     echo "* Configuration BootCD to start up pl_boot"
107     # first attempt was to totally replace everything with pl_boot.target
108     # this however leads to physical f21 nodes not starting up properly
109     # because biosdevname is not properly honored and so pl_netinit gets confused
110     # select pl_boot target this way instead of using kargs, as kargs apply to kexec boot as well
111     # ln -sf /etc/systemd/system/pl_boot.target $bootcd/etc/systemd/system/default.target
112     #[ -d $bootcd/etc/systemd/system/pl_boot.target.wants ] || mkdir -p $bootcd/etc/systemd/system/pl_boot.target.wants
113     # Let's try another approach completely
114     # xxx if that worked we would not need pl_boot.target at all
115     mkdir -p $bootcd/etc/systemd/system/default.target.wants
116     ln -sf /etc/systemd/system/pl_boot.service $bootcd/etc/systemd/system/default.target.wants
117     echo "* Enabling getty on tty2"
118     #ln -sf /usr/lib/systemd/system/getty@.service $bootcd/etc/systemd/system/pl_boot.target.wants/getty@tty2.service
119     ln -sf /usr/lib/systemd/system/getty@.service $bootcd/etc/systemd/system/default.target.wants/getty@tty2.service
120 fi
121
122 # Install fallback node configuration file
123 echo "* Installing fallback node configuration file"
124 install -D -m 644 usr-boot/default-node.txt $bootcd/usr/boot/default-node.txt
125
126 # Copy /etc/passwd out
127 install -D -m 644 $bootcd/etc/passwd build/passwd
128
129 # Root of the isofs
130 isofs=$PWD/build/isofs
131 install -d -m 755 $isofs
132
133 # Copy the kernel out
134 echo "* BootCD - locating kernel"
135 for kernel in $bootcd/boot/vmlinuz-* ; do
136     if [ -f "$kernel" ] ; then
137         echo "* BootCD kernel (1) creating from $kernel"
138         echo "* kernel created (1) from $kernel" > $isofs/kernel.from
139         install -D -m 644 $kernel $isofs/kernel
140     fi
141 done
142
143 # patch - Thierry - dec. 2015
144 # somehow we see this good-old code produce a bootcd without a /kernel
145 # this is odd because as far as rpm is concerned, the name should not have changed
146 # anyways, at this point, here's what can be found in /boot
147 # [root@2015-12-07--f23-bcd boot]# pwd
148 # /build/BUILD/bootcd-lxc-f23-x86_64-5.3/bootcd/build/bootcd/boot
149 # [root@2015-12-07--f23-bcd boot]# ls -R
150 # .:
151 # 8adf0b93a7f44be69499d21fa18ab5b8  loader
152
153 # ./8adf0b93a7f44be69499d21fa18ab5b8:
154 # 4.2.6-301.fc23.x86_64
155
156 # ./8adf0b93a7f44be69499d21fa18ab5b8/4.2.6-301.fc23.x86_64:
157 # initrd  linux
158
159 # ./loader:
160 # entries
161
162 # ./loader/entries:
163 # 8adf0b93a7f44be69499d21fa18ab5b8-4.2.6-301.fc23.x86_64.conf
164
165 # second chance if first approach would not work
166 if [ ! -f $isofs/kernel ] ; then
167     kernel=$(find $bootcd/boot -name linux)
168     if [ -f "$kernel" ] ; then
169         echo "* BootCD kernel (2) creating from $kernel"
170         echo "* kernel created (2) from $kernel" > $isofs/kernel.from
171         install -D -m 644 $kernel $isofs/kernel
172     fi
173 fi
174
175 if [ ! -f $isofs/kernel ] ; then
176     echo "* BootCD prep.sh : FATAL: could not locate kernel - exiting"
177     exit 1
178 fi
179
180 # Don't need /boot anymore
181 rm -rf $bootcd/boot
182
183 # initramfs requires that /init be present
184 ln -sf /sbin/init $bootcd/init
185
186 # Pack the rest into a compressed archive
187 echo "* Compressing reference image"
188 (cd $bootcd && find . | cpio --quiet -c -o) | gzip -9 > $isofs/bootcd.img
189
190 exit 0