Mounting usb drives in freebsd 11. File system. Automatic mounting of NFS file systems

You can change the access rights and owner of files and directories in using the commands chmod And chown. Mask for setting rights to created files, can be changed globally, in /etc/profile for Linux and /etc/login.conf for FreeBSD. Usually, the default mask 022 . Meaning umask subtracted from 777 , so permissions will matter 755 . exec - execution allowed read - read permission write - write permission SUID bit - the file attribute, in conjunction with the executable file attribute, allows the file to be executed to be executed with the effective UID of the file's owner, rather than the one running the file 1 --x execute # Permissions 764 = exec/read/write | read/write | read 2 -w- write # For: |-- Owner --| |-Group-| |Oth| 4 r-- read ugo=a u=user, g=group, o=others, a=everyone# chmod MODE[,MODE] FILE # MODE has the form: *([-+=]()) # chmod 640 /var/log/maillog # Set permissions to equal -rw-r----- # chmod u=rw,g=r,o= /var/log/maillog # Same as above # chmod -R o-r /home/* # Recursively change permissions, disallow reading for Other # chmod u+s /path/to/prog # Install SUID bit per executable file (be careful here, you must understand what you are doing)# find / -perm -u+s -print # Find all programs with installed SUID bit# chown user:group /path/to/file # Set the user and group as the owner of the file# chgrp group /path/to/file # Change the group that owns the file# chmod 640 `find ./ -type f -print` # Change permissions to 640 for all files# chmod 751 `find ./ -type d -print` # Change permissions to 751 for all directories

Disk Information

# diskinfo -v /dev/ad2 # View disk information ( sector/size) FreeBSD# hdparm -I /dev/sda # Information about IDE/ATA disk (Linux)# fdisk /dev/ad2 # Show change disk partitions# smartctl -a /dev/ad2 # Show SMART disk information

Loading

FreeBSD

To load the old kernel in an emergency situation, for example after an unsuccessful build and installation of a new one, stop the download by pressing 6 during the countdown to get to the command line prompt. # unload # load kernel.old # boot

Mount points, disk usage

#mount | column -t # Show mounted filesystems#df # Show quantity free space and mounted devices# cat /proc/partitions # Show all registered partitions (Linux)

Directory information

# du -sh * # Directory sizes as a list# du -csh # Total volume of the current directory# du -ks * | sort -n -r # List of directories sorted by size in kilobytes# ls -lSr # List of directories, reverse sorting

Who opened which files

Sometimes it is necessary to find out which file has locked a partition, causing the command umount gives the corresponding error. # umount /home/ umount: unmount of /home # It is not possible to unmount a partition until /home blocked failed: Device busy

FreeBSD and most Unix-like systems

# fstat -f /home # for mount point# fstat -p PID # for application with PID# fstat -u user # for username Find open file For Xorg: # ps ax | grep Xorg | awk "(print $1)" 1252 # fstat -p 1252 USER CMD PID FD MOUNT INUM MODE SZ|DV R/W root Xorg 1252 root / 2 drwxr-xr-x 512 r root Xorg 1252 text /usr 216016 -rws-- x--x 1679848 r root Xorg 1252 0 /var 212042 -rw-r--r-- 56987 w Find file with inum 212042 in the directory /var you can do this: # find -x /var -inum 212042 /var/log/Xorg.0.log

Linux

Find an open file in a directory using fuser or lsof: # fuser -m /home # List of processes that have access to /home # lsof /home COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME tcsh 29029 eedcoba cwd DIR 0.18 12288 1048587 /home/eedcoba (guam:/home) lsof 29140 eedcoba cwd DIR 0.18 12288 1048587 /home/eedco ba (guam: /home) Find by PID applications: ps ax | grep Xorg | awk "(print $1)" 3324 # lsof -p 3324 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME Xorg 3324 root 0w REG 8.6 56296 12492 /var/log/Xorg.0.log By file name: # lsof /var /log/Xorg.0.log COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME Xorg 3324 root 0w REG 8.6 56296 12492 /var/log/Xorg.0.log

Mounting/remounting file systems

For example cdrom, registered in /etc/fstab: # mount /cdrom Or you can find the device in /dev or in output dmesg

FreeBSD

