Skip to content

Instantly share code, notes, and snippets.

@takumin
Last active June 1, 2017 15:12
Show Gist options
  • Save takumin/35d1ee83c08254567206e7bc3c1c4222 to your computer and use it in GitHub Desktop.
Save takumin/35d1ee83c08254567206e7bc3c1c4222 to your computer and use it in GitHub Desktop.
Jetson TX1/2 Ubuntu Debootstrap
#!/bin/sh
#
# Debootstrap Root FileSystem for NVIDIA Jetson TX1
#
# Certified Version: L4T R24.2.1
# Require Package: Jetson TX1 64-bit Driver Package
#
# Step by Step
#
# 1. Connect USB Drive or SATA SSD/HDD
# 2. Boot Jetson TX1
# 3. Open Terminal
# 3. $ wget https://gist.githubusercontent.com/takumin/35d1ee83c08254567206e7bc3c1c4222/raw/jetson-debootstrap.sh
# 4. Edit ./jetson-debootstrap.sh
# 5. $ chmod +x jetson-debootstrap.sh
# 6. $ sudo ./jetson-debootstrap.sh
# 7. Edit /boot/extlinux/extlinux.conf
# 8. $ sudo reboot
#
set -e
################################################################################
# Variables
################################################################################
# Generic
: ${DISKID:="ata-SanDisk_SDSSDA120G_171445453305"}
: ${RELEASE:="xenial"}
: ${ROOTFS:="rootfs"}
: ${MIRROR:="http://jp.archive.ubuntu.com/ports/"}
: ${PROXY:="http://192.168.0.100:3142"}
# Address
: ${NETTYPE="STATIC"}
: ${IP_ADDR="192.168.0.250"}
: ${NETMASK="255.255.255.0"}
: ${GATEWAY="192.168.0.1"}
: ${DNSSERV="8.8.8.8 8.8.4.4"}
: ${NTPSERV="ntp1.jst.mfeed.ad.jp ntp2.jst.mfeed.ad.jp ntp3.jst.mfeed.ad.jp"}
: ${NTPBACK="ntp.nict.jp"}
# Hostname
: ${HOSTNAME:="jetsontx1"}
: ${DOMAIN:="local"}
# User
: ${USER_NAME:="ubuntu"}
: ${USER_FULL:="Ubuntu"}
: ${USER_KEYS:="https://github.com/takumin.keys"}
################################################################################
# Require
################################################################################
# Install Require Packages
dpkg -l | awk '{print $2}' | grep -qs '^sed$' || apt-get -y install sed
dpkg -l | awk '{print $2}' | grep -qs '^mawk$' || apt-get -y install mawk
dpkg -l | awk '{print $2}' | grep -qs '^wget$' || apt-get -y install wget
dpkg -l | awk '{print $2}' | grep -qs '^ca-certificates$' || apt-get -y install ca-certificates
dpkg -l | awk '{print $2}' | grep -qs '^hdparm$' || apt-get -y install hdparm
dpkg -l | awk '{print $2}' | grep -qs '^gdisk$' || apt-get -y install gdisk
dpkg -l | awk '{print $2}' | grep -qs '^e2fsprogs$' || apt-get -y install e2fsprogs
dpkg -l | awk '{print $2}' | grep -qs '^uuid-runtime$' || apt-get -y install uuid-runtime
dpkg -l | awk '{print $2}' | grep -qs '^ipcalc$' || apt-get -y install ipcalc
dpkg -l | awk '{print $2}' | grep -qs '^debootstrap$' || apt-get -y install debootstrap
################################################################################
# Cleanup
################################################################################
# Get Disk ID
DISKS="`realpath /dev/disk/by-id/${DISKID}`"
# Unmount RootFs
awk '{print $2}' /proc/mounts | grep -s "${ROOTFS}" | sort -r | xargs --no-run-if-empty umount
# Unmount Disk
awk '{print $1}' /proc/mounts | grep -s "${DISKS}" | sort -r | xargs --no-run-if-empty umount
################################################################################
# Disk
################################################################################
# Set Password
hdparm --user-master u --security-set-pass P@ssW0rd "${DISKS}"
# Secure Erase
hdparm --user-master u --security-erase P@ssW0rd "${DISKS}"
# Clear Partition Table
sgdisk -Z "${DISKS}"
# Create GPT Partition Table
sgdisk -o "${DISKS}"
# Create RootFs Partition
sgdisk -n 1:: -c 1:"RootFs" -t 1:8300 "${DISKS}"
# Format RootFs Partition
mkfs.ext4 -L "RootFs" "${DISKS}1"
# Mount RootFs Directory
mount -o relatime,barrier=0 "${DISKS}1" "${ROOTFS}"
################################################################################
# Bootstrap
################################################################################
# Check Apt Proxy
if [ -n "${PROXY}" ]; then
# Apt Proxy
export http_proxy="${PROXY}"
fi
# Install Base System
debootstrap "${RELEASE}" "${ROOTFS}" "${MIRROR}"
# Require Environment
export HOME="/root"
export LC_ALL="C"
export LANGUAGE="C"
export LANG="C"
export DEBIAN_FRONTEND="noninteractive"
export DEBIAN_PRIORITY="critical"
export DEBCONF_NONINTERACTIVE_SEEN="true"
# Cleanup Files
find "${ROOTFS}/dev/*" | xargs --no-run-if-empty rm -fr
find "${ROOTFS}/proc/*" | xargs --no-run-if-empty rm -fr
find "${ROOTFS}/run/*" | xargs --no-run-if-empty rm -fr
find "${ROOTFS}/sys/*" | xargs --no-run-if-empty rm -fr
find "${ROOTFS}/tmp/*" | xargs --no-run-if-empty rm -fr
find "${ROOTFS}/var/log/*" | xargs --no-run-if-empty rm -fr
find "${ROOTFS}/var/spool/*" | xargs --no-run-if-empty rm -fr
find "${ROOTFS}/var/tmp/*" | xargs --no-run-if-empty rm -fr
# Require Mount
mount -t devtmpfs devtmpfs "${ROOTFS}/dev"
mount -t devpts -o gid=5,mode=620 devpts "${ROOTFS}/dev/pts"
mount -t proc proc "${ROOTFS}/proc"
mount -t tmpfs tmpfs "${ROOTFS}/run"
mount -t sysfs sysfs "${ROOTFS}/sys"
mount -t tmpfs tmpfs "${ROOTFS}/tmp"
mount -t tmpfs tmpfs "${ROOTFS}/var/log"
mount -t tmpfs tmpfs "${ROOTFS}/var/spool"
mount -t tmpfs tmpfs "${ROOTFS}/var/tmp"
chmod 1777 "${ROOTFS}/dev/shm"
################################################################################
# FileSystem
################################################################################
# Symlink Mount Table
ln -s /proc/self/mounts "${ROOTFS}/etc/mtab"
# Create Mount Point
cat > "${ROOTFS}/etc/fstab" << __EOF__
# <file system> <dir> <type> <options> <dump> <pass>
${DISKS}1 / ext4 defaults,relatime,barrier=0 0 1
tmpfs /tmp tmpfs defaults 0 0
tmpfs /var/log tmpfs defaults 0 0
tmpfs /var/spool tmpfs defaults 0 0
tmpfs /var/tmp tmpfs defaults 0 0
__EOF__
################################################################################
# Network
################################################################################
# Configure Hostname
echo "${HOSTNAME}" > "${ROOTFS}/etc/hostname"
# Resolve Hostname
echo "127.0.1.1 ${HOSTNAME}.${DOMAIN} ${HOSTNAME}" >> "${ROOTFS}/etc/hosts"
# Configure Resolve
rm "${ROOTFS}/etc/resolv.conf"
echo '# DNS Server' > "${ROOTFS}/etc/resolv.conf"
for i in ${DNSSERV}; do
echo "nameserver ${i}" >> "${ROOTFS}/etc/resolv.conf"
done
################################################################################
# Localize
################################################################################
# Timezone
echo 'Asia/Tokyo' > "${ROOTFS}/etc/timezone"
ln -fs /usr/share/zoneinfo/Asia/Tokyo "${ROOTFS}/etc/localtime"
chroot "${ROOTFS}" dpkg-reconfigure tzdata
# Locale
chroot "${ROOTFS}" locale-gen ja_JP.UTF-8
chroot "${ROOTFS}" update-locale LANG=ja_JP.UTF-8
# Keyboard
sed -i -e 's@XKBMODEL="pc105"@XKBMODEL="jp106"@' "${ROOTFS}/etc/default/keyboard"
sed -i -e 's@XKBLAYOUT="us"@XKBLAYOUT="jp"@' "${ROOTFS}/etc/default/keyboard"
sed -i -e 's@XKBOPTIONS=""@XKBOPTIONS="ctrl:nocaps"@' "${ROOTFS}/etc/default/keyboard"
################################################################################
# Admin User
################################################################################
# Add Group
chroot "${ROOTFS}" addgroup --system admin
chroot "${ROOTFS}" addgroup --system lpadmin
chroot "${ROOTFS}" addgroup --system sambashare
# Add User
chroot "${ROOTFS}" adduser --disabled-password --gecos "${USER_FULL},,," "${USER_NAME}"
chroot "${ROOTFS}" adduser "${USER_NAME}" adm
chroot "${ROOTFS}" adduser "${USER_NAME}" admin
chroot "${ROOTFS}" adduser "${USER_NAME}" audio
chroot "${ROOTFS}" adduser "${USER_NAME}" cdrom
chroot "${ROOTFS}" adduser "${USER_NAME}" dialout
chroot "${ROOTFS}" adduser "${USER_NAME}" dip
chroot "${ROOTFS}" adduser "${USER_NAME}" lpadmin
chroot "${ROOTFS}" adduser "${USER_NAME}" netdev
chroot "${ROOTFS}" adduser "${USER_NAME}" plugdev
chroot "${ROOTFS}" adduser "${USER_NAME}" sambashare
chroot "${ROOTFS}" adduser "${USER_NAME}" staff
chroot "${ROOTFS}" adduser "${USER_NAME}" sudo
chroot "${ROOTFS}" adduser "${USER_NAME}" users
chroot "${ROOTFS}" adduser "${USER_NAME}" video
# SSH Public Key
mkdir -p "${ROOTFS}/home/${USER_NAME}/.ssh"
chmod 0700 "${ROOTFS}/home/${USER_NAME}/.ssh"
wget -O "${ROOTFS}/home/${USER_NAME}/.ssh/authorized_keys" "${USER_KEYS}"
chmod 0644 "${ROOTFS}/home/${USER_NAME}/.ssh/authorized_keys"
# User Dir Permission
chroot "${ROOTFS}" chown -R "${USER_NAME}:${USER_NAME}" "/home/${USER_NAME}"
# Sudo No Password
echo "${USER_NAME} ALL=(ALL) NOPASSWD: ALL" > "${ROOTFS}/etc/sudoers.d/${USER_NAME}"
chmod 0440 "${ROOTFS}/etc/sudoers.d/${USER_NAME}"
################################################################################
# Repository
################################################################################
# Official Repository
cat > "${ROOTFS}/etc/apt/sources.list" << __EOF__
# Official Repository
deb ${MIRROR} ${RELEASE} main restricted universe multiverse
deb-src ${MIRROR} ${RELEASE} main restricted universe multiverse
deb ${MIRROR} ${RELEASE}-updates main restricted universe multiverse
deb-src ${MIRROR} ${RELEASE}-updates main restricted universe multiverse
deb ${MIRROR} ${RELEASE}-security main restricted universe multiverse
deb-src ${MIRROR} ${RELEASE}-security main restricted universe multiverse
__EOF__
# Check Apt Proxy
if [ -n "${PROXY}" ]; then
# Apt Proxy
echo "// Apt Proxy" > "${ROOTFS}/etc/apt/apt.conf"
echo "Acquire::http::proxy \"${PROXY}\";" >> "${ROOTFS}/etc/apt/apt.conf"
fi
################################################################################
# Upgrade
################################################################################
# Update Repository
chroot "${ROOTFS}" apt-get -y update
# Upgrade System
chroot "${ROOTFS}" apt-get -y dist-upgrade
################################################################################
# Require
################################################################################
# Standard
chroot "${ROOTFS}" apt-get -y install ubuntu-standard
################################################################################
# Systemd
################################################################################
# timesyncd
sed -i -E "s@^#?NTP=.*@NTP=${NTPSERV}@" "${ROOTFS}/etc/systemd/timesyncd.conf"
sed -i -E "s@^#?FallbackNTP=.*@FallbackNTP=${NTPBACK}@" "${ROOTFS}/etc/systemd/timesyncd.conf"
################################################################################
# OpenSSH
################################################################################
# OpenSSH
chroot "${ROOTFS}" apt-get -y install openssh-server
################################################################################
# NetworkManager
################################################################################
# NetworkManager
chroot "${ROOTFS}" apt-get -y --no-install-recommends install network-manager
# Environment Variables
UUID="`uuidgen`"
MAC_ADDR="`ip link show dev eth0 | grep -s 'link/ether' | awk '{print $2}'`"
IP_PREFIX="`ipcalc -nb ${IP_ADDR} ${NETMASK} | grep -s 'Netmask' | awk '{print $4}'`"
DNSSERVER="`echo ${DNSSERV} | sed -e 's/ /;/g'`"
# Common Configure
cat > "${ROOTFS}/etc/NetworkManager/system-connections/Wired" << __EOF__
[connection]
id=Wired
uuid=${UUID}
type=ethernet
interface-name=eth0
[ethernet]
mac-address=${MAC_ADDR}
[ipv6]
method=ignore
addr-gen-mode=stable-privacy
[ipv4]
__EOF__
# Check Network Type
if [ "x${NETTYPE}" = "xSTATIC" ]; then
# Static
echo "method=manual" >> "${ROOTFS}/etc/NetworkManager/system-connections/Wired"
echo "address1=${IP_ADDR}/${IP_PREFIX},${GATEWAY}" >> "${ROOTFS}/etc/NetworkManager/system-connections/Wired"
echo "dns=${DNSSERVER};" >> "${ROOTFS}/etc/NetworkManager/system-connections/Wired"
else
# DHCP
echo "method=auto" >> "${ROOTFS}/etc/NetworkManager/system-connections/Wired"
fi
# Change Owner
chown root:root "${ROOTFS}/etc/NetworkManager/system-connections/Wired"
# Change Permission
chmod 0600 "${ROOTFS}/etc/NetworkManager/system-connections/Wired"
################################################################################
# Desktop
################################################################################
# GNOME
chroot "${ROOTFS}" apt-get -y install ubuntu-desktop
# Language Pack
chroot "${ROOTFS}" apt-get -y install language-pack-gnome-ja
# User Directory
chroot "${ROOTFS}" su -c "LANG=C xdg-user-dirs-update" "${USER_NAME}"
rm "${ROOTFS}/home/${USER_NAME}/.config/user-dirs.locale"
################################################################################
# TightVNC
################################################################################
#
# # TightVNC
# chroot "${ROOTFS}" apt-get -y install tightvncserver
#
# # Desktop
# chroot "${ROOTFS}" apt-get -y install xfce4 xfce4-goodies
#
# # Require Directory
# mkdir -p "${ROOTFS}/home/${USER_NAME}/.vnc"
#
# # Permission Directory
# chmod 0700 "${ROOTFS}/home/${USER_NAME}/.vnc"
#
# # Create Password
# chroot "${ROOTFS}" su -c "echo 'ubuntu' | vncpasswd -f > /home/${USER_NAME}/.vnc/passwd"
#
# # Permission Password
# chmod 0600 "${ROOTFS}/home/${USER_NAME}/.vnc/passwd"
#
# # Create Script
# cat > "${ROOTFS}/home/${USER_NAME}/.vnc/xstartup" << '__EOF__'
# #!/bin/sh
# xrdb "${HOME}/.Xresources"
# startxfce4 &
# __EOF__
#
# # Permission Script
# chmod 0700 "${ROOTFS}/home/${USER_NAME}/.vnc/xstartup"
#
# # User Dir Permission
# chroot "${ROOTFS}" chown -R "${USER_NAME}:${USER_NAME}" "/home/${USER_NAME}"
#
# # Systemd Bootup Script
# cat > "${ROOTFS}/etc/systemd/system/[email protected]" << __EOF__
# [Unit]
# Description=TightVNC Server
# After=sshd.service
#
# [Service]
# Type=forking
# User=${USER_NAME}
# PAMName=login
# PIDFile=/home/${USER_NAME}/.vnc/%H:%i.pid
# ExecStartPre=-/usr/bin/vncserver -kill :%i > /dev/null 2>&1
# ExecStart=/usr/bin/vncserver :%i
# ExecStop=/usr/bin/vncserver -kill :%i
#
# [Install]
# WantedBy=multi-user.target
# __EOF__
#
# # Systemd Create Symlink
# ln -s "/etc/systemd/system/[email protected]" "${ROOTFS}/etc/systemd/system/multi-user.target.wants/[email protected]"
#
################################################################################
# Vendor
################################################################################
# NVIDIA L4T
./apply_binaries.sh
# Change Owner
find "${ROOTFS}/usr" -user 1000 | xargs --no-run-if-empty chown root:root
################################################################################
# ROS
################################################################################
# Official Repository
echo "deb http://packages.ros.org/ros/ubuntu ${RELEASE} main" > "${ROOTFS}/etc/apt/sources.list.d/ros.list"
# Add APT Keyring
chroot "${ROOTFS}" apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116
# Update Repository
chroot "${ROOTFS}" apt-get -y update
# Install Desktop Full
chroot "${ROOTFS}" apt-get -y install ros-kinetic-desktop-full
# Initialize System rosdep
chroot "${ROOTFS}" rosdep init
# Initialize User rosdep
chroot "${ROOTFS}" su -c "rosdep update" "${USER_NAME}"
# Load Environment
echo 'source "/opt/ros/kinetic/setup.bash"' >> "${ROOTFS}/home/${USER_NAME}/.bashrc"
# Install rosinstall
chroot "${ROOTFS}" apt-get -y install python-rosinstall
################################################################################
# Cleanup
################################################################################
# Remove Resolve
rm "${ROOTFS}/etc/resolv.conf"
rm "${ROOTFS}/etc/resolvconf/resolv.conf.d/original"
# Symlink Resolve
ln -s "/run/NetworkManager/resolv.conf" "${ROOTFS}/etc/resolv.conf"
# Disk Sync
sync;sync;sync
# TRIM
fstrim -v "${ROOTFS}"
# Unmount RootFs
awk '{print $2}' /proc/mounts | grep -s "${ROOTFS}" | sort -r | xargs --no-run-if-empty umount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment