Mount file as drive linux

Mount a File System on Linux

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

Mounting or unmounting a file system on Linux is usually straightforward, except when it isn’t. This article teaches you how to mount and unmount file systems, as well as list available and currently mounted file systems. It also explains how to handle the case where file systems won’t unmount because they are in use.

Before You Begin

  1. If you have not already done so, create a Linode account and Compute Instance. See our Getting Started with Linode and Creating a Compute Instance guides.
  2. Follow our Setting Up and Securing a Compute Instance guide to update your system. You may also wish to set the timezone, configure your hostname, create a limited user account, and harden SSH access.

The steps in this guide require root privileges. Be sure to run the steps below as root or with the sudo prefix. For more information on privileges, see our Users and Groups guide.

File Systems Available for Linux

  1. On a Linux system, you can list the currently available file system types from the command line with:
nodev sysfs nodev tmpfs nodev bdev nodev proc nodev cgroup nodev cgroup2 nodev cpuset nodev devtmpfs nodev configfs nodev debugfs nodev tracefs nodev securityfs nodev sockfs nodev bpf nodev pipefs nodev ramfs nodev hugetlbfs nodev devpts ext3 ext2 ext4 squashfs vfat nodev ecryptfs fuseblk nodev fuse nodev fusectl nodev mqueue nodev pstore nambtrfs nodev autofs
FILESYSTEMS(5) Linux Programmer's Manual FILESYSTEMS(5) NAME filesystems - Linux filesystem types: ext, ext2, ext3, ext4, hpfs, iso9660, JFS, minix, msdos, ncpfs nfs, ntfs, proc, Reiserfs, smb, sysv, umsdos, vfat, XFS, xiafs DESCRIPTION When, as is customary, the proc filesystem is mounted on /proc, you can find in the file /proc/filesystems which filesystems…

How to List Currently Mounted File Systems on Linux

  1. You can list the currently mounted file systems from a Linux command line with a simple mount command:
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) udev on /dev type devtmpfs (rw,nosuid,relatime,size=441300k,nr_inodes=110325,mode=755,inode64) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=99448k,mode=755,inode64) /dev/sda on / type ext4 (rw,relatime,errors=remount-ro) securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime) tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64) tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k,inode64) cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot) pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime) bpf on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,relatime,mode=700) systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18031) hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,pagesize=2M) mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime) debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime) tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,noexec,relatime) fusectl on /sys/fs/fuse/connections type fusectl (rw,nosuid,nodev,noexec,relatime) configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,noexec,relatime) none on /run/credentials/systemd-sysusers.service type ramfs (ro,nosuid,nodev,noexec,relatime,mode=700) tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=99444k,nr_inodes=24861,mode=700,inode64)
# /etc/fstab: static file system information. # # Use 'blkid' to print the universally unique identifier for a # device; this may be used with UUID= as a more robust way to name devices # that works even if disks are added and removed. See fstab(5). # #      /dev/sda / ext4 errors=remount-ro 0 1 /dev/sdb none swap sw 0 0
TARGET SOURCE FSTYPE OPTIONS / /dev/sda ext4 rw,relatime,errors=remount-ro ├─/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime │ ├─/sys/kernel/security securityfs securityfs rw,nosuid,nodev,noexec,relatime │ ├─/sys/fs/cgroup cgroup2 cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot │ ├─/sys/fs/pstore pstore pstore rw,nosuid,nodev,noexec,relatime │ ├─/sys/fs/bpf bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 │ ├─/sys/kernel/debug debugfs debugfs rw,nosuid,nodev,noexec,relatime │ ├─/sys/kernel/tracing tracefs tracefs rw,nosuid,nodev,noexec,relatime │ ├─/sys/fs/fuse/connections fusectl fusectl rw,nosuid,nodev,noexec,relatime │ └─/sys/kernel/config configfs configfs rw,nosuid,nodev,noexec,relatime ├─/proc proc proc rw,nosuid,nodev,noexec,relatime │ └─/proc/sys/fs/binfmt_misc systemd-1 autofs rw,relatime,fd=29,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=18031 ├─/dev udev devtmpfs rw,nosuid,relatime,size=441300k,nr_inodes=110325,mode=755,inode64 │ ├─/dev/pts devpts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 │ ├─/dev/shm tmpfs tmpfs rw,nosuid,nodev,inode64 │ ├─/dev/hugepages hugetlbfs hugetlbfs rw,relatime,pagesize=2M │ └─/dev/mqueue mqueue mqueue rw,nosuid,nodev,noexec,relatime └─/run tmpfs tmpfs rw,nosuid,nodev,noexec,relatime,size=99448k,mode=755,inode64 ├─/run/lock tmpfs tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k,inode64 ├─/run/credentials/systemd-sysusers.service │ none ramfs ro,nosuid,nodev,noexec,relatime,mode=700 └─/run/user/0 tmpfs tmpfs rw,nosuid,nodev,relatime,size=99444k,nr_inodes=24861,mode=700,inode64
TARGET SOURCE FSTYPE OPTIONS / /dev/sda ext4 rw,relatime,errors=remount-ro
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 24.5G 0 disk / sdb 8:16 0 512M 0 disk [SWAP]

How to Mount File Systems on Linux