# mount -v -t cd9660 /dev/cd0c /mnt # Mount the disk Cdrom(method one)# mount_cd9660 /dev/wcd0c /cdrom # Mount the disk Cdrom(method two)# mount -v -t msdos /dev/fd0c /mnt # Floppy disk Write to /etc/fstab: # Device Mountpoint FStype Options Dump Pass# /dev/acd0 /cdrom cd9660 ro,noauto 0 0 Allow users to mount disks: # sysctl vfs.usermount=1 # Or enter the line "vfs.usermount=1" in /etc/sysctl.conf

Linux

# mount -t auto /dev/cdrom /mnt/cdrom # Typical disk mount command cdrom # mount /dev/hdc -t iso9660 -r /cdrom # Mount the disk IDE # mount /dev/scd0 -t iso9660 -r /cdrom # Mount the disk SCSI cdrom# mount /dev/sdc0 -t ntfs-3g /windows # Mount the disk SCSI Sign up for /etc/fstab: /dev/cdrom /media/cdrom subfs noauto,fs=cdfss,ro,procuid,nosuid,nodev,exec 0 0

Mounting a FreeBSD partition with Linux

Look at the section number in fdisk, usually this is the root partition, but it can be on another BSD slice. If there are many slices on the FreeBSD partition, they will not be visible through fdisk, but they can be found in dev/sda* or /dev/hda*. # fdisk /dev/sda # Find FreeBSD partition/dev/sda3 * 5357 7905 20474842+ a5 FreeBSD # mount -t ufs -o ufstype=ufs2,ro /dev/sda3 /mnt /dev/sda10 = /tmp; /dev/sda11 /usr # Another slice

Rewiring

Remount the device without first unmounting it, for example to fsck# mount -o remount,ro / # Linux # mount -o ro / # FreeBSD Copy data stream from CDROM"and to the file ISO image. # dd if=/dev/cd0c of=file.iso

Creating a swap partition on the fly

Suppose you need to increase the swap partition, say to 2 gigabyte, /swap2gb(for Linux) # dd if=/dev/zero of=/swap2gb bs=1024k count=2000 # mkswap /swap2gb # Create swap # swapon /swap2gb # Enable swap, now it can be used# swapoff /swap2gb # Disable swap # rm /swap2gb

Mounting an SMB partition

CIFS- Common Internet File System SMB- server message block Suppose you need to access a shared SMB section myshare on the server smbserver, the address dialed to Windows machine will \\smbserver\myshare\. We will mount it on /mnt/smbshare. Don't forget for cifs IP address or domain name required.

Linux

# smbclient -U user -I 192.168.16.229 -L //smbshare/ # List the shares # mount -t smbfs -o username=winuser //smbserver/myshare /mnt/smbshare # mount -t cifs -o username=winuser, password=winpwd //192.168.16.229/myshare /mnt/share Additionally package mount.cifs allows you to store privileges in a file, for example /home/user/.smb: username=winuser password=winpwd And now we mount: # mount -t cifs -o credentials=/home/user/.smb //192.168.16.229/myshare /mnt/smbshare

FreeBSD

Use the key -I to set the IP address (or DNS); smbserver, this is a Windows name. # smbutil view -I 192.168.16.229 //winuser@smbserver # List of shared resources# mount_smbfs -I 192.168.16.229 //winuser@smbserver/myshare /mnt/smbshare

Mount image

Linux loop-back

# mount -t iso9660 -o loop file.iso /mnt # Mount CD image# mount -t ext3 -o loop file.img /mnt # Mount image with file system ext3

FreeBSD

Using md- memory device (if necessary, make kldload md.ko): # mdconfig -a -t vnode -f file.iso -u 0 # mount -t cd9660 /dev/md0 /mnt # umount /mnt; mdconfig -d -u 0 # Clear memory device Or using a pseudo device( VN, Virtual node): # vnconfig /dev/vn0c file.iso; mount -t cd9660 /dev/vn0c /mnt # umount /mnt; vnconfig -u /dev/vn0c # Clear pseudo device

Creating and Burning an ISO Image

We will copy a CD or DVD sector by sector. # dd if=/dev/hdc of=/tmp/mycd.iso bs=2048 conv=notrunc Use mkisofs to create an image from a file in a directory. To overcome filename restrictions, use the option -r, including the extension RockRidge, basic for UNIX systems, -J includes Joliet, used by Microsoft, -L allows ISO9660 names starting with a dot. # mkisofs -J -L -r -V TITLE -o imagefile.iso /path/to/dir On FreeBSD, mkisofs can be installed from /usr/ports/sysutils/cdrtools.

Burning CD/DVD ISO images

FreeBSD

