#!/bin/zsh -f setopt sh_word_split target=$1 gptstart=34 EFIsz=131038 # 64MB - 34blk DOS_MB=512 nbroot_MB=1024 disks=`sysctl -n hw.disknames` ffsdevs=`df -htffs|awk '/\/dev\// {print $1}'` #echo ffsdevs="$ffsdevs" checkused() { local d dk=`dkctl $1 listwedges 2>/dev/null|grep '^dk[0-9][0-9]*:'|cut -d: -f1` for d in $1 $dk; do echo "$ffsdevs" | fgrep -wq $d && return 0 done return 1 } delwedges() { local d dk=`dkctl $1 listwedges 2>/dev/null|grep '^dk[0-9][0-9]*:'|cut -d: -f1` for d in $dk; do dkctl $1 delwedge $d done gpt destroy $1 } dklabel() { # $1=disk $2=index (eg. dk wd0 3) :<<_EOC_ /dev/rwd0d: 5 wedges: dk5: EFI system, 131038 blocks at 34, type: efi dk6: DOS, 524288 blocks at 131072, type: windows dk7: _root, 1048576 blocks at 655360, type: ffs dk8: swap, 524288 blocks at 1703936, type: swap dk9: _data, 1071513567 blocks at 2228224, type: ffs _EOC_ dkctl $1 listwedges|tail +2|sed -n "${2}s/dk.*: \([^,]*\),.*/\1/p" } dkname() { # $1=PhysDev $2=index (eg. dkname wd0 2) dkctl $1 listwedges|tail +2|sed -n "${2}s/:.*//p" } gpt_add() { # $1=disk $2=type $3=size $4=label label=$4 while ! gpt add -t $2 ${3:+-s} ${3:+$3} ${label:+"-l"} ${label:+"$label"} $1 do label=_$label done trial=3 gpt show -l $1 | grep -- "- \"$label\"" | while read start size rest; do while ! dkctl $1 addwedge "$label" $start $size $2; do label=_$label if [ $((--trial)) -le 0 ]; then echo "Cannot set correct label to $1:$2" >&2; exit 3 fi done #echo ::: dkctl $1 addwedge "$label" $start $size $2 done } if [ -z "$target" ]; then installable="" echo '========== Install Learning NetBSD system ==========' echo ' Available disks are as follows:' for d in ${=disks}; do case "$d" in [ws]d*|vnd*) if ! checkused $d; then grep "^${d}: .*sectors" /var/run/dmesg.boot installable="$installable${installable:+ }$d" fi ;; *) ;; esac done echo '=================================================' if [ -n "$installable" ]; then echo Choose install target DISK: >&2 select i in ${=installable}; do case "$i" in "") ;; *) target=$i; break ;; esac done echo $target is selected fi fi if checkused $target; then echo "Disk $target is in used" >&2 exit 1 fi if [ -z "$target" ]; then echo "Usage: $0 [targetdisk]" >&2 exit 0 fi echo -n Configure target = $target: "Sure? " read x case "$x" in [Nn]*) exit 0 ;; esac # # Create GPT # dd if=/dev/zero of=/dev/r${target}d bs=1m count=1 delwedges $target gpt destroy $target gpt create $target DOSsz=$((DOS_MB*1024**2/512)) NBROOTsz=$((nbroot_MB*1024**2/512)) swapsz=${SWAPSZ:-$DOSsz} lb_dos="${target}DOS" lb_root="${target}root" lb_swap="${target}swap" lb_data="${target}data" # EFI gpt_add $target efi $EFIsz 'EFI system' # FAT data storage gpt_add $target windows $DOSsz "$lb_dos" # 3. NetBSD root partition gpt_add $target ffs $NBROOTsz "$lb_root" echo ::: dkctl $target addwedge root $((gptstart+EFIsz+DOSsz)) $NBROOTsz ffs # 4. NetBSD swap partition gpt_add $target swap $swapsz "$lb_swap" # 5. NetBSD usr partition gpt_add $target ffs '' "$lb_data" # # Collect label names of index 3,4,5 # p_dos=`dklabel $target 2` p_root=`dklabel $target 3` p_swap=`dklabel $target 4` p_usr=`dklabel $target 5` # format and copy dev_efi=`dkname $target 1` dev_dos=`dkname $target 2` dev_root=`dkname $target 3` newfs_msdos -F 16 /dev/r$dev_efi newfs_msdos -F 16 /dev/r$dev_dos newfs -O 2 NAME=$p_root newfs -O 2 NAME=$p_usr m=/mnt finalize() { umount $m/usr; umount $m/Windows; umount $m } trap finalize INT QUIT TERM HUP # Copy efi images mount_msdos /dev/$dev_efi $m && { mkdir -p $m/EFI/boot cp /usr/mdec/*.efi $m/EFI/boot umount $m } # Calculate disk usage from df totaldu=`df -mtffs|awk '{sum+=$3}END{print sum}'`m # Copy other whole files mount -o log,noatime NAME=$p_root $m && { mkdir $m/usr $m/Windows mount -o log,noatime NAME=$p_usr $m/usr && { (cd /; tar lcf - . usr | progress -l $totaldu tar xpfC - $m) echo -n Synching...; sync; echo . Done umount $m/usr } # produce fstab cat<<-_EOF_ > $m/etc/fstab # fstab generated from `basename $0` NAME=$p_root / ffs rw,log 1 1 NAME=$p_swap none swap sw,dp 0 0 NAME=$p_usr /usr ffs rw,log 1 2 NAME=$p_dos /Windows msdos rw,-l,noexec tmpfs /tmp tmpfs rw,-sram%25 ptyfs /dev/pts ptyfs rw kernfs /kern kernfs rw procfs /proc procfs rw linpro /emul/linux/proc procfs rw,linux tmpfs /var/shm tmpfs rw,-sram%25 /usr/home /home null rw _EOF_ cat $m/etc/fstab umount $m gpt biosboot -i 3 $target gpt set -a bootme -i 3 $target installboot -v /dev/r$dev_root /usr/mdec/bootxx_ffsv2 # Final: automatic reboot count=6 echo -n "(Ctrl-C to quit): Reboot now? $count" while [ $count -gt 0 ]; do sleep 1 echo -n "\b$((--count))" done echo -n "\b " /sbin/shutdown -r now }