You can mount file systems for a single session using the mount command, and permanently by editing /etc/fstab. Mounting needs to be done by an administrator, either by logging in as root or by using the sudo command. There are some cases where mounting is done automatically, like when you insert a USB flash drive. Here are a few examples using the mount command, plus the preparatory mkdir command to create the mount point.

Читайте также:  Deny ssh access linux

Mount a Windows Floppy Disk

  1. This command is only necessary if the /mnt/floppy directory doesn’t already exist:
mount -t msdos /dev/fd0 /mnt/floppy 

Mount a CD-ROM

mount -t iso9660 /dev/cdrom /mnt/cdrom 

Mount a Disk Drive Permanently

/dev/sdc /mnt/disk-drive ext4 defaults 0 0 
. /dev/sdc on /mnt/disk-drive type ext4 (rw,relatime) . 

Mount a USB Drive

Most modern distros automatically mount USB drives when you insert them.

    If that doesn’t happen, create a mount point if it doesn’t already exist:

If the USB drive uses the exFAT file system, you may need to install the FUSE exFAT module and tools.

Mount an ISO File

  1. The example below assumes that the ISO file is in the /root directory and that you are logged in as root:
mount ~/my_image.iso /media/iso -o loop 

Mount a Remote File System

The network file system (NFS) supports mounting remote file systems as shares for local access.

    If you don’t already have an NFS client, you need to install it: Ubuntu or Debian:

123.45.67.8:/my_share /media/nfs nfs defaults 0 0 

Instead of using NFS to mount a remote file system, you can instead use SSHFS. It’s not as stable as NFS, but has fewer dependencies. SSHFS is part of FUSE, and is available for most Linux distros, macOS, and Windows.

Unmounting File Systems

  1. You can unmount a file system using the umount command. Either the device name or the mount point is sufficient to specify what you wish to unmount:
umount /media/nfs umount /media/iso umount /media/usb umount /dev/cdrom umount /dev/fd0 

You can add the -l (lazy) switch to umount to instruct the system to unmount the device when it’s free. Alternatively, the -f (force) switch makes the system unmount the device right away, at the possible risk of corrupting the file system. The -f switch is primarily intended to unmount unreachable NFS shares.

Читайте также:  Linux домашняя директория пользователя root

Conclusion

Mounting a file system on Linux is generally a straightforward two-step process: create a mount point directory, and use the mount command to mount the device at the mount point. Unless the file system is in use, unmounting is even simpler, requiring only the umount command. File system mounting and unmounting requires you to be logged in as root, or use the sudo prefix to temporarily take on root privileges.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on Thursday, September 15, 2022.

Источник

Use A File As A Linux Block Device

Just like when creating a SWAP file, you can create a file on a disk and present it as a block device. The block device would have a maximum file size of the backing file, and (as long as it’s not in use) be moved around like a normal file. For example, I could create a 1GB file on the filesystem and make Linux treat the file as a disk mounted in /dev/. And guess what – that’s what we’re going to do.

Create a file and filesystem to use as a block device

First off, use dd to create a 1GB file on an existing disk that we’ll use for our storage device:

dd if=/dev/zero of=/root/diskimage bs=1M count=1024

Then ‘format’ the file to give it the structure of a filesystem. For this example we’re going to use ext4 but you could choose any filesystem that meets your needs.

You’ll be promoted with Proceed anyway?. Type y and press return to proceed with the process.

mke2fs 1.42.5 (29-Jul-2012) /root/diskimage is not a block special device. Proceed anyway? (y,n) y Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536 inodes, 262144 blocks 13107 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=268435456 8 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376 Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done

Mounting a loop device

Before mounting the file we need to check that there is a free /dev/loopX loopback device that we can use to represent our new block device.

Читайте также:  Astra linux госуслуги эцп

Run the below command, and if there is any output then check if it’s one of your loop devices, which will more than likely reference /dev/loop as the mounted device. If you do have a reference to our loop device then see the below section on Unmounting a loop device, or choose a number higher than the highest listed loop device, for example: usually there are several loop devices, starting with loop0 and going up in value to loop1, loop2, and so on.

cat /proc/mounts | grep /dev/loop

Once you have the file that you’d like to mount and a free loop device then you can go ahead and mount the file as a block device. You have two options:

  1. Mount the file as a block device only
  2. Mount the file as a block device and mount the filesystem of it on a local mount point (eg. /mnt/mymountpoint).

For option 1; to only mount the file as a device in /dev/, run the below command and change /root/diskimage to the path of the file you’d like to mount. loop0 can also be incremented as explained above.

losetup /dev/loop0 /root/diskimage

If you’d like this to be remounted after a machine reboot then add the above line to the rc.local file.

losetup /dev/loop0 /root/diskimage

For option 2; to mount the file and the filesystem on it, use the mount command. You must have already created the mount point locally before running the command, as you would when mounting a disk or NFS share.

Then run the mount command and specify the loop device, the path of the file and the path to mount the filesystem on:

mount -o loop=/dev/loop0 /root/diskimage /mnt/mymountpoint

To check the file has been mounted you can use the df command:

df -h | grep mymountpoint /dev/loop0 976M 1.3M 924M 1% /mnt/mymountpoint

Unmounting a loop device

If you’ve mounted the filesystem on the block device using the mount command then make sure it’s unmounted before proceeding.

To then free the loop0 device (or which ever loop device you’ve used) you’ll need the losetup command with the d switch.

Источник

Оцените статью
Adblock
detector