FreeBSD does not install DMA on ATAPI devices, this can be done through a variable sysctl or in a file /boot/loader.conf, the following entries. hw.ata.ata_dma="1" hw.ata.atapi_dma="1" Use burncd for ATAPI devices ( burncd, standard program, part of the base system) and cdrecord(from /usr/ports/sysutils/cdrtools) for SCSI devices. # burncd -f /dev/acd0 data imagefile.iso fixate # For ATAPI devices# cdrecord -scanbus # Find recorder # cdrecord dev=1,0,0 imagefile.iso

Linux

Use it the same way cdrecord, as described above. In addition, you can use the native ATAPI interface: # cdrecord dev=ATAPI -scanbus Record as described above.

dvd+rw-tools

The dvd+rw-tools package (FreeBSD: ports/sysutils/dvd+rw-tools) has all the functionality needed to work with DVDs, plus growisofs, for burning CD or DVD. Documentation with examples can be found in the FreeBSD handbook Chapter 18.7 # -dvd-compat closes the disk# growisofs -dvd-compat -Z /dev/dvd=imagefile.iso # Write existing iso image # growisofs -dvd-compat -Z /dev/dvd -J -R /p/to/data # Write directly

Convert image from Nero .nrg file to .iso file

Nero adds a 300kb header to the image, which can be trimmed using dd. # dd bs=1k if=imagefile.nrg of=imagefile.iso skip=300

Convert bin/cue image to .iso

This can be done using a small program, bchunk. On FreeBSD it can be found in ports /usr/ports/sysutils/bchunk. # bchunk imagefile.bin imagefile.cue imagefile.iso

Creating an image from a file

For example, a 1GB partition uses the file /usr/vdisk.img. In this case we use the key -u 0, but the number can be anything.

FreeBSD

# dd if=/dev/random of=/usr/vdisk.img bs=1K count=1M # mdconfig -a -t vnode -f /usr/vdisk.img -u 0 # Create a device /dev/md1 # bsdlabel -w /dev/md0 # newfs /dev/md0c # mount /dev/md0c /mnt # umount /mnt; mdconfig -d -u 0; rm /usr/vdisk.img # Clear md An image created from a file can be mounted during system boot by writing a line to /etc/rc.conf And /etc/fstab. You can check if your settings are correct using the command /etc/rc.d/mdconfig start(after removing the device md0 using the command # mdconfig -d -u 0). Keep in mind that automatic image mounting will only work if the image file is NOT in the root partition, due to the fact that the script /etc/rc.d/mdconfig performed at an early stage of boot, when the root partition is not yet writable. Images located outside the root partition will be mounted later by the script /etc/rc.d/mdconfig2.
/boot/loader.conf: md_load="YES" /etc/rc.conf: mdconfig_md0="-t vnode -f /usr/vdisk.img" # /usr not in the root partition/etc/fstab: (0 0 at the end, very important, this will indicate fsck ignore device check since it does not exist yet) /dev/md0 /usr/vdisk ufs rw 0 0
In addition, you can subsequently increase the image size, say by 300 MB. #umount/mnt; mdconfig -d -u 0 # dd if=/dev/zero bs=1m count=300 >> /usr/vdisk.img # mdconfig -a -t vnode -f /usr/vdisk.img -u 0 # growfs /dev /md0 # mount /dev/md0c /mnt # Now the file partition is 300 MB larger

Linux

# dd if=/dev/zero of=/usr/vdisk.img bs=1024k count=1024 # mkfs.ext3 /usr/vdisk.img # mount -o loop /usr/vdisk.img /mnt # umount /mnt; rm /usr/vdisk.img # Clear

Linux and losetup

/dev/zero much faster than urandom, but less secure for encryption. # dd if=/dev/urandom of=/usr/vdisk.img bs=1024k count=1024 # losetup /dev/loop0 /usr/vdisk.img # Create /dev/loop0 # mkfs.ext3 /dev/loop0 # mount /dev/loop0 /mnt # losetup -a # Check # umount /mnt # losetup -d /dev/loop0 # Disconnect # rm /usr/vdisk.img

Creating an In-Memory File System

The in-memory file system is very fast, it makes sense to use it for applications with high disk IO. Let's create a partition of 64 MB in size and mount it in /memdisk:

FreeBSD

# mount_mfs -o rw -s 64M md /memdisk # umount /memdisk; mdconfig -d -u 0 # Clear md device md /memdisk mfs rw,-s64M 0 0 # write to /etc/fstab

Linux

