Move linux to another drive

I Learned How To…

Learning how to do things is difficult, and I tend to forget it. I’ll take a note about them, and as I write them it will help me to make the global vision

Archives

Search in this blog

Recent Posts

Tags

How to move an existing installation of Ubuntu to another disk

Under some circumstances, we may have the need of moving a working installation of Ubuntu to another disk. The most common case is when your current disk runs out of space and you want to move it to a bigger one. But you could also want to move to an SSD disk or to create an LVM raid…

How to move an existing installation of Ubuntu to another disk

I have a 14Gb disk that contains my / partition (vda), and I want to move to a new 80Gb disk (vdb).

root@somove:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 252:0 0 14G 0 disk └─vda1 252:1 0 13.9G 0 part / vdb 252:16 0 80G 0 disk vdc 252:32 0 4G 0 disk [SWAP]

First of all, I will create a partition for my / system in /dev/vdb.

root@somove:~# fdisk /dev/vdb Welcome to fdisk (util-linux 2.31.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p Partition number (1-4, default 1): First sector (2048-167772159, default 2048): Last sector, +sectors or +size (2048-167772159, default 167772159): Created a new partition 1 of type 'Linux' and of size 80 GiB. Command (m for help): w The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.

NOTE: The inputs from the user are: n for the new partition and the defaults (i.e. return) for any setting to get the whole disk. Then w to write the partition table.

Now that I have the new partition, we’ll create the filesystem (ext4):

root@somove:~# mkfs.ext4 /dev/vdb1 mke2fs 1.44.1 (24-Mar-2018) Creating filesystem with 20971264 4k blocks and 5242880 inodes Filesystem UUID: ea7ee2f5-749e-4e74-bcc3-2785297291a4 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000 Allocating group tables: done Writing inode tables: done Creating journal (131072 blocks): done Writing superblocks and filesystem accounting information: done

We have to transfer the content of the running filesystem to the new disk. But first, we’ll make sure that any other mount point except for / is unmounted (to avoid copying files in other disks).:

root@somove:~# umount -a umount: /run/user/1000: target is busy. umount: /sys/fs/cgroup/unified: target is busy. umount: /sys/fs/cgroup: target is busy. umount: /: target is busy. umount: /run: target is busy. umount: /dev: target is busy. root@somove:~# mount 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=2006900k,nr_inodes=501725,mode=755) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=403912k,mode=755) /dev/vda1 on / type ext4 (rw,relatime,data=ordered) tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755) cgroup on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime) tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=403908k,mode=700,uid=1000,gid=1000) root@somove:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 252:0 0 14G 0 disk └─vda1 252:1 0 13.9G 0 part / vdb 252:16 0 80G 0 disk └─vdb1 252:17 0 80G 0 part vdc 252:32 0 4G 0 disk [SWAP]

Now we will create a mount point for the new filesystem and we’ll copy everything from / to it, except for the special folders (i.e. /tmp, /sys, /dev, etc.). Once completed, we’ll create the Linux special folders:

root@somove:~# mkdir /mnt/vdb1 root@somove:~# mount /dev/vdb1 /mnt/vdb1/ root@somove:~# rsync -aHAXx --delete --exclude= / /mnt/vdb1/

Instead of using rsync, we could use cp -ax /bin /etc /home /lib /lib64 …, but you need make sure that all folders and files are copied. You also need to make sure that the special folders are created by running mkdir /mnt/vdb1/ . The rsync version is easier to control and to understand.

Now that we have the same directory tree, we just need to make the magic of chroot to prepare the new disk:

root@somove:~# mount --bind /dev /mnt/vdb1/dev root@somove:~# mount --bind /sys /mnt/vdb1/sys root@somove:~# mount -t proc /proc /mnt/vdb1/proc root@somove:~# chroot /mnt/vdb1/

We need to make sure that the new system will try to mount in / the new partition (i.e. /dev/vdb1), but we cannot use /dev/vdb1 id because if we remove the other disk, it will modify its device name to /dev/vda1. So we are using the UUID of the disk. To get it, we can use blkid:

root@somove:/# blkid /dev/vda1: UUID="135ecb53-0b91-4a6d-8068-899705b8e046" TYPE="ext4" /dev/vdb1: UUID="eb8d215e-d186-46b8-bd37-4b244cbb8768" TYPE="ext4"

And now we have to update file /etc/fstab to mount the proper UUID in the / folder. The new file /etc/fstab for our example is the next:

UUID="eb8d215e-d186-46b8-bd37-4b244cbb8768" / ext4 defaults 0 0

At this point, we need to update grub to match our disks (it will get the UUID or labels), and install it in the new disk:

root@somove:/# update-grub Generating grub configuration file . Found linux image: /boot/vmlinuz-4.15.0-43-generic Found initrd image: /boot/initrd.img-4.15.0-43-generic WARNING: Failed to connect to lvmetad. Falling back to device scanning. Found Ubuntu 18.04.1 LTS (18.04) on /dev/vda1 done root@somove:/# grub-install /dev/vdb Installing for i386-pc platform. Installation finished. No error reported.

WARNING: In case we get error “error: will not proceed with blocklists.”, please go to the end part of this post.

WARNING: If you plan to keep the original disk in its place (e.g. a Virtual Machine in Amazon or OpenStack), you must install grub in /dev/vda. Otherwise, it will boot the previous system.

Finally, you can exit from chroot, power off, remove the old disk, and boot using the new one. The result will be next:

root@somove:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 252:16 0 80G 0 disk └─vda1 252:17 0 80G 0 part / vdc 252:32 0 4G 0 disk [SWAP]

