Skip to content

Commit

Permalink
Syntax, structure, indent and unneeded logic fixes
Browse files Browse the repository at this point in the history
- Fix unset usage.
- Fix some missing -e in echo where needed.
- Add some echos, before running some commands.
- Minor alignment fixes.
- Don't use 'newgrp' upon lxd install, seems to have no effect.
- Unify command output redirection formatting.
- Make some multi-line commands to be in one line.
  • Loading branch information
jezek committed Sep 17, 2021
1 parent 4d2b345 commit f70abcc
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions crossbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ check_lxd_accessible () {
# /bin/lxd is replaced with a script which tells you to install the lxd snap
# on later versions of Ubuntu.
LXD=`which lxd`
if (grep 'snap install lxd' "$LXD" >/dev/null) && [ ! -f /snap/bin/lxd ] ; then
if (grep 'snap install lxd' "$LXD" > /dev/null) && [ ! -f /snap/bin/lxd ] ; then
$LXD
exit 1
fi
Expand Down Expand Up @@ -231,24 +231,24 @@ setup_lxd () {
echo "this for you."
echo "Press Enter to start"
read KEY
unset $KEY
unset KEY
# Ubuntu 16.04 specific workaround to set up
sudo dpkg-reconfigure -p medium lxd
fi

if [ -n "$ENCRYPTED_HOME" ] ; then
echo -e -n "${ERROR_COLOR}Your home folder is encrypted. $PROGRAM_NAME will use priviledged "
echo -e -n "${ERROR_COLOR}Your home folder is encrypted. $PROGRAM_NAME will use privileged "
echo -e -n "LXD containers and the default storage backend (slower).\n${NC}"
sudo lxd init --auto
else
echo -e -n "${POSITIVE_COLOR}Would you like to setup LXD with ZFS in your home directory? (y/n) \n${NC}"
echo -n "This is recommended for faster operation, and also in case there is not enough "
echo -n "space in your / partition. \n"
echo -e -n "This is recommended for faster operation, and also in case there is not enough "
echo -e -n "space in your / partition. \n"
read REPLY
echo
if [ "$REPLY" = y ]
then
if ! which zpool > /dev/null ; then
if ! which zpool > /dev/null 2>&1; then
echo -e "${POSITIVE_COLOR}Installing ZFS.${NC}"
sudo apt-get install -y zfsutils-linux
fi
Expand All @@ -261,6 +261,7 @@ setup_lxd () {
# add automatic mount of zfs pool upon boot
echo "zpool import -c /etc/zfs/zpool.cache -aN" | sudo tee /etc/rc.local
else
echo "sudo lxd init --auto"
sudo lxd init --auto
fi
fi
Expand All @@ -276,22 +277,20 @@ config_container_dir_mount () {
}

start_container () {
STATUS=$(lxc query "/1.0/containers/${LXD_CONTAINER}/state" | \
jq --raw-output '.status')
STATUS=$(lxc query "/1.0/containers/${LXD_CONTAINER}/state" | jq --raw-output '.status')
if [ $STATUS = 'Stopped' ]; then
# lxc start may give a failure code. It also may not, so we check
# again in a bit.
if ! lxc start $LXD_CONTAINER; then
echo $LXD_CONTAINER_FAILURE_MSG
echo -e $LXD_CONTAINER_FAILURE_MSG
exit 1
fi
fi

# Unfortunately we need to check again. We check for Started this time
# because the monitor may be hung with the container failing, which will
# give us much different output
STATUS=$(lxc query "/1.0/containers/${LXD_CONTAINER}/state" | \
jq --raw-output '.status')
STATUS=$(lxc query "/1.0/containers/${LXD_CONTAINER}/state" | jq --raw-output '.status')
if [ "$STATUS" != 'Running' ]; then
echo $LXD_CONTAINER_FAILURE_MSG
exit 1
Expand All @@ -308,7 +307,7 @@ nonsdk_container_setup () {
focal) ubports_repo_line="deb http://repo2.ubports.com/ $container_ubuntu_version main" ;;
esac
if [ -n "$ubports_repo_line" ]; then
exec_container_root "echo '$ubports_repo_line' >/etc/apt/sources.list.d/ubports.list"
exec_container_root "echo '$ubports_repo_line' > /etc/apt/sources.list.d/ubports.list"
fi

# Skip multiarch setup if not crossbuilding
Expand All @@ -330,7 +329,7 @@ nonsdk_container_setup () {
exec_container_root "sed -E \
-e 's:(archive|security)\.ubuntu\.com/ubuntu/:ports.ubuntu.com/ubuntu-ports/:' \
-e 's:^deb :deb [arch=${TARGET_ARCH}] :' \
/etc/apt/sources.list >/etc/apt/sources.list.d/ports.list"
/etc/apt/sources.list > /etc/apt/sources.list.d/ports.list"
exec_container_root "sed -i -E \
-e 's:^deb :deb [arch=${HOST_ARCH}] :' \
/etc/apt/sources.list"
Expand All @@ -341,7 +340,7 @@ nonsdk_container_setup () {
-e 's:ports\.ubuntu\.com/ubuntu-ports/ ([a-z]+)-security :security.ubuntu.com/ubuntu/ \1-security :' \
-e 's:ports\.ubuntu\.com/ubuntu-ports/ ([a-z-]+):archive.ubuntu.com/ubuntu/ \1 :' \
-e 's:^deb :deb [arch=${TARGET_ARCH}] :' \
/etc/apt/sources.list >/etc/apt/sources.list.d/non-ports.list"
/etc/apt/sources.list > /etc/apt/sources.list.d/non-ports.list"
exec_container_root "sed -i -E \
-e 's:^deb :deb [arch=${HOST_ARCH}] :' \
/etc/apt/sources.list"
Expand Down Expand Up @@ -377,7 +376,7 @@ create_container () {
exec_container_root "add-apt-repository -y ppa:ubports-developers/overlay"
exec_container_root "add-apt-repository 'deb http://repo.ubports.com vivid main' >> /etc/apt/sources.list"
fi
wget -qO - "https://repo.ubports.com/keyring.gpg" | exec_container_root 'cat >/etc/apt/trusted.gpg.d/ubports-keyring.gpg'
wget -qO - "https://repo.ubports.com/keyring.gpg" | exec_container_root 'cat > /etc/apt/trusted.gpg.d/ubports-keyring.gpg'
if ! echo "$LXD_IMAGE" | grep -q "ubuntu-sdk"; then
nonsdk_container_setup
fi
Expand Down Expand Up @@ -593,7 +592,7 @@ install_foreign () {
exec_container "mkdir -p foreign && cd foreign && apt-get download $dep && dpkg-deb -R $dep* tmp"
exec_container "grep -vi '^multi' foreign/tmp/DEBIAN/control > foreign/tmp/DEBIAN/control.tmp"
exec_container "mv foreign/tmp/DEBIAN/control.tmp foreign/tmp/DEBIAN/control"
exec_container "echo 'Multi-Arch: foreign' >> foreign/tmp/DEBIAN/control"
exec_container "echo 'Multi-Arch: foreign' >> foreign/tmp/DEBIAN/control"
exec_container "cd foreign && dpkg-deb -b tmp $dep*"
exec_container_root "dpkg -i $SOURCE_PATH_CONTAINER/foreign/$dep*"
exec_container "rm -r foreign/"
Expand Down Expand Up @@ -649,8 +648,8 @@ copy_build_to_container () {
lxc file push $SCRIPT_DIR/$CREATE_REPO_SCRIPT $LXD_CONTAINER$SOURCE_REPOSITORY/
exec_container $SOURCE_REPOSITORY/$CREATE_REPO_SCRIPT $SOURCE_REPOSITORY

exec_container_root "echo 'deb [trusted=yes] file://$SOURCE_REPOSITORY/ /' >/etc/apt/sources.list.d/localrepo.list"
exec_container_root "printf 'Package: *\nPin: release o=local\nPin-Priority: 2000' >/etc/apt/preferences.d/localrepo.pref"
exec_container_root "echo 'deb [trusted=yes] file://$SOURCE_REPOSITORY/ /' > /etc/apt/sources.list.d/localrepo.list"
exec_container_root "printf 'Package: *\nPin: release o=local\nPin-Priority: 2000' > /etc/apt/preferences.d/localrepo.pref"
}

clean () {
Expand All @@ -662,9 +661,7 @@ check_for_container_network() {
NETWORK_UP=0
for i in `seq 1 10`
do
if lxc query "/1.0/containers/${LXD_CONTAINER}/state" | \
jq -e '.network.eth0.addresses | any( .family == "inet" )' \
> /dev/null 2>&1 ; then
if lxc query "/1.0/containers/${LXD_CONTAINER}/state" | jq -e '.network.eth0.addresses | any( .family == "inet" )' > /dev/null 2>&1 ; then
NETWORK_UP=1
break
fi
Expand Down Expand Up @@ -765,7 +762,7 @@ deploy_to_device () {
fi;

# setup sudo on device
exec_device "printf '#\041/bin/sh\necho $DEVICE_PASSWORD' >/tmp/askpass.sh"
exec_device "printf '#\041/bin/sh\necho $DEVICE_PASSWORD' > /tmp/askpass.sh"
exec_device chmod +x /tmp/askpass.sh

# check password is correct
Expand Down Expand Up @@ -821,10 +818,11 @@ if stat --file-system $HOME | grep ecrypt ; then
fi

if [ -e "/snap/bin/lxc" ]; then
echo "Force privileged container"
FORCE_PRIVILEGED=1
fi

if ! which lxd > /dev/null ; then
if ! which lxd > /dev/null 2>&1 ; then
echo -e "${POSITIVE_COLOR}$PROGRAM_NAME uses LXD to download dependencies and build.${NC}"
echo -e -n "${POSITIVE_COLOR}Would you like to install LXD? (y/n) ${NC}"
read REPLY
Expand All @@ -835,8 +833,8 @@ if ! which lxd > /dev/null ; then
sudo apt-get install -y lxd lxd-client
setup_lxd
ensure_lxd_subuid
newgrp lxd
sudo usermod -a -G lxd $(whoami)
echo "Adding user to lxd group"
sudo usermod -a -G lxd $(whoami)
echo -e "${ERROR_COLOR}LXD is now setup but will only work after you restart your computer.${NC}"
exit 0
else
Expand Down Expand Up @@ -912,7 +910,7 @@ while [ "$1" != "" ]; do
*)
display_help
echo ""
echo -e "${ERROR_COLOR}error: unknown option: $OPTION${NC}"
echo -e "${ERROR_COLOR}Error: unknown option: $OPTION${NC}"
exit 1
;;
esac
Expand Down Expand Up @@ -1148,7 +1146,7 @@ else
*)
display_help
echo ""
echo -e "${ERROR_COLOR}error: unknown command: $COMMAND${NC}"
echo -e "${ERROR_COLOR}Error: unknown command: $COMMAND${NC}"
exit 1
;;
esac
Expand Down

0 comments on commit f70abcc

Please sign in to comment.