<!-- $NetBSD: chap-misc.xml,v 1.6 2019/04/20 08:08:21 maya Exp $ -->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>
<!DOCTYPE chapter [
<!ENTITY os "NetBSD">
<!ENTITY rprompt "#">
] >

<chapter id="chap-misc">
  <title>Miscellaneous operations</title>

  <para>This chapter collects various topics, in sparse order</para>

  <sect1 id="chap-misc-bootmanager">
    <title>Installing the boot manager</title>

    <para>Sysinst, the &os; installation program usually installs the &os;
      boot manager on the hard disk.
      The boot manager can also be installed or reconfigured at a later
      time, if needed, with the <command>fdisk</command> command.
      For example:</para>

    <screen>&rprompt; <userinput>fdisk -B wd0</userinput></screen>

    <para>If &os; doesn't boot from the hard disk, you can boot it from
      the installation floppy and start the kernel on the hard disk.
      Insert the installation disk and, at the boot prompt, give the
      following command:</para>

    <screen><prompt>&gt;</prompt> <userinput>boot wd0a:netbsd</userinput></screen>

    <para>This boots the kernel on the hard disk (use the correct device,
      for example sd0a for a SCSI disk).</para>

    <note>
      <para>Sometimes <command>fdisk -B</command> doesn't give the expected
        result (at least it happened to me), probably if you
        install/remove other operating systems.
        In this case, try running <command>fdisk -i</command> and
        then run again <command>fdisk</command> from &os;.</para>
    </note>
  </sect1>

  <!-- ============================================================= -->

  <sect1 id="chap-misc-delete-disklabel">
    <title>Deleting the disklabel</title>

    <para>Though this is not an operation that you need to perform
      frequently, it can be useful to know how to do it in case of
      need.
      Please be sure to know exactly what you are doing before
      performing this kind of operation.
      For example:</para>

    <screen>&rprompt; <userinput>dd if=/dev/zero of=/dev/rwd0c bs=8k count=1</userinput></screen>

    <para>The previous command deletes the disklabel (not the MBR partition
      table).
      To completely delete the disk, the whole device <filename>rwd0d</filename>
      must be used.
      For example:</para>

    <screen>&rprompt; <userinput>dd if=/dev/zero of=/dev/rwd0d bs=8k</userinput></screen>
    <para>The commands above will only work as expected on the i386 and amd64 ports
    of &os;. On other ports, the whole device will end in c, not d (e.g.
    <filename>rwd0c</filename>).</para>
  </sect1>

  <!-- ============================================================= -->

  <sect1 id="chap-misc-speaker">
    <title>Speaker</title>

    <para>To output a sound from the speaker (for example at the end of a
      long script) the <emphasis>spkr</emphasis> driver can be used in
      the kernel config, which is mapped on
      <filename>/dev/speaker</filename>. For example:</para>

    <screen>echo 'BPBPBPBPBP' &gt; /dev/speaker</screen>

    <note>
      <para>The <emphasis>spkr</emphasis> device is not enabled in the
        generic kernel; a customized kernel is needed.</para>
    </note>

  </sect1>

  <!-- ============================================================= -->

  <sect1 id="chap-misc-forgot-root-password">
    <title>Forgot root password?</title>

    <para>If you forget root's password, not all is lost and you can still
      recover the system with the following steps: boot
      single user, mount / and change root's password.
      In detail:</para>

    <orderedlist>
      <listitem>
	<para>Boot single user: when the boot prompt appears and the five
	  seconds countdown starts, give the following command:</para>

        <screen><prompt>&gt;</prompt> <userinput>boot -s</userinput></screen>
      </listitem>

      <listitem>
	<para>At the following prompt</para>

        <screen>Enter pathname of shell or RETURN for sh:</screen>

        <para>press Enter.</para>
      </listitem>

      <listitem>
	<para>Write the following commands:</para>

        <screen>&rprompt; <userinput>fsck -y /</userinput>
&rprompt; <userinput>mount -u /</userinput>
&rprompt; <userinput>fsck -y /usr</userinput>
&rprompt; <userinput>mount /usr</userinput></screen>
      </listitem>

      <listitem>
	<para>Change root's password:</para>

	<screen>&rprompt; <userinput>passwd root</userinput>