# mount -t tmpfs -osize=64m tmpfs /memdisk

Disk performance

Read and write 1GB file in section ad4s3c (/home) # time dd if=/dev/ad4s3c of=/dev/null bs=1024k count=1000 # time dd if=/dev/zero bs=1024k count=1000 of=/home/1Gb.file # hdparm -tT / dev/hda # Linux only

They need to be mounted, and when all the necessary actions are completed, they need to be unmounted. Usually the mount(8) / umount(8) commands are used to mount/unmount, and no one thinks about automating these operations. In my opinion, this is not the most optimal approach, especially considering the fact that the FreeBSD operating system includes a special automatic mounting daemon.

Statement of the problem

The automount daemon of the FreeBSD amd(8) operating system is designed to transparently mount absolutely any file systems as the files and folders located on them are accessed, as well as to subsequently unmount these file systems when there is no activity for a specified period of time. This article is about configuring amd to automatically mount file systems exported by NFS servers and shared folders provided by SMB servers. In addition, it describes diagnosing amd using the amq(8) utility and allocating its own log using regular funds syslogd(8) and newsyslog(8) log management.

Initial data

To solve the problem under consideration, no additional software, however, you will have to create one that allows the root user to mount selected shares provided by SMB servers without entering a password.

Automatic mounting of NFS file systems

To ensure automatic mounting of NFS file systems, simply add the following line to the /etc/rc.conf file:

Amd_enable="YES"

and start amd with the command /etc/rc.d/amd start . By default, amd will use the alternate /.amd_mnt folder, report its status to the daemon section of the syslog, and allow NFS filesystems to be mounted to the local filesystem folders /host and /net according to the mount map /etc/amd.map (this mount map included with the FreeBSD operating system and contains rules governing how NFS file systems can be mounted and unmounted using the Network Host Filesystem). If we translate what has been said into human language, then immediately after starting amd you will be able to access the share file system exported by the NFS server nfsserver as a regular local folder named /host/nfsserver/share or /net/nfsserver/share. For any such access and the required file system is not in the list of mounted file systems, amd will create a mount point /.amd_mnt/nfsserver/host/share , mount the share file system to it, create a folder /host/nfsserver or /net/nfsserver and add it to it a symbolic link to the above mount point. If the /host/nfsserver/share or /net/nfsserver/share folder is not used for 5 minutes, amd will unmount the share file system and then delete the no longer needed folders and the symbolic link.

Automatic mounting of SMBFS file systems

To enable automatic mounting of SMBFS file systems, you will have to create one or more additional mount maps, each of which must contain rules based on the use of Program Filesystem. To be able to contact shared folders using template names like /smbfs/smbserver/share, you should allocate a separate mount card for each SMB server. For example, to specify the mount rules for the shared folders share1 and share2 provided by the SMB server smbserver, you can create a mount map named /etc/amd.map-smbserver (You can change this name to suit your own tastes) and add the following lines to it:

Share1 type:=program;fs:=$(autodir)/$(path);mount:="/sbin/mount mount -t smbfs \\\/\\\/user@smbserver/share1 $(fs)"; share2 type:=program;fs:=$(autodir)/$(path);mount:="/sbin/mount mount -t smbfs \\\/\\\/user@smbserver/share2 $(fs)";

The specified rules will force amd to mount shared folders to the mount point $(autodir) (the variable $(autodir) contains the name of the alternative folder) with the commands /sbin/mount -t smbfs //user@smbserver/share... . The absence of the umount or unmount option requires the default unmount commands umount $(fs) to be used (the $(fs) variable contains the name of the mount point).
To associate the created mount map with the mount point /smbfs/smbserver, you need to add the following line to the /etc/rc.conf file:

Amd_flags="$amd_flags /smbfs/smbserver /etc/amd.map-smbserver"

and restart amd with the command /etc/rc.d/amd restart . Immediately after completing these steps, you will be able to access the shared folders share1 and share2 provided by the SMB server smbserver as normal local folders /smbfs/smbserver/share1 and /smbfs/smbserver/share2.

Analyzing the state of amd using the amq utility

To quickly find out the current state of amd, you can use the amq -m command, which is designed to display a list of mounted file systems, including the number of links to each of them and containing information about mounting errors, something like this:

"root" root 1 localhost is up /etc/amd.map /host toplvl 1 localhost is up /etc/amd.map /net toplvl 1 localhost is up /etc/amd.map-smbserver /smbfs/smbserver toplvl 1 localhost is up nfsserver:/host/nfsserver /.amd_mnt/nfsserver host 1 nfsserver is up mount -t smbfs //user@smbserver/share1 ... /.amd_mnt/smbfs/smbserver/share1 program 1 localhost is up