What if we get error “error: will not proceed with blocklists.”

If we get this error (only if we get this error), we’ll need to wipe the gap between the init of the disk and the partition and then we’ll be able to install grub in the disk.

WARNING: make sure that you know what you are doing, or the disk is new, because this can potentialle erase the data of /dev/vdb.

$ grub-install /dev/vdb Installing for i386-pc platform. grub-install: warning: Attempting to install GRUB to a disk with multiple partition labels. This is not supported yet.. grub-install: warning: Embedding is not possible. GRUB can only be installed in this setup by using blocklists. However, blocklists are UNRELIABLE and their use is discouraged.. grub-install: error: will not proceed with blocklists.

In this case, we need to check the partition table of /dev/vdb

root@somove:/# fdisk -l /dev/vdb Disk /dev/vdb: 80 GiB, 85899345920 bytes, 167772160 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 Disklabel type: dos Disk identifier: 0x00000000 Device Boot Start End Sectors Size Id Type /dev/vdb1 2048 167772159 167770112 80G 83 Linux

And now we will put zeros in /dev/vdb (skipping the first sector where the partition table is stored), up to the sector before to where our partition starts (in our case, partition /dev/vdb1 starts in sector 2048 and so we will zero 2047 sectors):

root@somove:/# dd if=/dev/zero of=/dev/vdb seek=1 count=2047 2047+0 records in 2047+0 records out 1048064 bytes (1.0 MB, 1.0 MiB) copied, 0.0245413 s, 42.7 MB/s

If this was the problem, now you should be able to install grub:

root@somove:/# grub-install /dev/vdb Installing for i386-pc platform. Installation finished. No error reported.

Источник

Перенос Linux на другой диск на примере Debian/Ubuntu

В этой статье мы покажем, как склонировать/перенести установленную операционную систему Linux на другой (новый) диск на примере Debian и Ubuntu.

Установите утилиту parted с помощью пакетного менеджера:

$ sudo apt install parted -y

Проверьте какая таблица разделов используется на диске (MBR или GPT) с помощью fdisk:

fdisk проверить тип таблицы разделов в linux: gpt или mbr

Выключите компьютер (виртуальную машину) с Linux и подключите новый диск (/dev/sdb).

Создайте на новом диске таблицу разделов:

Если на исходном диске была таблица разделов GPT (EFI), выполните команду:

Для таблицы разделов MBR (DOS BIOS):

Теперь с помощью команды DD склонируйте диск dev/sda в /dev/sdb:

$ sudo dd if=/dev/sda of=/dev/sdb bs=1M conv=noerror,sync

Выключите компьютер, отключите старый диск и загрузитесь с нового.

Если размер нового диска больше, чем размер старого, нужно расширить корневой раздел.

Установите утилиты growpart:

$ sudo apt install -y cloud-guest-utils

Расширьте раздел 3 на диске /dev/sda:

Теперь нужно расширить физический том (physical volume):

Проверьте путь и тип файловой системы:

/dev/mapper/debian-root xfs 10G 2.5G 7.5G 25% /

Расширьте данный логический том (logical volume):

$ sudo lvextend -r -l +100%FREE /dev/mapper/debian-root

Теперь нужно расширить файловую систему (в нашем примере это XFS):

Если у вас EXT4, то для ее расширения, выполните:

$ sudo resize2fs /dev/mapper/debian-root

Источник

Move linux to another hard drive (dump, restore, backup)

First of all it’s necessary to partition new hard drive in the same way as it’s done with old drive (Linux is running at). I usually use ‘fdisk’ utility. Let’s assume that old drive is /dev/hda and new one is /dev/hdb. To view hda’s partition table please run ‘fdisk -l /dev/hda’ which should show something like this:

Disk /dev/hda: 60.0 GB, 60022480896 bytes
255 heads, 63 sectors/track, 7297 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 15 120456 83 Linux
/dev/hda2 16 276 2096482+ 82 Linux swap
/dev/hda3 277 7297 56396182+ 83 Linux

After this run ‘fdisk /dev/hdb’ and make the same partitions at it. Interactive mode of fdisk utility is well documented and is very intuitive, so I don’t think it would be difficult to perform partitioning.

After this is done, we should make new filesystems at partitions we’ve created:

mkfs -t ext3 /dev/hdb1
mkfs -t ext3 /dev/hdb3
mkswap /dev/hdb2

When it’s done it’s NECESSARY to mark newly created filesystems as it’s done with old ones. To check filesystem volume name run command ‘tune2fs -l /dev/hda1 | grep volume’ and etc. You’ll see something like this:

Filesystem volume name: /boot

It means that we should mark new hdb1 with label /boot. It can be done by command:

The same should be performed for all partitions except swap one. In my case I should label hdb3 by command:

At this point new hard drive preparation is finished and we can proceed with moving Linux to it. Mount new filesystem and change directory to it:

mount /dev/hdb1 /mnt/hdb1
cd /mnt/hdb1

When it’s done we can perform moving by command:

dump -0uan -f – /boot | restore -r -f –

And the same with / partition:

mount /dev/hdb3 /mnt/hdb3
cd /mnt/hdb3
dump -0uan -f – / | restore -r -f –

When dump/restore procedures are done we should install boot loader to new HDD. Run ‘grub’ utility and execute in it’s console:

In case everything is done carefully and right (I’ve tested this method by myself) you can boot from new hard drive and have ‘old’ Linux running at new hard drive running.

Источник

Читайте также:  Ora 01034 ora 27101 linux error 2
Оцените статью
Adblock
detector