Changing local password for root.
New password: <userinput>(not echoed)</userinput>
Retype new password: <userinput>(not echoed)</userinput>
&rprompt; </screen>
      </listitem>

      <listitem>
	<para>Exit the shell to go to multiuser
	  mode.</para>

	<screen>&rprompt; <userinput>exit</userinput></screen>
      </listitem>
    </orderedlist>
    <para>If you get the error <quote>Password file is busy</quote>,
    please see the section below.</para>
  </sect1>

  <!-- ============================================================= -->

  <sect1 id="chap-misc-password-file-busy">
    <title>Password file is busy?</title>

    <para>If you try to modify a password and you get the mysterious
      message <quote>Password file is busy</quote>, it probably
      means that the file <filename>/etc/ptmp</filename> has not
      been deleted from the system.  This file is a temporary copy
      of the <filename>/etc/master.passwd</filename> file; check
      that you are not losing important information and then
      delete it:</para>

    <screen> &rprompt; <userinput>rm /etc/ptmp</userinput> </screen>

    <note>
      <para>If the file <filename>/etc/ptmp</filename> exists you can
	also receive a warning message at system startup.  For
	example:</para>

      <screen>root: password file may be incorrect - /etc/ptmp exists</screen>
    </note>
  </sect1>

  <!-- ============================================================= -->

  <sect1 id="chap-misc-adding-new-disk">
    <title>Adding a new hard disk</title>

    <para>This section describes how to add a new hard disk to an
      already working &os; system.</para>

    <sect2>
      <title>
        A new SATA disk and GPT
      </title>

      <para>
        In the following example a new SATA hard disc will be added.
      </para>

      <para>
        When the SATA disk is physically plugged in to the SATA port,
        it is time to restart the computer and check that the device
        is correctly detected, using the <command>dmesg</command>
	command.
	This example kernel message is with a Seagate Barracuda drive:
      </para>
<screen><computeroutput>wd1 at atabus0 drive 1: &lt;ST380013AS&gt;
wd1: drive supports 16-sector PIO transfers, LBA48 addressing
wd1: 76293 MB, 155010 cyl, 16 head, 63 sec, 512 bytes/sect x 156250080 sectors
wd1: 32-bit data port
wd1: drive supports PIO mode 4, DMA mode 2, Ultra-DMA mode 6 (Ultra/133)
</computeroutput></screen>

      <para>
        Now the partitions can be created using the <command>gpt</command>
	command.
	First, check the current status of the disk:
      </para>

<screen><computeroutput>&rprompt; </computeroutput><userinput>gpt show wd1
</userinput><computeroutput>start         size  index  contents
         0  156250080
</computeroutput></screen>

      <para>
        In this example the hard disk is blank, with no partition tables or
        disk labels.
        To create, for example, an EFI System Partition followed by a &os;
        partition, the commands are:
      </para>

<screen><computeroutput>&rprompt; </computeroutput><userinput>gpt create wd1
</userinput><computeroutput>&rprompt; </computeroutput><userinput>gpt add -t efi -l EFI -s 1m wd1
</userinput><computeroutput>Partition 1 added, use:
  dkctl wd1 addwedge &lt;wedgename&gt; xxx yyyyyyy &lt;type&gt;
to create a wedge for it
&rprompt; </computeroutput><userinput>gpt add -t ffs -l netbsd-data wd1
</userinput><computeroutput>Partition 2 added, use:
  dkctl wd1 addwedge &lt;wedgename&gt; xxx yyyyyyy &lt;type&gt;
to create a wedge for it
</computeroutput> </screen>

      <para>
        For simplicity, tell &os; to automatically re&#x2010;make the
	disk wedges for the newly defined partitions.
      </para>

<screen><computeroutput>&rprompt; </computeroutput><userinput>dkctl wd1 makewedges
</userinput><computeroutput></computeroutput> </screen>

      <para>
	Let us assume that the resultant disk wedges are <filename>dk2</filename> 
	and <filename>dk3</filename>.
        The final operation is the creation of the file systems for
        the newly defined partitions.
      </para>

<screen><computeroutput>&rprompt; </computeroutput><userinput>newfs_msdos -F 32 -L EFISYS dk2
</userinput><computeroutput>
&rprompt; </computeroutput><userinput>newfs -O2ea dk3
</userinput><computeroutput>
</computeroutput> </screen>

      <para>
        The disk is now ready for usage, and the partitions can
        be mounted.
	For example:
      </para>