The first column of this table contains the name of the mount map for mount points or mount options for mounted file systems, the second is the name of the mount point, and the third is the type of file system. amd systems, the fourth is the number of links to the mount point or file system, the fifth is the name of the computer on which the file system is located, the sixth is the status of the mount point or file system, the seventh is mount error messages (if there are no errors, the seventh column is not displayed). Because amd and the rpcbind(8) it runs support TCP Wrappers, running the amq -m command may result in error messages similar to the following:

Amq: localhost: RPC: Port mapper failure - RPC: Authentication error amq: localhost: RPC: Authentication error; why = Failed (unspecified error)

To eliminate these errors, you should add rules to the /etc/hosts.allow file that allow access to amd and rpcbind from localhost:

Amd: 127.0.0.1: allow amd: ALL: deny rpcbind: 127.0.0.1: allow rpcbind: ALL: deny

Providing amd with its own log

The default startup options in /etc/defaults/rc.conf cause amd to report its status to the daemon section of the syslog. If you want to provide amd with a separate log called /var/log/amd.log , you will have to redirect these messages to another syslog section (local6 in my case), and also change the syslogd daemon configuration accordingly.
Taking into account all the changes in the configuration files described in this article, in order to redirect amd messages to the log /var/log/amd.log, you must first change the definition of the amd_flags variable in the /etc/rc.conf file to this form:

Amd_flags="-a /.amd_mnt -l syslog:local6 /host /etc/amd.map /net /etc/amd.map /smbfs/smbserver1 /etc/amd.map-smbserver1"

and restart amd with the command /etc/rc.d/amd restart , and secondly, add the line to the /etc/syslog.conf file:

Local6.* /var/log/amd.log

create an empty log with the touch /var/log/amd.log command and restart syslogd with the /etc/rc.d/syslogd restart command.
To prevent the amd log from growing, you should enable its rotation using the newsyslog utility. For example, to daily truncate the /var/log/amd.log log while preserving seven previous copies compressed with the bzip2(1) archiver, you should add the following line to the /etc/newsyslog.conf file:

/var/log/amd.log 644 7 * @T00 JC

Conclusion

After completing the steps described in this article, you will no longer have to worry about mounting and unmounting NFS and SMBFS file systems. I hope that you will find this opportunity useful and be sure to pay attention to amd.

from

I often draw attention to the fact that simple questions are often poorly covered on the Internet. This is probably because all the gurus are sure that no one will ever ask such stupid questions, because everyone knows this. But my practice has shown that it is precisely these small simple questions that are the most common not only among beginners, but also among serious administrators who simply have not had to deal with this. Even serious administrators do not do this every day, but, in order not to forget, they keep some kind of cheat sheet for themselves, without admitting it to anyone. Let's fix everything. Now you will learn how to add a hard drive to FreeBSD in 5 minutes. So. First, complete instructions will be given to understand the process, and at the end there will be a short list of actions, which will only contain a list of commands as a cheat sheet.

Detailed instructions with explanations

Selecting a hard drive name

First we need to determine the name of the device we just added. The following command will help us with this:

Geom disk list

Or this command:

Camcontrol devlist

IN real system these commands will show more useful information, namely device names and serial numbers.

Before installing the new device, we knew that our system was installed on ada0, which means, logically, our new drive is ada1. You can determine this by the name of the new device, it serial number or volume.

Now let's check if there is marking on our new disk

Gpart show ada1

The disk does not have any markings.

Removing existing markup

If the disk has already been used and there is a need to remove partitions from it, simply run:

Gpart destroy -F ada1

Creating GPT markup

First, we must create a disk partition. I highly recommend forgetting about MBR and switching to a new, more convenient and functional one - GPT.

We create GPT markup on the disk, then check what happens:

Gpart create -s gpt /dev/ada1 gpart show ada1

Now our disk has GPT markup. From the output you can see that absolutely the entire disk, starting from LBA 34 and ending with LBA 8388541, is empty. LBA 0−33 - reserved by the system for the partition table.

Let's say we need to create two partitions on this disk:

  • swap- swap partition
  • data- a section of the ufs type for storing any data we need.

Creating sections (slices)

