fix(make_chroot): Fix SDK setup w/ empty passwd and group files

New baselayout 3 based SDKs define system files in separate read-only
files so the normal group and passwd files don't exist yet.
This commit is contained in:
Michael Marineau 2014-02-17 22:31:43 -08:00
parent 10025571d9
commit 16bc3521e5

View File

@ -138,12 +138,17 @@ delete_existing() {
} }
init_users () { init_users () {
if grep -q "^${SUDO_USER}:[^:]*:${SUDO_UID}:${SUDO_GID}:" \ # make sure user/group database files exist
"${FLAGS_chroot}/etc/passwd"; then touch "${FLAGS_chroot}/etc/"{group,gshadow,passwd,shadow}
chmod 640 "${FLAGS_chroot}/etc/"{gshadow,shadow}
# update or add developer user and group
local userent=$(bare_chroot getent passwd "${SUDO_USER}") || true
if [[ "${userent}" =~ ^[^:]*:[^:]*:${SUDO_UID}:${SUDO_GID}: ]]; then
info "Updating ${SUDO_USER} (already exists in chroot)..." info "Updating ${SUDO_USER} (already exists in chroot)..."
bare_chroot usermod -a -G "${DEFGROUPS}" \ bare_chroot usermod -a -G "${DEFGROUPS}" \
-s /bin/bash -m -d "/home/${SUDO_USER}" "${SUDO_USER}" -s /bin/bash -m -d "/home/${SUDO_USER}" "${SUDO_USER}"
elif grep -q "^${SUDO_USER}:" "${FLAGS_chroot}/etc/passwd"; then elif [[ -n "${userent}" ]]; then
die "User ${SUDO_USER} exists in chroot with different UID/GID" die "User ${SUDO_USER} exists in chroot with different UID/GID"
else else
info "Adding user ${SUDO_USER}..." info "Adding user ${SUDO_USER}..."
@ -152,28 +157,34 @@ init_users () {
group_name=$(getent group "${SUDO_GID}" | cut -d: -f1) group_name=$(getent group "${SUDO_GID}" | cut -d: -f1)
[[ -n "${group_name}" ]] || die "Looking up gid $SUDO_GID failed." [[ -n "${group_name}" ]] || die "Looking up gid $SUDO_GID failed."
if grep -q "^${group_name}:[^:]*:${SUDO_GID}:" "${FLAGS_chroot}/etc/group" local groupent=$(bare_chroot getent group "${group_name}") || true
then if [[ "${groupent}" =~ ^[^:]*:[^:]*:${SUDO_GID}: ]]; then
true # group/gid exists, don't need to add it true # group/gid exists, don't need to add it
elif grep -q "^${group_name}:" "${FLAGS_chroot}/etc/group"; then elif [[ -n "${groupent}" ]]; then
die "Group ${group_name} exists in chroot with different GID" die "Group ${group_name} exists in chroot with different GID"
else else
bare_chroot groupadd -o -g "${SUDO_GID}" "${group_name}" bare_chroot groupadd -o -g "${SUDO_GID}" "${group_name}"
fi fi
bare_chroot useradd -o \
-G "${DEFGROUPS}" -g "${SUDO_GID}" -u "${SUDO_UID}" \
-s /bin/bash -m -c "${full_name}" "${SUDO_USER}"
# TODO(marineam): this can be removed once baselayout 3 is merged
# We need the UID to match the host user's. This can conflict with # We need the UID to match the host user's. This can conflict with
# a particular chroot UID. At the same time, the added user has to # a particular chroot UID. At the same time, the added user has to
# be a primary user for the given UID for sudo to work, which is # be a primary user for the given UID for sudo to work, which is
# determined by the order in /etc/passwd. Let's put ourselves on top # determined by the order in /etc/passwd. Let's put ourselves on top
# of the file. # of the file.
bare_chroot useradd -o \
-G "${DEFGROUPS}" -g "${SUDO_GID}" -u "${SUDO_UID}" \
-s /bin/bash -m -c "${full_name}" "${SUDO_USER}"
# Because passwd generally isn't sorted and the entry ended up at the # Because passwd generally isn't sorted and the entry ended up at the
# bottom, it is safe to just take it and move it to top instead. # bottom, it is safe to just take it and move it to top instead.
sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/group" if [[ $(wc -l <"${FLAGS_chroot}/etc/passwd") -gt 1 ]]; then
sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd" sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/passwd"
fi fi
if [[ $(wc -l <"${FLAGS_chroot}/etc/group") -gt 1 ]]; then
sed -e '1{h;d};$!{H;d};$G' -i "${FLAGS_chroot}/etc/group"
fi
fi
} }
init_setup () { init_setup () {