<screen><computeroutput>&rprompt; </computeroutput><userinput>mount /dev/dk3 /mnt
</userinput><computeroutput>
&rprompt; </computeroutput><userinput>mount /dev/dk2 /boot/efi
</userinput></screen>

    </sect2>
    <sect2>
    <title>A new SCSI disk and disklabel</title>

    <para> In the following example a
      new SCSI controller and a new hard disk, connected to the
      controller, will be added.  If you don't need to add a new
      controller, skip the relevant part and go to the hard disk
      configuration.  The installation of an IDE hard disk is
      identical; only the device name will be different
      (<filename>wd#</filename> instead of
      <filename>sd#</filename>).</para>

    <para>As always, before buying new hardware, consult the hardware
      compatibility list of &os; and make sure that the new device
      is supported by the system.</para>

    <para>When the SCSI controller has been physically installed in the
      system and the new hard disk has been connected, it's time to
      restart the computer and check that the device is correctly
      detected, using the <command>dmesg</command> command.  This
      is the sample output for an NCR-875 controller:</para>

    <screen>ncr0 at pci0 dev 15 function 0: ncr 53c875 fast20 wide scsi
ncr0: interrupting at irq 10
ncr0: minsync=12, maxsync=137, maxoffs=16, 128 dwords burst, large dma fifo
ncr0: single-ended, open drain IRQ driver, using on-chip SRAM
ncr0: restart (scsi reset).
scsibus0 at ncr0: 16 targets, 8 luns per target
sd0(ncr0:2:0): 20.0 MB/s (50 ns, offset 15)
sd0: 2063MB, 8188 cyl, 3 head, 172 sec, 512 bytes/sect x 4226725 sectors</screen>

    <para>If the device doesn't appear in the output, check that it is
      supported by the kernel that you are using; if necessary,
      compile a customized kernel (see  <xref
      linkend="chap-kernel" />).</para>

    <para>Now the partitions can be created using the <command>fdisk</command>
      command.  First, check the current status of the disk:</para>

    <screen>&rprompt; <userinput>fdisk sd0</userinput>
NetBSD disklabel disk geometry:
cylinders: 8188 heads: 3 sectors/track: 172 (516 sectors/cylinder)

BIOS disk geometry:
cylinders: 524 heads: 128 sectors/track: 63 (8064 sectors/cylinder)

Partition table:
0: sysid 6 (Primary 'big' DOS, 16-bit FAT (> 32MB))
    start 63, size 4225473 (2063 MB), flag 0x0
        beg: cylinder    0, head   1, sector  1
        end: cylinder  523, head 127, sector 63
1: &lt;UNUSED&gt;
2: &lt;UNUSED&gt;
3: &lt;UNUSED&gt;</screen>

    <para>In this example the hard disk already contains a DOS
      partition, which will be deleted and replaced with a native
      &os; partition.  The command <command>fdisk -u sd0</command>
      allows to modify interactively the partitions.  The modified
      data will be written on the disk only before exiting and fdisk
      will request a confirmation before writing, so you can work
      relaxedly.</para>

    <sidebar>
      <title>Disk geometries</title>

      <para>The geometry of the disk reported by fdisk can appear
	confusing.  Dmesg reports 4226725 sectors with 8188/3/172
	for C/H/S, but 8188*3*172 gives 4225008 and not 4226725.
	What happens is that most modern disks don't have a fixed
	geometry and the number of sectors per track changes
	depending on the cylinder: the only interesting parameter
	is the number of sectors.  The disk reports the C/H/S
	values but it's a fictitious geometry: the value 172 is the
	result of the total number of sectors (4226725) divided by
	8188 and then by 3.</para>

      <para>To make things more confusing, the BIOS uses yet another
	<quote>fake</quote> geometry (C/H/S 524/128/63) which gives
	a total of 4225536, a value which is a better approximation
	to the real one than 425008.  To partition the disk we will
	use the BIOS geometry, to maintain compatibility with other
	operating systems, although we will lose some sectors
	(4226725 - 4225536 = 1189 sectors = 594 KB).</para>
    </sidebar>

    <para>To create the BIOS partitions the command <command>fdisk -u</command>
      must be used; the result is the following:</para>

      <screen>Partition table:
0: sysid 169 (NetBSD)
    start 63, size 4225473 (2063 MB), flag 0x0
        beg: cylinder    0, head   1, sector  1
        end: cylinder  523, head 127, sector 63
1: &lt;UNUSED&gt;
2: &lt;UNUSED&gt;
3: &lt;UNUSED&gt;</screen>

    <para>Now it's time to create the disklabel for the &os;
      partition.  The correct steps to do this are:</para>

    <screen>&rprompt; <userinput>disklabel sd0 > tempfile</userinput>
&rprompt; <userinput>vi tempfile</userinput>
&rprompt; <userinput>disklabel -R -r sd0 tempfile</userinput></screen>

    <sidebar>
      <para>If you try to create the disklabel directly with</para>

      <screen>&rprompt; <userinput>disklabel -e sd0</userinput></screen>

      <para>you get the following message</para>

      <screen>disklabel: ioctl DIOCWDINFO: No disk label on disk;
use "disklabel -I" to install initial label</screen>

      <para>because the disklabel does not yet exist on the disk.</para>
    </sidebar>

    <para>Now we create some disklabel partitions, editing the
      <filename>tempfile</filename> as already explained.  The
      result is:</para>

    <programlisting>#      size   offset   fstype [fsize bsize   cpg]
a:  2048004       63   4.2BSD   1024  8192    16 # (Cyl.  0*- 3969*)
c:  4226662       63   unused      0     0       # (Cyl.  0*- 8191*)
d:  4226725        0   unused      0     0       # (Cyl.  0 - 8191*)
e:  2178658  2048067   4.2BSD   1024  8192    16 # (Cyl.  3969*- 8191*)</programlisting>

    <note>
      <para>When the disklabel has been created it is possible to
	optimize it studying the output of the command
	<command>newfs -N /dev/rsd0a</command>, which warns about
	the existence of unallocated sectors at the end of a
	disklabel partition.  The values reported by newfs can be
	used to adjust the sizes of the partitions with an
	iterative process.</para>
    </note>

    <para>The final operation is the creation of the file systems for
      the newly defined partitions (<emphasis>a</emphasis> and
      <emphasis>e</emphasis>).</para>

    <screen>&rprompt; <userinput>newfs /dev/rsd0a</userinput>
&rprompt; <userinput>newfs /dev/rsd0e</userinput></screen>

    <para>The disk is now ready for usage, and the two partitions can
      be mounted.  For example:</para>

    <screen>&rprompt; <userinput>mount /dev/sd0a /mnt</userinput></screen>

    <para>If this succeeds, you may want to put an entry for the partition
      into <filename>/etc/fstab</filename>.</para>
    </sect2>
  </sect1>

  <!-- ============================================================= -->

  <sect1 id="chap-misc-howto-rebuild-devices">
    <title>How to rebuild the devices in /dev</title>

    <para>First shutdown to single user, partitions still mounted
      <quote>rw</quote>
      (read-write); You can do that by just typing <command>shutdown
      now</command> while you are in multi user mode, or reboot with
      the <option>-s</option> option and make <filename>/</filename>
      and <filename>/dev</filename> read-writable by doing.</para>

    <screen>&rprompt; <userinput>mount -u /</userinput>
&rprompt; <userinput>mount -u /dev</userinput></screen>

    <para>Then:</para>

    <screen>&rprompt; <userinput>mkdir /newdev</userinput>
&rprompt; <userinput>cd /newdev</userinput>
&rprompt; <userinput>cp /dev/MAKEDEV* .</userinput>
&rprompt; <userinput>sh ./MAKEDEV all</userinput>
&rprompt; <userinput>cd /</userinput>
&rprompt; <userinput>mv dev olddev</userinput>
&rprompt; <userinput>mv newdev dev</userinput>
&rprompt; <userinput>rm -r olddev</userinput></screen>

    <para>Or if you fetched all the sources in
      <filename>/usr/src</filename>:</para>

    <screen>&rprompt; <userinput>mkdir /newdev</userinput>
&rprompt; <userinput>cd /newdev</userinput>
&rprompt; <userinput>cp /usr/src/etc/MAKEDEV.local .</userinput>
&rprompt; <userinput>( cd /usr/src/etc ; make MAKEDEV )</userinput>
&rprompt; <userinput>cp /usr/src/etc/obj*/MAKEDEV .</userinput>
&rprompt; <userinput>sh ./MAKEDEV all</userinput>
&rprompt; <userinput>cd /</userinput>
&rprompt; <userinput>mv dev olddev; mv newdev dev</userinput>
&rprompt; <userinput>rm -r olddev</userinput></screen>

    <para>You can determine $arch by</para>

    <screen>&rprompt; <userinput>uname -m</userinput></screen>

    <para>or</para>

    <screen>&rprompt; <userinput>sysctl hw.machine_arch</userinput></screen>
  </sect1>
</chapter>