If the installation is carried out on modern hard drives, whose sector size = 4 KB, then when creating partitions (partitions) it is necessary to use alignment. You can do this in two ways: 1) if we specify the section parameters in blocks, then enter the block number as a multiple of 8, for example: -b 40; 2) if we indicate the partition size in bytes, or do not indicate the beginning and size at all, use the parameter -a 4k, which will adjust the beginning and end of the section to sectors of 4 kb in size. Since we are in in this example If we are performing a test installation on a virtual hard disk, then this may not be necessary. In any case, before creating partitions, you need to know exactly the sector size of your drive, otherwise it will result in terrible work slowdowns.

Now let's create the partitions. To do this, there is a gpart add command with various parameters. First parameter -t- indicates the type of file system being created. In our case, two types will be used: freebsd-swap and freebsd-ufs. Next are two optional parameters: -b- indicates the LBA number from which the partition needs to be created. If you do not specify this parameter, the partition will be created automatically from the first free LBA. -s- indicates the size of the partition in the LBA. Size of one LBA block = 512 bytes. It is advisable to indicate in the number of LBA blocks, but it is also possible in kilo/mega/giga/… bytes (suffix k/M/G). If you do not specify this parameter, the partition will be created to the maximum possible LBA within the empty area. You can also specify the section label as a parameter, for example: -l swap1- in this case, the label /dev/gpt/swap1 will be created, which can be used to more conveniently access the partition. The last required parameter is the path to the disk. In our case: /dev/ada1.

Let's create two partitions and then see what we have. We will create the first partition without specifying the initial LBA, but specifying the size of 1 GB (2097152 blocks). We will create the second partition without specifying the initial LBA and without specifying the size - thus it will be created on the entire free space.

Gpart add -t freebsd-swap -s 2097152 /dev/ada1 gpart add -t freebsd-ufs /dev/ada1 gpart show ada1

The size can be specified in bytes rather than blocks. It's much more convenient. The only negative is that the system cannot always correctly calculate the number of blocks. There may be cases when a certain number of blocks will remain empty on the disk when specifying the partition size in bytes.

Creating a file system (formatting)

There is no need to format swap partitions. But partitions like ufs must be formatted before use. It would be more correct to say: a file system should be created on them.

In order to create a file system on the second partition, just run the following command:

Newfs -U /dev/ada1p2

In this case, the -U parameter was used - it indicates that the Soft Updates mechanism should be used in this file system. You can omit this option to avoid enabling this mechanism.

Mounting

The next step is to mount the partitions. First, so as not to forget, let's add our new sections to /etc/fstab. My file after editing looks like this:

In order to remount all partitions according to the /etc/fstab file, simply run the command:

Mount -a

As you can see from the output, the /dev/ada1p2 partition is mounted. Now let's see what happened to the SWAP section. Let's run the command:

As you can see, new section SWAP is not mounted. In order for SWAP to be mounted, you need to enable it with a special command:

Swapon /dev/ada1p1

In the same way, using the swapoff command, you need to disable the SWAP partition before performing any actions on it.

That's all for adding a new one hard drive into the system are completed.

Brief instructions

Given: hard drive /dev/ada1

Target: Delete the existing partition, create a new GPT partition, create two partitions: swap and data and connect them to the working system.

After each action, do gpart show to observe the result. Sequence of actions:

  1. Remove existing partition: gpart destroy -F ada1
  2. Create a new partition: gpart create -s gpt /dev/ada1
  3. Create two partitions: swap and data: gpart add -t freebsd-swap -s 2097152 /dev/ada1 gpart add -t freebsd-ufs /dev/ada1
  4. Create a file system UFSv2 on the second partition: newfs -U /dev/ada1p2
  5. Add lines to the /etc/fstab file for automounting at boot: /dev/ada1p1 none swap sw 0 0 /dev/ada1p2 /mnt ufs rw 2 2
  6. Mount a new partition (the command mounts all partitions from the /etc/fstab file): mount -a
  7. Enable the new swap partition with the command: swapon /dev/ada1p1

This completes the setup.

Home > Operating systems > UNIX > BSD > FreeBSD

Add a hard drive to FreeBSD in 5 minutes

I often draw attention to the fact that simple questions are often poorly covered on the Internet. This is probably because all the gurus are sure that no one will ever ask such stupid questions, because everyone knows this. But my practice has shown that it is precisely these small simple questions that are the most common not only among beginners, but also among serious administrators who simply have not had to deal with this. Even serious administrators do not do this every day, but, in order not to forget, they keep some kind of cheat sheet for themselves, without admitting it to anyone. Let's fix everything. Now you will learn how to add a hard drive to FreeBSD in 5 minutes. So. First, complete instructions will be given to understand the process, and at the end there will be a short list of actions, which will only contain a list of commands as a cheat sheet.

