LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Raspberry Pi: move home & var folders to USB HDD? (http://www.linuxquestions.org/questions/linux-server-73/raspberry-pi-move-home-and-var-folders-to-usb-hdd-4175467848/)

doni49 06-29-2013 04:20 PM

Raspberry Pi: move home & var folders to USB HDD?
 
I have a Raspberry Pi running raspian. I've installed Apache2, PHP & MySQL. Apache & MySQL are both configured to use sub-directories of /var. I'd like to use a directory on my USB HDD instead of my SD card.

I'd also like to move the /home directory to the USB HDD.

I'd like to avoid re-partioning my HDD if possible. I thought maybe I could use a symlink to tell raspian that /var is really at /media/USBHDD1/var and /home is really at /media/USBHDD1/home. I tried it but couldn't get it to work.

Since I couldn't get the symlink to work, I also tried resize my partition so that I could go ahead and mount new partions. I used this tutorial. But it didn't work either. I don't know if I missed a step or what. But it didn't work and when I tried to start over and try again, I got a message telling me that ntfsresize did not work because the drive was scheduled
for a check.

I just had fdisk list the partitions for this HDD.
Following is the output:
Quote:

sudo fdisk /dev/sda1
Command (m for help): p

Disk /dev/sda1: 2000.4 GB, 2000396746752 bytes
255 heads, 63 sectors/track, 243201 cylinders, total 3907024896 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x6e697373

This doesn't look like a partition table
Probably you selected the wrong device.

Device Boot Start End Blocks Id System
/dev/sda1p1 ? 1936269394 3772285809 918008208 4f QNX4.x 3rd part
/dev/sda1p2 ? 1917848077 2462285169 272218546+ 73 Unknown
/dev/sda1p3 ? 1818575915 2362751050 272087568 2b Unknown
/dev/sda1p4 ? 2844524554 2844579527 27487 61 SpeedStor

Partition table entries are not in disk order

Command (m for help):

GlennsPref 06-29-2013 06:45 PM

Hi, seems the naming of your partitions is not the way the system would like to read them.

they should be(not familiar with Pi) listed and named like this...

for hdd sda.
sda(n) where n = partition number (integers only)

so yours should look like...
Quote:

Device Boot Start End Blocks Id System
/dev/sda1 1936269394 3772285809 918008208 4f QNX4.x 3rd part
/dev/sda2 1917848077 2462285169 272218546+ 73 Unknown
/dev/sda3 1818575915 2362751050 272087568 2b Unknown
/dev/sda4 2844524554 2844579527 27487 61 SpeedStor
The mount points for your partitons are quite flexible and you may try again once you have corrected the partition naming conventions.

If you move /var while the system is running a migration will take place, writing to usb chips is slow.

I hope this helps.

michaelk 06-29-2013 06:49 PM

As the error message stated you selected the wrong device. The MBR is not associated with a partition. You need to select the drive device i.e. /dev/sda vs /dev/sda1 when running fdisk.

doni49 06-29-2013 07:53 PM

Thanks guys. I'll dig into that. But would you mind commenting on the question about using symlinks to ID where these directories are?

And do I need to umount the home and var directories before I mount them to the HDD? If yes, how do I go about that?

I greatly appreciate your time and efforts!

GlennsPref 06-29-2013 08:14 PM

the linking would be taken care of by /etc/fstab which holds most, if not all the mount points.

for instance, mine looks like this.
/etc/fstab
Code:

# Entry for /dev/sdb5 :
UUID=13d5963d-e719-4f21-8c0a-961a3cbbf639 / ext4 acl,relatime 1 1
# Entry for /dev/sdb1 :
UUID=61f81580-1eff-42b8-b699-8cf84f543e31 /boot ext4 acl,relatime 1 2
# Entry for /dev/sdb6 :
UUID=a6553178-8e7b-49da-815c-c738c1a4d54e /home ext4 acl,relatime 1 2
# Entry for /dev/sda7 :
UUID=6b198f0d-a1f1-4e7a-b4ea-3991bb8c6971 /home/glenn/local/Archive ext4 acl,relatime 1 2
# Entry for /dev/sda12 :
UUID=49ee8726-6d3f-4487-8405-68716f191d7f /home/glenn/local/backup ext4 acl,relatime 1 2
# Entry for /dev/sdc5 :
UUID=92702c8f-be86-4ef3-937e-4daf454193e5 /home/glenn/local/data ext4 acl,relatime 1 2
# Entry for /dev/sdc7 :
UUID=5d0126f8-cda3-46dd-8fbf-89ea3db6e6f7 /home/glenn/local/music ext4 acl,relatime 1 2
# Entry for /dev/sdb11 :
UUID=80afabb4-b9a3-448f-856f-5a583ce179bc /home/glenn/local/vm1 ext4 acl,relatime 1 2
# Entry for /dev/sdc6 :
UUID=a0b61e55-5353-4d2c-a111-64dcb3f6b385 /home/glenn/local/video ext4 acl,relatime 1 2
# Entry for /dev/sda11 :
UUID=8d40e507-73d5-4af3-8603-a39270b33c1e /home/glenn/local/spare ext4 acl,relatime 1 2
# Entry for /dev/sda8 :
UUID=FE2A27D12A27862D /media/win_c ntfs-3g defaults,umask=000 0 0
# Entry for /dev/sdc1 :
UUID=587C2B0D7C2AE60A /media/win_c2 ntfs-3g defaults,umask=000 0 0
none /proc proc defaults 0 0
# Entry for /dev/sdb8 :
UUID=c14ad173-62d7-45ac-9dd1-e9ca93539c0a /tmp ext4 acl,relatime 1 2
# Entry for /dev/sda1 :
UUID=a55b2d80-d67e-465a-92d4-e8e5caa48255 /usr ext4 acl,relatime 1 2
# Entry for /dev/sdb9 :
UUID=8469f117-92aa-4b7b-b8c7-232b347d6099 /usr/local ext4 acl,relatime 1 2
# Entry for /dev/sda9 :
UUID=a642f6dc-ad12-4580-b3ce-0a95398423ee /usr/src ext4 acl,relatime 1 2
# Entry for /dev/sda5 :
UUID=e58b782d-da89-45e0-9c33-128887857be0 /var ext4 acl,relatime 1 2
# Entry for /dev/sdb10 :
UUID=5984d472-bb1b-4f27-b6c9-c0affcac8d47 /var/spool ext4 acl,relatime 1 2
# Entry for /dev/sda10 :
UUID=793cff6f-5a0b-41e0-8984-dc899345ce93 /var/www ext4 acl,relatime 1 2
# Entry for /dev/sda6 :
UUID=dcd0b80e-51dd-4fee-a4f6-a0bf7217f903 swap swap defaults 0 0
# Entry for /dev/sdb7 :
UUID=70b0a67c-4b3f-40dc-9018-1cc534c833d2 swap swap defaults 0 0
# load tmpfs
tmpfs /dev/shm tmpfs defaults 0 0
#Get read/write access to usb, load usbfs
none /proc/bus/usb usbfs devgid=43,devmode=664 0 0

fstabmanual.

/var is a system directory and might be in use, the migration would occur upon next boot. That is after you change fstab to point to your new /var directory.

/home you can move/copy as root. and clean up after. change fstab to point to your new /home.

There's probably a cleaner way to do this...

Shadow_7 06-29-2013 10:02 PM

UUID is the way to go on removeable media. Or pretty much any media these days. It's an ID that is partition specific.

Just change the /etc/fstab to match your wishes (in theory).

# blkid /dev/sdg1

On whatever device applies to find the actual UUID of it. If you're changing the / (root) location, you'll want to change /etc/fstab and the boot loaders configuration (/boot/grub/grub.cfg). YMMV

$ cat /proc/partitions

To see what /dev/ names exist for known devices at any given moment.

EDIT: the mount points (directories) should exist on the host partition (/ aka root). But should not contain any files. Not that having stuff there will outright fail, but it can get a little unpredictable.

doni49 06-30-2013 07:35 AM

Thanks guys. I think I better understand how fstab & fdisk work.

However, I was able to figure out how to use a symlink to put my home folder on the HDD. Now I'd like to put the var folder on it too. But to do so, I'm going to need to use the mv command to actually move the folders/files over and then create a symlink to point at the new location.

How do I figure out what service(s) use this folder so that I can stop them long enough to make the move? I'd really rather not make a separate partition just for var -- which means I'd like to use a symlink here too if possible.

Shadow_7 06-30-2013 10:40 AM

No need to figure that out. Just boot a live disc, or a usb stick with linux on it and do it from a different running linux. To do it on a live system is hazardous at best.

But to answer your question for curiosities sake, use lsof or fuser to see what files are actively in use and by what. But still don't move that on a running system. And you'll probably want to avoid using a symlink for /var/. /home/ should be okay perhaps, but only because it doesn't really get used until a non-root user logs in.

doni49 06-30-2013 06:34 PM

Quote:

Originally Posted by Shadow_7 (Post 4981200)
No need to figure that out. Just boot a live disc, or a usb stick with linux on it and do it from a different running linux.

I have 3 RasPi units. I can take an SD card from one of the others and boot from that. Then I'll put the SD card from this one in my USB SD card reader. But how do I tell raspian to create the "/var" symlink for the INACTIVE RasPi session?

TIA!

EDIT: I just now saw your comment about not using a symlink with var.

I currently have two folders on the HDD:

/media/USBHDD1/shares (users can place files/folders via SAMBA).
/media/USBHDD1/home (symlink for /home)

If I mount the partition to /var, can I safely create "shares" and "home" folders on this same partion?

Shadow_7 07-02-2013 04:23 PM

In days of old, mount points were dedicated devices or partitions. So all you needed to do was mount the device to that location. There's a certain efficiency in terms of write and read speeds going this route. And for /var/log/ that has relatively heavy traffic on a server, you can swap out the hardware as needed. Obviously on a desktop /home/ gets most of the heavy traffic. If only from a browsers cache.

Certain things need to happen in a certain sequence. A symlink is pointless if the thing it points to isn't available. With a regular directory, it can write to / in the absence of the availability of the pointed to thing. Which gives you an opportunity to diagnose and recover. Versus not having a /var/ which implies not having /var/log/ and other potentially hazardous outcomes.

As far as making the change. We're basically talking about editing files on a filesystem. You do not need to do this with a pi. Any linux that can read the filesystem can make the change. A linux laptop or desktop, or whatever works. It could be arm based like the pi or x86 or powerpc, or whatever runs linux and supports the filesystem. As in you're probably over thinking it. I know I tend to do that a lot of the time.

doni49 07-02-2013 06:53 PM

As to the point of not needing to do it on the Pi goes, my 3 RasPi devices are the only linux machines I have access to.

I think I accomplished what I needed last night.
  1. I cleaned up my HDD so that there were ONLY 2 folders on it: shares and home.
  2. I took the SD card out of this RasPi and put it in my USB SD card reader.
  3. I took an SD card from one of the other RasPi units and put it in this one.
  4. I booted the RasPi.
  5. I used sudo fdisk -l to find out what the devices were named.
  6. I created a folder in root called homepi.
  7. I mounted /homepi on /dev/sda2 (the partion of the inactive linux that contained 'var' and 'home').
  8. I mounted /media/USBHDD1 on /dev/sdb1 (what the currently running linux sees as the HDD).
  9. Using mv, I moved /homepi/var to /media/USBHDD1/var.
  10. I edited /homepi/etc/fstab to tell RasPi to mount /var on the HDD.

Having done this, everything that was under /var is now STILL under /var but that is on the HDD. And because I had shares and home on the HDD before rebooting, now I see /var/home and /var/shares.

Last, I created a symlink pointing /home at /var/home.

I'm not concerned about browser cache (and for the most part, I'm not really concerned about user files at all). This device is headless there will not be multiple users actively using it. But it does use cron and there are user files involved there as well as bash scripts that get run via cron. I don't want to have to recreate those user files if and when the SD card crashes.

Shadow_7 07-03-2013 11:17 PM

I'm sure symlinks work. But on the surface there isn't a fail gracefully option if the link is wrong or the linked to location isn't available at the time that it's needed. And /var/ is needed super early in the boot sequence. To include /var/log/kern.log. And certain methods of replicating a system (like cp) that do not play well with some link types. At least not with the "default options". Which can vary between distros.


All times are GMT -5. The time now is 12:36 AM.