]> CyberLeo.Net >> Repos - CDN/j.git/blob - seeds/debian.sh
j/seeds/debian: robustify sudoers editing and password hash insertion
[CDN/j.git] / seeds / debian.sh
1 # Initialize a debian chroot using debootstrap into the provided directory
2
3 # Parameters that I want on the command line:
4 # <arch> <suite> <repo>
5
6 [ "$(which debootstrap 2>&-)" ] || pebkac "j_init: debian: debootstrap not found"
7
8 : ${arch=${1}}
9 : ${suite=${2}}
10 : ${repo=${3}}
11 : ${include=curl,file,less,locales,sudo,vim,whois}
12
13 [ "${arch}" ] || pebkac "Debian seed needs <arch> <suite> [repo]"
14
15 case "${arch}" in
16 x86|i386) arch=i386 ;;
17 amd64|x86_64|x64) arch=amd64 ;;
18 *) pebkac "Unsupported arch '${arch}'" ;;
19 esac
20
21 [ "${suite}" ] || pebkac "Debian seed needs <arch> <suite> [repo]"
22
23 [ "${repo}" ] || {
24   if [ -f "${jseed}/debian/dists/${suite}/Release" ]
25   then
26     repo="file://${jseed}/debian"
27   else
28     repo="http://archive.debian.org/debian"
29   fi
30 }
31
32 keyring="${jseed}/debian/debian-archive-keyring.gpg"
33 [ -f "${keyring}" ] || unset keyring
34
35 mkdir -p "${jroot}/var/cache/apt/archives"
36
37 [ ! -d "${jseed}/debian/debs" ] || {
38   echo "Seeding deb archives" >&2
39   ( cd "${jseed}/debian/debs"; find . | cpio -pl "${jroot}/var/cache/apt/archives" )
40 }
41
42 cmd="debootstrap '--arch=${arch}' ${keyring+'--keyring=${jseed}/debian/debian-archive-keyring.gpg'} '--include=${include}' '${suite}' '${jroot}' '${repo}'"
43 echo "Executing: ${cmd}"
44 eval "${cmd}"
45
46 # Make sure locales are generated on first start
47 mkdir -p "${jroot}/etc/rcJ.d"
48 cat > "${jroot}/etc/rcJ.d/S00localegen" <<"EOF"
49 #!/bin/sh
50 /bin/sed -i '/en_US/s/^# //' /etc/locale.gen
51 /usr/sbin/locale-gen
52 /bin/rm -f "${0}"
53 EOF
54 chmod 755 "${jroot}/etc/rcJ.d/S00localegen"
55
56 echo "Adding user"
57 printf '$a\n%%sudo ALL=(ALL) NOPASSWD: ALL' | ed "${jroot}/etc/sudoers"
58 user="${ORIG_USER}"
59 uid="$(id -u "${ORIG_USER}")"
60 [ "${user}" -a "${uid}" ] || { echo "Cannot add nonexistent user"; false; }
61 chroot "${jroot}" /usr/sbin/useradd -m -o -u "${uid}" -G root,sudo "${user}"
62 echo "Propagating password hash"
63 [ -f /etc/shadow ] && {
64   hash="$(grep "^${user}:" /etc/shadow | cut -d: -f2)"
65   [ "${hash}" ] && sed -i "s?^${user}:[^:]*:?${user}:${hash}:?" "${jroot}/etc/shadow"
66 }