Detailed instructions with explanations

Selecting a hard drive name

First we need to determine the name of the device we just added. The following command will help us with this:

Geom disk list

Or this command:

Camcontrol devlist

In a real system, these commands will show more useful information, namely device names and serial numbers.

Before installing the new device, we knew that our system was installed on ada0, which means, logically, our new drive is ada1. You can determine this by the name of the new device, its serial number or volume.

Now let's check if there is marking on our new disk

Gpart show ada1

The disk does not have any markings.

Removing existing markup

If the disk has already been used and there is a need to remove partitions from it, simply run:

Gpart destroy -F ada1

Creating GPT markup

First, we must create a disk partition. I highly recommend forgetting about MBR and switching to a new, more convenient and functional one - GPT.

We create GPT markup on the disk, then check what happens:

Gpart create -s gpt /dev/ada1 gpart show ada1

Now our disk has GPT markup. From the output you can see that absolutely the entire disk, starting from LBA 34 and ending with LBA 8388541, is empty. LBA 0-33 - reserved by the system for the partition table.

Let's say we need to create two partitions on this disk:

  • swap- swap partition
  • data- a section of the ufs type for storing any data we need.

Creating sections (slices)

If the installation is carried out on modern hard drives with a sector size of 4 KB, then alignment must be used when creating partitions. You can do it in two ways:
1) if we specify the section parameters in blocks, then enter the block number as a multiple of 8, for example: -b 40;
2) if we indicate the partition size in bytes, or do not indicate the beginning and size at all, use the parameter -a 4k, which will adjust the beginning and end of the section to sectors of 4 kb in size. Since in this example we are performing a test installation on a virtual hard disk, we don’t have to do this. In any case, before creating partitions, you need to know exactly the sector size of your drive, otherwise it will result in terrible work slowdowns.

Now let's create the partitions. To do this, there is a gpart add command with various parameters. First parameter -t- indicates the type of file system being created. In our case, two types will be used: freebsd-swap and freebsd-ufs. Next are two optional parameters: -b- indicates the LBA number from which the partition needs to be created. If you do not specify this parameter, the partition will be created automatically from the first free LBA. -s- indicates the size of the partition in the LBA. Size of one LBA block = 512 bytes. It is advisable to indicate in the number of LBA blocks, but it is also possible in kilo/mega/giga/… bytes (suffix k/M/G). If you do not specify this parameter, the partition will be created to the maximum possible LBA within the empty area. You can also specify the section label as a parameter, for example: -l swap1- in this case, the label /dev/gpt/swap1 will be created, which can be used to more conveniently access the partition. The last required parameter is the path to the disk. In our case: /dev/ada1.

Let's create two partitions and then see what we have. We will create the first partition without specifying the initial LBA, but specifying the size of 1 GB (2097152 blocks). We will create the second partition without specifying the initial LBA and without specifying the size - thus it will be created on the entire free space.

Gpart add -t freebsd-swap -s 2097152 /dev/ada1 gpart add -t freebsd-ufs /dev/ada1 gpart show ada1

The size can be specified in bytes rather than blocks. It's much more convenient. The only negative is that the system cannot always correctly calculate the number of blocks. There may be cases when a certain number of blocks will remain empty on the disk when specifying the partition size in bytes.

Creating a file system (formatting)

There is no need to format swap partitions. But partitions like ufs must be formatted before use. It would be more correct to say: a file system should be created on them.

In order to create a file system on the second partition, just run the following command:

Newfs -U /dev/ada1p2

In this case, the -U parameter was used - it indicates that the Soft Updates mechanism should be used in this file system. You can omit this option to avoid enabling this mechanism.

Mounting

The next step is to mount the partitions. First, so as not to forget, let's add our new sections to /etc/fstab. My file after editing looks like this:

In order to remount all partitions according to the /etc/fstab file, simply run the command:

Mount -a

As you can see from the output, the /dev/ada1p2 partition is mounted. Now let's see what happened to the SWAP section. Let's run the command:

As you can see, the new SWAP partition is not mounted. In order for SWAP to be mounted, you need to enable it with a special command:

Swapon /dev/ada1p1

In the same way, using the swapoff command, you need to disable the SWAP partition before performing any actions on it.

