Advertisement
Guest User

Untitled

a guest
Apr 11th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.41 KB | None | 0 0
  1. #!/bin/sh
  2. # note: default alpine shell is ash.
  3.  
  4. ARCH=x86_64
  5.  
  6. ONLN_REPO="http://nl.alpinelinux.org/alpine/latest-stable/main/"
  7. OFLN_REPO="/media/repository/"
  8.  
  9. ONLN_HOST="nl.alpinelinux.org"
  10. CIFS_HOST="vmhst"
  11.  
  12. CDFS_MSRC="/media/cdrom/repository/"
  13. CIFS_MSRC="share/alpine-mirror/latest-stable/main/"
  14.  
  15. CIFS_OPTS="username=share,password=none,domain=NIL"
  16. MIRROR="/00-systems/linux-x64/00-alpine/00-mirror/"
  17.  
  18. VMHST=10.20.20.242
  19. VMGST=10.20.20.240
  20.  
  21. # TODO: explain/document the 3 NIC settings.
  22.  
  23. main () {
  24. # note:
  25. # up to setup-disk in init_hd, configuration will be propagated
  26. # to installed system except apk repository and db, this include
  27. # previously installed packages.
  28.  
  29. local pset pdoc
  30. init_inst () {
  31. pset="$1" ; shift
  32. case "$1" in
  33. with-doc) pdoc=true ;;
  34. '') pdoc=false ;;
  35. *) uerrx moipx ;;
  36. esac
  37. shift
  38. pkgset "$pset" lsdoc > /dev/null || uerrx moipx
  39. oncdx; init_kbd; init_net;
  40. }
  41.  
  42. local gotl
  43. init_net () {
  44. init_dyn_net;
  45. init_stt_net && gotl=true || gotl=false
  46. init_vht_net && gotl=true
  47. # wihtout dns, assume there is no lan other than guest <-> host.
  48. $gotl && ! getent hosts vmhst > /dev/null 2>&1 && CIFS_HOST="$VMHST"
  49. true
  50. }
  51.  
  52. local repo
  53. set_ofln_repo () { mkdir "$OFLN_REPO" && repo="$OFLN_REPO" ; }
  54. set_onln_repo () { host_test "$ONLN_HOST" && repo="$ONLN_REPO" ; }
  55.  
  56. local mode="$1" ; shift
  57. case "$mode" in
  58. onln) init_inst "$@"; set_onln_repo ;;
  59. ofln) init_inst "$@"; set_ofln_repo ;;
  60. cifs) init_inst "$@"; set_ofln_repo ;;
  61. cimt) oncdx; init_kbd; init_net; set_ofln_repo ;;
  62. init) oncdx; init_kbd; init_net; exit ;;
  63. psls) pkgset ls doc; exit ;;
  64. dkey) pubkey; exit ;;
  65. help) usage; exit ;;
  66. pfls) bashx && pkfiles "$@"; echo; exit ;;
  67. srls) bashx && pset2repofiles "$@"; exit ;;
  68. mkrp) bashx && makerepo "$@"; exit ;;
  69. mkim) bashx && makeiso "$@"; exit ;;
  70. test) bashx && do_test "$@"; exit ;;
  71. *) errx usagx ;;
  72. esac
  73. [ "$repo" ] || errx repox
  74.  
  75. # mount repository and prepare fstab data for ofln/cifs repo.
  76. local mfst
  77. case "$mode" in
  78. ofln)
  79. mntfx --rbind "$CDFS_MSRC" "$repo"
  80. mfst="$CDFS_MSRC $repo none rbind,ro,noauto 0 0" ;;
  81. cifs|cimt)
  82. $gotl || errx cflnx
  83. mfst="//${CIFS_HOST}/${CIFS_MSRC}"
  84. mntfx -t cifs -o "$CIFS_OPTS" "$mfst" "$repo"
  85. mfst="$mfst $repo cifs ${CIFS_OPTS},ro,noauto 0 0" ;;
  86. esac
  87. inrpx "$repo"
  88. [ "$mode" == "cimt" ] && exit
  89.  
  90. init_hd # setup-disk done, configuration propagation ended.
  91.  
  92. # propagate repository setup and set fstab entry for ofln/cifs repo.
  93. [ -d "$repo" ] && {
  94. mkdir "/mnt/${repo/#\//}" || errx
  95. printf "${mfst}\n" >> /mnt/etc/fstab || errx
  96. }
  97.  
  98. # initialize system.
  99. system_setup "$HOSTNAME" "$pset" "$pdoc" || errx
  100. }
  101.  
  102. init_kbd () {
  103. setup-keymap fr fr-latin9
  104. }
  105.  
  106. init_dyn_net () {
  107. cat <<- EOF > /etc/network/interfaces
  108.  
  109. auto lo
  110. iface lo inet loopback
  111.  
  112. EOF
  113. ifconfig eth0 up
  114. if udhcpc -i eth0 -R -n -q -t 3 ; then
  115. cat <<- EOF >> /etc/network/interfaces
  116. auto eth0
  117. iface eth0 inet dhcp
  118.  
  119. EOF
  120. fi
  121. rc-update add networking default
  122. openrc
  123. }
  124.  
  125. init_stt_net () {
  126. export HOSTNAME=localhost
  127. local wan_ip="$(ifconfig -a)"
  128. { grep -q eth1 || return; } <<- EOF
  129. $wan_ip
  130. EOF
  131.  
  132. wan_ip="$(ifconfig eth0 | sed -n '/inet addr:/s,.*r:\([^ ]*\).*,\1,p')"
  133. [ "$wan_ip" ] || return
  134.  
  135. local hsedexp='s,^[^[:blank:]]*[[:blank:]]*\([^[:blank:]\.]*\).*,\1,p'
  136. local hname="$(getent hosts "$wan_ip" | sed -n "$hsedexp")"
  137. [ "$hname" ] || return
  138.  
  139. local isedexp='s/\([[:digit:]\.]*\).*/\1/p'
  140. local lan_ip="$(getent hosts "${hname}-lan" | sed -n "$isedexp")"
  141. { [ "$lan_ip" ] && [ "$lan_ip" != "$wan_ip" ]; } || return
  142.  
  143. hname="$(getent hosts "$lan_ip" | sed -n "$hsedexp")";
  144. [ "$hname" ] && export HOSTNAME="$hname"
  145.  
  146. local tab=$'\t'
  147. cat <<- EOF >> /etc/network/interfaces
  148. auto eth1
  149. iface eth1 inet static
  150. ${tab}address ${lan_ip}
  151. ${tab}netmask 255.255.255.0
  152.  
  153. EOF
  154. /etc/init.d/networking restart
  155. }
  156.  
  157. init_vht_net () {
  158. local vnet_ip="$(ifconfig -a)"
  159. { grep -q eth2 || return; } <<- EOF
  160. $vnet_ip
  161. EOF
  162. vmnet_ip=''
  163. [ "$HOSTNAME" ] && [ "$HOSTNAME" != "localhost" ] && {
  164. local isedexp='s/\([[:digit:]\.]*\).*/\1/p'
  165. vmnet_ip="$(getent hosts ${HOSTNAME}-vmnet | sed -n "$isedexp")"
  166. }
  167. [ "$vmnet_ip" ] || vmnet_ip="$VMGST"
  168.  
  169. local tab=$'\t'
  170. cat <<- EOF >> /etc/network/interfaces
  171. auto eth2
  172. iface eth2 inet static
  173. ${tab}address ${vmnet_ip}
  174. ${tab}netmask 255.255.255.0
  175.  
  176. EOF
  177. /etc/init.d/networking restart
  178. ping -W 1 -w 3 -c 3 $VMHST > /dev/null 2>&1
  179. }
  180.  
  181. init_hd () {
  182. # printf "*** setting partition table\n"
  183. pkadx sfdisk
  184. sfdisk /dev/sda <<- EOF
  185. label: gpt
  186. label-id: 28062016-00FF-DDFF-0000-000000000000
  187. device: /dev/sda
  188. unit: sectors
  189. first-lba: 34
  190. last-lba: 33554398
  191.  
  192. /dev/sda2 : start= 2048, size= 1048576,\
  193. type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,\
  194. uuid=00000000-0000-0000-0000-0000000000FF,\
  195. name="Linux filesystem"
  196. /dev/sda4 : start=1050624, size=32503775,\
  197. type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,\
  198. uuid=00000000-0000-0000-0000-0000000000EE,\
  199. name="Linux filesystem"
  200. EOF
  201.  
  202. # note: attrs setting using dump input fail.
  203. sfdisk --part-attrs /dev/sda 2 LegacyBIOSBootable
  204. sync
  205. mdev -s
  206. sync
  207. apk del sfdisk # prevent propagation.
  208.  
  209. # printf "*** initializing filesystems\n"
  210. pkadx e2fsprogs btrfs-progs
  211. mkfs.ext4 -L boot -U 28062016-00EE-DDFF-00FF-000000000000 /dev/sda2
  212. mkfs.btrfs -L root -U 28062016-00EE-DDFF-00EE-000000000000 /dev/sda4
  213. mount -t btrfs /dev/sda4 /mnt
  214. btrfs su create /mnt/57
  215. btrfs su create /mnt/default
  216. btrfs su create /mnt/59
  217. btrfs su create /mnt/default/usr
  218. btrfs su create /mnt/61
  219. btrfs su create /mnt/default/usr/local
  220. btrfs su create /mnt/63
  221. btrfs su create /mnt/default/var
  222. btrfs su create /mnt/default/tmp
  223. btrfs su create /mnt/default/home
  224. local i
  225. for i in 57 59 61 63; do
  226. btrfs su delete -c /mnt/$i
  227. done
  228. btrfs su set-default 258 /mnt
  229. umount /mnt
  230.  
  231. # NOTE: filesystems fsck and detection do require
  232. # propagation of filesystems tools to installed system.
  233. # DO NOT remove e2fsprogs or btrfs-progs here.
  234.  
  235. # printf "*** mounting filesystems\n"
  236. mount -t btrfs /dev/sda4 /mnt
  237. mkdir /mnt/boot
  238. mount -t ext4 /dev/sda2 /mnt/boot
  239.  
  240. printf "*** initializing system (setup-disk)\n"
  241. setup-disk -s0 -k vanilla /mnt
  242.  
  243. # printf "*** initializing mbr\n"
  244. # note: syslinux pkg is loaded by setup-disk.
  245. dd if=/usr/share/syslinux/gptmbr.bin of=/dev/sda bs=440 count=1
  246. sync
  247. }
  248.  
  249. system_setup () {
  250. local hostname="$1"; shift
  251. local pset="$1"; shift
  252. local pdoc="$1"; shift
  253.  
  254. # printf "*** set hostname\n"
  255. printf "%s\n" "$hostname" > /mnt/etc/hostname
  256.  
  257. # printf "*** set repository for new system\n"
  258. cat /etc/apk/repositories > /mnt/etc/apk/repositories
  259.  
  260. # printf "*** add packages from set\n"
  261. pset="$(pkgset "$pset")" || errx badsx
  262. for p in $pset; do
  263. apk -p /mnt add "$p" || errx pkadx -p /mnt add "$p"
  264. done
  265.  
  266. # printf "*** add docs packages\n"
  267. [ "$pdoc" ] && $pdoc && {
  268. for p in $(apk -p /mnt info); do
  269. apk info ${p}-doc > /dev/null && apk -p /mnt add ${p}-doc
  270. done
  271. }
  272.  
  273. ### packages independent setup
  274.  
  275. # printf "*** move root home to /home/root\n"
  276. sed -i '/^root:/s,:/root,:/home/root,' /mnt/etc/passwd
  277. mv /mnt/root /mnt/home/
  278.  
  279. # printf "*** add admn user, group and home\n"
  280. sed -i '/^guest:/s,.*,\0\nadmn:x:654:654:admn:/home/admn:/bin/ash,' \
  281. /mnt/etc/passwd
  282. sed -i '/^guest:/s,.*,\0\nadmn:::0:::::,' /mnt/etc/shadow
  283. sed -i '/^utmp:/s,.*,\0\nadmn:x:654:,' /mnt/etc/group
  284. mkdir /mnt/home/admn
  285. chown 654.654 /mnt/home/admn
  286. chmod 755 /mnt/home/admn
  287.  
  288. # printf "*** cleanup usr/local\n"
  289. rm -rf /mnt/usr/local/*
  290.  
  291. # printf "*** set default prompt\n"
  292. sed -i '/export PS1/s,\\h,\\u@\\h,' /mnt/etc/profile
  293.  
  294. # printf "*** set ll alias for bash\n"
  295. printf "[ \"\$BASH_VERSION\" ] && alias ll='ls -la'\n" \
  296. > /mnt/etc/profile.d/ll.sh
  297.  
  298. # printf "*** cleanup motd\n"
  299. printf 'motd: no news is good news.\n' > /mnt/etc/motd
  300.  
  301. ### packages dependent setup
  302.  
  303. [ -x /mnt/bin/bash ] && {
  304. # printf "*** set bash as root and admn shell\n"
  305. sed -i '/^root:/s,/bin/ash,/bin/bash,;
  306. /^admn:/s,/bin/ash,/bin/bash,' /mnt/etc/passwd
  307. }
  308.  
  309. [ -x /mnt/usr/bin/sudo ] && {
  310. # printf "*** add admn sudoer\n"
  311. sed -i '/^root/s,.*,\0\nadmn ALL=(ALL) NOPASSWD: ALL,' \
  312. /mnt/etc/sudoers
  313. }
  314.  
  315. [ -x /mnt/usr/bin/ssh ] && {
  316. # printf "*** add admn ssh key\n"
  317. mkdir /mnt/home/admn/.ssh
  318. chown 654.654 /mnt/home/admn/.ssh
  319. chmod 700 /mnt/home/admn/.ssh
  320. pubkey > /mnt/home/admn/.ssh/authorized_keys
  321. chown 654.654 /mnt/home/admn/.ssh/authorized_keys
  322. chmod 644 /mnt/home/admn/.ssh/authorized_keys
  323.  
  324. # printf "*** ssh server setup\n"
  325. sed -i 's/^#PermitRootLogin.*/PermitRootLogin no/;
  326. s/^#PasswordAuthentication.*/PasswordAuthentication no/;
  327. s/^#PermitEmptyPasswords.*/PermitEmptyPasswords no/' \
  328. /mnt/etc/ssh/sshd_config
  329. chroot /mnt /sbin/rc-update add sshd default
  330. }
  331.  
  332. ### locally packages dependent setup
  333.  
  334. # printf "*** forge and forget root and admn passwords\n"
  335. # note: show them if ssh is'nt installed... :)
  336. apk add openssh > /dev/null 2>&1 && {
  337. ssh-keygen -q -t rsa -b 4096 -N '' -f /tmp/passgen
  338. sed -n '4s/.*/root:\0/p;5s/.*/admn:\0/p' < /tmp/passgen > \
  339. /tmp/newpass
  340. chroot /mnt /usr/sbin/chpasswd < /tmp/newpass
  341. [ -x /mnt/usr/bin/ssh ] || {
  342. printf "passwords:\n"
  343. cat /tmp/newpass
  344. }
  345. }
  346.  
  347. # printf "*** prevent boot clock skew report\n"
  348. apk add coreutils findutils > /dev/null 2>&1 && {
  349. find /mnt -print0 | xargs -0 touch -c -t 0505050505.05 2> \
  350. /dev/null
  351. }
  352. true
  353. }
  354.  
  355. # helpers
  356.  
  357. host_test () {
  358. getent hosts "$@" > /dev/null 2>&1
  359. }
  360.  
  361. oncdx () {
  362. [ -d /.modloop ] || errx oncdx
  363. }
  364.  
  365. bashx () {
  366. [ "$BASH_VERSION" ] || errx bashx
  367. }
  368.  
  369. mntfx () {
  370. mount "$@" || errx mntfx "$@"
  371. }
  372.  
  373. inrpx () {
  374. cat <<- EOF > /etc/apk/repositories
  375. $1
  376. EOF
  377. apk update || errx inrpx
  378. }
  379.  
  380. pkadx () {
  381. apk add "$@" || errx pkadx "$@"
  382. }
  383.  
  384. uerrx () { # print usage before error.
  385. usage 0 && errx "$@"
  386. }
  387.  
  388. serrx () { # report sub-process error.
  389. errx strnx $?
  390. }
  391.  
  392. # data
  393.  
  394. usage () {
  395. local tab=" " out
  396. [ "$1" -ge 1 ] > /dev/null 2>&1 && out=/dev/stderr || {
  397. { [ -z "$1" ] || [ "$1" == 0 ] ; } && out=/dev/stdout || errx
  398. }
  399.  
  400. cat <<- EOF > ${out}
  401. commands for system install:
  402.  
  403. setup.sh {onln|ofln|cifs} <packages set> [with-doc]
  404. ${tab}onln: from online repository.
  405. ${tab}ofln: from cdrom repository.
  406. ${tab}cifs: from cifs repository.
  407.  
  408. assuming their repositories provides the needed packages,
  409. onln and cifs installations does'nt require anything beside
  410. this script; ofln require repository on cdrom root.
  411.  
  412. utilities:
  413.  
  414. setup.sh {psls|dkey|cimt|help}
  415. ${tab}init: only initialize keyboard and network(s).
  416. ${tab}cimt: init then mount cifs repository only.
  417. ${tab}psls: list and describe packages sets.
  418. ${tab}dkey: dump public ssh key to standard output.
  419. ${tab}help: show this help.
  420.  
  421. commands availables only in full featured environment:
  422.  
  423. setup.sh {pfls} <remote|mirror>
  424. ${tab}list packages files in remote or mirror repository.
  425.  
  426. setup.sh {srls} <remote|mirror> <pkg set> [with-doc]
  427. ${tab}list packages files on remote or mirror repository
  428. ${tab}which are locally required to install system with
  429. ${tab}packages set.
  430.  
  431. setup.sh {mkrp} <dir> <remote|mirror> <pkg set> [with-doc]
  432. ${tab}create a repository in <dir> copying packages files
  433. ${tab}which are locally required to install system with
  434. ${tab}packages set. files are copied from <remote|mirror>
  435. ${tab}repository including repository index file.
  436.  
  437. setup.sh {mkim} <out> <ofln|onln|cifs> <pkg set> [with-doc]
  438. ${tab}create iso, ofln mode not yet supported.
  439.  
  440. notes:
  441. minisys and setupsys sets deploy different systems but
  442. generates the same repository when used with mkrp
  443. since setupsys is required to deploy minisys.
  444.  
  445. the [with-doc] option try to determine and automatically
  446. install documentation packages based on the packages
  447. requested by a package set and what is effectively
  448. available in the repository, but there is no guarantee
  449. that they will be available and effectively installed.
  450. EOF
  451. [ "$1" != 0 ] && exit $1
  452. true
  453. }
  454.  
  455. errx () {
  456. local err="$1" xtv=0 ; shift
  457.  
  458. errchk () { let ++xtv && [ "$err" == "$1" ] ;}
  459. errchk usagx && usage $xtv
  460.  
  461. local hdr="abort:" out="/dev/stderr"
  462. local tab="${hdr//?/ }"
  463. errexc () { exit $xtv ; }
  464.  
  465. errchk badsx && cat <<- EOF > ${out} && errexc
  466. ${hdr} packages set error.
  467. EOF
  468. errchk bashx && cat <<- EOF > ${out} && errexc
  469. ${hdr} full featured environment required.
  470. EOF
  471. errchk repox && cat <<- EOF > ${out} && errexc
  472. ${hdr} unable to define source repository.
  473. EOF
  474. errchk cflnx && cat <<- EOF > ${out} && errexc
  475. ${hdr} lan unavailable for cifs mounting.
  476. EOF
  477. errchk inrpx && cat <<- EOF > ${out} && errexc
  478. ${hdr} packages database update failure.
  479. EOF
  480. errchk mrrfx && cat <<- EOF > ${out} && errexc
  481. ${hdr} missing required file(s) in repository.
  482. EOF
  483. errchk moipx && cat <<- EOF > ${out} && errexc
  484. ${hdr} missing or invalid parameter(s).
  485. EOF
  486. errchk oncdx && cat <<- EOF > ${out} && errexc
  487. ${hdr} no loop mount of boot cd detected,
  488. ${tab} initialization or installation
  489. ${tab} procedure unknown.
  490. EOF
  491. errchk strnx && cat <<- EOF > ${out} && errexc
  492. ${hdr} internal subprocess error: ${@}.
  493. EOF
  494. errchk xdirx && cat <<- EOF > ${out} && errexc
  495. ${hdr} file or directory already exist:
  496. ${tab} ${@}
  497. EOF
  498. errchk mntfx && cat <<- EOF > ${out} && errexc
  499. ${hdr} mount failure with parameters:
  500. ${tab} ${@}
  501. EOF
  502. errchk pkadx && cat <<- EOF > ${out} && errexc
  503. ${hdr} packages add failure with parameters:
  504. ${tab} ${@}
  505. ${tab} (missing from repository ?)
  506. EOF
  507.  
  508. errchk "$err" && cat <<- EOF > ${out}
  509. ${hdr} unknown error.
  510. EOF
  511. errexc
  512. }
  513.  
  514. pkgset () {
  515. { [ "$#" -gt 0 ] && [ "$#" -lt 3 ]; } || return
  516. [ "$1" == "ls" ] && {
  517. case "$2" in
  518. doc|def)
  519. local sets='minisys setupsys setupext configsys default'
  520. for s in $sets; do pkgset $s "ls$2" ; done ;;
  521. *) errx badsx ;;
  522. esac
  523. return
  524. }
  525.  
  526. local pset="$1" def doc ; shift
  527. case "$pset" in
  528. minisys)
  529. doc='minisys:\n\t alpine system base (boot) packages.\n';
  530. def='' ;;
  531. setupsys)
  532. doc='setupsys:\n\t add packages required by this script to\n'
  533. doc="$doc\tminisys packages set.\n"
  534. def='init_hd_set' ;;
  535. setupext)
  536. doc='setupext:\n\t add packages optionnally needed by this\n'
  537. doc="$doc\tscript to setupsys packages set.\n"
  538. def='init_hd_set system_setup_set' ;;
  539. configsys)
  540. doc='configsys:\n\t add packages configured by this script\n'
  541. doc="$doc\tto minisys packages set.\n";
  542. def=setup_support_set ;;
  543. default)
  544. doc='default:\n\t combine setupext and configsys.\n'
  545. def='init_hd_set system_setup_set setup_support_set' ;;
  546. esac
  547. [ "$doc" ] || errx badsx
  548.  
  549. case "$1" in
  550. lsdoc) printf "$doc";;
  551. lsdef) printf "$def";;
  552. '') pkgdef $def;;
  553. *) errx badsx ;;
  554. esac
  555. }
  556.  
  557. pkgdef () {
  558. while [ "$1" ]; do
  559. case "$1" in
  560. # musl to alpine-base : loaded at boot
  561. # kbd-bkeymaps : required by setup-keymap script
  562. mini_dep) cat <<- EOF
  563. musl busybox alpine-baselayout openrc alpine-conf \
  564. libressl2.6-libcrypto libressl2.6-libssl zlib apk-tools \
  565. busybox-suid busybox-initscripts scanelf musl-utils \
  566. libc-utils alpine-keys alpine-base \
  567. kbd-bkeymaps
  568. EOF
  569. ;;
  570. init_hd_set) cat <<- EOF
  571. sfdisk xfsprogs e2fsprogs btrfs-progs
  572. EOF
  573. ;;
  574. ### setup-disk script deps :
  575. # syslinux and linux-vanilla : required by setup-disk script
  576. # linux-firmware : required by linux-vanilla.
  577. # blkid, mtools and mkinitfs: required by syslinux
  578. # lddtree, kmod and cryptsetup-libs : required by mkinitfs
  579. # device-mapper-libs : required by cryptsetup-libs
  580. # xz-libs : required by kmod
  581. # acct : not needed but prevent error message in setup-disk script.
  582. ### init_hd_set deps :
  583. # libblkid and libuuid : required by blkid, sfdisk, e2fsprogs and btrfs-progs
  584. # libfdisk, libsmartcols and ncurses-* : required by sfdisk
  585. # e2fsprogs-libs : required by e2fsprogs
  586. # libcom_err : required by e2fsprogs-libs
  587. # lzo : required by btrfs-progs
  588. init_hd_dep) cat <<- EOF
  589. linux-vanilla linux-firmware \
  590. syslinux blkid mtools mkinitfs \
  591. lddtree kmod cryptsetup-libs \
  592. device-mapper-libs xz-libs \
  593. acct \
  594. libblkid libuuid \
  595. libfdisk libsmartcols \
  596. ncurses-terminfo-base \
  597. ncurses-terminfo ncurses-libs \
  598. e2fsprogs-libs libcom_err \
  599. lzo
  600. EOF
  601. ;;
  602. system_setup_set) cat <<- EOF
  603. openssh findutils coreutils
  604. EOF
  605. ;;
  606. # libacl and libattr : required by coreutils
  607. # openssh-* : required by openssh
  608. system_setup_dep) cat <<- EOF
  609. libacl libattr \
  610. openssh-client openssh-server openssh-keygen \
  611. openssh-server-common openssh-sftp-server
  612. EOF
  613. ;;
  614. setup_support_set) cat <<- EOF
  615. bash sudo openssh
  616. EOF
  617. ;;
  618. # pkgconf and readline : required by bash
  619. setup_support_dep) cat <<- EOF
  620. pkgconf readline
  621. EOF
  622. ;;
  623. esac
  624. shift
  625. done
  626. }
  627.  
  628. pubkey () {
  629. local key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDHz6HtOdGknQJvSNsFWY"
  630. key="${key}k8Yu9rKhznLVDDgzc9wZgcQOL23R/FqltWoiVVlC4siY8uXni/4AnKpAZo"
  631. key="${key}PokVRrHLo+TE1xgOdgEHszPgdDe/idTaqX8fKWvks0DglJkWu4rnHD8NfI"
  632. key="${key}Fld0Q9ECURbehBS65bw8Msi5Zm9g7FwzuCBxKfmQ4GWcJeWRO9f4VQqneE"
  633. key="${key}XA1wjsPCQdY48PQLQB8Xk9jwctHqQGQDeeJNbkSwE009Z1dThMzEexsU0g"
  634. key="${key}TOB0E01pdrN9QLuSGvfSqlCjEbiioYl2UPXpforQ/0//J6F8WX4cC7kckV"
  635. key="${key}SSJ3qOn9K65qlIF+LKcHGjqLpftemQmz1w/9cQItesoEwBYagaKncUOV72"
  636. key="${key}DMR+4/aLrUSYwR2IDGPGTt4nvfdhBzktEOXey/+DszQ+TkN4VCbChpC5Om"
  637. key="${key}UJtc4sMsDXl9rb/fkrWd1293u/GUragiSimbS6uSNfcEZOLdF82k5I6+n1"
  638. key="${key}HmE1h5aNDJMPYe1CL0CtmJ7biPd/rFilwKTwh68iquiQrcUE+qqWqrab+s"
  639. key="${key}dXiK0K5pfdq/kgUYZOI27ldNCf2Nzg3Q2scEfbu3ZexUjpEiue1MkYV3LU"
  640. key="${key}TzGZgx9O6Uq4Q/5l13lFOmG0Vphuk4njrlbqRbyB+VvfgbSuO0rFWnBvby"
  641. key="${key}najEgSEDZcPyTAi6PTcn+v2vvw== admn@alpine"
  642. printf "%s\n" "${key}"
  643. }
  644.  
  645. # let's do it
  646.  
  647. [ "$BASH_VERSION" ] || {
  648. main "$@"
  649. exit $?
  650. }
  651.  
  652. # assuming bash shell and full runtime environment from here
  653.  
  654. pkfiles () {
  655. local addr lst sedlst='/^-r/s,.*[[:blank:]]\([^[:blank:]]*\)$,\1,p'
  656. case "$1" in
  657. mirror) addr="${MIRROR}latest-stable/main/${ARCH}"
  658. lst="$(ls -l $addr | sed -n "$sedlst")" || serrx ;;
  659. remote) addr="${ONLN_REPO}${ARCH}"
  660. # getent required, not available in msys
  661. # host_test "$ONLN_HOST" || return
  662. lst="$(lftp -e 'ls;quit' $addr | sed -n "$sedlst")" || serrx ;;
  663. *) uerrx moipx ;;
  664. esac
  665. [ "$lst" ] && printf "%s" "$lst"
  666. }
  667.  
  668. pdef2fflt () {
  669. local filter="$1" ; shift
  670.  
  671. local cntchk="$1" ; shift
  672. case "$cntchk" in true|false) ;; *) uerrx moipx ;; esac
  673.  
  674. local files="$1" ; shift
  675. case "$files" in mirror|remote) ;; *) uerrx moipx ;; esac
  676.  
  677. local pdef
  678. pdef="$(pkgdef "$@")" || serrx
  679. [ "$pdef" ] || errx
  680.  
  681. files="$(pkfiles $files)" || serrx
  682. [ "$files" ] || errx
  683.  
  684. filter () { sort | uniq | sed -n -e "$1"; }
  685.  
  686. filter='s,\.,\\.,;s,.*,/^\0'"$filter"'\\.apk$/p;,p'
  687. pdef=($pdef)
  688. pdef=($(printf "%s\n" "${pdef[@]}" | filter "$filter")) || serrx
  689.  
  690. filter="${pdef[*]}"
  691. files=($files)
  692. files=($(printf "%s\n" "${files[@]}" | filter "$filter")) || serrx
  693.  
  694. $cntchk && {
  695. [ "${#pdef[@]}" == "${#files[@]}" ] || errx mrrfx
  696. }
  697.  
  698. printf "%s\n" "${files[@]}"
  699. }
  700.  
  701. pdef2docs () {
  702. pdef2fflt '-doc-[[:digit:]].*' false "$@"
  703. }
  704.  
  705. pdef2files () {
  706. pdef2fflt '-[[:digit:]].*' true "$@"
  707. }
  708.  
  709. pset2repofiles () {
  710. local source="$1" ; shift
  711. case "$source" in mirror|remote) ;; *) uerrx moipx ;; esac
  712.  
  713. pkgset "$1" lsdoc > /dev/null || errx badsx
  714.  
  715. [ "$#" -le 2 ] || uerrx moipx
  716. case "$2" in with-doc|'') ;; *) uerrx moipx ;; esac
  717.  
  718. local def
  719. def=(mini_dep init_hd_set $(pkgset "$1" lsdef)) || serrx
  720.  
  721. local ssetm='\([^[:blank:]]\+\)_set\([[:blank:]]\|$\)'
  722. local saddd='\1_set \1_dep\2'
  723. def=($(printf "%s\n" "${def[@]}" | sed "s,$ssetm,$saddd,g")) || serrx
  724.  
  725. files=($(pdef2files $source "${def[@]}")) || serrx
  726. [ "$2" ] && { files+=($(pdef2docs $source "${def[@]}")) || serrx ; }
  727.  
  728. printf "%s\n" "${files[@]}"
  729. }
  730.  
  731. makerepo () {
  732. local dest="$1" ; shift
  733. [ -e "$dest" ] && errx xdirx "$dest"
  734.  
  735. local files
  736. files=(APKINDEX.tar.gz $(pset2repofiles "$@")) || serrx
  737.  
  738. mkdir "$dest" "${dest/#\//}/${ARCH}" || errx
  739. dest="${dest/#\//}/${ARCH}"
  740.  
  741. case "$1" in
  742. mirror)
  743. files=("${files[@]/#/${MIRROR}latest-stable/main/${ARCH}/}")
  744. cp -t "$dest" "${files[@]}" || errx ;;
  745. remote)
  746. files="mget -O ${dest} ${files[*]}; quit"
  747. lftp -e "${files}" ${ONLN_REPO}${ARCH}/ || errx ;;
  748. esac
  749. }
  750.  
  751. # makeiso
  752.  
  753. function ok { printf '[done]\n'; }
  754. function ko { printf '[fail]\n'; }
  755.  
  756. function fail {
  757. ko
  758. local garbage
  759. printf "%s\n" "$@"
  760. printf 'press a key to exit\n'
  761. read -s -N1 garbage
  762. exit 1
  763. }
  764.  
  765. function onfail { ((!$?)) && ok || fail "$@"; }
  766.  
  767. makeiso () {
  768. [ "$1" ] || usage 1
  769. [ "$1" != "${1%/}" ] && fail 'bad output filename' "$1"
  770.  
  771. case "$2" in
  772. ofln|onln|cifs) true ;;
  773. *) fail 'bad install mode parameter' "$2" ;;
  774. esac
  775.  
  776. case "$3" in
  777. minisys|setupsys|setupext|configsys|default) true ;;
  778. *) fail 'bad packages set parameter' "$3" ;;
  779. esac
  780.  
  781. case "$4" in
  782. with-doc|"") true ;;
  783. *) fail 'invalid parameter' "$4" ;;
  784. esac
  785.  
  786. set +H || fail 'disable history substitution...'
  787. shopt -s globstar || fail 'enable bash globstar option...'
  788.  
  789. local pad=' '
  790. local rels_f=''
  791. local init_f=init
  792. local irfs_f=/boot/initramfs-vanilla
  793.  
  794. local iso_output_dir iso_output
  795. iso_output="$(realpath -m "$1")"
  796. iso_output_dir="$(dirname "$iso_output")"
  797. ! [ -a "$iso_output" ] || fail 'already exist' "$iso_output"
  798. [ -d "$iso_output_dir" ] || fail 'directory does not exist' "$iso_output_dir"
  799. shift
  800.  
  801. local releases_d="$(dirname $MIRROR)/02-releases"
  802. [ -d "$releases_d" ] || fail 'lookup releases directory'
  803.  
  804. local tmpwd
  805. tmpwd=$(mktemp -d "/tmp/isoset-tmpwd-XXXXXX")
  806. ((!$?)) || fail 'create temporary working directory:' "$pad$tmpwd"
  807.  
  808. local tmpwf
  809. tmpwf=$(mktemp "$tmpwd/tmpwf-XXXXXX.gz")
  810. ((!$?)) || fail 'create temporary file:' "$pad$tmpwf"
  811.  
  812. cd "$tmpwd" || fail "unknow error when cwd to $tmpwd"
  813.  
  814. printf 'lookup release file in:\n%s%s\n' "$pad" "$releases_d"
  815. printf -v rels_f "%s" $releases_d/alpine-vanilla-*-x86_64.iso
  816. [ -r "$rels_f" ] && printf 'using\n%s%s\n' "$pad" "$rels_f"
  817. onfail 'not a regular file' "$pad$rels_f"
  818.  
  819. printf 'lookup initramfs in:\n%s%s\n' "$pad" "$rels_f"
  820. xorriso -abort_on warning -indev $rels_f -ls $irfs_f &> /dev/null
  821. onfail 'unable to find' "$pad$irfs_f" 'in' "$pad$rels_f"
  822.  
  823. printf 'extract initramfs from iso to:\n%s%s\n' "$pad" "$tmpwf"
  824. xorriso -osirrox on -indev $rels_f -extract_single $irfs_f $tmpwf &> /dev/null
  825. onfail 'unable to extract' "$pad$irfs_f" 'from' "$pad$rels_f"
  826.  
  827. printf 'uncompress:\n%s%s\nto:\n%s%s\n' "$pad" "$tmpwf" "$pad" "${tmpwf%.gz}"
  828. gzip -d "$tmpwf" && ok || fail
  829.  
  830. tmpwf="${tmpwf%.gz}"
  831. [ -r "$tmpwf" ] || fail 'unknow error'
  832.  
  833. # MEMO: when using with msys2
  834. # trying to extract full initramfs to rebuild it rely on
  835. # filesystem symlinks ability which may be unavailable.
  836.  
  837. printf 'extract %s script...\n' "$init_f"
  838. cpio -i -u "$init_f" < "$tmpwf" && [ -r "$init_f" ]
  839. onfail 'error extracting script' "$pad$init_f" 'from' "$pad$tmpwf"
  840.  
  841. printf 'patch %s script...\n' "$init_f"
  842. lnum=( $(grep -n 'exec /bin/busybox switch_root' "$init_f" | cut -d ':' -f 1) )
  843. (( ${#lnum[@]} == 2 && lnum[0] > 2 && lnum[1] > 2))
  844. ((!$?)) || fail 'unexpected file format' "$pad$init_f"
  845. sed -n "1,$((${lnum[0]}-1))p" "$init_f" > "${init_f}.intro"
  846. sed -n "${lnum[0]},$((${lnum[1]}-1))p" "$init_f" > "${init_f}.inter"
  847. sed -n "${lnum[1]},\$p" "$init_f" > "${init_f}.eof"
  848. cat <<- EOF > "${init_f}.mod"
  849. if [ -r /sysroot/media/cdrom/setup/sysroot-prep.rc ]
  850. then
  851. source /sysroot/media/cdrom/setup/sysroot-prep.rc
  852. fi
  853. EOF
  854.  
  855. cat "${init_f}"{.intro,.mod,.inter,.mod,.eof} > "${init_f}"
  856. onfail 'error patching script' "$pad$init_f" 'from' "$pad$tmpwf"
  857.  
  858. printf 'build %s script cpio archive...\n' "$init_f"
  859. cpio -o --format=newc > "${init_f}.cpio" <<- EOF
  860. $init_f
  861. EOF
  862. onfail 'error building' "$pad${init_f}.cpio" 'from' "$pad$init_f"
  863.  
  864. printf 'build new initramfs...\n'
  865. cat "$tmpwf" "${init_f}.cpio" > ./initramfs-vanilla && \
  866. gzip -9 ./initramfs-vanilla &&
  867. mv ./initramfs-vanilla.gz ./initramfs-vanilla
  868. onfail 'error building new initramfs'
  869.  
  870. mkdir ./setup
  871. cat <<- S0EOF > ./setup/sysroot-prep.rc
  872. #!/bin/sh
  873. # MEMO: this is a busybox ash script.
  874. # exec /bin/busybox sh # for testing.
  875. cat <<- S1EOF > /sysroot/etc/local.d/cdsetup.start
  876. #!/bin/sh
  877. if [ -r /media/cdrom/setup/setup-prep.rc ]; then
  878. nohup /bin/sh /media/cdrom/setup/setup-prep.rc &
  879. fi
  880. S1EOF
  881. chmod 755 /sysroot/etc/local.d/cdsetup.start
  882. sed -i 's/^tty1/#tty1/' /sysroot/etc/inittab
  883. echo 'cdrom setup generated as local service and tty1 disabled'
  884. sync
  885. chroot /sysroot/ /sbin/rc-update add local default
  886. S0EOF
  887.  
  888. cat <<- S0EOF > ./setup/setup-prep.rc
  889. #!/bin/sh
  890. # MEMO: this is a busybox ash script.
  891. sleep 2 # let local service init terminate.
  892. exec > /dev/tty1
  893. exec < /dev/tty1
  894. exec 2> /dev/tty1
  895. echo
  896. echo 'restore tty1 and remove cdrom setup local service'
  897. echo
  898. sed -i 's/^#tty1/tty1/' /etc/inittab
  899. rm -f /etc/local.d/cdsetup.start
  900. echo 'press any key within 3 seconds to prevent auto setup...'
  901. sync
  902. read -s -t 3 -n 1
  903. [ \$? == 0 ] && {
  904. echo 'auto setup aborted, use alternate tty to login!'
  905. exit
  906. }
  907. echo 'starting auto setup...'
  908. /media/cdrom/setup/setup.sh $@
  909. sync
  910. echo 'auto setup done, reboot...'
  911. sync
  912. reboot
  913. S0EOF
  914.  
  915. [ -r "$(dirname $MIRROR)/setup.sh" ] | fail 'missing setup.sh script'
  916. cp "$(dirname $MIRROR)/setup.sh" ./setup/
  917.  
  918. # TODO ofln mode support (repository) using makerepo function
  919. # this work manually just need to add option to select mirror
  920. # (online vs local) and add repository graft point to iso.
  921.  
  922. printf 'generate new iso...\n'
  923. xorrisofs.exe -dev "$rels_f" --for_backup \
  924. -output "$iso_output" -l -J -R \
  925. -b boot/syslinux/isolinux.bin \
  926. -c boot/syslinux/boot.cat \
  927. -boot-info-table -no-emul-boot \
  928. -boot-load-size 4 \
  929. -graft-points \
  930. /boot/initramfs-vanilla=initramfs-vanilla \
  931. /setup=./setup
  932. onfail 'unknow error'
  933.  
  934. # cleanup
  935. rm "$tmpwf" "${init_f}"{,.intro,.mod,.inter,.eof,.cpio}
  936. rm ./initramfs-vanilla ./setup/{sysroot-prep.rc,setup-prep.rc,setup.sh}
  937. rmdir ./setup
  938. cd "$OLDPWD"
  939. rmdir "$tmpwd"
  940.  
  941. }
  942.  
  943. do_test () {
  944. printf "nothing to test right now!\nargs:"
  945. printf " %s" "$@" $'\n' '----------' $'\n'
  946. false
  947. }
  948.  
  949. main "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement