armbian_rootenc_setup.sh 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. #!/bin/bash
  2. PATH="$PATH:/usr/sbin:/sbin"
  3. RED="\e[31;1m" GREEN="\e[32;1m" YELLOW="\e[33;1m" BLUE="\e[34;1m" PURPLE="\e[35;1m" RESET="\e[0m"
  4. PROGNAME=$(basename $0)
  5. TITLE='Armbian Encrypted Root Filesystem Setup'
  6. CONFIG_VARS='
  7. ARMBIAN_IMAGE
  8. BOOTPART_LABEL
  9. ROOTFS_NAME
  10. DISK_PASSWD
  11. UNLOCKING_USERHOST
  12. IP_ADDRESS
  13. ADD_ALL_MODS
  14. USE_LOCAL_AUTHORIZED_KEYS
  15. '
  16. STATES='
  17. card_partitioned
  18. bootpart_copied
  19. bootpart_label_created
  20. rootpart_copied
  21. target_configured
  22. '
  23. USER_OPTS_INFO="
  24. NO_CLEANUP no cleanup of mounts after program run
  25. FORCE_REBUILD force full rebuild
  26. FORCE_RECONFIGURE force reconfiguration
  27. FORCE_REFORMAT_ROOT force reformat of encrypted root partition
  28. ADD_ALL_MODS add all currently loaded modules to initramfs
  29. USE_LOCAL_AUTHORIZED_KEYS use local 'authorized_keys' file
  30. PARTITION_ONLY partition and create filesystems only
  31. ERASE zero boot sector, boot partition and beginning of root partition
  32. ROOTENC_REUSE_FS reuse existing filesystems (for development only)
  33. ROOTENC_TESTING developer tweaks
  34. ROOTENC_PAUSE pause along the way
  35. ROOTENC_IGNORE_APT_ERRORS continue even if apt update fails
  36. "
  37. RSYNC_VERBOSITY='--info=progress2'
  38. print_help() {
  39. echo " ${PROGNAME^^}: Create an Armbian image with encrypted root filesystem
  40. USAGE: $PROGNAME [options] <SD card device name>
  41. OPTIONS: '-h' Print this help message
  42. '-C' Don't perform unmounts or clean up build directory at exit
  43. '-d' Produce tons of debugging output
  44. '-f' Force reconfiguration of target system
  45. '-F' Force a complete rebuild of target system
  46. '-m' Add all currently loaded modules to the initramfs (may help
  47. fix blank screen on bootup issues)
  48. '-p' Partition and create filesystems only. Do not copy data
  49. '-R' Force reformat of encrypted root partition
  50. '-s' Use 'authorized_keys' file from working directory, if available
  51. (see below)
  52. '-v' Be more verbose
  53. '-u' Perform an 'apt upgrade' after each 'apt update'
  54. '-z' Erase boot sector and first partition of SD card before partitioning
  55. (an extra paranoia step, but it can’t hurt)
  56. For non-interactive operation, set the following variables in your environment
  57. or on the command line:
  58. ROOTFS_NAME - device mapper name of target root filesystem
  59. IP_ADDRESS - IP address of target (set to 'dhcp' for dynamic IP
  60. or 'none' to disable remote SSH unlocking support)
  61. BOOTPART_LABEL - Boot partition label of target
  62. DISK_PASSWD - Disk password of target root filesystem
  63. UNLOCKING_USERHOST - USER@HOST of remote unlocking host
  64. SERIAL_CONSOLE - Set this to 'y' to enable disk unlocking from the
  65. serial console
  66. INSTRUCTIONS FOR USE
  67. This script must be invoked as superuser on a running Armbian system.
  68. Packages will be installed using APT, so the system must be Internet-
  69. connected and its clock correctly set.
  70. If remote unlocking via SSH is desired, the unlocking host must be reachable.
  71. Alternatively, SSH public keys for the unlocking host or hosts may be listed
  72. in the file 'authorized_keys' in the current directory. This file has the
  73. same format as a standard SSH 'authorized_keys' file.
  74. Architecture of host and target (e.g. 64-bit or 32-bit ARM) must be the same.
  75. For best results, the host and target hardware should also be identical or
  76. similar. Building on a host with more memory than the target, for example,
  77. may lead to disk unlocking failure on the target. For most users, who’ll be
  78. building for the currently-running board, this point is a non-issue.
  79. 1. Place an Armbian boot image file for the target system in the current
  80. directory. For best results, the image file should match the Debian
  81. or Ubuntu release of the host system.
  82. 2. Insert a USB card reader with a blank micro-SD card for the target
  83. system into the host’s USB port.
  84. 3. Determine the SD card’s device name using 'dmesg' or 'lsblk'.
  85. 4. Invoke the script with the device name as argument. If any options
  86. are desired, they must precede the device name.
  87. If the board has an eMMC, it may be used as the target device instead of
  88. an SD card." | less
  89. }
  90. pause() {
  91. echo -ne $GREEN'(Press any key to continue)'$RESET >&$stderr_dup
  92. read
  93. no_fmsg=1
  94. }
  95. _debug_pause() { [ "$ROOTENC_PAUSE" ] && pause; true; }
  96. imsg() { echo -e "$1" >&$stdout_dup; no_fmsg=1; }
  97. imsg_nonl() { echo -ne "$1" >&$stdout_dup; no_fmsg=1; }
  98. tmsg() {
  99. no_fmsg=1
  100. [ "$ROOTENC_TESTING" ] || return 0
  101. echo -e "$1" >&$stdout_dup
  102. }
  103. warn() { echo -e "$YELLOW$1$RESET" >&$stdout_dup; no_fmsg=1; }
  104. warn_nonl() { echo -ne "$YELLOW$1$RESET" >&$stdout_dup; no_fmsg=1; }
  105. rmsg() { echo -e "$RED$1$RESET" >&$stdout_dup; no_fmsg=1; }
  106. gmsg() { echo -e "$GREEN$1$RESET" >&$stdout_dup; no_fmsg=1; }
  107. pu_msg() { echo -e "$PURPLE$1$RESET" >&$stdout_dup; no_fmsg=1; }
  108. do_partprobe() {
  109. if [ "$VERBOSE" ]; then partprobe; else partprobe 2>/dev/null; fi
  110. no_fmsg=1
  111. }
  112. _show_output() { [ "$VERBOSE" ] || exec 1>&$stdout_dup 2>&$stderr_dup; }
  113. _hide_output() { [ "$VERBOSE" ] || exec &>'/dev/null'; }
  114. bail() { exit; }
  115. die() {
  116. echo -e "$RED$1$RESET" >&$stdout_dup
  117. no_fmsg=1
  118. exit 1
  119. }
  120. _return_handler() {
  121. local funcname=${FUNCNAME[1]} exitval=$? res
  122. if [ "${funcname:0:1}" == '_' -o "$no_fmsg" ]; then
  123. no_fmsg=
  124. return 0
  125. fi
  126. if [ "$exitval" -eq 0 ]; then res='OK'; else res="False ($exitval)"; fi
  127. printf "$BLUE%-32s $res$RESET\n" "$funcname" >&$stdout_dup
  128. }
  129. _sigint_handler() {
  130. warn "\nExiting at user request"
  131. exit 1
  132. }
  133. _error_handler() {
  134. local exitval=$?
  135. warn "$(basename ${BASH_SOURCE[1]}):${BASH_LINENO[0]}: ${FUNCNAME[1]}() failed at command '$BASH_COMMAND'"
  136. rmsg "$SCRIPT_DESC exiting with error ($exitval)"
  137. }
  138. _do_header() {
  139. echo
  140. local reply
  141. if banner=$(toilet --filter border --filter gay --width 51 -s -f smbraille "$TITLE" 2>/dev/null); then
  142. while read reply; do
  143. echo -e " $reply"
  144. done <<-EOF
  145. $banner
  146. EOF
  147. else
  148. echo -n ' '
  149. echo $TITLE
  150. echo
  151. fi
  152. echo " For detailed usage information,"
  153. echo " invoke with the '-h' switch"
  154. echo
  155. }
  156. _warn_user_opts() {
  157. local out
  158. while read opt text; do
  159. [ "$opt" ] || continue
  160. if [ $(eval echo -n \$$opt) ]; then out+=" + $text\n"; fi
  161. done <<-EOF
  162. $USER_OPTS_INFO
  163. EOF
  164. if [ "$out" ]; then
  165. warn " The following user options are in effect:"
  166. warn_nonl "${out}"
  167. fi
  168. }
  169. _set_host_vars() {
  170. BUILD_DIR='armbian_rootenc_build'
  171. SRC_ROOT="$BUILD_DIR/src"
  172. BOOT_ROOT="$BUILD_DIR/boot"
  173. TARGET_ROOT="$BUILD_DIR/target"
  174. CONFIG_VARS_FILE="$BOOT_ROOT/.rootenc_config_vars"
  175. host_distro=$(lsb_release --short --codename)
  176. host_kernel=$(ls '/boot' | egrep '^vmlinu[xz]') # allow 'vmlinux' or 'vmlinuz'
  177. }
  178. check_sdcard_name_and_params() {
  179. local dev chk
  180. dev=$1
  181. [ "$dev" ] || die "You must supply a device name"
  182. [ "${dev:0:5}" == '/dev/' ] || dev="/dev/$dev"
  183. [ -e "$dev" ] || die "$dev does not exist"
  184. chk="$(lsblk --noheadings --nodeps --list --output=TYPE $dev 2>/dev/null)"
  185. [ "$chk" != 'disk' ] && {
  186. [ "$chk" == 'part' ] && die "$dev is a partition, not a block device!"
  187. die "$dev is not a block device!"
  188. }
  189. local pttype size nodos oversize removable non_removable part_sep
  190. pttype=$(blkid --output=udev $dev | grep TYPE | cut -d '=' -f2)
  191. size="$(lsblk --noheadings --nodeps --list --output=SIZE --bytes $dev 2>/dev/null)"
  192. removable="$(lsblk --noheadings --nodeps --list --output=RM $dev 2>/dev/null)"
  193. nodos=$([ "$pttype" -a "$pttype" != 'dos' ] && echo "Partition type is ${pttype^^}") || true
  194. oversize=$([ $size -gt 137438953472 ] && echo 'Size is > 128GiB') || true
  195. non_removable=$([ $removable -ne 0 ] || echo 'Device is non-removable')
  196. SD_INFO="$(lsblk --noheadings --nodeps --list --output=VENDOR,MODEL,SIZE $dev 2>/dev/null)"
  197. SD_INFO=${SD_INFO// / }
  198. if [ "$nodos" -o "$oversize" -o "$non_removable" ]; then
  199. warn " $dev ($SD_INFO) doesn’t appear to be an SD card"
  200. warn " for the following reasons:"
  201. if [ "$non_removable" ]; then warn " $non_removable"; fi
  202. if [ "$nodos" ]; then warn " $nodos"; fi
  203. if [ "$oversize" ]; then warn " $oversize"; fi
  204. _user_confirm ' Are you sure this is the correct device of your blank SD card?' 'no'
  205. fi
  206. SDCARD_DEVNAME=${dev:5}
  207. [ "${SDCARD_DEVNAME%[0-9]}" == $SDCARD_DEVNAME ] || part_sep='p'
  208. BOOT_DEVNAME=$SDCARD_DEVNAME${part_sep}1
  209. ROOT_DEVNAME=$SDCARD_DEVNAME${part_sep}2
  210. [ "$SDCARD_DEVNAME" ] || die 'You must supply a device name for the SD card!'
  211. pu_msg "Will write to target $dev ($SD_INFO)"
  212. }
  213. _get_user_var() {
  214. local var desc dfl prompt pat pat_errmsg vtest cprompt seen_prompt reply redo
  215. var=$1 desc=$2 dfl=$3 prompt=$4 pat=$5 pat_errmsg=$6 vtest=$7
  216. while true; do
  217. if [ -z "${!var}" -o "$seen_prompt" -o "$redo" ]; then
  218. if [ "$seen_prompt" ]; then
  219. echo -n " Enter $desc: "
  220. else
  221. cprompt=
  222. while read reply; do
  223. cprompt+=" ${reply## }\n"
  224. done <<-EOF
  225. $prompt
  226. EOF
  227. echo
  228. if [ "$dfl" ]; then
  229. printf "${cprompt:0:-2} " "$dfl"
  230. else
  231. echo -ne "${cprompt:0:-2} "
  232. fi
  233. seen_prompt=1
  234. fi
  235. eval "read $var"
  236. fi
  237. redo=1
  238. if [ -z "${!var}" -a "$dfl" ]; then eval "$var=$dfl"; fi
  239. [ "${!var}" ] || {
  240. rmsg " $desc must not be empty"
  241. continue
  242. }
  243. if [ "$pat" ]; then
  244. echo "${!var}" | egrep -qi "$pat" || {
  245. rmsg " ${!var}: $pat_errmsg"
  246. continue
  247. }
  248. fi
  249. if [ "$vtest" ]; then
  250. $vtest || continue
  251. fi
  252. break
  253. done
  254. }
  255. _get_user_vars() {
  256. _get_user_var 'IP_ADDRESS' 'IP address' '' \
  257. "Enter the IP address of the target machine.
  258. Enter 'dhcp' for a dynamic IP or 'none' for no remote SSH unlocking support
  259. IP address:" \
  260. '^(dhcp|none|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]+\.[0-9]{1,3})$' \
  261. 'malformed IP address'
  262. IP_ADDRESS=${IP_ADDRESS,,}
  263. _get_user_var 'BOOTPART_LABEL' 'boot partition label' 'ARMBIAN_BOOT' \
  264. "Enter a boot partition label for the target machine,
  265. or hit ENTER for the default (%s): " \
  266. '^[A-Za-z0-9_]{1,16}$' \
  267. "Label must contain no more than 16 characters in the set 'A-Za-z0-9_'"
  268. _get_user_var 'ROOTFS_NAME' 'root filesystem device name' 'rootfs' \
  269. "Enter a device name for the encrypted root filesystem,
  270. or hit ENTER for the default (%s):" \
  271. '^[a-z0-9_]{1,48}$' \
  272. "Name must contain no more than 48 characters in the set 'a-z0-9_'" \
  273. '_test_rootfs_mounted'
  274. _get_user_var 'DISK_PASSWD' 'disk password' '' \
  275. "Choose a simple disk password for the installation process.
  276. Once your encrypted system is up and running, you can change
  277. the password using the 'cryptsetup' command.
  278. Enter password:" \
  279. '^[A-Za-z0-9_ ]{1,10}$' \
  280. "Temporary disk password must contain no more than 10 characters in the set 'A-Za-z0-9_ '"
  281. if [ "$IP_ADDRESS" == 'none' ]; then
  282. UNLOCKING_USERHOST=
  283. elif [ -e 'authorized_keys' -a "$USE_LOCAL_AUTHORIZED_KEYS" ]; then
  284. UNLOCKING_USERHOST=
  285. else
  286. _get_user_var 'UNLOCKING_USERHOST' 'USER@HOST' '' \
  287. "Enter the user@host of the machine you'll be unlocking from:" \
  288. '\S+@\S+' \
  289. 'malformed USER@HOST' \
  290. '_test_unlocking_host_available'
  291. fi
  292. _get_user_var 'SERIAL_CONSOLE' 'serial console unlocking' '' \
  293. "Unlock the disk from the serial console. WARNING: enabling this will
  294. make it impossible to unlock the disk using the keyboard and monitor,
  295. though unlocking via SSH will still work.
  296. Enable serial console unlocking? (y/n):" \
  297. '^[ynYN]*$' \
  298. "You must type 'y' or 'n'"
  299. if [[ $SERIAL_CONSOLE =~ ^[Yy]$ ]]; then SERIAL_CONSOLE='yes'; else SERIAL_CONSOLE='no'; fi
  300. true
  301. }
  302. _test_rootfs_mounted() {
  303. [ -e "/dev/mapper/$ROOTFS_NAME" ] && {
  304. local mnt=$(lsblk --list --noheadings --output=MOUNTPOINT /dev/mapper/$ROOTFS_NAME)
  305. [ "$mnt" ] && {
  306. rmsg " Device '$ROOTFS_NAME' is in use and mounted on $mnt"
  307. return 1
  308. }
  309. }
  310. return 0
  311. }
  312. _test_unlocking_host_available() {
  313. local ul_host=${UNLOCKING_USERHOST#*@}
  314. ping -c1 $ul_host &>/dev/null || {
  315. rmsg " Unable to ping host '$ul_host'"
  316. return 1
  317. }
  318. }
  319. _test_sdcard_mounted() {
  320. local chk="$(lsblk --noheadings --list --output=MOUNTPOINT /dev/$SDCARD_DEVNAME)"
  321. [ -z "$chk" ] || {
  322. lsblk --output=NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT /dev/$SDCARD_DEVNAME
  323. die "Device /dev/$SDCARD_DEVNAME has mounted partitions!"
  324. }
  325. }
  326. get_authorized_keys() {
  327. [ -e 'authorized_keys' -a "$USE_LOCAL_AUTHORIZED_KEYS" ] || {
  328. rsync "$UNLOCKING_USERHOST:.ssh/id_*.pub" 'authorized_keys'
  329. }
  330. }
  331. _apt_update() {
  332. [ "$ROOTENC_IGNORE_APT_ERRORS" ] && set +e
  333. apt --yes update
  334. [ "$APT_UPGRADE" ] && apt --yes upgrade
  335. [ "$ROOTENC_IGNORE_APT_ERRORS" ] && set -e
  336. true
  337. }
  338. _print_pkgs_to_install() {
  339. local pkgs pkgs_ssh
  340. case $1 in
  341. 'host')
  342. case "$host_distro" in
  343. focal|bionic|buster) pkgs='cryptsetup-bin ed' ;;
  344. *) pkgs='cryptsetup ed'
  345. warn "Warning: unrecognized host distribution '$host_distro'" ;;
  346. esac ;;
  347. 'target')
  348. case "$target_distro" in
  349. focal|buster) pkgs='cryptsetup-initramfs' pkgs_ssh='dropbear-initramfs' ;;
  350. bionic) pkgs='cryptsetup' pkgs_ssh='dropbear-initramfs' ;;
  351. *) pkgs='cryptsetup' pkgs_ssh='dropbear'
  352. warn "Warning: unrecognized target distribution '$target_distro'" ;;
  353. esac
  354. [ "$IP_ADDRESS" != 'none' ] && pkgs+=" $pkgs_ssh" ;;
  355. esac
  356. for i in $pkgs; do
  357. dpkg -l $i 2>/dev/null | grep -q ^ii || echo $i
  358. done
  359. }
  360. apt_install_host() {
  361. local pkgs=$(_print_pkgs_to_install 'host')
  362. [ "$pkgs" ] && {
  363. _apt_update
  364. apt --yes install $pkgs
  365. }
  366. true
  367. }
  368. create_build_dir() {
  369. mkdir -p $BUILD_DIR
  370. mkdir -p $SRC_ROOT
  371. mkdir -p $BOOT_ROOT
  372. mkdir -p $TARGET_ROOT
  373. }
  374. umount_target() {
  375. for i in $BOOT_ROOT $TARGET_ROOT; do
  376. while mountpoint -q $i; do
  377. umount -Rl $i
  378. done
  379. done
  380. }
  381. remove_build_dir() {
  382. [ -d $TARGET_ROOT ] && rmdir $TARGET_ROOT
  383. [ -d $BOOT_ROOT ] && rmdir $BOOT_ROOT
  384. [ -d $SRC_ROOT ] && rmdir $SRC_ROOT
  385. [ -d $BUILD_DIR ] && rmdir $BUILD_DIR
  386. true
  387. }
  388. _get_device_maps() {
  389. local dm_type=$1
  390. local varname="device_maps_${dm_type}"
  391. eval "$varname="
  392. local data=$(lsblk --list --noheadings --output=KNAME,MOUNTPOINT | egrep '^dm-[0-9]')
  393. while read kname mountpoint; do
  394. [ "$dm_type" == 'unmounted' -a "$mountpoint" ] && continue
  395. [ "$dm_type" == 'mounted_on_target' -a \
  396. "${mountpoint: -${#TARGET_ROOT}}" != "$TARGET_ROOT" ] && continue
  397. eval "$varname+=/dev/$kname "
  398. done <<-EOF
  399. $data
  400. EOF
  401. tmsg "$varname=[${!varname}]"
  402. }
  403. _close_device_maps() {
  404. local dm_type=$1
  405. local varname="device_maps_${dm_type}"
  406. for i in ${!varname}; do
  407. tmsg "closing $i"
  408. cryptsetup status $i > '/dev/null' && cryptsetup luksClose $i
  409. done
  410. true
  411. }
  412. _preclean() {
  413. close_loopmount
  414. _get_device_maps 'unmounted'
  415. _close_device_maps 'unmounted'
  416. _get_device_maps 'mounted_on_target'
  417. umount_target
  418. _close_device_maps 'mounted_on_target'
  419. remove_build_dir
  420. }
  421. _clean() {
  422. pu_msg "Cleaning up, please wait..."
  423. _show_output
  424. close_loopmount
  425. _get_device_maps 'mounted_on_target'
  426. umount_target
  427. update_config_vars_file
  428. _close_device_maps 'mounted_on_target'
  429. [ -e 'authorized_keys' -a -z "$USE_LOCAL_AUTHORIZED_KEYS" ] && shred -u 'authorized_keys'
  430. remove_build_dir
  431. }
  432. get_armbian_image() {
  433. ARMBIAN_IMAGE="$(ls *.img)"
  434. [ "$ARMBIAN_IMAGE" ] || die 'You must place an Armbian image in the current directory!'
  435. local count=$(echo "$ARMBIAN_IMAGE" | wc -l)
  436. [ "$count" == 1 ] || die "More than one image file present!:\n$ARMBIAN_IMAGE"
  437. }
  438. _confirm_user_vars() {
  439. echo
  440. echo " Armbian image: $ARMBIAN_IMAGE"
  441. echo " Target device: /dev/$SDCARD_DEVNAME ($SD_INFO)"
  442. echo " Root filesystem device name: /dev/mapper/$ROOTFS_NAME"
  443. echo " Target IP address: $IP_ADDRESS"
  444. echo " Boot partition label: $BOOTPART_LABEL"
  445. echo " Disk password: $DISK_PASSWD"
  446. echo " Serial console unlocking: $SERIAL_CONSOLE"
  447. [ "$UNLOCKING_USERHOST" ] && echo " user@host of unlocking machine: $UNLOCKING_USERHOST"
  448. echo
  449. _user_confirm ' Are these settings correct?' 'yes'
  450. }
  451. setup_loopmount() {
  452. LOOP_DEV=$(losetup -f)
  453. losetup -P $LOOP_DEV $ARMBIAN_IMAGE
  454. mount ${LOOP_DEV}p1 $SRC_ROOT
  455. START_SECTOR=$(fdisk -l $LOOP_DEV -o Start | tail -n1 | tr -d ' ') # usually 32768
  456. BOOT_SECTORS=409600 # 200MB
  457. }
  458. _umount_with_check() {
  459. mountpoint -q $1 && umount $1
  460. }
  461. update_config_vars_file() {
  462. mount "/dev/$BOOT_DEVNAME" $BOOT_ROOT
  463. _print_config_vars $CONFIG_VARS_FILE
  464. umount $BOOT_ROOT
  465. }
  466. _print_states() {
  467. for i in $STATES; do
  468. echo $i: ${!i}
  469. done
  470. }
  471. _update_state_from_config_vars() {
  472. [ -e $CONFIG_VARS_FILE ] || return 0
  473. local reply
  474. while read reply; do eval "c$reply"; done <<-EOF
  475. $(cat $CONFIG_VARS_FILE)
  476. EOF
  477. local saved_states cfgvar_changed
  478. saved_states="$(_print_states)"
  479. cfgvar_changed=
  480. [ $cARMBIAN_IMAGE != $ARMBIAN_IMAGE ] && cfgvar_changed+=' ARMBIAN_IMAGE' card_partitioned='n'
  481. [ $cBOOTPART_LABEL != $BOOTPART_LABEL ] && cfgvar_changed+=' BOOTPART_LABEL' bootpart_label_created='n'
  482. [ $cROOTFS_NAME != $ROOTFS_NAME ] && cfgvar_changed+=' ROOTFS_NAME' target_configured='n'
  483. [ $cDISK_PASSWD != $DISK_PASSWD ] && cfgvar_changed+=' DISK_PASSWD' rootpart_copied='n'
  484. [ "$UNLOCKING_USERHOST" -a "$cUNLOCKING_USERHOST" != "$UNLOCKING_USERHOST" ] && {
  485. cfgvar_changed+=' UNLOCKING_USERHOST' target_configured='n'
  486. }
  487. [ $cIP_ADDRESS != $IP_ADDRESS ] && cfgvar_changed+=' IP_ADDRESS' target_configured='n'
  488. [ "$cADD_ALL_MODS" != "$ADD_ALL_MODS" ] && cfgvar_changed+=' ADD_ALL_MODS' target_configured='n'
  489. [ "$IP_ADDRESS" -a "$cUSE_LOCAL_AUTHORIZED_KEYS" != "$USE_LOCAL_AUTHORIZED_KEYS" ] && {
  490. cfgvar_changed+=' USE_LOCAL_AUTHORIZED_KEYS' target_configured='n'
  491. }
  492. [ $card_partitioned == 'n' ] && {
  493. bootpart_copied='n'
  494. bootpart_label_created='n'
  495. rootpart_copied='n'
  496. target_configured='n'
  497. }
  498. [ $bootpart_copied == 'n' ] && bootpart_label_created='n'
  499. [ $rootpart_copied == 'n' ] && target_configured='n'
  500. [ "$saved_states" != "$(_print_states)" ] && {
  501. warn "Install state altered due to changed config vars:$cfgvar_changed"
  502. for i in $STATES; do
  503. if [ "${!i}" == 'n' ]; then
  504. imsg " $i: ${RED}no$RESET"
  505. else
  506. imsg " $i: ${GREEN}yes$RESET"
  507. fi
  508. done
  509. _delete_state_files
  510. }
  511. true
  512. }
  513. _add_state_file() {
  514. local state=$1 cmd=$2
  515. if [ "$cmd" == 'target' ]; then
  516. touch "$TARGET_ROOT/boot/.rootenc_install_state/$state"
  517. else
  518. [ "$cmd" == 'mount' ] && mount "/dev/$BOOT_DEVNAME" $BOOT_ROOT
  519. mkdir -p "$BOOT_ROOT/.rootenc_install_state"
  520. touch "$BOOT_ROOT/.rootenc_install_state/$state"
  521. [ "$cmd" == 'mount' ] && umount $BOOT_ROOT
  522. fi
  523. eval "$state='y'"
  524. tmsg "added state file '$state'"
  525. }
  526. _delete_state_files() {
  527. for i in $STATES; do
  528. local fn="$BOOT_ROOT/.rootenc_install_state/$i"
  529. [ ${!i} == 'n' -a -e $fn ] && /bin/rm $fn
  530. done
  531. true
  532. }
  533. _get_state_from_state_files() {
  534. for i in $STATES; do
  535. if [ -e "$BOOT_ROOT/.rootenc_install_state/$i" ]; then
  536. eval "$i=y"
  537. else
  538. eval "$i=n"
  539. fi
  540. done
  541. }
  542. _print_state_from_state_files() {
  543. imsg 'Install state:'
  544. for i in $STATES; do
  545. if [ -e "$BOOT_ROOT/.rootenc_install_state/$i" ]; then
  546. imsg " $i: ${GREEN}yes$RESET"
  547. else
  548. imsg " $i: ${RED}no$RESET"
  549. fi
  550. done
  551. }
  552. check_install_state() {
  553. for i in $STATES; do eval "$i=n"; done
  554. if [ "$FORCE_REBUILD" ]; then
  555. return
  556. else
  557. do_partprobe
  558. lsblk --noheadings --list /dev/$SDCARD_DEVNAME -o 'NAME' | grep -q $BOOT_DEVNAME || return 0
  559. lsblk --noheadings --list /dev/$BOOT_DEVNAME -o 'FSTYPE' | grep -q 'ext4' || return 0
  560. mount "/dev/$BOOT_DEVNAME" $BOOT_ROOT
  561. _get_state_from_state_files
  562. if [ "$target_configured" == 'y' -a "$FORCE_RECONFIGURE" ]; then
  563. target_configured='n'
  564. _delete_state_files
  565. fi
  566. _print_state_from_state_files
  567. _update_state_from_config_vars
  568. _umount_with_check $BOOT_ROOT
  569. fi
  570. }
  571. close_loopmount() {
  572. while mountpoint -q $SRC_ROOT; do
  573. umount $SRC_ROOT
  574. done
  575. for i in $(losetup --noheadings --raw --list -j $ARMBIAN_IMAGE | awk '{print $1}'); do
  576. losetup -d $i
  577. done
  578. }
  579. _user_confirm() {
  580. local prompt1 prompt2 dfl_action reply
  581. prompt1=$1 dfl_action=$2
  582. if [ "$dfl_action" == 'yes' ]; then
  583. prompt2='(Y/n)'
  584. else
  585. prompt2='(y/N)'
  586. fi
  587. imsg_nonl "$prompt1 $prompt2 "
  588. read -n1 reply
  589. [ "$reply" ] && imsg ''
  590. [ "$dfl_action" == 'yes' -a -z "$reply" ] && return
  591. [ "$reply" == 'y' -o "$reply" == 'Y' ] && return
  592. warn "Exiting at user request"
  593. exit 1
  594. }
  595. erase_boot_sector_and_first_partition() {
  596. local sectors count
  597. sectors=$((START_SECTOR+BOOT_SECTORS+100))
  598. count=$(((sectors/8192)+1))
  599. pu_msg "Erasing up to beginning of second partition ($sectors sectors, ${count}M):"
  600. _show_output
  601. dd if=/dev/zero \
  602. of=/dev/$SDCARD_DEVNAME \
  603. status=progress \
  604. bs=$((512*8192)) \
  605. count=$count
  606. _hide_output
  607. }
  608. create_partition_label() {
  609. pu_msg "Creating new partition label on /dev/$SDCARD_DEVNAME"
  610. local fdisk_cmds="o\nw\n"
  611. set +e
  612. echo -e "$fdisk_cmds" | fdisk "/dev/$SDCARD_DEVNAME"
  613. set -e
  614. do_partprobe
  615. }
  616. copy_boot_loader() {
  617. local count
  618. count=$((START_SECTOR/2048))
  619. pu_msg "Copying boot loader ($START_SECTOR sectors, ${count}M):"
  620. _show_output
  621. dd if=$ARMBIAN_IMAGE \
  622. of=/dev/$SDCARD_DEVNAME \
  623. status=progress \
  624. bs=$((512*2048)) \
  625. count=$count
  626. _hide_output
  627. do_partprobe
  628. }
  629. _print_config_vars() {
  630. local outfile=$1
  631. local data="$(for i in $CONFIG_VARS; do echo "$i=${!i}"; done)"
  632. if [ "$outfile" ]; then echo "$data" > $outfile; else echo "$data"; fi
  633. }
  634. partition_sd_card() {
  635. local p1_end p2_start fdisk_cmds bname rname fstype
  636. p1_end=$((START_SECTOR+BOOT_SECTORS-1))
  637. p2_start=$((p1_end+1))
  638. fdisk_cmds="o\nn\np\n1\n$START_SECTOR\n$p1_end\nn\np\n2\n$p2_start\n\nw\n"
  639. set +e
  640. echo -e "$fdisk_cmds" | fdisk "/dev/$SDCARD_DEVNAME"
  641. set -e
  642. do_partprobe
  643. bname="$(lsblk --noheadings --list --output=NAME /dev/$BOOT_DEVNAME)"
  644. [ "$bname" == $BOOT_DEVNAME ] || die 'Partitioning failed!'
  645. rname="$(lsblk --noheadings --list --output=NAME /dev/$ROOT_DEVNAME)"
  646. [ "$rname" == $ROOT_DEVNAME ] || die 'Partitioning failed!'
  647. # filesystem is required by call to _add_state_file(), so we must create it here
  648. fstype=$(lsblk --noheadings --list --output=FSTYPE "/dev/$BOOT_DEVNAME")
  649. [ "$fstype" == 'ext4' -a "$ROOTENC_REUSE_FS" ] || mkfs.ext4 -F "/dev/$BOOT_DEVNAME"
  650. do_partprobe
  651. _add_state_file 'card_partitioned' 'mount'
  652. }
  653. _do_partition() {
  654. imsg "All data on /dev/$SDCARD_DEVNAME ($SD_INFO) will be destroyed!!!"
  655. _user_confirm 'Are you sure you want to continue?' 'no'
  656. if [ "$ERASE" ]; then
  657. erase_boot_sector_and_first_partition
  658. else
  659. create_partition_label
  660. fi
  661. copy_boot_loader
  662. partition_sd_card
  663. }
  664. copy_system_boot() {
  665. [ "$PARTITION_ONLY" ] && {
  666. _add_state_file 'bootpart_copied' 'mount'
  667. return
  668. }
  669. mount "/dev/$BOOT_DEVNAME" $BOOT_ROOT
  670. pu_msg "Copying files to boot partition:"
  671. _show_output
  672. rsync $RSYNC_VERBOSITY --archive $SRC_ROOT/boot/* $BOOT_ROOT
  673. _hide_output
  674. [ -e "$BOOT_ROOT/boot" ] || (cd $BOOT_ROOT && ln -s . 'boot')
  675. _add_state_file 'bootpart_copied'
  676. umount $BOOT_ROOT
  677. }
  678. create_bootpart_label() {
  679. e2label "/dev/$BOOT_DEVNAME" "$BOOTPART_LABEL"
  680. do_partprobe
  681. _add_state_file 'bootpart_label_created' 'mount'
  682. }
  683. copy_system_root() {
  684. if [ "$FORCE_REFORMAT_ROOT" ] || ! cryptsetup isLuks "/dev/$ROOT_DEVNAME"; then
  685. pu_msg "Formatting encrypted root partition:"
  686. echo -n $DISK_PASSWD | cryptsetup luksFormat "/dev/$ROOT_DEVNAME" '-'
  687. fi
  688. echo $DISK_PASSWD | cryptsetup luksOpen "/dev/$ROOT_DEVNAME" $ROOTFS_NAME
  689. local fstype=$(lsblk --noheadings --list --output=FSTYPE "/dev/mapper/$ROOTFS_NAME")
  690. [ "$fstype" == 'ext4' -a "$ROOTENC_REUSE_FS" ] || mkfs.ext4 -F "/dev/mapper/$ROOTFS_NAME"
  691. [ "$PARTITION_ONLY" ] || {
  692. mount "/dev/mapper/$ROOTFS_NAME" $TARGET_ROOT
  693. pu_msg "Copying system to encrypted root partition:"
  694. _show_output
  695. rsync $RSYNC_VERBOSITY --archive --exclude=boot $SRC_ROOT/* $TARGET_ROOT
  696. _hide_output
  697. sync
  698. mkdir -p "$TARGET_ROOT/boot"
  699. touch "$TARGET_ROOT/root/.no_rootfs_resize"
  700. umount $TARGET_ROOT
  701. }
  702. cryptsetup luksClose $ROOTFS_NAME
  703. do_partprobe
  704. _add_state_file 'rootpart_copied' 'mount'
  705. }
  706. mount_target() {
  707. echo $DISK_PASSWD | cryptsetup luksOpen "/dev/$ROOT_DEVNAME" $ROOTFS_NAME
  708. mount "/dev/mapper/$ROOTFS_NAME" $TARGET_ROOT
  709. mount "/dev/$BOOT_DEVNAME" "$TARGET_ROOT/boot"
  710. local src dest args
  711. while read src dest args; do
  712. mount $args $src $TARGET_ROOT/$dest
  713. done <<-EOF
  714. udev dev -t devtmpfs -o rw,relatime,nosuid,mode=0755
  715. devpts dev/pts -t devpts
  716. tmpfs dev/shm -t tmpfs -o rw,nosuid,nodev,relatime
  717. proc proc -t proc
  718. sys sys -t sysfs
  719. EOF
  720. }
  721. _copy_to_target() {
  722. local fn=$1
  723. if [ -e $fn ]; then
  724. echo "Copying '$fn'"
  725. cat $fn > $TARGET_ROOT/$fn
  726. else
  727. imsg "Unable to copy '$fn' to target (file does not exist)"
  728. false
  729. fi
  730. }
  731. create_etc_crypttab() {
  732. local root_uuid="$(lsblk --noheadings --list --nodeps --output=UUID /dev/$ROOT_DEVNAME)"
  733. echo "$ROOTFS_NAME UUID=$root_uuid none initramfs,luks" > "$TARGET_ROOT/etc/crypttab"
  734. _display_file "$TARGET_ROOT/etc/crypttab"
  735. }
  736. copy_etc_files() {
  737. _copy_to_target '/etc/resolv.conf'
  738. _copy_to_target '/etc/hosts'
  739. set +e
  740. _copy_to_target /etc/apt/apt.conf.d/*proxy
  741. set -e
  742. }
  743. _set_target_vars() {
  744. target_distro=$(chroot $TARGET_ROOT 'lsb_release' '--short' '--codename')
  745. target_kernel=$(chroot $TARGET_ROOT 'ls' '/boot' | egrep '^vmlinu[xz]')
  746. imsg "$(printf '%-8s %-28s %s' '' 'Host' 'Target')"
  747. imsg "$(printf '%-8s %-28s %s' '' '----' '------')"
  748. imsg "$(printf '%-8s %-28s %s' 'distro:' $host_distro $target_distro)"
  749. imsg "$(printf '%-8s %-28s %s' 'kernel:' $host_kernel $target_kernel)"
  750. }
  751. _distros_match() {
  752. [ $host_distro == $target_distro ]
  753. }
  754. _kernels_match() {
  755. [ ${host_kernel%.*} == ${target_kernel%.*} ] || return 1
  756. [ ${host_kernel##*-} == ${target_kernel##*-} ]
  757. }
  758. copy_etc_files_distro_specific() {
  759. local files='/etc/apt/sources.list /etc/apt/sources.list.d/armbian.list'
  760. if _distros_match; then
  761. for i in $files; do _copy_to_target $i; done
  762. else
  763. warn 'Warning: host and target distros do not match:'
  764. for i in $files; do imsg " not copying $i"; done
  765. fi
  766. }
  767. _display_file() {
  768. local name text reply
  769. if [ "$2" ]; then
  770. name="$1"
  771. text="$2"
  772. else
  773. name=${1#$TARGET_ROOT}
  774. text="$(cat $1)"
  775. fi
  776. hl='────────────────────────────────────────'
  777. hl="$hl$hl$hl"
  778. hls=${hl:0:${#name}+1}
  779. echo "┌─$hls─┐"
  780. echo "│ $name: │"
  781. echo "├─$hls─┘"
  782. echo "$text" | sed 's/^/│ /'
  783. }
  784. edit_armbianEnv() {
  785. local file text console_arg
  786. file="$TARGET_ROOT/boot/armbianEnv.txt"
  787. ed $file <<-'EOF'
  788. g/^\s*rootdev=/d
  789. g/^\s*console=/d
  790. g/^\s*bootlogo=/d
  791. wq
  792. EOF
  793. case $SERIAL_CONSOLE in
  794. 'yes') console_arg='serial' ;;
  795. *) console_arg='display' ;;
  796. esac
  797. text="rootdev=/dev/mapper/$ROOTFS_NAME
  798. console=$console_arg
  799. bootlogo=false"
  800. echo "$text" >> $file
  801. _display_file $file
  802. }
  803. # Add the following lines to '/etc/initramfs-tools/initramfs.conf'. If
  804. # your board’s IP address will be statically configured, substitute the
  805. # correct static IP address after 'IP='. If it will be configured via
  806. # DHCP, omit the IP line entirely:
  807. edit_initramfs_conf() {
  808. local file="$TARGET_ROOT/etc/initramfs-tools/initramfs.conf"
  809. ed $file <<-'EOF'
  810. g/^\s*IP=/s/^/# /
  811. g/^\s*DEVICE=/d
  812. wq
  813. EOF
  814. [ "$IP_ADDRESS" == 'dhcp' -o "$IP_ADDRESS" == 'none' ] || {
  815. echo "IP=$IP_ADDRESS:::255.255.255.0::eth0:off" >> $file
  816. }
  817. [ "$IP_ADDRESS" == 'none' ] || echo "DEVICE=eth0" >> $file
  818. _display_file $file
  819. }
  820. edit_initramfs_modules() {
  821. local modlist file hdr
  822. [ "$ADD_ALL_MODS" ] && {
  823. if ! _kernels_match; then
  824. warn 'Host and target kernels do not match. Not adding modules to initramfs'
  825. elif ! _distros_match; then
  826. warn 'Host and target distros do not match. Not adding modules to initramfs'
  827. else
  828. modlist=$(lsmod | cut -d ' ' -f1 | tail -n+2)
  829. fi
  830. }
  831. file="$TARGET_ROOT/etc/initramfs-tools/modules"
  832. hdr="# List of modules that you want to include in your initramfs.
  833. # They will be loaded at boot time in the order below.
  834. #
  835. # Syntax: module_name [args ...]
  836. #
  837. # You must run update-initramfs(8) to effect this change.
  838. #
  839. "
  840. echo "$hdr$modlist" > $file
  841. _display_file $file
  842. }
  843. copy_authorized_keys() {
  844. local dest="$TARGET_ROOT/etc/dropbear-initramfs"
  845. mkdir -p $dest
  846. /bin/cp 'authorized_keys' $dest
  847. _display_file "$dest/authorized_keys"
  848. }
  849. create_fstab() {
  850. local boot_uuid file text
  851. boot_uuid="$(lsblk --noheadings --list --output=UUID /dev/$BOOT_DEVNAME)"
  852. file="$TARGET_ROOT/etc/fstab"
  853. text="/dev/mapper/$ROOTFS_NAME / ext4 defaults,noatime,nodiratime,commit=600,errors=remount-ro 0 1
  854. UUID=$boot_uuid /boot ext4 defaults,noatime,nodiratime,commit=600,errors=remount-ro 0 2
  855. tmpfs /tmp tmpfs defaults,nosuid 0 0"
  856. echo "$text" > $file
  857. _display_file $file
  858. }
  859. edit_dropbear_cfg() {
  860. local dest file text
  861. dest="$TARGET_ROOT/etc/dropbear-initramfs"
  862. file="$dest/config"
  863. text='DROPBEAR_OPTIONS="-p 2222"
  864. DROPBEAR=y'
  865. if [ "$IP_ADDRESS" == 'none' ]; then
  866. [ -e $file ] && rm -v $file
  867. true
  868. else
  869. mkdir -p $dest
  870. [ -e $file ] && grep -q '^DROPBEAR_OPTIONS="-p 2222"' $file || echo "$text" >> $file
  871. _display_file $file
  872. fi
  873. }
  874. create_cryptroot_unlock_sh() {
  875. local dest file text
  876. dest="$TARGET_ROOT/etc/initramfs-tools/hooks"
  877. file="$dest/cryptroot-unlock.sh"
  878. text='#!/bin/sh
  879. if [ "$1" = "prereqs" ]; then echo "dropbear-initramfs"; exit 0; fi
  880. . /usr/share/initramfs-tools/hook-functions
  881. source="/tmp/cryptroot-unlock-profile"
  882. root_home=$(echo $DESTDIR/root-*)
  883. root_home=${root_home#$DESTDIR}
  884. echo "if [ \"\$SSH_CLIENT\" ]; then /usr/bin/cryptroot-unlock; fi" > $source
  885. copy_file ssh_login_profile $source $root_home/.profile
  886. exit 0'
  887. mkdir -p $dest
  888. echo "$text" > $file
  889. chmod 755 $file
  890. _display_file $file
  891. }
  892. # begin chroot functions:
  893. apt_install_target() {
  894. local pkgs=$(_print_pkgs_to_install 'target')
  895. [ "$pkgs" ] && {
  896. echo "target packages to install: $pkgs"
  897. local ls1 ls2
  898. _show_output
  899. ls1=$(ls -l /boot/initrd.img-*)
  900. # DEBUG:
  901. # dpkg-reconfigure $pkgs # doesn't work in chroot
  902. # apt --yes purge $pkgs
  903. # apt-get --yes --purge autoremove
  904. dpkg --configure --pending --force-confdef
  905. set +e
  906. apt --yes purge 'bash-completion'
  907. apt --yes purge 'command-not-found'
  908. set -e
  909. _apt_update
  910. echo 'force-confdef' > /root/.dpkg.cfg
  911. apt --yes install $pkgs
  912. rm /root/.dpkg.cfg
  913. apt --yes autoremove
  914. ls2=$(ls -l /boot/initrd.img-*)
  915. [ "$ls1" != "$ls2" ] && initramfs_updated='y'
  916. _hide_output
  917. }
  918. true
  919. }
  920. update_initramfs() {
  921. [ "$ROOTENC_TESTING" ] && return 0
  922. _show_output
  923. local ver=$(echo /boot/vmlinu?-* | sed 's/.boot.vmlinu.-//')
  924. update-initramfs -k $ver -u
  925. _hide_output
  926. }
  927. check_initramfs() {
  928. local text chk count
  929. text="$(lsinitramfs /boot/initrd.img*)"
  930. set +e
  931. chk=$(echo "$text" | grep 'cryptsetup')
  932. count=$(echo "$chk" | wc -l)
  933. [ "$count" -gt 5 ] || { echo "$text"; die 'Cryptsetup scripts missing in initramfs image'; }
  934. _display_file "lsinitramfs /boot/initrd.img* | grep 'cryptsetup'" "$chk"
  935. [ "$IP_ADDRESS" == 'none' ] || {
  936. chk=$(echo "$text" | grep 'dropbear')
  937. count=$(echo "$chk" | wc -l)
  938. [ "$count" -gt 5 ] || { echo "$text"; die 'Dropbear scripts missing in initramfs image'; }
  939. _display_file "lsinitramfs /boot/initrd.img* | grep 'dropbear'" "$chk"
  940. chk=$(echo "$text" | grep 'authorized_keys')
  941. count=$(echo "$chk" | wc -l)
  942. [ "$count" -eq 1 ] || { echo "$text"; die 'authorized_keys missing in initramfs image'; }
  943. _display_file "lsinitramfs /boot/initrd.img* | grep 'authorized_keys'" "$chk"
  944. }
  945. set -e
  946. }
  947. configure_target() {
  948. [ "$PARTITION_ONLY" ] && return
  949. mount_target
  950. _set_target_vars
  951. copy_etc_files
  952. copy_etc_files_distro_specific
  953. edit_initramfs_conf
  954. edit_initramfs_modules
  955. [ "$IP_ADDRESS" == 'none' ] || copy_authorized_keys
  956. create_etc_crypttab
  957. create_fstab
  958. edit_dropbear_cfg
  959. [ "$IP_ADDRESS" == 'none' ] || create_cryptroot_unlock_sh
  960. edit_armbianEnv
  961. _debug_pause
  962. _show_output # this must be done before entering chroot
  963. /bin/cp $0 $TARGET_ROOT
  964. export 'ROOTFS_NAME' 'IP_ADDRESS' 'target_distro' 'ROOTENC_TESTING' 'ROOTENC_PAUSE' 'ROOTENC_IGNORE_APT_ERRORS' 'APT_UPGRADE'
  965. chroot $TARGET_ROOT "./$PROGNAME" $ORIG_OPTS 'in_target'
  966. /bin/cp -a '/etc/resolv.conf' "$TARGET_ROOT/etc" # this could be a symlink
  967. /bin/rm "$TARGET_ROOT/$PROGNAME"
  968. _add_state_file 'target_configured' 'target'
  969. }
  970. _set_env_vars() {
  971. shopt -s extglob
  972. local name val
  973. while [ $# -gt 0 ]; do
  974. name=${1%=?*} val=${1#+([A-Z_])=}
  975. [ "$name" == "$1" -o "$val" == "$1" ] && die "$1: illegal argument (must be in format 'NAME=value')"
  976. eval "$name=$val"
  977. shift
  978. done
  979. shopt -u extglob
  980. }
  981. # begin execution
  982. set -e
  983. while getopts hCdmfFpRsuvz OPT
  984. do
  985. case "$OPT" in
  986. h) print_help; exit ;;
  987. C) NO_CLEANUP='y' ;;
  988. F) FORCE_REBUILD='y' ;;
  989. f) FORCE_RECONFIGURE='y' ;;
  990. m) ADD_ALL_MODS='y' ;;
  991. p) PARTITION_ONLY='y' ;;
  992. R) FORCE_REFORMAT_ROOT='y' ;;
  993. s) USE_LOCAL_AUTHORIZED_KEYS='y' ;;
  994. u) APT_UPGRADE='y' ;;
  995. d) DEBUG='y' ;&
  996. v) VERBOSE='y' RSYNC_VERBOSITY='--verbose' ;;
  997. z) ERASE='y' ;;
  998. *) exit ;;
  999. esac
  1000. ORIG_OPTS+="-$OPT "
  1001. done
  1002. shift $((OPTIND-1))
  1003. trap '_return_handler' RETURN
  1004. trap '_sigint_handler' INT
  1005. trap '_error_handler' ERR
  1006. set -o functrace
  1007. set -o errtrace
  1008. exec {stdout_dup}>&1
  1009. exec {stderr_dup}>&2
  1010. [ $UID == 0 -o $EUID == 0 ] || die 'This program must be run as root!'
  1011. export HOME='/root'
  1012. [ "$DEBUG" ] && set -x
  1013. ARG1=$1; shift
  1014. _set_env_vars $@
  1015. if [ "$ARG1" == 'in_target' ]; then
  1016. SCRIPT_DESC='Target script'
  1017. _hide_output
  1018. [ "$target_distro" == 'bionic' ] && {
  1019. echo 'export CRYPTSETUP=y' > '/etc/initramfs-tools/conf.d/cryptsetup'
  1020. }
  1021. apt_install_target
  1022. [ "$initramfs_updated" ] || update_initramfs
  1023. check_initramfs
  1024. else
  1025. SCRIPT_DESC='Host script'
  1026. _do_header
  1027. _set_host_vars
  1028. get_armbian_image
  1029. apt_install_host # we need cryptsetup in next cmd
  1030. _preclean
  1031. check_sdcard_name_and_params $ARG1
  1032. _get_user_vars
  1033. _test_sdcard_mounted
  1034. _warn_user_opts
  1035. _confirm_user_vars
  1036. [ "$IP_ADDRESS" == 'none' ] || get_authorized_keys
  1037. create_build_dir
  1038. [ "$NO_CLEANUP" ] || trap '_clean' EXIT
  1039. setup_loopmount
  1040. _debug_pause
  1041. check_install_state
  1042. _hide_output
  1043. [ "$card_partitioned" == 'n' ] && _do_partition
  1044. _debug_pause
  1045. [ "$bootpart_copied" == 'n' ] && copy_system_boot
  1046. [ "$bootpart_label_created" == 'n' ] && create_bootpart_label
  1047. [ "$rootpart_copied" == 'n' ] && copy_system_root
  1048. [ "$target_configured" == 'n' ] && configure_target
  1049. sync
  1050. gmsg 'All done!'
  1051. if [ "$IP_ADDRESS" != 'none' ]; then
  1052. imsg "To unlock the target disk, execute the following from the unlocking host:"
  1053. imsg " ssh -p 2222 root@${IP_ADDRESS/dhcp/TARGET_IP}"
  1054. fi
  1055. fi