This completes all steps to add a new hard drive to the system.

Brief instructions

Given: hard drive /dev/ada1

Target: Delete the existing partition, create a new GPT partition, create two partitions: swap and data and connect them to the working system.

After each action, do gpart show to observe the result. Sequence of actions:

  1. Remove existing markup:
    gpart destroy -F ada1
  2. Create new markup:
    gpart create -s gpt /dev/ada1
  3. Create two partitions: swap and data:
    gpart add -t freebsd-swap -s 2097152 /dev/ada1 gpart add -t freebsd-ufs /dev/ada1
  4. Create a file system UFSv2 on the second section:
    newfs -U /dev/ada1p2
  5. Add lines to /etc/fstab for auto-mounting at boot:
    /dev/ada1p1 none swap sw 0 0 /dev/ada1p2 /mnt ufs rw 2 2
  6. Mount a new partition (the command mounts all partitions from the /etc/fstab file):
    mount -a
  7. Enable the new swap partition with the command:
    swapon /dev/ada1p1 Material taken from the site:

NTFS - “native” file Windows system. Therefore, it won’t be possible to simply connect flash drives, hard drives or other removable media unknown to us (in fact, it will work, but there will be problems). Therefore, we are rolling out our comprehensive manual on this matter.


  1. As always, it all starts with . Install the driver for NTFS: #cd /usr/ports/sysutils/fusefs-ntfs #make install clean
  2. As of FreeBSD 10, Fuse is part of the kernel. We make it load with system modules #nano /boot/loader.conf fuse_load="YES"

    This will work after reboot. In the current session, we will load this module manually

    #kldload fuse

    You can check whether the module is loaded or not with the command

    #kldstat

    If the list contains fuse.ko, that means everything is fine.

    #kldstat Id Refs Address Size Name 1 3 0xffffffff80200000 1fa7c38 kernel 2 1 0xffffffff821a9000 1a7c8 fuse.ko

  3. Next, we must decide how the system sees a flash drive or an external hard drive: #dmesg | grep da

    The output will be something like this:

    Da0 at umass-sim0 bus 0 scbus1 target 0 lun 0 da0: s/n 00H79BHRYGX22JBN detached (da0:umass-sim0:0:0:0): Periph destroyed da0 at umass-sim0 bus 0 scbus1 target 0 lun 0 da0: Removable Direct Access SPC-4 SCSI device da0: Serial Number 00H79BHRYGX22JBN da0: 40.000MB/s transfers da0: 14870MB (30453760 512 byte sectors) da0: quirks=0x12 da1 at umass-sim1 bus 1 scbus2 target 0 lun 0 da1: s/n 8968888304C9BB52 detached (da1:umass-sim1:1:0:0): Periph destroyed da1 at umass-sim1 bus 1 scbus2 target 0 lun 0 da1: Removable Direct Access SCSI-2 device da1: Serial Number 8968888306C9BB52 da1: 40.000MB/s transfers da1: 1999MB (4093952 512 byte sectors) da1: quirks=0x2

    We see that in the system we have two flash drives with identifiers da0 And da1. In our example we will only use a flash drive da0.

  4. We mount it with the following command: ntfs-3g /dev/da0 /mnt

    /dev/da0- this is our flash drive, we found this out in point 3.
    /mnt is the mount point. She can be anyone.

    If an error appears, mount the flash drive partition. After entering

    Ntfs-3g /dev/da0

    press TAB and see the partitions of the flash drive

    Da0 da0s1

    And we mount this section

    Ntfs-3g /dev/da0s1 /mnt

  5. We go to the directory where we mounted the flash drive and see its contents there: #cd /mnt #ll total 13 drwxrwxrwx 1 root wheel 0 4 Nov. 17:23 System Volume Information/ -rwxrwxrwx 1 root wheel 9 Nov 4 18:05 xxx.xxx* -rwxrwxrwx 1 root wheel 22 Nov 4 18:04 ZIP archive- WinRAR.zip* -rwxrwxrwx 1 root wheel 9904 Nov 4 18:04 Leaf Microsoft Office Excel.xlsx*

    Now you can write files to the flash drive and read them from it.

  6. In order to unmount the flash drive, we first leave the directory where it is mounted. For example, #cd /

    And after that, let's use the command

    #umount /mnt

    note that The argument is not a flash drive, but its mount point!
    IMPORTANT: Do not pull out the flash drive immediately after entering the command! It can only be pulled out after a few seconds, when the input prompt appears in the terminal again!